blob: 1aa04e83890af3389a70c350e39440614b0fd3d2 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/profile.h"
6
7#include "base/command_line.h"
[email protected]04fba9a92008-10-28 17:25:258#include "base/file_path.h"
initial.commit09911bf2008-07-26 23:55:299#include "base/path_service.h"
10#include "base/scoped_ptr.h"
11#include "base/string_util.h"
[email protected]a9afddb2009-02-12 17:49:4212#include "chrome/browser/bookmarks/bookmark_model.h"
initial.commit09911bf2008-07-26 23:55:2913#include "chrome/browser/browser_list.h"
14#include "chrome/browser/browser_process.h"
[email protected]67a46b7f2009-06-16 21:41:0215#include "chrome/browser/browser_theme_provider.h"
[email protected]b7f05882009-02-22 01:21:5616#include "chrome/browser/download/download_manager.h"
[email protected]89ebc7e2009-08-24 22:11:0717#include "chrome/browser/extensions/extension_devtools_manager.h"
[email protected]7120f132009-07-20 21:05:3718#include "chrome/browser/extensions/extension_message_service.h"
[email protected]481e1a42009-05-06 20:56:0519#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]6014d672008-12-05 00:38:2520#include "chrome/browser/extensions/extensions_service.h"
[email protected]0938d3c2009-01-09 20:37:3521#include "chrome/browser/extensions/user_script_master.h"
[email protected]0189bc722009-08-28 21:56:4822#include "chrome/browser/favicon_service.h"
[email protected]a9afddb2009-02-12 17:49:4223#include "chrome/browser/history/history.h"
[email protected]3bf335a2009-06-26 20:46:0624#include "chrome/browser/in_process_webkit/webkit_context.h"
[email protected]6ab9b202008-12-23 22:34:5025#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]db36938c2009-08-19 21:48:4226#include "chrome/browser/net/ssl_config_service_manager.h"
[email protected]e69d33952009-06-03 22:00:4127#include "chrome/browser/password_manager/password_store_default.h"
[email protected]eaadd9052009-06-23 18:02:2328#include "chrome/browser/privacy_blacklist/blacklist.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2630#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]1132436e2009-04-08 20:06:3331#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]f63ae312009-02-04 17:58:4632#include "chrome/browser/search_engines/template_url_model.h"
[email protected]85e921fb82009-02-11 23:19:4433#include "chrome/browser/sessions/session_service.h"
[email protected]bd580a252009-02-12 01:16:3034#include "chrome/browser/sessions/tab_restore_service.h"
[email protected]f0a644292009-02-25 23:32:4735#include "chrome/browser/spellchecker.h"
[email protected]1132436e2009-04-08 20:06:3336#include "chrome/browser/ssl/ssl_host_state.h"
[email protected]48352c12009-08-15 01:19:1137#include "chrome/browser/sync/profile_sync_service.h"
[email protected]449478302009-06-09 20:04:2838#include "chrome/browser/thumbnail_store.h"
initial.commit09911bf2008-07-26 23:55:2939#include "chrome/browser/visitedlink_master.h"
[email protected]3e90d4a2009-07-03 17:38:3940#include "chrome/browser/visitedlink_event_listener.h"
initial.commit09911bf2008-07-26 23:55:2941#include "chrome/browser/webdata/web_data_service.h"
[email protected]23f1ef12009-09-01 22:30:3042#include "chrome/common/appcache/chrome_appcache_service.h"
initial.commit09911bf2008-07-26 23:55:2943#include "chrome/common/chrome_constants.h"
44#include "chrome/common/chrome_paths.h"
45#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5846#include "chrome/common/extensions/extension_error_reporter.h"
[email protected]47accfd62009-05-14 18:46:2147#include "chrome/common/net/cookie_monster_sqlite.h"
initial.commit09911bf2008-07-26 23:55:2948#include "chrome/common/notification_service.h"
49#include "chrome/common/pref_names.h"
[email protected]f7011fcb2009-01-28 21:54:3250#include "chrome/common/render_messages.h"
[email protected]f90f5c512009-02-18 19:10:5851#include "grit/locale_settings.h"
[email protected]a9cea7542009-05-20 04:30:2352#include "net/base/force_tls_state.h"
initial.commit09911bf2008-07-26 23:55:2953
[email protected]a5166af62009-07-03 00:42:2954#if defined(OS_LINUX)
[email protected]2a0c0a52009-07-31 07:51:3255#include "net/ocsp/nss_ocsp.h"
[email protected]a5166af62009-07-03 00:42:2956#include "chrome/browser/gtk/gtk_theme_provider.h"
57#endif
58
[email protected]e1acf6f2008-10-27 20:43:3359using base::Time;
60using base::TimeDelta;
61
[email protected]e5642992009-06-26 23:06:3162namespace {
63
initial.commit09911bf2008-07-26 23:55:2964// Delay, in milliseconds, before we explicitly create the SessionService.
65static const int kCreateSessionServiceDelayMS = 500;
66
[email protected]e5642992009-06-26 23:06:3167enum ContextType {
68 kNormalContext,
69 kMediaContext
70};
71
72// Gets the cache parameters from the command line. |type| is the type of
73// request context that we need, |cache_path| will be set to the user provided
74// path, or will not be touched if there is not an argument. |max_size| will
75// be the user provided value or zero by default.
76void GetCacheParameters(ContextType type, FilePath* cache_path,
77 int* max_size) {
78 DCHECK(cache_path);
79 DCHECK(max_size);
80
81 // Override the cache location if specified by the user.
82 std::wstring user_path(CommandLine::ForCurrentProcess()->GetSwitchValue(
83 switches::kDiskCacheDir));
84
85 if (!user_path.empty()) {
86 *cache_path = FilePath::FromWStringHack(user_path);
87 }
88
89 const wchar_t* arg = kNormalContext == type ? switches::kDiskCacheSize :
90 switches::kMediaCacheSize;
91 std::string value =
92 WideToASCII(CommandLine::ForCurrentProcess()->GetSwitchValue(arg));
93
94 // By default we let the cache determine the right size.
95 *max_size = 0;
96 if (!StringToInt(value, max_size)) {
97 *max_size = 0;
98 } else if (max_size < 0) {
99 *max_size = 0;
100 }
101}
102
103} // namespace
104
initial.commit09911bf2008-07-26 23:55:29105// A pointer to the request context for the default profile. See comments on
106// Profile::GetDefaultRequestContext.
107URLRequestContext* Profile::default_request_context_;
108
[email protected]47accfd62009-05-14 18:46:21109static void CleanupRequestContext(ChromeURLRequestContext* context) {
110 if (context) {
111 context->CleanupOnUIThread();
112
113 // Clean up request context on IO thread.
114 g_browser_process->io_thread()->message_loop()->ReleaseSoon(FROM_HERE,
115 context);
116 }
117}
118
[email protected]23f1ef12009-09-01 22:30:30119static void CleanupAppCacheService(ChromeAppCacheService* service) {
120 if (service) {
121 MessageLoop* io_thread = ChromeThread::GetMessageLoop(ChromeThread::IO);
122 if (io_thread)
123 io_thread->ReleaseSoon(FROM_HERE, service);
124 else
125 service->Release();
126 }
127}
128
[email protected]34cc84f2009-02-13 10:04:35129// static
initial.commit09911bf2008-07-26 23:55:29130void Profile::RegisterUserPrefs(PrefService* prefs) {
[email protected]430d3f72008-10-27 17:56:55131 prefs->RegisterBooleanPref(prefs::kSearchSuggestEnabled, true);
initial.commit09911bf2008-07-26 23:55:29132 prefs->RegisterBooleanPref(prefs::kSessionExitedCleanly, true);
133 prefs->RegisterBooleanPref(prefs::kSafeBrowsingEnabled, true);
[email protected]74c8b422009-03-11 00:34:12134 // TODO(estade): IDS_SPELLCHECK_DICTIONARY should be an ASCII string.
[email protected]e7244d82008-10-29 18:13:26135 prefs->RegisterLocalizedStringPref(prefs::kSpellCheckDictionary,
136 IDS_SPELLCHECK_DICTIONARY);
137 prefs->RegisterBooleanPref(prefs::kEnableSpellCheck, true);
[email protected]154a4332009-06-03 20:20:58138 prefs->RegisterBooleanPref(prefs::kEnableAutoSpellCorrect, true);
[email protected]f93fe782009-02-19 01:26:13139 prefs->RegisterBooleanPref(prefs::kEnableUserScripts, false);
[email protected]a5166af62009-07-03 00:42:29140#if defined(OS_LINUX)
141 prefs->RegisterBooleanPref(prefs::kUsesSystemTheme, false);
142#endif
[email protected]51c490b2009-08-03 16:34:34143 prefs->RegisterStringPref(prefs::kCurrentThemeID,
144 UTF8ToWide(BrowserThemeProvider::kDefaultThemeID));
[email protected]4a190632009-05-09 01:07:42145 prefs->RegisterDictionaryPref(prefs::kCurrentThemeImages);
146 prefs->RegisterDictionaryPref(prefs::kCurrentThemeColors);
147 prefs->RegisterDictionaryPref(prefs::kCurrentThemeTints);
[email protected]7895ea22009-06-02 20:53:50148 prefs->RegisterDictionaryPref(prefs::kCurrentThemeDisplayProperties);
[email protected]6d60703b2009-08-29 01:29:23149 prefs->RegisterBooleanPref(prefs::kDisableExtensions, false);
initial.commit09911bf2008-07-26 23:55:29150}
151
[email protected]34cc84f2009-02-13 10:04:35152// static
[email protected]f7011fcb2009-01-28 21:54:32153Profile* Profile::CreateProfile(const FilePath& path) {
initial.commit09911bf2008-07-26 23:55:29154 return new ProfileImpl(path);
155}
156
[email protected]34cc84f2009-02-13 10:04:35157// static
initial.commit09911bf2008-07-26 23:55:29158URLRequestContext* Profile::GetDefaultRequestContext() {
159 return default_request_context_;
160}
161
[email protected]e69d33952009-06-03 22:00:41162#if defined(OS_LINUX)
163// Temporarily disabled while we figure some stuff out.
164// http://code.google.com/p/chromium/issues/detail?id=12351
165// #include "chrome/browser/password_manager/password_store_gnome.h"
166// #include "chrome/browser/password_manager/password_store_kwallet.h"
167#elif defined(OS_WIN)
168#include "chrome/browser/password_manager/password_store_win.h"
[email protected]034eba52009-06-03 22:50:56169#elif defined(OS_MACOSX)
170#include "chrome/browser/keychain_mac.h"
[email protected]e0411ae52009-06-30 23:59:17171#include "chrome/browser/password_manager/login_database_mac.h"
[email protected]034eba52009-06-03 22:50:56172#include "chrome/browser/password_manager/password_store_mac.h"
[email protected]e69d33952009-06-03 22:00:41173#endif
initial.commit09911bf2008-07-26 23:55:29174
initial.commit09911bf2008-07-26 23:55:29175////////////////////////////////////////////////////////////////////////////////
176//
177// OffTheRecordProfileImpl is a profile subclass that wraps an existing profile
[email protected]4a190632009-05-09 01:07:42178// to make it suitable for the off the record mode.
initial.commit09911bf2008-07-26 23:55:29179//
180////////////////////////////////////////////////////////////////////////////////
181class OffTheRecordProfileImpl : public Profile,
182 public NotificationObserver {
183 public:
184 explicit OffTheRecordProfileImpl(Profile* real_profile)
185 : profile_(real_profile),
[email protected]47accfd62009-05-14 18:46:21186 extensions_request_context_(NULL),
initial.commit09911bf2008-07-26 23:55:29187 start_time_(Time::Now()) {
[email protected]6ab9b202008-12-23 22:34:50188 request_context_ = ChromeURLRequestContext::CreateOffTheRecord(this);
initial.commit09911bf2008-07-26 23:55:29189 request_context_->AddRef();
[email protected]481e1a42009-05-06 20:56:05190
initial.commit09911bf2008-07-26 23:55:29191 // Register for browser close notifications so we can detect when the last
192 // off-the-record window is closed, in which case we can clean our states
193 // (cookies, downloads...).
[email protected]88cf3292009-05-22 01:48:43194 registrar_.Add(this, NotificationType::BROWSER_CLOSED,
195 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29196 }
197
198 virtual ~OffTheRecordProfileImpl() {
[email protected]47accfd62009-05-14 18:46:21199 CleanupRequestContext(request_context_);
[email protected]47accfd62009-05-14 18:46:21200 CleanupRequestContext(extensions_request_context_);
[email protected]23f1ef12009-09-01 22:30:30201 CleanupAppCacheService(appcache_service_.release());
initial.commit09911bf2008-07-26 23:55:29202 }
203
[email protected]f7011fcb2009-01-28 21:54:32204 virtual FilePath GetPath() { return profile_->GetPath(); }
initial.commit09911bf2008-07-26 23:55:29205
206 virtual bool IsOffTheRecord() {
207 return true;
208 }
209
210 virtual Profile* GetOffTheRecordProfile() {
211 return this;
212 }
213
[email protected]860f55492009-03-27 19:50:59214 virtual void DestroyOffTheRecordProfile() {
215 // Suicide is bad!
216 NOTREACHED();
217 }
218
initial.commit09911bf2008-07-26 23:55:29219 virtual Profile* GetOriginalProfile() {
220 return profile_;
221 }
222
[email protected]23f1ef12009-09-01 22:30:30223 virtual ChromeAppCacheService* GetAppCacheService() {
224 if (!appcache_service_.get()) {
225 appcache_service_ = new ChromeAppCacheService();
226 appcache_service_->InitializeOnUIThread(GetPath(), true);
227 }
228 return appcache_service_.get();
229 }
230
initial.commit09911bf2008-07-26 23:55:29231 virtual VisitedLinkMaster* GetVisitedLinkMaster() {
[email protected]7fb6c862009-03-13 02:51:49232 // We don't provide access to the VisitedLinkMaster when we're OffTheRecord
233 // because we don't want to leak the sites that the user has visited before.
234 return NULL;
initial.commit09911bf2008-07-26 23:55:29235 }
236
[email protected]6014d672008-12-05 00:38:25237 virtual ExtensionsService* GetExtensionsService() {
[email protected]1bd54132009-06-11 00:05:34238 return NULL;
[email protected]6014d672008-12-05 00:38:25239 }
240
[email protected]0938d3c2009-01-09 20:37:35241 virtual UserScriptMaster* GetUserScriptMaster() {
242 return profile_->GetUserScriptMaster();
[email protected]04fba9a92008-10-28 17:25:25243 }
244
[email protected]89ebc7e2009-08-24 22:11:07245 virtual ExtensionDevToolsManager* GetExtensionDevToolsManager() {
246 return NULL;
247 }
248
[email protected]481e1a42009-05-06 20:56:05249 virtual ExtensionProcessManager* GetExtensionProcessManager() {
[email protected]1bd54132009-06-11 00:05:34250 return NULL;
[email protected]481e1a42009-05-06 20:56:05251 }
252
[email protected]7120f132009-07-20 21:05:37253 virtual ExtensionMessageService* GetExtensionMessageService() {
254 return NULL;
255 }
256
[email protected]34cc84f2009-02-13 10:04:35257 virtual SSLHostState* GetSSLHostState() {
258 if (!ssl_host_state_.get())
259 ssl_host_state_.reset(new SSLHostState());
260
261 DCHECK(ssl_host_state_->CalledOnValidThread());
262 return ssl_host_state_.get();
263 }
264
[email protected]a9cea7542009-05-20 04:30:23265 virtual net::ForceTLSState* GetForceTLSState() {
266 if (!force_tls_state_.get())
267 force_tls_state_.reset(new net::ForceTLSState());
268
269 return force_tls_state_.get();
270 }
271
initial.commit09911bf2008-07-26 23:55:29272 virtual HistoryService* GetHistoryService(ServiceAccessType sat) {
273 if (sat == EXPLICIT_ACCESS) {
274 return profile_->GetHistoryService(sat);
275 } else {
276 NOTREACHED() << "This profile is OffTheRecord";
277 return NULL;
278 }
279 }
280
[email protected]0189bc722009-08-28 21:56:48281 virtual FaviconService* GetFaviconService(ServiceAccessType sat) {
282 if (sat == EXPLICIT_ACCESS) {
283 return profile_->GetFaviconService(sat);
284 } else {
285 NOTREACHED() << "This profile is OffTheRecord";
286 return NULL;
287 }
288 }
289
initial.commit09911bf2008-07-26 23:55:29290 virtual WebDataService* GetWebDataService(ServiceAccessType sat) {
291 if (sat == EXPLICIT_ACCESS) {
292 return profile_->GetWebDataService(sat);
293 } else {
294 NOTREACHED() << "This profile is OffTheRecord";
295 return NULL;
296 }
297 }
298
[email protected]e69d33952009-06-03 22:00:41299 virtual PasswordStore* GetPasswordStore(ServiceAccessType sat) {
300 if (sat == EXPLICIT_ACCESS) {
301 return profile_->GetPasswordStore(sat);
302 } else {
303 NOTREACHED() << "This profile is OffTheRecord";
304 return NULL;
305 }
306 }
307
initial.commit09911bf2008-07-26 23:55:29308 virtual PrefService* GetPrefs() {
309 return profile_->GetPrefs();
310 }
311
312 virtual TemplateURLModel* GetTemplateURLModel() {
313 return profile_->GetTemplateURLModel();
314 }
315
316 virtual TemplateURLFetcher* GetTemplateURLFetcher() {
317 return profile_->GetTemplateURLFetcher();
318 }
319
320 virtual DownloadManager* GetDownloadManager() {
321 if (!download_manager_.get()) {
322 scoped_refptr<DownloadManager> dlm(new DownloadManager);
323 dlm->Init(this);
324 download_manager_.swap(dlm);
325 }
326 return download_manager_.get();
327 }
328
329 virtual bool HasCreatedDownloadManager() const {
330 return (download_manager_.get() != NULL);
331 }
332
[email protected]4a190632009-05-09 01:07:42333 virtual void InitThemes() {
334 GetOriginalProfile()->InitThemes();
335 }
336
337 virtual void SetTheme(Extension* extension) {
338 GetOriginalProfile()->SetTheme(extension);
339 }
340
[email protected]a5166af62009-07-03 00:42:29341 virtual void SetNativeTheme() {
342 GetOriginalProfile()->SetNativeTheme();
343 }
344
[email protected]4a190632009-05-09 01:07:42345 virtual void ClearTheme() {
346 GetOriginalProfile()->ClearTheme();
347 }
348
[email protected]51c490b2009-08-03 16:34:34349 virtual Extension* GetTheme() {
350 return GetOriginalProfile()->GetTheme();
351 }
352
[email protected]4a190632009-05-09 01:07:42353 virtual ThemeProvider* GetThemeProvider() {
354 return GetOriginalProfile()->GetThemeProvider();
355 }
356
initial.commit09911bf2008-07-26 23:55:29357 virtual URLRequestContext* GetRequestContext() {
358 return request_context_;
359 }
360
[email protected]e7f29642009-03-02 22:53:18361 virtual URLRequestContext* GetRequestContextForMedia() {
[email protected]d14c7ac2009-05-29 20:38:11362 // In OTR mode, media request context is the same as the original one.
363 return request_context_;
[email protected]e7f29642009-03-02 22:53:18364 }
365
[email protected]47accfd62009-05-14 18:46:21366 URLRequestContext* GetRequestContextForExtensions() {
367 if (!extensions_request_context_) {
368 extensions_request_context_ =
369 ChromeURLRequestContext::CreateOffTheRecordForExtensions(this);
370 extensions_request_context_->AddRef();
371
372 DCHECK(extensions_request_context_->cookie_store());
373 }
374
375 return extensions_request_context_;
376 }
377
[email protected]db36938c2009-08-19 21:48:42378 virtual net::SSLConfigService* GetSSLConfigService() {
379 return GetOriginalProfile()->GetSSLConfigService();
380 }
381
[email protected]eaadd9052009-06-23 18:02:23382 virtual Blacklist* GetBlacklist() {
383 return GetOriginalProfile()->GetBlacklist();
384 }
385
initial.commit09911bf2008-07-26 23:55:29386 virtual SessionService* GetSessionService() {
387 // Don't save any sessions when off the record.
388 return NULL;
389 }
390
391 virtual void ShutdownSessionService() {
392 // We don't allow a session service, nothing to do.
393 }
394
395 virtual bool HasSessionService() const {
396 // We never have a session service.
397 return false;
398 }
399
400 virtual std::wstring GetName() {
401 return profile_->GetName();
402 }
403
404 virtual void SetName(const std::wstring& name) {
405 profile_->SetName(name);
406 }
407
408 virtual std::wstring GetID() {
409 return profile_->GetID();
410 }
411
412 virtual void SetID(const std::wstring& id) {
413 profile_->SetID(id);
414 }
415
initial.commit09911bf2008-07-26 23:55:29416 virtual bool DidLastSessionExitCleanly() {
417 return profile_->DidLastSessionExitCleanly();
418 }
419
[email protected]d8e41ed2008-09-11 15:22:32420 virtual BookmarkModel* GetBookmarkModel() {
421 return profile_->GetBookmarkModel();
initial.commit09911bf2008-07-26 23:55:29422 }
423
[email protected]48352c12009-08-15 01:19:11424 virtual ProfileSyncService* GetProfileSyncService() {
425 return NULL;
[email protected]3a453fa2008-08-15 18:46:34426 }
[email protected]3a453fa2008-08-15 18:46:34427
initial.commit09911bf2008-07-26 23:55:29428 virtual bool IsSameProfile(Profile* profile) {
429 if (profile == static_cast<Profile*>(this))
430 return true;
431 return profile == profile_;
432 }
433
434 virtual Time GetStartTime() const {
435 return start_time_;
436 }
437
438 virtual TabRestoreService* GetTabRestoreService() {
439 return NULL;
440 }
441
[email protected]e7244d82008-10-29 18:13:26442 virtual void ResetTabRestoreService() {
[email protected]20930852008-10-15 19:30:41443 }
444
[email protected]e7244d82008-10-29 18:13:26445 virtual void ReinitializeSpellChecker() {
446 profile_->ReinitializeSpellChecker();
initial.commit09911bf2008-07-26 23:55:29447 }
448
449 virtual SpellChecker* GetSpellChecker() {
450 return profile_->GetSpellChecker();
451 }
452
[email protected]3bf335a2009-06-26 20:46:06453 virtual WebKitContext* GetWebKitContext() {
454 if (!webkit_context_.get())
455 webkit_context_ = new WebKitContext(GetPath(), true);
456 DCHECK(webkit_context_.get());
457 return webkit_context_.get();
458}
459
[email protected]449478302009-06-09 20:04:28460 virtual ThumbnailStore* GetThumbnailStore() {
461 return NULL;
462 }
463
initial.commit09911bf2008-07-26 23:55:29464 virtual void MarkAsCleanShutdown() {
465 }
466
[email protected]bdbc87c2009-01-25 05:08:54467 virtual void InitExtensions() {
468 NOTREACHED();
469 }
470
[email protected]ea0c98cf2009-06-18 23:02:54471 virtual void InitWebResources() {
472 NOTREACHED();
473 }
474
initial.commit09911bf2008-07-26 23:55:29475 virtual void ExitedOffTheRecordMode() {
476 // Drop our download manager so we forget about all the downloads made
477 // in off-the-record mode.
478 download_manager_ = NULL;
479 }
480
481 virtual void Observe(NotificationType type,
482 const NotificationSource& source,
483 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56484 DCHECK_EQ(NotificationType::BROWSER_CLOSED, type.value);
initial.commit09911bf2008-07-26 23:55:29485 // We are only interested in OTR browser closing.
486 if (Source<Browser>(source)->profile() != this)
487 return;
488
489 // Let's check if we still have an Off The Record window opened.
490 // Note that we check against 1 as this notification is sent before the
491 // browser window is actually removed from the list.
492 if (BrowserList::GetBrowserCount(this) <= 1)
493 ExitedOffTheRecordMode();
494 }
495
496 private:
[email protected]88cf3292009-05-22 01:48:43497 NotificationRegistrar registrar_;
498
initial.commit09911bf2008-07-26 23:55:29499 // The real underlying profile.
500 Profile* profile_;
501
[email protected]6ab9b202008-12-23 22:34:50502 // The context to use for requests made from this OTR session.
503 ChromeURLRequestContext* request_context_;
initial.commit09911bf2008-07-26 23:55:29504
[email protected]47accfd62009-05-14 18:46:21505 ChromeURLRequestContext* extensions_request_context_;
506
[email protected]23f1ef12009-09-01 22:30:30507 // Use a seperate appcache service for OTR.
508 scoped_refptr<ChromeAppCacheService> appcache_service_;
509
initial.commit09911bf2008-07-26 23:55:29510 // The download manager that only stores downloaded items in memory.
511 scoped_refptr<DownloadManager> download_manager_;
512
[email protected]4a190632009-05-09 01:07:42513 scoped_refptr<BrowserThemeProvider> theme_provider_;
514
[email protected]3bf335a2009-06-26 20:46:06515 // Use a special WebKit context for OTR browsing.
516 scoped_refptr<WebKitContext> webkit_context_;
517
[email protected]34cc84f2009-02-13 10:04:35518 // We don't want SSLHostState from the OTR profile to leak back to the main
519 // profile because then the main profile would learn some of the host names
520 // the user visited while OTR.
521 scoped_ptr<SSLHostState> ssl_host_state_;
522
[email protected]a9cea7542009-05-20 04:30:23523 // The ForceTLSState that only stores enabled sites in memory.
524 scoped_ptr<net::ForceTLSState> force_tls_state_;
525
initial.commit09911bf2008-07-26 23:55:29526 // Time we were started.
527 Time start_time_;
528
529 DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl);
530};
531
[email protected]f7011fcb2009-01-28 21:54:32532ProfileImpl::ProfileImpl(const FilePath& path)
initial.commit09911bf2008-07-26 23:55:29533 : path_(path),
[email protected]3e90d4a2009-07-03 17:38:39534 visited_link_event_listener_(new VisitedLinkEventListener()),
[email protected]89ebc7e2009-08-24 22:11:07535 extension_devtools_manager_(NULL),
[email protected]f7011fcb2009-01-28 21:54:32536 request_context_(NULL),
[email protected]363347b2009-03-13 20:06:57537 media_request_context_(NULL),
[email protected]47accfd62009-05-14 18:46:21538 extensions_request_context_(NULL),
[email protected]eaadd9052009-06-23 18:02:23539 blacklist_(NULL),
[email protected]f7011fcb2009-01-28 21:54:32540 history_service_created_(false),
[email protected]0189bc722009-08-28 21:56:48541 favicon_service_created_(false),
[email protected]f7011fcb2009-01-28 21:54:32542 created_web_data_service_(false),
[email protected]e69d33952009-06-03 22:00:41543 created_password_store_(false),
[email protected]f7011fcb2009-01-28 21:54:32544 created_download_manager_(false),
[email protected]4a190632009-05-09 01:07:42545 created_theme_provider_(false),
[email protected]f7011fcb2009-01-28 21:54:32546 start_time_(Time::Now()),
547 spellchecker_(NULL),
initial.commit09911bf2008-07-26 23:55:29548 shutdown_session_service_(false) {
549 DCHECK(!path.empty()) << "Using an empty path will attempt to write " <<
550 "profile files to the root directory!";
[email protected]2d316662008-09-03 18:18:14551 create_session_service_timer_.Start(
552 TimeDelta::FromMilliseconds(kCreateSessionServiceDelayMS), this,
553 &ProfileImpl::EnsureSessionServiceCreated);
[email protected]bdbc87c2009-01-25 05:08:54554
[email protected]89ebc7e2009-08-24 22:11:07555 if (CommandLine::ForCurrentProcess()->HasSwitch(
556 switches::kEnableExtensionTimelineApi)) {
557 extension_devtools_manager_ = new ExtensionDevToolsManager(this);
558 }
559
[email protected]382a0702009-06-26 17:12:27560 extension_process_manager_.reset(new ExtensionProcessManager(this));
[email protected]7120f132009-07-20 21:05:37561 extension_message_service_ = new ExtensionMessageService(this);
[email protected]382a0702009-06-26 17:12:27562
[email protected]e7244d82008-10-29 18:13:26563 PrefService* prefs = GetPrefs();
564 prefs->AddPrefObserver(prefs::kSpellCheckDictionary, this);
565 prefs->AddPrefObserver(prefs::kEnableSpellCheck, this);
[email protected]154a4332009-06-03 20:20:58566 prefs->AddPrefObserver(prefs::kEnableAutoSpellCorrect, this);
[email protected]380ab462009-04-24 01:23:58567
[email protected]eaadd9052009-06-23 18:02:23568 if (CommandLine::ForCurrentProcess()->
569 HasSwitch(switches::kPrivacyBlacklist)) {
570 std::wstring option = CommandLine::ForCurrentProcess()->GetSwitchValue(
571 switches::kPrivacyBlacklist);
572#if defined(OS_POSIX)
573 FilePath path(WideToUTF8(option));
574#else
575 FilePath path(option);
576#endif
577 blacklist_ = new Blacklist(path);
578 }
579
[email protected]bc96d562009-08-11 18:45:13580 if (!PathService::IsOverridden(chrome::DIR_USER_DATA))
581 PathService::Get(chrome::DIR_USER_CACHE, &base_cache_path_);
582 if (base_cache_path_.empty())
583 base_cache_path_ = path_;
584
[email protected]4a190632009-05-09 01:07:42585 // Listen for theme installation.
[email protected]88cf3292009-05-22 01:48:43586 registrar_.Add(this, NotificationType::THEME_INSTALLED,
587 NotificationService::AllSources());
[email protected]48352c12009-08-15 01:19:11588
589 // Listen for bookmark model load, to bootstrap the sync service.
590 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
591 Source<Profile>(this));
[email protected]db36938c2009-08-19 21:48:42592
593 ssl_config_service_manager_.reset(
594 SSLConfigServiceManager::CreateDefaultManager(this));
initial.commit09911bf2008-07-26 23:55:29595}
596
[email protected]bdbc87c2009-01-25 05:08:54597void ProfileImpl::InitExtensions() {
[email protected]8c756ac2009-01-30 23:36:41598 if (user_script_master_ || extensions_service_)
599 return; // Already initialized.
600
[email protected]bdbc87c2009-01-25 05:08:54601 const CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]f93fe782009-02-19 01:26:13602 PrefService* prefs = GetPrefs();
[email protected]bdbc87c2009-01-25 05:08:54603 bool user_scripts_enabled =
[email protected]f0a51fb52009-03-05 12:46:38604 command_line->HasSwitch(switches::kEnableUserScripts) ||
[email protected]f93fe782009-02-19 01:26:13605 prefs->GetBoolean(prefs::kEnableUserScripts);
[email protected]bdbc87c2009-01-25 05:08:54606
[email protected]f7011fcb2009-01-28 21:54:32607 FilePath script_dir;
[email protected]bdbc87c2009-01-25 05:08:54608 if (user_scripts_enabled) {
[email protected]0cd957b2009-03-06 20:13:23609 if (command_line->HasSwitch(switches::kUserScriptsDir)) {
610 std::wstring path_string =
611 command_line->GetSwitchValue(switches::kUserScriptsDir);
612 script_dir = FilePath::FromWStringHack(path_string);
613 } else {
614 script_dir = GetPath();
615 script_dir = script_dir.Append(chrome::kUserScriptsDirname);
616 }
[email protected]bdbc87c2009-01-25 05:08:54617 }
618
[email protected]bb28e062009-02-27 17:19:18619 ExtensionErrorReporter::Init(true); // allow noisy errors.
[email protected]bdbc87c2009-01-25 05:08:54620 user_script_master_ = new UserScriptMaster(
[email protected]9197f3b2009-06-02 00:49:27621 g_browser_process->file_thread()->message_loop(),
622 script_dir);
[email protected]894bb502009-05-21 22:39:57623 extensions_service_ = new ExtensionsService(
[email protected]a9b00ac2009-06-25 21:03:23624 this,
625 CommandLine::ForCurrentProcess(),
626 GetPrefs(),
627 GetPath().AppendASCII(ExtensionsService::kInstallDirectoryName),
628 MessageLoop::current(),
[email protected]93fd78f42009-07-10 16:43:17629 g_browser_process->file_thread()->message_loop(),
[email protected]c8b437d2009-07-20 21:26:36630 true);
[email protected]bdbc87c2009-01-25 05:08:54631
[email protected]9197f3b2009-06-02 00:49:27632 extensions_service_->Init();
[email protected]919ddc82009-07-15 04:30:12633
634 // Load any extensions specified with --load-extension.
635 if (command_line->HasSwitch(switches::kLoadExtension)) {
636 std::wstring path_string =
637 command_line->GetSwitchValue(switches::kLoadExtension);
638 FilePath path = FilePath::FromWStringHack(path_string);
639 extensions_service_->LoadExtension(path);
640
641 // Tell UserScriptMaser to watch this extension's directory for changes so
642 // you can live edit content scripts during development.
643 user_script_master_->AddWatchedPath(path);
644 }
[email protected]bdbc87c2009-01-25 05:08:54645}
646
[email protected]ea0c98cf2009-06-18 23:02:54647void ProfileImpl::InitWebResources() {
[email protected]0c274c202009-07-12 02:59:07648 if (web_resource_service_)
649 return; // Already initialized.
650
[email protected]ea0c98cf2009-06-18 23:02:54651 web_resource_service_ = new WebResourceService(
652 this,
653 g_browser_process->file_thread()->message_loop());
654
655 web_resource_service_->StartAfterDelay();
656}
657
initial.commit09911bf2008-07-26 23:55:29658ProfileImpl::~ProfileImpl() {
[email protected]169627b2008-12-06 19:30:19659 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29660
661 StopCreateSessionServiceTimer();
662 // TemplateURLModel schedules a task on the WebDataService from its
663 // destructor. Delete it first to ensure the task gets scheduled before we
664 // shut down the database.
665 template_url_model_.reset();
666
667 // The download manager queries the history system and should be deleted
668 // before the history is shutdown so it can properly cancel all requests.
669 download_manager_ = NULL;
670
[email protected]4a190632009-05-09 01:07:42671 // The theme provider provides bitmaps to whoever wants them.
672 theme_provider_ = NULL;
673
[email protected]44961292009-07-20 20:03:09674 // The ThumbnailStore saves thumbnails used by the NTP. Call Shutdown to
675 // save any new thumbnails to disk and release its reference to the
676 // HistoryService.
677 if (thumbnail_store_.get())
678 thumbnail_store_->Shutdown();
679
[email protected]e7244d82008-10-29 18:13:26680 // Remove pref observers.
681 PrefService* prefs = GetPrefs();
682 prefs->RemovePrefObserver(prefs::kSpellCheckDictionary, this);
683 prefs->RemovePrefObserver(prefs::kEnableSpellCheck, this);
[email protected]154a4332009-06-03 20:20:58684 prefs->RemovePrefObserver(prefs::kEnableAutoSpellCorrect, this);
[email protected]e7244d82008-10-29 18:13:26685
[email protected]3a453fa2008-08-15 18:46:34686#ifdef CHROME_PERSONALIZATION
[email protected]48352c12009-08-15 01:19:11687 sync_service_.reset();
[email protected]3a453fa2008-08-15 18:46:34688#endif
689
initial.commit09911bf2008-07-26 23:55:29690 // Both HistoryService and WebDataService maintain threads for background
691 // processing. Its possible each thread still has tasks on it that have
692 // increased the ref count of the service. In such a situation, when we
693 // decrement the refcount, it won't be 0, and the threads/databases aren't
694 // properly shut down. By explicitly calling Cleanup/Shutdown we ensure the
695 // databases are properly closed.
696 if (web_data_service_.get())
697 web_data_service_->Shutdown();
698
699 if (history_service_.get())
700 history_service_->Cleanup();
701
702 // The I/O thread may be NULL during testing.
[email protected]ab820df2008-08-26 05:55:10703 base::Thread* io_thread = g_browser_process->io_thread();
initial.commit09911bf2008-07-26 23:55:29704
705 if (spellchecker_) {
706 // The spellchecker must be deleted on the I/O thread. During testing, we
707 // don't have an I/O thread.
708 if (io_thread)
709 io_thread->message_loop()->ReleaseSoon(FROM_HERE, spellchecker_);
710 else
711 spellchecker_->Release();
712 }
713
[email protected]2a0c0a52009-07-31 07:51:32714 if (default_request_context_ == request_context_) {
715#if defined(OS_LINUX)
716 // We use default_request_context_ for OCSP.
717 // Release URLRequestContext used in OCSP handlers.
718 net::SetURLRequestContextForOCSP(NULL);
719#endif
[email protected]47accfd62009-05-14 18:46:21720 default_request_context_ = NULL;
[email protected]2a0c0a52009-07-31 07:51:32721 }
[email protected]6ab9b202008-12-23 22:34:50722
[email protected]47accfd62009-05-14 18:46:21723 CleanupRequestContext(request_context_);
724 CleanupRequestContext(media_request_context_);
725 CleanupRequestContext(extensions_request_context_);
[email protected]23f1ef12009-09-01 22:30:30726 CleanupAppCacheService(appcache_service_.release());
[email protected]1e744f22009-04-08 01:00:17727
[email protected]eaadd9052009-06-23 18:02:23728 // When the request contexts are gone, the blacklist wont be needed anymore.
729 delete blacklist_;
730 blacklist_ = 0;
731
[email protected]d8e41ed2008-09-11 15:22:32732 // HistoryService may call into the BookmarkModel, as such we need to
733 // delete HistoryService before the BookmarkModel. The destructor for
[email protected]90ef13132008-08-27 03:27:46734 // HistoryService will join with HistoryService's backend thread so that
735 // by the time the destructor has finished we're sure it will no longer call
[email protected]d8e41ed2008-09-11 15:22:32736 // into the BookmarkModel.
[email protected]90ef13132008-08-27 03:27:46737 history_service_ = NULL;
738 bookmark_bar_model_.reset();
739
[email protected]0189bc722009-08-28 21:56:48740 // FaviconService depends on HistoryServce so make sure we delete
741 // HistoryService first.
742 favicon_service_ = NULL;
743
[email protected]7120f132009-07-20 21:05:37744 extension_message_service_->ProfileDestroyed();
745
[email protected]6ef635e42009-07-26 06:16:12746 if (extensions_service_)
747 extensions_service_->ProfileDestroyed();
748
initial.commit09911bf2008-07-26 23:55:29749 MarkAsCleanShutdown();
750}
751
[email protected]f7011fcb2009-01-28 21:54:32752FilePath ProfileImpl::GetPath() {
initial.commit09911bf2008-07-26 23:55:29753 return path_;
754}
755
756bool ProfileImpl::IsOffTheRecord() {
757 return false;
758}
759
760Profile* ProfileImpl::GetOffTheRecordProfile() {
761 if (!off_the_record_profile_.get()) {
762 scoped_ptr<OffTheRecordProfileImpl> p(new OffTheRecordProfileImpl(this));
763 off_the_record_profile_.swap(p);
764 }
765 return off_the_record_profile_.get();
766}
767
[email protected]860f55492009-03-27 19:50:59768void ProfileImpl::DestroyOffTheRecordProfile() {
769 off_the_record_profile_.reset();
770}
771
initial.commit09911bf2008-07-26 23:55:29772Profile* ProfileImpl::GetOriginalProfile() {
773 return this;
774}
775
[email protected]23f1ef12009-09-01 22:30:30776ChromeAppCacheService* ProfileImpl::GetAppCacheService() {
777 if (!appcache_service_.get()) {
778 appcache_service_ = new ChromeAppCacheService();
779 appcache_service_->InitializeOnUIThread(GetPath(), false);
780 }
781 return appcache_service_.get();
782}
783
initial.commit09911bf2008-07-26 23:55:29784VisitedLinkMaster* ProfileImpl::GetVisitedLinkMaster() {
785 if (!visited_link_master_.get()) {
786 scoped_ptr<VisitedLinkMaster> visited_links(
787 new VisitedLinkMaster(g_browser_process->file_thread(),
[email protected]3e90d4a2009-07-03 17:38:39788 visited_link_event_listener_.get(), this));
initial.commit09911bf2008-07-26 23:55:29789 if (!visited_links->Init())
790 return NULL;
791 visited_link_master_.swap(visited_links);
792 }
793
794 return visited_link_master_.get();
795}
796
[email protected]6014d672008-12-05 00:38:25797ExtensionsService* ProfileImpl::GetExtensionsService() {
798 return extensions_service_.get();
799}
800
[email protected]0938d3c2009-01-09 20:37:35801UserScriptMaster* ProfileImpl::GetUserScriptMaster() {
[email protected]0938d3c2009-01-09 20:37:35802 return user_script_master_.get();
[email protected]04fba9a92008-10-28 17:25:25803}
804
[email protected]89ebc7e2009-08-24 22:11:07805ExtensionDevToolsManager* ProfileImpl::GetExtensionDevToolsManager() {
806 return extension_devtools_manager_.get();
807}
808
[email protected]481e1a42009-05-06 20:56:05809ExtensionProcessManager* ProfileImpl::GetExtensionProcessManager() {
[email protected]382a0702009-06-26 17:12:27810 return extension_process_manager_.get();
[email protected]481e1a42009-05-06 20:56:05811}
812
[email protected]7120f132009-07-20 21:05:37813ExtensionMessageService* ProfileImpl::GetExtensionMessageService() {
814 return extension_message_service_.get();
815}
816
[email protected]34cc84f2009-02-13 10:04:35817SSLHostState* ProfileImpl::GetSSLHostState() {
818 if (!ssl_host_state_.get())
819 ssl_host_state_.reset(new SSLHostState());
820
821 DCHECK(ssl_host_state_->CalledOnValidThread());
822 return ssl_host_state_.get();
823}
824
[email protected]a9cea7542009-05-20 04:30:23825net::ForceTLSState* ProfileImpl::GetForceTLSState() {
826 if (!force_tls_state_.get())
827 force_tls_state_.reset(new net::ForceTLSState());
828
829 return force_tls_state_.get();
830}
831
initial.commit09911bf2008-07-26 23:55:29832PrefService* ProfileImpl::GetPrefs() {
833 if (!prefs_.get()) {
[email protected]6faa0e0d2009-04-28 06:50:36834 prefs_.reset(new PrefService(GetPrefFilePath(),
835 g_browser_process->file_thread()));
initial.commit09911bf2008-07-26 23:55:29836
837 // The Profile class and ProfileManager class may read some prefs so
838 // register known prefs as soon as possible.
839 Profile::RegisterUserPrefs(prefs_.get());
840 ProfileManager::RegisterUserPrefs(prefs_.get());
841
842 // The last session exited cleanly if there is no pref for
843 // kSessionExitedCleanly or the value for kSessionExitedCleanly is true.
844 last_session_exited_cleanly_ =
845 prefs_->GetBoolean(prefs::kSessionExitedCleanly);
846 // Mark the session as open.
847 prefs_->SetBoolean(prefs::kSessionExitedCleanly, false);
848 // Make sure we save to disk that the session has opened.
[email protected]6faa0e0d2009-04-28 06:50:36849 prefs_->ScheduleSavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29850 }
851
852 return prefs_.get();
853}
854
[email protected]f7011fcb2009-01-28 21:54:32855FilePath ProfileImpl::GetPrefFilePath() {
856 FilePath pref_file_path = path_;
857 pref_file_path = pref_file_path.Append(chrome::kPreferencesFilename);
initial.commit09911bf2008-07-26 23:55:29858 return pref_file_path;
859}
860
861URLRequestContext* ProfileImpl::GetRequestContext() {
862 if (!request_context_) {
[email protected]f7011fcb2009-01-28 21:54:32863 FilePath cookie_path = GetPath();
864 cookie_path = cookie_path.Append(chrome::kCookieFilename);
[email protected]bc96d562009-08-11 18:45:13865 FilePath cache_path = base_cache_path_;
[email protected]e5642992009-06-26 23:06:31866 int max_size;
867 GetCacheParameters(kNormalContext, &cache_path, &max_size);
[email protected]2b2830a2009-02-07 01:58:42868
[email protected]f7011fcb2009-01-28 21:54:32869 cache_path = cache_path.Append(chrome::kCacheDirname);
[email protected]6ab9b202008-12-23 22:34:50870 request_context_ = ChromeURLRequestContext::CreateOriginal(
[email protected]e5642992009-06-26 23:06:31871 this, cookie_path, cache_path, max_size);
initial.commit09911bf2008-07-26 23:55:29872 request_context_->AddRef();
873
[email protected]6ab9b202008-12-23 22:34:50874 // The first request context is always a normal (non-OTR) request context.
875 // Even when Chromium is started in OTR mode, a normal profile is always
876 // created first.
877 if (!default_request_context_) {
878 default_request_context_ = request_context_;
879 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56880 NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE,
[email protected]6ab9b202008-12-23 22:34:50881 NotificationService::AllSources(), NotificationService::NoDetails());
[email protected]2a0c0a52009-07-31 07:51:32882#if defined(OS_LINUX)
883 // TODO(ukai): find a better way to set the URLRequestContext for OCSP.
884 net::SetURLRequestContextForOCSP(default_request_context_);
885#endif
[email protected]6ab9b202008-12-23 22:34:50886 }
887
initial.commit09911bf2008-07-26 23:55:29888 DCHECK(request_context_->cookie_store());
889 }
890
891 return request_context_;
892}
893
[email protected]e7f29642009-03-02 22:53:18894URLRequestContext* ProfileImpl::GetRequestContextForMedia() {
895 if (!media_request_context_) {
[email protected]bc96d562009-08-11 18:45:13896 FilePath cache_path = base_cache_path_;
[email protected]e5642992009-06-26 23:06:31897 int max_size;
898 GetCacheParameters(kMediaContext, &cache_path, &max_size);
[email protected]e3edeba2009-03-23 18:57:14899
[email protected]4e7b5dbb2009-03-25 00:09:23900 cache_path = cache_path.Append(chrome::kMediaCacheDirname);
[email protected]e7f29642009-03-02 22:53:18901 media_request_context_ = ChromeURLRequestContext::CreateOriginalForMedia(
[email protected]e5642992009-06-26 23:06:31902 this, cache_path, max_size);
[email protected]e7f29642009-03-02 22:53:18903 media_request_context_->AddRef();
904
905 DCHECK(media_request_context_->cookie_store());
906 }
907
908 return media_request_context_;
909}
910
[email protected]0189bc722009-08-28 21:56:48911FaviconService* ProfileImpl::GetFaviconService(ServiceAccessType sat) {
912 if (!favicon_service_created_) {
913 favicon_service_created_ = true;
914 scoped_refptr<FaviconService> service(new FaviconService(this));
915 favicon_service_.swap(service);
916 }
917 return favicon_service_.get();
918}
919
[email protected]47accfd62009-05-14 18:46:21920URLRequestContext* ProfileImpl::GetRequestContextForExtensions() {
921 if (!extensions_request_context_) {
922 FilePath cookie_path = GetPath();
923 cookie_path = cookie_path.Append(chrome::kExtensionsCookieFilename);
924
925 extensions_request_context_ =
926 ChromeURLRequestContext::CreateOriginalForExtensions(this, cookie_path);
927 extensions_request_context_->AddRef();
928
929 DCHECK(extensions_request_context_->cookie_store());
930 }
931
932 return extensions_request_context_;
933}
934
[email protected]db36938c2009-08-19 21:48:42935net::SSLConfigService* ProfileImpl::GetSSLConfigService() {
936 return ssl_config_service_manager_->Get();
937}
938
[email protected]eaadd9052009-06-23 18:02:23939Blacklist* ProfileImpl::GetBlacklist() {
940 return blacklist_;
941}
942
initial.commit09911bf2008-07-26 23:55:29943HistoryService* ProfileImpl::GetHistoryService(ServiceAccessType sat) {
944 if (!history_service_created_) {
[email protected]90ef13132008-08-27 03:27:46945 history_service_created_ = true;
initial.commit09911bf2008-07-26 23:55:29946 scoped_refptr<HistoryService> history(new HistoryService(this));
[email protected]d8e41ed2008-09-11 15:22:32947 if (!history->Init(GetPath(), GetBookmarkModel()))
initial.commit09911bf2008-07-26 23:55:29948 return NULL;
949 history_service_.swap(history);
initial.commit09911bf2008-07-26 23:55:29950
951 // Send out the notification that the history service was created.
952 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56953 Notify(NotificationType::HISTORY_CREATED, Source<Profile>(this),
initial.commit09911bf2008-07-26 23:55:29954 Details<HistoryService>(history_service_.get()));
955 }
956 return history_service_.get();
957}
958
initial.commit09911bf2008-07-26 23:55:29959TemplateURLModel* ProfileImpl::GetTemplateURLModel() {
960 if (!template_url_model_.get())
961 template_url_model_.reset(new TemplateURLModel(this));
962 return template_url_model_.get();
963}
964
965TemplateURLFetcher* ProfileImpl::GetTemplateURLFetcher() {
966 if (!template_url_fetcher_.get())
967 template_url_fetcher_.reset(new TemplateURLFetcher(this));
968 return template_url_fetcher_.get();
969}
970
971WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) {
972 if (!created_web_data_service_)
973 CreateWebDataService();
974 return web_data_service_.get();
975}
976
977void ProfileImpl::CreateWebDataService() {
978 DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL);
979 created_web_data_service_ = true;
980 scoped_refptr<WebDataService> wds(new WebDataService());
981 if (!wds->Init(GetPath()))
982 return;
983 web_data_service_.swap(wds);
984}
985
[email protected]e69d33952009-06-03 22:00:41986PasswordStore* ProfileImpl::GetPasswordStore(ServiceAccessType sat) {
987 if (!created_password_store_)
988 CreatePasswordStore();
989 return password_store_.get();
990}
991
992void ProfileImpl::CreatePasswordStore() {
993 DCHECK(!created_password_store_ && password_store_.get() == NULL);
994 created_password_store_ = true;
995 scoped_refptr<PasswordStore> ps;
996#if defined(OS_LINUX)
997// Temporarily disabled while we figure some stuff out.
998// http://code.google.com/p/chromium/issues/detail?id=12351
999// if (getenv("KDE_FULL_SESSION")) {
1000// ps = new PasswordStoreKWallet();
1001// } else {
1002// ps = new PasswordStoreGnome();
1003// }
1004 NOTIMPLEMENTED();
1005#elif defined(OS_WIN)
1006 ps = new PasswordStoreWin(GetWebDataService(Profile::IMPLICIT_ACCESS));
[email protected]034eba52009-06-03 22:50:561007#elif defined(OS_MACOSX)
[email protected]e0411ae52009-06-30 23:59:171008 FilePath login_db_file_path = GetPath();
1009 login_db_file_path = login_db_file_path.Append(chrome::kLoginDataFileName);
1010 LoginDatabaseMac* login_db = new LoginDatabaseMac();
1011 if (!login_db->Init(login_db_file_path)) {
1012 LOG(ERROR) << "Could not initialize login database.";
1013 delete login_db;
1014 return;
1015 }
1016 ps = new PasswordStoreMac(new MacKeychain(), login_db);
[email protected]e69d33952009-06-03 22:00:411017#else
1018 NOTIMPLEMENTED();
1019#endif
1020 if (!ps || !ps->Init()) {
1021 // Try falling back to the default password manager
1022 LOG(WARNING) << "Could not initialise native password manager - "
1023 "falling back to default";
1024 ps = new PasswordStoreDefault(GetWebDataService(Profile::IMPLICIT_ACCESS));
1025 if (!ps->Init())
1026 return;
1027 }
1028 password_store_.swap(ps);
1029}
1030
initial.commit09911bf2008-07-26 23:55:291031DownloadManager* ProfileImpl::GetDownloadManager() {
1032 if (!created_download_manager_) {
1033 scoped_refptr<DownloadManager> dlm(new DownloadManager);
1034 dlm->Init(this);
1035 created_download_manager_ = true;
1036 download_manager_.swap(dlm);
1037 }
1038 return download_manager_.get();
1039}
1040
1041bool ProfileImpl::HasCreatedDownloadManager() const {
1042 return created_download_manager_;
1043}
1044
[email protected]4a190632009-05-09 01:07:421045void ProfileImpl::InitThemes() {
1046 if (!created_theme_provider_) {
[email protected]0ef42ff92009-07-06 20:17:351047#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
[email protected]a5166af62009-07-03 00:42:291048 scoped_refptr<BrowserThemeProvider> themes(new GtkThemeProvider);
1049#else
[email protected]4a190632009-05-09 01:07:421050 scoped_refptr<BrowserThemeProvider> themes(new BrowserThemeProvider);
[email protected]a5166af62009-07-03 00:42:291051#endif
[email protected]4a190632009-05-09 01:07:421052 themes->Init(this);
1053 created_theme_provider_ = true;
1054 theme_provider_.swap(themes);
1055 }
1056}
1057
1058void ProfileImpl::SetTheme(Extension* extension) {
1059 InitThemes();
1060 theme_provider_.get()->SetTheme(extension);
1061}
1062
[email protected]a5166af62009-07-03 00:42:291063void ProfileImpl::SetNativeTheme() {
1064 InitThemes();
1065 theme_provider_.get()->SetNativeTheme();
1066}
1067
[email protected]4a190632009-05-09 01:07:421068void ProfileImpl::ClearTheme() {
1069 InitThemes();
1070 theme_provider_.get()->UseDefaultTheme();
1071}
1072
[email protected]51c490b2009-08-03 16:34:341073Extension* ProfileImpl::GetTheme() {
1074 InitThemes();
1075
1076 std::string id = theme_provider_.get()->GetThemeID();
1077 if (id == BrowserThemeProvider::kDefaultThemeID)
1078 return NULL;
1079
1080 return extensions_service_->GetExtensionById(id);
1081}
1082
[email protected]4a190632009-05-09 01:07:421083ThemeProvider* ProfileImpl::GetThemeProvider() {
1084 InitThemes();
1085 return theme_provider_.get();
1086}
1087
initial.commit09911bf2008-07-26 23:55:291088SessionService* ProfileImpl::GetSessionService() {
1089 if (!session_service_.get() && !shutdown_session_service_) {
1090 session_service_ = new SessionService(this);
1091 session_service_->ResetFromCurrentBrowsers();
1092 }
1093 return session_service_.get();
1094}
1095
1096void ProfileImpl::ShutdownSessionService() {
1097 if (shutdown_session_service_)
1098 return;
1099
1100 // We're about to exit, force creation of the session service if it hasn't
1101 // been created yet. We do this to ensure session state matches the point in
1102 // time the user exited.
1103 GetSessionService();
1104 shutdown_session_service_ = true;
1105 session_service_ = NULL;
1106}
1107
1108bool ProfileImpl::HasSessionService() const {
1109 return (session_service_.get() != NULL);
1110}
1111
1112std::wstring ProfileImpl::GetName() {
1113 return GetPrefs()->GetString(prefs::kProfileName);
1114}
1115void ProfileImpl::SetName(const std::wstring& name) {
1116 GetPrefs()->SetString(prefs::kProfileName, name);
1117}
1118
1119std::wstring ProfileImpl::GetID() {
1120 return GetPrefs()->GetString(prefs::kProfileID);
1121}
1122void ProfileImpl::SetID(const std::wstring& id) {
1123 GetPrefs()->SetString(prefs::kProfileID, id);
1124}
1125
initial.commit09911bf2008-07-26 23:55:291126bool ProfileImpl::DidLastSessionExitCleanly() {
1127 // last_session_exited_cleanly_ is set when the preferences are loaded. Force
1128 // it to be set by asking for the prefs.
1129 GetPrefs();
1130 return last_session_exited_cleanly_;
1131}
1132
[email protected]d8e41ed2008-09-11 15:22:321133BookmarkModel* ProfileImpl::GetBookmarkModel() {
[email protected]90ef13132008-08-27 03:27:461134 if (!bookmark_bar_model_.get()) {
[email protected]d8e41ed2008-09-11 15:22:321135 bookmark_bar_model_.reset(new BookmarkModel(this));
[email protected]90ef13132008-08-27 03:27:461136 bookmark_bar_model_->Load();
1137 }
initial.commit09911bf2008-07-26 23:55:291138 return bookmark_bar_model_.get();
1139}
1140
1141bool ProfileImpl::IsSameProfile(Profile* profile) {
1142 if (profile == static_cast<Profile*>(this))
1143 return true;
1144 OffTheRecordProfileImpl* otr_profile = off_the_record_profile_.get();
1145 return otr_profile && profile == static_cast<Profile*>(otr_profile);
1146}
1147
1148Time ProfileImpl::GetStartTime() const {
1149 return start_time_;
1150}
1151
1152TabRestoreService* ProfileImpl::GetTabRestoreService() {
1153 if (!tab_restore_service_.get())
[email protected]169627b2008-12-06 19:30:191154 tab_restore_service_ = new TabRestoreService(this);
initial.commit09911bf2008-07-26 23:55:291155 return tab_restore_service_.get();
1156}
1157
[email protected]449478302009-06-09 20:04:281158ThumbnailStore* ProfileImpl::GetThumbnailStore() {
1159 if (!thumbnail_store_.get()) {
1160 thumbnail_store_ = new ThumbnailStore;
[email protected]daa82dc2009-07-28 01:22:251161 thumbnail_store_->Init(
1162 GetPath().Append(chrome::kNewTabThumbnailsFilename), this);
[email protected]449478302009-06-09 20:04:281163 }
1164 return thumbnail_store_.get();
1165}
1166
initial.commit09911bf2008-07-26 23:55:291167void ProfileImpl::ResetTabRestoreService() {
[email protected]169627b2008-12-06 19:30:191168 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:291169}
1170
[email protected]a9afddb2009-02-12 17:49:421171// To be run in the IO thread to notify all resource message filters that the
[email protected]20930852008-10-15 19:30:411172// spellchecker has changed.
1173class NotifySpellcheckerChangeTask : public Task {
1174 public:
1175 NotifySpellcheckerChangeTask(
[email protected]e7244d82008-10-29 18:13:261176 Profile* profile,
1177 const SpellcheckerReinitializedDetails& spellchecker)
[email protected]20930852008-10-15 19:30:411178 : profile_(profile),
1179 spellchecker_(spellchecker) {
1180 }
1181
1182 private:
1183 void Run(void) {
1184 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:561185 NotificationType::SPELLCHECKER_REINITIALIZED,
[email protected]20930852008-10-15 19:30:411186 Source<Profile>(profile_),
1187 Details<SpellcheckerReinitializedDetails>(&spellchecker_));
1188 }
1189
1190 Profile* profile_;
1191 SpellcheckerReinitializedDetails spellchecker_;
1192};
1193
[email protected]e7244d82008-10-29 18:13:261194void ProfileImpl::InitializeSpellChecker(bool need_to_broadcast) {
[email protected]20930852008-10-15 19:30:411195 // The I/O thread may be NULL during testing.
1196 base::Thread* io_thread = g_browser_process->io_thread();
1197 if (spellchecker_) {
1198 // The spellchecker must be deleted on the I/O thread.
1199 // A dummy variable to aid in logical clarity.
1200 SpellChecker* last_spellchecker = spellchecker_;
1201
1202 if (io_thread)
1203 io_thread->message_loop()->ReleaseSoon(FROM_HERE, last_spellchecker);
1204 else // during testing, we don't have an I/O thread
1205 last_spellchecker->Release();
[email protected]20930852008-10-15 19:30:411206 }
1207
[email protected]20930852008-10-15 19:30:411208 // Retrieve the (perhaps updated recently) dictionary name from preferences.
1209 PrefService* prefs = GetPrefs();
[email protected]e7244d82008-10-29 18:13:261210 bool enable_spellcheck = prefs->GetBoolean(prefs::kEnableSpellCheck);
[email protected]20930852008-10-15 19:30:411211
[email protected]e7244d82008-10-29 18:13:261212 if (enable_spellcheck) {
[email protected]bd17b702009-02-25 20:44:081213 FilePath dict_dir;
[email protected]e7244d82008-10-29 18:13:261214 PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
[email protected]a9afddb2009-02-12 17:49:421215 // Note that, as the object pointed to by previously by spellchecker_
[email protected]e7244d82008-10-29 18:13:261216 // is being deleted in the io thread, the spellchecker_ can be made to point
1217 // to a new object (RE-initialized) in parallel in this UI thread.
[email protected]4b4d1adc2008-12-10 22:28:581218 spellchecker_ = new SpellChecker(dict_dir,
[email protected]74c8b422009-03-11 00:34:121219 WideToASCII(prefs->GetString(prefs::kSpellCheckDictionary)),
1220 GetRequestContext(),
[email protected]bd17b702009-02-25 20:44:081221 FilePath());
[email protected]e7244d82008-10-29 18:13:261222 spellchecker_->AddRef(); // Manual refcounting.
1223 } else {
1224 spellchecker_ = NULL;
1225 }
[email protected]20930852008-10-15 19:30:411226
[email protected]154a4332009-06-03 20:20:581227 // Set auto spell correct status for spellchecker.
1228 if (spellchecker_) {
1229 spellchecker_->EnableAutoSpellCorrect(
1230 prefs->GetBoolean(prefs::kEnableAutoSpellCorrect));
1231 }
1232
[email protected]20930852008-10-15 19:30:411233 if (need_to_broadcast && io_thread) { // Notify resource message filters.
1234 SpellcheckerReinitializedDetails scoped_spellchecker;
1235 scoped_spellchecker.spellchecker = spellchecker_;
[email protected]e7244d82008-10-29 18:13:261236 if (io_thread) {
1237 io_thread->message_loop()->PostTask(
[email protected]a9afddb2009-02-12 17:49:421238 FROM_HERE,
[email protected]e7244d82008-10-29 18:13:261239 new NotifySpellcheckerChangeTask(this, scoped_spellchecker));
1240 }
[email protected]20930852008-10-15 19:30:411241 }
1242}
1243
[email protected]e7244d82008-10-29 18:13:261244void ProfileImpl::ReinitializeSpellChecker() {
1245 InitializeSpellChecker(true);
1246}
1247
initial.commit09911bf2008-07-26 23:55:291248SpellChecker* ProfileImpl::GetSpellChecker() {
1249 if (!spellchecker_) {
[email protected]20930852008-10-15 19:30:411250 // This is where spellchecker gets initialized. Note that this is being
1251 // initialized in the ui_thread. However, this is not a problem as long as
1252 // it is *used* in the io thread.
[email protected]34cc84f2009-02-13 10:04:351253 // TODO(sidchat): One day, change everything so that spellchecker gets
[email protected]20930852008-10-15 19:30:411254 // initialized in the IO thread itself.
[email protected]e7244d82008-10-29 18:13:261255 InitializeSpellChecker(false);
initial.commit09911bf2008-07-26 23:55:291256 }
[email protected]20930852008-10-15 19:30:411257
initial.commit09911bf2008-07-26 23:55:291258 return spellchecker_;
1259}
1260
[email protected]3bf335a2009-06-26 20:46:061261WebKitContext* ProfileImpl::GetWebKitContext() {
1262 if (!webkit_context_.get())
1263 webkit_context_ = new WebKitContext(path_, false);
1264 DCHECK(webkit_context_.get());
1265 return webkit_context_.get();
1266}
1267
initial.commit09911bf2008-07-26 23:55:291268void ProfileImpl::MarkAsCleanShutdown() {
1269 if (prefs_.get()) {
1270 // The session cleanly exited, set kSessionExitedCleanly appropriately.
1271 prefs_->SetBoolean(prefs::kSessionExitedCleanly, true);
1272
1273 // NOTE: If you change what thread this writes on, be sure and update
1274 // ChromeFrame::EndSession().
[email protected]6faa0e0d2009-04-28 06:50:361275 prefs_->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:291276 }
1277}
1278
[email protected]e7244d82008-10-29 18:13:261279void ProfileImpl::Observe(NotificationType type,
1280 const NotificationSource& source,
1281 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:561282 if (NotificationType::PREF_CHANGED == type) {
[email protected]e7244d82008-10-29 18:13:261283 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
1284 PrefService* prefs = Source<PrefService>(source).ptr();
1285 DCHECK(pref_name_in && prefs);
1286 if (*pref_name_in == prefs::kSpellCheckDictionary ||
[email protected]154a4332009-06-03 20:20:581287 *pref_name_in == prefs::kEnableSpellCheck ||
1288 *pref_name_in == prefs::kEnableAutoSpellCorrect) {
[email protected]e7244d82008-10-29 18:13:261289 InitializeSpellChecker(true);
1290 }
[email protected]4a190632009-05-09 01:07:421291 } else if (NotificationType::THEME_INSTALLED == type) {
1292 Extension* extension = Details<Extension>(details).ptr();
1293 SetTheme(extension);
[email protected]48352c12009-08-15 01:19:111294 } else if (NotificationType::BOOKMARK_MODEL_LOADED == type) {
1295 GetProfileSyncService(); // Causes lazy-load if sync is enabled.
1296 registrar_.Remove(this, NotificationType::BOOKMARK_MODEL_LOADED,
1297 Source<Profile>(this));
[email protected]e7244d82008-10-29 18:13:261298 }
1299}
1300
initial.commit09911bf2008-07-26 23:55:291301void ProfileImpl::StopCreateSessionServiceTimer() {
[email protected]2d316662008-09-03 18:18:141302 create_session_service_timer_.Stop();
initial.commit09911bf2008-07-26 23:55:291303}
[email protected]3a453fa2008-08-15 18:46:341304
[email protected]48352c12009-08-15 01:19:111305ProfileSyncService* ProfileImpl::GetProfileSyncService() {
[email protected]3a453fa2008-08-15 18:46:341306#ifdef CHROME_PERSONALIZATION
[email protected]48352c12009-08-15 01:19:111307 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableSync)) {
1308 if (!sync_service_.get())
1309 InitSyncService();
1310 return sync_service_.get();
1311 }
license.botbf09a502008-08-24 00:55:551312#endif
[email protected]48352c12009-08-15 01:19:111313 return NULL;
1314}
1315
1316void ProfileImpl::InitSyncService() {
1317#ifdef CHROME_PERSONALIZATION
1318 sync_service_.reset(new ProfileSyncService(this));
1319 sync_service_->Initialize();
1320#endif
1321}