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