browser_process_impl.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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// When each service is created, we set a flag indicating this. At this point,
6// the service initialization could fail or succeed. This allows us to remember
7// if we tried to create a service, and not try creating it over and over if
8// the creation failed.
9
10#ifndef CHROME_BROWSER_BROWSER_PROCESS_IMPL_H_
11#define CHROME_BROWSER_BROWSER_PROCESS_IMPL_H_
12
13#include <string>
14
15#include "base/basictypes.h"
16#include "base/message_loop.h"
17#include "base/non_thread_safe.h"
18#include "base/timer.h"
19#include "base/scoped_ptr.h"
20#include "chrome/browser/automation/automation_provider_list.h"
21#include "chrome/browser/browser_process.h"
22#include "chrome/browser/tab_contents/thumbnail_generator.h"
23#include "ipc/ipc_message.h"
24
25class CommandLine;
26class DebuggerWrapper;
27class FilePath;
28class NotificationService;
29class TabCloseableStateWatcher;
30
31// Real implementation of BrowserProcess that creates and returns the services.
32class BrowserProcessImpl : public BrowserProcess, public NonThreadSafe {
33 public:
34  explicit BrowserProcessImpl(const CommandLine& command_line);
35  virtual ~BrowserProcessImpl();
36
37  virtual void EndSession();
38
39  virtual ResourceDispatcherHost* resource_dispatcher_host() {
40    DCHECK(CalledOnValidThread());
41    if (!created_resource_dispatcher_host_)
42      CreateResourceDispatcherHost();
43    return resource_dispatcher_host_.get();
44  }
45
46  virtual MetricsService* metrics_service() {
47    DCHECK(CalledOnValidThread());
48    if (!created_metrics_service_)
49      CreateMetricsService();
50    return metrics_service_.get();
51  }
52
53  virtual IOThread* io_thread() {
54    DCHECK(CalledOnValidThread());
55    if (!created_io_thread_)
56      CreateIOThread();
57    return io_thread_.get();
58  }
59
60  virtual base::Thread* file_thread() {
61    DCHECK(CalledOnValidThread());
62    if (!created_file_thread_)
63      CreateFileThread();
64    return file_thread_.get();
65  }
66
67  virtual base::Thread* db_thread() {
68    DCHECK(CalledOnValidThread());
69    if (!created_db_thread_)
70      CreateDBThread();
71    return db_thread_.get();
72  }
73
74  virtual base::Thread* process_launcher_thread() {
75    DCHECK(CalledOnValidThread());
76    if (!created_process_launcher_thread_)
77      CreateProcessLauncherThread();
78    return process_launcher_thread_.get();
79  }
80
81  virtual base::Thread* cache_thread() {
82    DCHECK(CalledOnValidThread());
83    if (!created_cache_thread_)
84      CreateCacheThread();
85    return cache_thread_.get();
86  }
87
88#if defined(USE_X11)
89  virtual base::Thread* background_x11_thread() {
90    DCHECK(CalledOnValidThread());
91    // The BACKGROUND_X11 thread is created when the IO thread is created.
92    if (!created_io_thread_)
93      CreateIOThread();
94    return background_x11_thread_.get();
95  }
96#endif
97
98  virtual ProfileManager* profile_manager() {
99    DCHECK(CalledOnValidThread());
100    if (!created_profile_manager_)
101      CreateProfileManager();
102    return profile_manager_.get();
103  }
104
105  virtual PrefService* local_state() {
106    DCHECK(CalledOnValidThread());
107    if (!created_local_state_)
108      CreateLocalState();
109    return local_state_.get();
110  }
111
112  virtual DevToolsManager* devtools_manager() {
113    DCHECK(CalledOnValidThread());
114    if (!created_devtools_manager_)
115      CreateDevToolsManager();
116    return devtools_manager_.get();
117  }
118
119  virtual Clipboard* clipboard() {
120    DCHECK(CalledOnValidThread());
121    return clipboard_.get();
122  }
123
124  virtual NotificationUIManager* notification_ui_manager() {
125    DCHECK(CalledOnValidThread());
126    if (!created_notification_ui_manager_)
127      CreateNotificationUIManager();
128    return notification_ui_manager_.get();
129  }
130
131  virtual StatusTrayManager* status_tray_manager() {
132    DCHECK(CalledOnValidThread());
133    if (!status_tray_manager_.get())
134      CreateStatusTrayManager();
135    return status_tray_manager_.get();
136  }
137
138  virtual IconManager* icon_manager() {
139    DCHECK(CalledOnValidThread());
140    if (!created_icon_manager_)
141      CreateIconManager();
142    return icon_manager_.get();
143  }
144
145  virtual ThumbnailGenerator* GetThumbnailGenerator() {
146    return &thumbnail_generator_;
147  }
148
149  virtual AutomationProviderList* InitAutomationProviderList() {
150    DCHECK(CalledOnValidThread());
151    if (automation_provider_list_.get() == NULL) {
152      automation_provider_list_.reset(AutomationProviderList::GetInstance());
153    }
154    return automation_provider_list_.get();
155  }
156
157  virtual void InitDebuggerWrapper(int port, bool useHttp) {
158    DCHECK(CalledOnValidThread());
159    if (!created_debugger_wrapper_)
160      CreateDebuggerWrapper(port, useHttp);
161  }
162
163  virtual unsigned int AddRefModule();
164
165  virtual unsigned int ReleaseModule();
166
167  virtual bool IsShuttingDown() {
168    DCHECK(CalledOnValidThread());
169    return did_start_ && 0 == module_ref_count_;
170  }
171
172  virtual printing::PrintJobManager* print_job_manager();
173
174  virtual GoogleURLTracker* google_url_tracker() {
175    DCHECK(CalledOnValidThread());
176    if (!google_url_tracker_.get())
177      CreateGoogleURLTracker();
178    return google_url_tracker_.get();
179  }
180
181  virtual IntranetRedirectDetector* intranet_redirect_detector() {
182    DCHECK(CalledOnValidThread());
183    if (!intranet_redirect_detector_.get())
184      CreateIntranetRedirectDetector();
185    return intranet_redirect_detector_.get();
186  }
187
188  virtual const std::string& GetApplicationLocale() {
189    DCHECK(!locale_.empty());
190    return locale_;
191  }
192  virtual void SetApplicationLocale(const std::string& locale);
193
194  virtual base::WaitableEvent* shutdown_event() {
195    return shutdown_event_.get();
196  }
197
198  virtual TabCloseableStateWatcher* tab_closeable_state_watcher() {
199    DCHECK(CalledOnValidThread());
200    if (!tab_closeable_state_watcher_.get())
201      CreateTabCloseableStateWatcher();
202    return tab_closeable_state_watcher_.get();
203  }
204
205  virtual void CheckForInspectorFiles();
206
207#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
208  void StartAutoupdateTimer();
209#endif
210
211  virtual bool have_inspector_files() const {
212    return have_inspector_files_;
213  }
214
215#if defined(IPC_MESSAGE_LOG_ENABLED)
216  virtual void SetIPCLoggingEnabled(bool enable);
217#endif
218
219 private:
220  void ClearLocalState(const FilePath& profile_path);
221  bool ShouldClearLocalState(FilePath* profile_path);
222
223  void CreateResourceDispatcherHost();
224  void CreatePrefService();
225  void CreateMetricsService();
226
227  void CreateIOThread();
228  static void CleanupOnIOThread();
229
230  void CreateFileThread();
231  void CreateDBThread();
232  void CreateProcessLauncherThread();
233  void CreateCacheThread();
234  void CreateTemplateURLModel();
235  void CreateProfileManager();
236  void CreateWebDataService();
237  void CreateLocalState();
238  void CreateViewedPageTracker();
239  void CreateIconManager();
240  void CreateDebuggerWrapper(int port, bool useHttp);
241  void CreateDevToolsManager();
242  void CreateGoogleURLTracker();
243  void CreateIntranetRedirectDetector();
244  void CreateNotificationUIManager();
245  void CreateStatusTrayManager();
246  void CreateTabCloseableStateWatcher();
247
248#if defined(IPC_MESSAGE_LOG_ENABLED)
249  void SetIPCLoggingEnabledForChildProcesses(bool enabled);
250#endif
251
252  bool created_resource_dispatcher_host_;
253  scoped_ptr<ResourceDispatcherHost> resource_dispatcher_host_;
254
255  bool created_metrics_service_;
256  scoped_ptr<MetricsService> metrics_service_;
257
258  bool created_io_thread_;
259  scoped_ptr<IOThread> io_thread_;
260#if defined(USE_X11)
261  // This shares a created flag with the IO thread.
262  scoped_ptr<base::Thread> background_x11_thread_;
263#endif
264
265  bool created_file_thread_;
266  scoped_ptr<base::Thread> file_thread_;
267
268  bool created_db_thread_;
269  scoped_ptr<base::Thread> db_thread_;
270
271  bool created_process_launcher_thread_;
272  scoped_ptr<base::Thread> process_launcher_thread_;
273
274  bool created_cache_thread_;
275  scoped_ptr<base::Thread> cache_thread_;
276
277  bool created_profile_manager_;
278  scoped_ptr<ProfileManager> profile_manager_;
279
280  bool created_local_state_;
281  scoped_ptr<PrefService> local_state_;
282
283  bool created_icon_manager_;
284  scoped_ptr<IconManager> icon_manager_;
285
286  bool created_debugger_wrapper_;
287  scoped_refptr<DebuggerWrapper> debugger_wrapper_;
288
289  bool created_devtools_manager_;
290  scoped_refptr<DevToolsManager> devtools_manager_;
291
292  scoped_ptr<Clipboard> clipboard_;
293
294  // Manager for desktop notification UI.
295  bool created_notification_ui_manager_;
296  scoped_ptr<NotificationUIManager> notification_ui_manager_;
297
298  // Manager for status tray.
299  scoped_ptr<StatusTrayManager> status_tray_manager_;
300
301  scoped_ptr<AutomationProviderList> automation_provider_list_;
302
303  scoped_ptr<GoogleURLTracker> google_url_tracker_;
304  scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector_;
305
306  scoped_ptr<NotificationService> main_notification_service_;
307
308  scoped_ptr<TabCloseableStateWatcher> tab_closeable_state_watcher_;
309
310  unsigned int module_ref_count_;
311  bool did_start_;
312
313  // Ensures that all the print jobs are finished before closing the browser.
314  scoped_ptr<printing::PrintJobManager> print_job_manager_;
315
316  std::string locale_;
317
318  bool checked_for_new_frames_;
319  bool using_new_frames_;
320
321  // This service just sits around and makes thumanails for tabs. It does
322  // nothing in the cosntructor so we don't have to worry about lazy init.
323  ThumbnailGenerator thumbnail_generator_;
324
325  // An event that notifies when we are shutting-down.
326  scoped_ptr<base::WaitableEvent> shutdown_event_;
327
328  // Runs on the file thread and stats the inspector's directory, filling in
329  // have_inspector_files_ with the result.
330  void DoInspectorFilesCheck();
331  // Our best estimate about the existence of the inspector directory.
332  bool have_inspector_files_;
333
334#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
335  base::RepeatingTimer<BrowserProcessImpl> autoupdate_timer_;
336
337  // Gets called by autoupdate timer to see if browser needs restart and can be
338  // restarted, and if that's the case, restarts the browser.
339  void OnAutoupdateTimer();
340  bool CanAutorestartForUpdate() const;
341  void RestartPersistentInstance();
342#endif  // defined(OS_WIN) || defined(OS_LINUX)
343
344  DISALLOW_COPY_AND_ASSIGN(BrowserProcessImpl);
345};
346
347#endif  // CHROME_BROWSER_BROWSER_PROCESS_IMPL_H_
348