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 <string> 9 10 #include "base/gtest_prod_util.h" 11 #include "base/memory/ref_counted.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/threading/sequenced_worker_pool.h" 14 #include "base/threading/thread.h" 15 #include "base/synchronization/waitable_event.h" 16 #include "chrome/service/cloud_print/cloud_print_proxy.h" 17 18 class ServiceProcessPrefs; 19 class ServiceIPCServer; 20 class ServiceURLRequestContextGetter; 21 class ServiceProcessState; 22 23 namespace base { 24 class CommandLine; 25 } 26 27 namespace net { 28 class NetworkChangeNotifier; 29 } 30 31 // The ServiceProcess does not inherit from ChildProcess because this 32 // process can live independently of the browser process. 33 // ServiceProcess Design Notes 34 // https://sites.google.com/a/chromium.org/dev/developers/design-documents/service-processes 35 class ServiceProcess : public cloud_print::CloudPrintProxy::Client { 36 public: 37 ServiceProcess(); 38 virtual ~ServiceProcess(); 39 40 // Initialize the ServiceProcess with the message loop that it should run on. 41 // ServiceProcess takes ownership of |state|. 42 bool Initialize(base::MessageLoopForUI* message_loop, 43 const base::CommandLine& command_line, 44 ServiceProcessState* state); 45 46 bool Teardown(); 47 // TODO(sanjeevr): Change various parts of the code such as 48 // net::ProxyService::CreateSystemProxyConfigService to take in 49 // MessageLoopProxy* instead of MessageLoop*. When we have done that, we can 50 // remove the io_thread() and file_thread() accessors and replace them with 51 // io_message_loop_proxy() and file_message_loop_proxy() respectively. 52 53 // Returns the thread that we perform I/O coordination on (network requests, 54 // communication with renderers, etc. 55 // NOTE: You should ONLY use this to pass to IPC or other objects which must 56 // need a MessageLoop*. If you just want to post a task, use the thread's 57 // message_loop_proxy() as it takes care of checking that a thread is still 58 // alive, race conditions, lifetime differences etc. 59 // If you still must use this, need to check the return value for NULL. io_thread()60 base::Thread* io_thread() const { 61 return io_thread_.get(); 62 } 63 // Returns the thread that we perform random file operations on. For code 64 // that wants to do I/O operations (not network requests or even file: URL 65 // requests), this is the thread to use to avoid blocking the UI thread. file_thread()66 base::Thread* file_thread() const { 67 return file_thread_.get(); 68 } 69 70 // A global event object that is signalled when the main thread's message 71 // loop exits. This gives background threads a way to observe the main 72 // thread shutting down. shutdown_event()73 base::WaitableEvent* shutdown_event() { 74 return &shutdown_event_; 75 } 76 77 // Shutdown the service process. This is likely triggered by a IPC message. 78 void Shutdown(); 79 SetUpdateAvailable()80 void SetUpdateAvailable() { 81 update_available_ = true; 82 } update_available()83 bool update_available() const { return update_available_; } 84 85 // Called by the IPC server when a client disconnects. A return value of 86 // true indicates that the IPC server should continue listening for new 87 // connections. 88 bool HandleClientDisconnect(); 89 90 cloud_print::CloudPrintProxy* GetCloudPrintProxy(); 91 92 // CloudPrintProxy::Client implementation. 93 virtual void OnCloudPrintProxyEnabled(bool persist_state) OVERRIDE; 94 virtual void OnCloudPrintProxyDisabled(bool persist_state) OVERRIDE; 95 96 ServiceURLRequestContextGetter* GetServiceURLRequestContextGetter(); 97 98 private: 99 friend class TestServiceProcess; 100 101 // Schedule a call to ShutdownIfNeeded. 102 void ScheduleShutdownCheck(); 103 104 // Shuts down the process if no services are enabled and no clients are 105 // connected. 106 void ShutdownIfNeeded(); 107 108 // Schedule a call to CloudPrintPolicyCheckIfNeeded. 109 void ScheduleCloudPrintPolicyCheck(); 110 111 // Launch the browser for a policy check if we're not connected. 112 void CloudPrintPolicyCheckIfNeeded(); 113 114 // Called exactly ONCE per process instance for each service that gets 115 // enabled in this process. 116 void OnServiceEnabled(); 117 118 // Called exactly ONCE per process instance for each service that gets 119 // disabled in this process (note that shutdown != disabled). 120 void OnServiceDisabled(); 121 122 // Terminate forces the service process to quit. 123 void Terminate(); 124 125 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; 126 scoped_ptr<base::Thread> io_thread_; 127 scoped_ptr<base::Thread> file_thread_; 128 scoped_refptr<base::SequencedWorkerPool> blocking_pool_; 129 scoped_ptr<cloud_print::CloudPrintProxy> cloud_print_proxy_; 130 scoped_ptr<ServiceProcessPrefs> service_prefs_; 131 scoped_ptr<ServiceIPCServer> ipc_server_; 132 scoped_ptr<ServiceProcessState> service_process_state_; 133 134 // An event that will be signalled when we shutdown. 135 base::WaitableEvent shutdown_event_; 136 137 // Pointer to the main message loop that host this object. 138 base::MessageLoop* main_message_loop_; 139 140 // Count of currently enabled services in this process. 141 int enabled_services_; 142 143 // Speficies whether a product update is available. 144 bool update_available_; 145 146 scoped_refptr<ServiceURLRequestContextGetter> request_context_getter_; 147 148 DISALLOW_COPY_AND_ASSIGN(ServiceProcess); 149 }; 150 151 extern ServiceProcess* g_service_process; 152 153 #endif // CHROME_SERVICE_SERVICE_PROCESS_H_ 154