1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_SERVICE_SERVICE_PROCESS_H_ 6 #define CHROME_SERVICE_SERVICE_PROCESS_H_ 7 8 #include <memory> 9 #include <string> 10 11 #include "base/macros.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/single_thread_task_runner.h" 14 #include "base/threading/thread.h" 15 #include "chrome/service/cloud_print/cloud_print_proxy.h" 16 #include "chrome/service/net/in_process_network_connection_tracker.h" 17 #include "chrome/service/service_ipc_server.h" 18 #include "mojo/public/cpp/platform/named_platform_channel.h" 19 #include "mojo/public/cpp/platform/platform_channel_server_endpoint.h" 20 #include "mojo/public/cpp/system/isolated_connection.h" 21 #include "mojo/public/cpp/system/message_pipe.h" 22 23 class ServiceProcessPrefs; 24 class ServiceURLRequestContextGetter; 25 class ServiceProcessState; 26 27 namespace base { 28 class CommandLine; 29 class WaitableEvent; 30 } 31 32 namespace mojo { 33 class IsolatedConnection; 34 namespace core { 35 class ScopedIPCSupport; 36 } 37 } 38 39 namespace net { 40 class NetworkChangeNotifier; 41 } 42 43 // The ServiceProcess does not inherit from ChildProcess because this 44 // process can live independently of the browser process. 45 // ServiceProcess Design Notes 46 // https://sites.google.com/a/chromium.org/dev/developers/design-documents/service-processes 47 class ServiceProcess : public ServiceIPCServer::Client, 48 public cloud_print::CloudPrintProxy::Client, 49 public cloud_print::CloudPrintProxy::Provider { 50 public: 51 ServiceProcess(); 52 ~ServiceProcess() override; 53 54 // Initialize the ServiceProcess. |quit_closure| will be run when the service 55 // process is ready to exit. 56 bool Initialize(base::OnceClosure quit_closure, 57 const base::CommandLine& command_line, 58 std::unique_ptr<ServiceProcessState> state); 59 60 bool Teardown(); 61 62 // Returns the SingleThreadTaskRunner for the service process io thread (used 63 // for e.g. network requests and IPC). Returns null before Initialize is 64 // called and after Teardown is called. io_task_runner()65 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner() { 66 return io_thread_ ? io_thread_->task_runner() : nullptr; 67 } 68 69 // A global event object that is signalled when the main thread's message 70 // loop exits. This gives background threads a way to observe the main 71 // thread shutting down. GetShutdownEventForTesting()72 base::WaitableEvent* GetShutdownEventForTesting() { 73 return &shutdown_event_; 74 } 75 76 // Shuts down the service process. 77 void Shutdown(); 78 79 // ServiceIPCServer::Client implementation. 80 void OnShutdown() override; 81 void OnUpdateAvailable() override; 82 bool OnIPCClientDisconnect() override; 83 mojo::ScopedMessagePipeHandle CreateChannelMessagePipe() override; 84 85 // CloudPrintProxy::Provider implementation. 86 cloud_print::CloudPrintProxy* GetCloudPrintProxy() override; 87 88 // CloudPrintProxy::Client implementation. 89 void OnCloudPrintProxyEnabled(bool persist_state) override; 90 void OnCloudPrintProxyDisabled(bool persist_state) override; 91 92 ServiceURLRequestContextGetter* GetServiceURLRequestContextGetter(); 93 94 private: 95 friend class TestServiceProcess; 96 97 // Schedule a call to ShutdownIfNeeded. 98 void ScheduleShutdownCheck(); 99 100 // Shuts down the process if no services are enabled and no IPC client is 101 // connected. 102 void ShutdownIfNeeded(); 103 104 // Called exactly ONCE per process instance for each service that gets 105 // enabled in this process. 106 void OnServiceEnabled(); 107 108 // Called exactly ONCE per process instance for each service that gets 109 // disabled in this process (note that shutdown != disabled). 110 void OnServiceDisabled(); 111 112 // Terminate forces the service process to quit. 113 void Terminate(); 114 115 std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier_; 116 std::unique_ptr<InProcessNetworkConnectionTracker> 117 network_connection_tracker_; 118 std::unique_ptr<base::Thread> io_thread_; 119 std::unique_ptr<cloud_print::CloudPrintProxy> cloud_print_proxy_; 120 std::unique_ptr<ServiceProcessPrefs> service_prefs_; 121 std::unique_ptr<ServiceIPCServer> ipc_server_; 122 std::unique_ptr<ServiceProcessState> service_process_state_; 123 std::unique_ptr<mojo::core::ScopedIPCSupport> mojo_ipc_support_; 124 std::unique_ptr<mojo::IsolatedConnection> mojo_connection_; 125 126 // An event that will be signalled when we shutdown. 127 base::WaitableEvent shutdown_event_; 128 129 // Closure to run to cause the main message loop to exit. 130 base::OnceClosure quit_closure_; 131 132 // Count of currently enabled services in this process. 133 int enabled_services_; 134 135 // Speficies whether a product update is available. 136 bool update_available_; 137 138 scoped_refptr<ServiceURLRequestContextGetter> request_context_getter_; 139 140 #if defined(OS_POSIX) 141 mojo::PlatformChannelServerEndpoint server_endpoint_; 142 #elif defined(OS_WIN) 143 mojo::NamedPlatformChannel::ServerName server_name_; 144 #endif 145 146 DISALLOW_COPY_AND_ASSIGN(ServiceProcess); 147 }; 148 149 extern ServiceProcess* g_service_process; 150 151 #endif // CHROME_SERVICE_SERVICE_PROCESS_H_ 152