blob: 2f6b1534070a0dea5688e94d5b9b5e1f6280821b [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
[email protected]2735e3692009-09-25 18:19:397#include "app/theme_provider.h"
initial.commit09911bf2008-07-26 23:55:298#include "base/command_line.h"
[email protected]04fba9a92008-10-28 17:25:259#include "base/file_path.h"
[email protected]405a64b2009-09-16 21:03:4410#include "base/file_util.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/path_service.h"
12#include "base/scoped_ptr.h"
13#include "base/string_util.h"
[email protected]a9afddb2009-02-12 17:49:4214#include "chrome/browser/bookmarks/bookmark_model.h"
initial.commit09911bf2008-07-26 23:55:2915#include "chrome/browser/browser_list.h"
16#include "chrome/browser/browser_process.h"
[email protected]67a46b7f2009-06-16 21:41:0217#include "chrome/browser/browser_theme_provider.h"
[email protected]b7f05882009-02-22 01:21:5618#include "chrome/browser/download/download_manager.h"
[email protected]89ebc7e2009-08-24 22:11:0719#include "chrome/browser/extensions/extension_devtools_manager.h"
[email protected]7120f132009-07-20 21:05:3720#include "chrome/browser/extensions/extension_message_service.h"
[email protected]481e1a42009-05-06 20:56:0521#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]6014d672008-12-05 00:38:2522#include "chrome/browser/extensions/extensions_service.h"
[email protected]0938d3c2009-01-09 20:37:3523#include "chrome/browser/extensions/user_script_master.h"
[email protected]0189bc722009-08-28 21:56:4824#include "chrome/browser/favicon_service.h"
[email protected]77f6fb432009-09-05 14:21:0925#include "chrome/browser/strict_transport_security_persister.h"
[email protected]a9afddb2009-02-12 17:49:4226#include "chrome/browser/history/history.h"
[email protected]3bf335a2009-06-26 20:46:0627#include "chrome/browser/in_process_webkit/webkit_context.h"
[email protected]6ab9b202008-12-23 22:34:5028#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]db36938c2009-08-19 21:48:4229#include "chrome/browser/net/ssl_config_service_manager.h"
[email protected]4bb336302009-10-12 05:44:2630#include "chrome/browser/notifications/desktop_notification_service.h"
[email protected]e69d33952009-06-03 22:00:4131#include "chrome/browser/password_manager/password_store_default.h"
[email protected]eaadd9052009-06-23 18:02:2332#include "chrome/browser/privacy_blacklist/blacklist.h"
initial.commit09911bf2008-07-26 23:55:2933#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2634#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]1132436e2009-04-08 20:06:3335#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]f63ae312009-02-04 17:58:4636#include "chrome/browser/search_engines/template_url_model.h"
[email protected]85e921fb82009-02-11 23:19:4437#include "chrome/browser/sessions/session_service.h"
[email protected]bd580a252009-02-12 01:16:3038#include "chrome/browser/sessions/tab_restore_service.h"
[email protected]f0a644292009-02-25 23:32:4739#include "chrome/browser/spellchecker.h"
[email protected]1132436e2009-04-08 20:06:3340#include "chrome/browser/ssl/ssl_host_state.h"
[email protected]48352c12009-08-15 01:19:1141#include "chrome/browser/sync/profile_sync_service.h"
[email protected]449478302009-06-09 20:04:2842#include "chrome/browser/thumbnail_store.h"
initial.commit09911bf2008-07-26 23:55:2943#include "chrome/browser/visitedlink_master.h"
[email protected]3e90d4a2009-07-03 17:38:3944#include "chrome/browser/visitedlink_event_listener.h"
initial.commit09911bf2008-07-26 23:55:2945#include "chrome/browser/webdata/web_data_service.h"
[email protected]23f1ef12009-09-01 22:30:3046#include "chrome/common/appcache/chrome_appcache_service.h"
initial.commit09911bf2008-07-26 23:55:2947#include "chrome/common/chrome_constants.h"
48#include "chrome/common/chrome_paths.h"
49#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5850#include "chrome/common/extensions/extension_error_reporter.h"
initial.commit09911bf2008-07-26 23:55:2951#include "chrome/common/notification_service.h"
52#include "chrome/common/pref_names.h"
[email protected]f7011fcb2009-01-28 21:54:3253#include "chrome/common/render_messages.h"
[email protected]f90f5c512009-02-18 19:10:5854#include "grit/locale_settings.h"
[email protected]77f6fb432009-09-05 14:21:0955#include "net/base/strict_transport_security_state.h"
initial.commit09911bf2008-07-26 23:55:2956
[email protected]a5166af62009-07-03 00:42:2957#if defined(OS_LINUX)
[email protected]2a0c0a52009-07-31 07:51:3258#include "net/ocsp/nss_ocsp.h"
[email protected]a5166af62009-07-03 00:42:2959#include "chrome/browser/gtk/gtk_theme_provider.h"
60#endif
61
[email protected]2627431b2009-09-15 20:21:5362#if defined(OS_CHROMEOS)
[email protected]fecc1522009-10-15 21:08:2963#include "chrome/browser/chromeos/preferences.h"
[email protected]2627431b2009-09-15 20:21:5364#endif
65
[email protected]e1acf6f2008-10-27 20:43:3366using base::Time;
67using base::TimeDelta;
68
[email protected]e5642992009-06-26 23:06:3169namespace {
70
initial.commit09911bf2008-07-26 23:55:2971// Delay, in milliseconds, before we explicitly create the SessionService.
72static const int kCreateSessionServiceDelayMS = 500;
73
[email protected]e5642992009-06-26 23:06:3174enum ContextType {
75 kNormalContext,
76 kMediaContext
77};
78
79// Gets the cache parameters from the command line. |type| is the type of
80// request context that we need, |cache_path| will be set to the user provided
81// path, or will not be touched if there is not an argument. |max_size| will
82// be the user provided value or zero by default.
83void GetCacheParameters(ContextType type, FilePath* cache_path,
84 int* max_size) {
85 DCHECK(cache_path);
86 DCHECK(max_size);
87
88 // Override the cache location if specified by the user.
89 std::wstring user_path(CommandLine::ForCurrentProcess()->GetSwitchValue(
90 switches::kDiskCacheDir));
91
92 if (!user_path.empty()) {
93 *cache_path = FilePath::FromWStringHack(user_path);
94 }
95
[email protected]b7e0a2a2009-10-13 02:07:2596 const char* arg = kNormalContext == type ? switches::kDiskCacheSize :
97 switches::kMediaCacheSize;
[email protected]e5642992009-06-26 23:06:3198 std::string value =
[email protected]b7e0a2a2009-10-13 02:07:2599 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(arg);
[email protected]e5642992009-06-26 23:06:31100
101 // By default we let the cache determine the right size.
102 *max_size = 0;
103 if (!StringToInt(value, max_size)) {
104 *max_size = 0;
105 } else if (max_size < 0) {
106 *max_size = 0;
107 }
108}
109
[email protected]405a64b2009-09-16 21:03:44110FilePath GetCachePath(const FilePath& base) {
111 return base.Append(chrome::kCacheDirname);
112}
113
114FilePath GetMediaCachePath(const FilePath& base) {
115 return base.Append(chrome::kMediaCacheDirname);
116}
117
118bool HasACacheSubdir(const FilePath &dir) {
119 return file_util::PathExists(GetCachePath(dir)) ||
120 file_util::PathExists(GetMediaCachePath(dir));
121}
122
[email protected]e5642992009-06-26 23:06:31123} // namespace
124
initial.commit09911bf2008-07-26 23:55:29125// A pointer to the request context for the default profile. See comments on
126// Profile::GetDefaultRequestContext.
[email protected]be180c802009-10-23 06:33:31127URLRequestContextGetter* Profile::default_request_context_;
initial.commit09911bf2008-07-26 23:55:29128
[email protected]be180c802009-10-23 06:33:31129static void CleanupRequestContext(ChromeURLRequestContextGetter* context) {
[email protected]47accfd62009-05-14 18:46:21130 if (context) {
131 context->CleanupOnUIThread();
132
133 // Clean up request context on IO thread.
134 g_browser_process->io_thread()->message_loop()->ReleaseSoon(FROM_HERE,
135 context);
136 }
137}
138
[email protected]34cc84f2009-02-13 10:04:35139// static
[email protected]4bf6afd2009-10-08 14:00:11140const ProfileId Profile::InvalidProfileId = static_cast<ProfileId>(0);
141
142// static
initial.commit09911bf2008-07-26 23:55:29143void Profile::RegisterUserPrefs(PrefService* prefs) {
[email protected]430d3f72008-10-27 17:56:55144 prefs->RegisterBooleanPref(prefs::kSearchSuggestEnabled, true);
initial.commit09911bf2008-07-26 23:55:29145 prefs->RegisterBooleanPref(prefs::kSessionExitedCleanly, true);
146 prefs->RegisterBooleanPref(prefs::kSafeBrowsingEnabled, true);
[email protected]74c8b422009-03-11 00:34:12147 // TODO(estade): IDS_SPELLCHECK_DICTIONARY should be an ASCII string.
[email protected]e7244d82008-10-29 18:13:26148 prefs->RegisterLocalizedStringPref(prefs::kSpellCheckDictionary,
149 IDS_SPELLCHECK_DICTIONARY);
150 prefs->RegisterBooleanPref(prefs::kEnableSpellCheck, true);
[email protected]154a4332009-06-03 20:20:58151 prefs->RegisterBooleanPref(prefs::kEnableAutoSpellCorrect, true);
[email protected]f93fe782009-02-19 01:26:13152 prefs->RegisterBooleanPref(prefs::kEnableUserScripts, false);
[email protected]a5166af62009-07-03 00:42:29153#if defined(OS_LINUX)
154 prefs->RegisterBooleanPref(prefs::kUsesSystemTheme, false);
155#endif
[email protected]51c490b2009-08-03 16:34:34156 prefs->RegisterStringPref(prefs::kCurrentThemeID,
157 UTF8ToWide(BrowserThemeProvider::kDefaultThemeID));
[email protected]4a190632009-05-09 01:07:42158 prefs->RegisterDictionaryPref(prefs::kCurrentThemeImages);
159 prefs->RegisterDictionaryPref(prefs::kCurrentThemeColors);
160 prefs->RegisterDictionaryPref(prefs::kCurrentThemeTints);
[email protected]7895ea22009-06-02 20:53:50161 prefs->RegisterDictionaryPref(prefs::kCurrentThemeDisplayProperties);
[email protected]6d60703b2009-08-29 01:29:23162 prefs->RegisterBooleanPref(prefs::kDisableExtensions, false);
initial.commit09911bf2008-07-26 23:55:29163}
164
[email protected]34cc84f2009-02-13 10:04:35165// static
[email protected]f7011fcb2009-01-28 21:54:32166Profile* Profile::CreateProfile(const FilePath& path) {
initial.commit09911bf2008-07-26 23:55:29167 return new ProfileImpl(path);
168}
169
[email protected]34cc84f2009-02-13 10:04:35170// static
[email protected]be180c802009-10-23 06:33:31171URLRequestContextGetter* Profile::GetDefaultRequestContext() {
initial.commit09911bf2008-07-26 23:55:29172 return default_request_context_;
173}
174
[email protected]e69d33952009-06-03 22:00:41175#if defined(OS_LINUX)
176// Temporarily disabled while we figure some stuff out.
177// http://code.google.com/p/chromium/issues/detail?id=12351
178// #include "chrome/browser/password_manager/password_store_gnome.h"
179// #include "chrome/browser/password_manager/password_store_kwallet.h"
180#elif defined(OS_WIN)
181#include "chrome/browser/password_manager/password_store_win.h"
[email protected]034eba52009-06-03 22:50:56182#elif defined(OS_MACOSX)
183#include "chrome/browser/keychain_mac.h"
[email protected]e0411ae52009-06-30 23:59:17184#include "chrome/browser/password_manager/login_database_mac.h"
[email protected]034eba52009-06-03 22:50:56185#include "chrome/browser/password_manager/password_store_mac.h"
[email protected]e69d33952009-06-03 22:00:41186#endif
initial.commit09911bf2008-07-26 23:55:29187
initial.commit09911bf2008-07-26 23:55:29188////////////////////////////////////////////////////////////////////////////////
189//
190// OffTheRecordProfileImpl is a profile subclass that wraps an existing profile
[email protected]4a190632009-05-09 01:07:42191// to make it suitable for the off the record mode.
initial.commit09911bf2008-07-26 23:55:29192//
193////////////////////////////////////////////////////////////////////////////////
194class OffTheRecordProfileImpl : public Profile,
195 public NotificationObserver {
196 public:
197 explicit OffTheRecordProfileImpl(Profile* real_profile)
198 : profile_(real_profile),
[email protected]47accfd62009-05-14 18:46:21199 extensions_request_context_(NULL),
initial.commit09911bf2008-07-26 23:55:29200 start_time_(Time::Now()) {
[email protected]be180c802009-10-23 06:33:31201 request_context_ = ChromeURLRequestContextGetter::CreateOffTheRecord(this);
initial.commit09911bf2008-07-26 23:55:29202 request_context_->AddRef();
[email protected]481e1a42009-05-06 20:56:05203
initial.commit09911bf2008-07-26 23:55:29204 // Register for browser close notifications so we can detect when the last
205 // off-the-record window is closed, in which case we can clean our states
206 // (cookies, downloads...).
[email protected]88cf3292009-05-22 01:48:43207 registrar_.Add(this, NotificationType::BROWSER_CLOSED,
208 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29209 }
210
211 virtual ~OffTheRecordProfileImpl() {
[email protected]47accfd62009-05-14 18:46:21212 CleanupRequestContext(request_context_);
[email protected]47accfd62009-05-14 18:46:21213 CleanupRequestContext(extensions_request_context_);
initial.commit09911bf2008-07-26 23:55:29214 }
215
[email protected]4bf6afd2009-10-08 14:00:11216 virtual ProfileId GetRuntimeId() {
217 return reinterpret_cast<ProfileId>(this);
218 }
219
[email protected]f7011fcb2009-01-28 21:54:32220 virtual FilePath GetPath() { return profile_->GetPath(); }
initial.commit09911bf2008-07-26 23:55:29221
222 virtual bool IsOffTheRecord() {
223 return true;
224 }
225
226 virtual Profile* GetOffTheRecordProfile() {
227 return this;
228 }
229
[email protected]860f55492009-03-27 19:50:59230 virtual void DestroyOffTheRecordProfile() {
231 // Suicide is bad!
232 NOTREACHED();
233 }
234
initial.commit09911bf2008-07-26 23:55:29235 virtual Profile* GetOriginalProfile() {
236 return profile_;
237 }
238
239 virtual VisitedLinkMaster* GetVisitedLinkMaster() {
[email protected]7fb6c862009-03-13 02:51:49240 // We don't provide access to the VisitedLinkMaster when we're OffTheRecord
241 // because we don't want to leak the sites that the user has visited before.
242 return NULL;
initial.commit09911bf2008-07-26 23:55:29243 }
244
[email protected]6014d672008-12-05 00:38:25245 virtual ExtensionsService* GetExtensionsService() {
[email protected]1bd54132009-06-11 00:05:34246 return NULL;
[email protected]6014d672008-12-05 00:38:25247 }
248
[email protected]0938d3c2009-01-09 20:37:35249 virtual UserScriptMaster* GetUserScriptMaster() {
250 return profile_->GetUserScriptMaster();
[email protected]04fba9a92008-10-28 17:25:25251 }
252
[email protected]89ebc7e2009-08-24 22:11:07253 virtual ExtensionDevToolsManager* GetExtensionDevToolsManager() {
254 return NULL;
255 }
256
[email protected]481e1a42009-05-06 20:56:05257 virtual ExtensionProcessManager* GetExtensionProcessManager() {
[email protected]1bd54132009-06-11 00:05:34258 return NULL;
[email protected]481e1a42009-05-06 20:56:05259 }
260
[email protected]7120f132009-07-20 21:05:37261 virtual ExtensionMessageService* GetExtensionMessageService() {
262 return NULL;
263 }
264
[email protected]34cc84f2009-02-13 10:04:35265 virtual SSLHostState* GetSSLHostState() {
266 if (!ssl_host_state_.get())
267 ssl_host_state_.reset(new SSLHostState());
268
269 DCHECK(ssl_host_state_->CalledOnValidThread());
270 return ssl_host_state_.get();
271 }
272
[email protected]77f6fb432009-09-05 14:21:09273 virtual net::StrictTransportSecurityState* GetStrictTransportSecurityState() {
274 if (!strict_transport_security_state_.get()) {
275 strict_transport_security_state_ =
276 new net::StrictTransportSecurityState();
277 }
[email protected]a9cea7542009-05-20 04:30:23278
[email protected]77f6fb432009-09-05 14:21:09279 return strict_transport_security_state_.get();
[email protected]a9cea7542009-05-20 04:30:23280 }
281
initial.commit09911bf2008-07-26 23:55:29282 virtual HistoryService* GetHistoryService(ServiceAccessType sat) {
283 if (sat == EXPLICIT_ACCESS) {
284 return profile_->GetHistoryService(sat);
285 } else {
286 NOTREACHED() << "This profile is OffTheRecord";
287 return NULL;
288 }
289 }
290
[email protected]0850fa62009-10-08 22:34:29291 virtual HistoryService* GetHistoryServiceWithoutCreating() {
292 return profile_->GetHistoryServiceWithoutCreating();
293 }
294
[email protected]0189bc722009-08-28 21:56:48295 virtual FaviconService* GetFaviconService(ServiceAccessType sat) {
296 if (sat == EXPLICIT_ACCESS) {
297 return profile_->GetFaviconService(sat);
298 } else {
299 NOTREACHED() << "This profile is OffTheRecord";
300 return NULL;
301 }
302 }
303
initial.commit09911bf2008-07-26 23:55:29304 virtual WebDataService* GetWebDataService(ServiceAccessType sat) {
305 if (sat == EXPLICIT_ACCESS) {
306 return profile_->GetWebDataService(sat);
307 } else {
308 NOTREACHED() << "This profile is OffTheRecord";
309 return NULL;
310 }
311 }
312
[email protected]e69d33952009-06-03 22:00:41313 virtual PasswordStore* GetPasswordStore(ServiceAccessType sat) {
314 if (sat == EXPLICIT_ACCESS) {
315 return profile_->GetPasswordStore(sat);
316 } else {
317 NOTREACHED() << "This profile is OffTheRecord";
318 return NULL;
319 }
320 }
321
initial.commit09911bf2008-07-26 23:55:29322 virtual PrefService* GetPrefs() {
323 return profile_->GetPrefs();
324 }
325
326 virtual TemplateURLModel* GetTemplateURLModel() {
327 return profile_->GetTemplateURLModel();
328 }
329
330 virtual TemplateURLFetcher* GetTemplateURLFetcher() {
331 return profile_->GetTemplateURLFetcher();
332 }
333
334 virtual DownloadManager* GetDownloadManager() {
335 if (!download_manager_.get()) {
336 scoped_refptr<DownloadManager> dlm(new DownloadManager);
337 dlm->Init(this);
338 download_manager_.swap(dlm);
339 }
340 return download_manager_.get();
341 }
342
343 virtual bool HasCreatedDownloadManager() const {
344 return (download_manager_.get() != NULL);
345 }
346
[email protected]4a190632009-05-09 01:07:42347 virtual void InitThemes() {
348 GetOriginalProfile()->InitThemes();
349 }
350
351 virtual void SetTheme(Extension* extension) {
352 GetOriginalProfile()->SetTheme(extension);
353 }
354
[email protected]a5166af62009-07-03 00:42:29355 virtual void SetNativeTheme() {
356 GetOriginalProfile()->SetNativeTheme();
357 }
358
[email protected]4a190632009-05-09 01:07:42359 virtual void ClearTheme() {
360 GetOriginalProfile()->ClearTheme();
361 }
362
[email protected]51c490b2009-08-03 16:34:34363 virtual Extension* GetTheme() {
364 return GetOriginalProfile()->GetTheme();
365 }
366
[email protected]4a190632009-05-09 01:07:42367 virtual ThemeProvider* GetThemeProvider() {
368 return GetOriginalProfile()->GetThemeProvider();
369 }
370
[email protected]be180c802009-10-23 06:33:31371 virtual URLRequestContextGetter* GetRequestContext() {
initial.commit09911bf2008-07-26 23:55:29372 return request_context_;
373 }
374
[email protected]be180c802009-10-23 06:33:31375 virtual URLRequestContextGetter* GetRequestContextForMedia() {
[email protected]d14c7ac2009-05-29 20:38:11376 // In OTR mode, media request context is the same as the original one.
377 return request_context_;
[email protected]e7f29642009-03-02 22:53:18378 }
379
[email protected]be180c802009-10-23 06:33:31380 URLRequestContextGetter* GetRequestContextForExtensions() {
[email protected]47accfd62009-05-14 18:46:21381 if (!extensions_request_context_) {
382 extensions_request_context_ =
[email protected]be180c802009-10-23 06:33:31383 ChromeURLRequestContextGetter::CreateOffTheRecordForExtensions(this);
[email protected]47accfd62009-05-14 18:46:21384 extensions_request_context_->AddRef();
[email protected]47accfd62009-05-14 18:46:21385 }
386
387 return extensions_request_context_;
388 }
389
[email protected]db36938c2009-08-19 21:48:42390 virtual net::SSLConfigService* GetSSLConfigService() {
391 return GetOriginalProfile()->GetSSLConfigService();
392 }
393
[email protected]eaadd9052009-06-23 18:02:23394 virtual Blacklist* GetBlacklist() {
395 return GetOriginalProfile()->GetBlacklist();
396 }
397
initial.commit09911bf2008-07-26 23:55:29398 virtual SessionService* GetSessionService() {
399 // Don't save any sessions when off the record.
400 return NULL;
401 }
402
403 virtual void ShutdownSessionService() {
404 // We don't allow a session service, nothing to do.
405 }
406
407 virtual bool HasSessionService() const {
408 // We never have a session service.
409 return false;
410 }
411
412 virtual std::wstring GetName() {
413 return profile_->GetName();
414 }
415
416 virtual void SetName(const std::wstring& name) {
417 profile_->SetName(name);
418 }
419
420 virtual std::wstring GetID() {
421 return profile_->GetID();
422 }
423
424 virtual void SetID(const std::wstring& id) {
425 profile_->SetID(id);
426 }
427
initial.commit09911bf2008-07-26 23:55:29428 virtual bool DidLastSessionExitCleanly() {
429 return profile_->DidLastSessionExitCleanly();
430 }
431
[email protected]d8e41ed2008-09-11 15:22:32432 virtual BookmarkModel* GetBookmarkModel() {
433 return profile_->GetBookmarkModel();
initial.commit09911bf2008-07-26 23:55:29434 }
435
[email protected]4bb336302009-10-12 05:44:26436 virtual DesktopNotificationService* GetDesktopNotificationService() {
437 return profile_->GetDesktopNotificationService();
438 }
439
[email protected]48352c12009-08-15 01:19:11440 virtual ProfileSyncService* GetProfileSyncService() {
441 return NULL;
[email protected]3a453fa2008-08-15 18:46:34442 }
[email protected]3a453fa2008-08-15 18:46:34443
initial.commit09911bf2008-07-26 23:55:29444 virtual bool IsSameProfile(Profile* profile) {
445 if (profile == static_cast<Profile*>(this))
446 return true;
447 return profile == profile_;
448 }
449
450 virtual Time GetStartTime() const {
451 return start_time_;
452 }
453
454 virtual TabRestoreService* GetTabRestoreService() {
455 return NULL;
456 }
457
[email protected]e7244d82008-10-29 18:13:26458 virtual void ResetTabRestoreService() {
[email protected]20930852008-10-15 19:30:41459 }
460
[email protected]e7244d82008-10-29 18:13:26461 virtual void ReinitializeSpellChecker() {
462 profile_->ReinitializeSpellChecker();
initial.commit09911bf2008-07-26 23:55:29463 }
464
465 virtual SpellChecker* GetSpellChecker() {
466 return profile_->GetSpellChecker();
467 }
468
[email protected]e3448ea2009-10-02 01:29:29469 virtual void DeleteSpellChecker() {
470 profile_->DeleteSpellChecker();
471 }
472
[email protected]3bf335a2009-06-26 20:46:06473 virtual WebKitContext* GetWebKitContext() {
474 if (!webkit_context_.get())
475 webkit_context_ = new WebKitContext(GetPath(), true);
476 DCHECK(webkit_context_.get());
477 return webkit_context_.get();
478}
479
[email protected]449478302009-06-09 20:04:28480 virtual ThumbnailStore* GetThumbnailStore() {
481 return NULL;
482 }
483
initial.commit09911bf2008-07-26 23:55:29484 virtual void MarkAsCleanShutdown() {
485 }
486
[email protected]bdbc87c2009-01-25 05:08:54487 virtual void InitExtensions() {
488 NOTREACHED();
489 }
490
[email protected]ea0c98cf2009-06-18 23:02:54491 virtual void InitWebResources() {
492 NOTREACHED();
493 }
494
initial.commit09911bf2008-07-26 23:55:29495 virtual void ExitedOffTheRecordMode() {
496 // Drop our download manager so we forget about all the downloads made
497 // in off-the-record mode.
498 download_manager_ = NULL;
499 }
500
501 virtual void Observe(NotificationType type,
502 const NotificationSource& source,
503 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56504 DCHECK_EQ(NotificationType::BROWSER_CLOSED, type.value);
initial.commit09911bf2008-07-26 23:55:29505 // We are only interested in OTR browser closing.
506 if (Source<Browser>(source)->profile() != this)
507 return;
508
509 // Let's check if we still have an Off The Record window opened.
510 // Note that we check against 1 as this notification is sent before the
511 // browser window is actually removed from the list.
512 if (BrowserList::GetBrowserCount(this) <= 1)
513 ExitedOffTheRecordMode();
514 }
515
516 private:
[email protected]88cf3292009-05-22 01:48:43517 NotificationRegistrar registrar_;
518
initial.commit09911bf2008-07-26 23:55:29519 // The real underlying profile.
520 Profile* profile_;
521
[email protected]6ab9b202008-12-23 22:34:50522 // The context to use for requests made from this OTR session.
[email protected]be180c802009-10-23 06:33:31523 ChromeURLRequestContextGetter* request_context_;
initial.commit09911bf2008-07-26 23:55:29524
[email protected]be180c802009-10-23 06:33:31525 ChromeURLRequestContextGetter* extensions_request_context_;
[email protected]23f1ef12009-09-01 22:30:30526
initial.commit09911bf2008-07-26 23:55:29527 // The download manager that only stores downloaded items in memory.
528 scoped_refptr<DownloadManager> download_manager_;
529
[email protected]3bf335a2009-06-26 20:46:06530 // Use a special WebKit context for OTR browsing.
531 scoped_refptr<WebKitContext> webkit_context_;
532
[email protected]34cc84f2009-02-13 10:04:35533 // We don't want SSLHostState from the OTR profile to leak back to the main
534 // profile because then the main profile would learn some of the host names
535 // the user visited while OTR.
536 scoped_ptr<SSLHostState> ssl_host_state_;
537
[email protected]77f6fb432009-09-05 14:21:09538 // The StrictTransportSecurityState that only stores enabled sites in memory.
539 scoped_refptr<net::StrictTransportSecurityState>
540 strict_transport_security_state_;
[email protected]a9cea7542009-05-20 04:30:23541
initial.commit09911bf2008-07-26 23:55:29542 // Time we were started.
543 Time start_time_;
544
545 DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl);
546};
547
[email protected]f7011fcb2009-01-28 21:54:32548ProfileImpl::ProfileImpl(const FilePath& path)
initial.commit09911bf2008-07-26 23:55:29549 : path_(path),
[email protected]3e90d4a2009-07-03 17:38:39550 visited_link_event_listener_(new VisitedLinkEventListener()),
[email protected]89ebc7e2009-08-24 22:11:07551 extension_devtools_manager_(NULL),
[email protected]f7011fcb2009-01-28 21:54:32552 request_context_(NULL),
[email protected]363347b2009-03-13 20:06:57553 media_request_context_(NULL),
[email protected]47accfd62009-05-14 18:46:21554 extensions_request_context_(NULL),
[email protected]eaadd9052009-06-23 18:02:23555 blacklist_(NULL),
[email protected]f7011fcb2009-01-28 21:54:32556 history_service_created_(false),
[email protected]0189bc722009-08-28 21:56:48557 favicon_service_created_(false),
[email protected]f7011fcb2009-01-28 21:54:32558 created_web_data_service_(false),
[email protected]e69d33952009-06-03 22:00:41559 created_password_store_(false),
[email protected]f7011fcb2009-01-28 21:54:32560 created_download_manager_(false),
[email protected]4a190632009-05-09 01:07:42561 created_theme_provider_(false),
[email protected]f7011fcb2009-01-28 21:54:32562 start_time_(Time::Now()),
563 spellchecker_(NULL),
initial.commit09911bf2008-07-26 23:55:29564 shutdown_session_service_(false) {
565 DCHECK(!path.empty()) << "Using an empty path will attempt to write " <<
566 "profile files to the root directory!";
[email protected]2d316662008-09-03 18:18:14567 create_session_service_timer_.Start(
568 TimeDelta::FromMilliseconds(kCreateSessionServiceDelayMS), this,
569 &ProfileImpl::EnsureSessionServiceCreated);
[email protected]bdbc87c2009-01-25 05:08:54570
[email protected]89ebc7e2009-08-24 22:11:07571 if (CommandLine::ForCurrentProcess()->HasSwitch(
572 switches::kEnableExtensionTimelineApi)) {
573 extension_devtools_manager_ = new ExtensionDevToolsManager(this);
574 }
575
[email protected]382a0702009-06-26 17:12:27576 extension_process_manager_.reset(new ExtensionProcessManager(this));
[email protected]7120f132009-07-20 21:05:37577 extension_message_service_ = new ExtensionMessageService(this);
[email protected]382a0702009-06-26 17:12:27578
[email protected]e7244d82008-10-29 18:13:26579 PrefService* prefs = GetPrefs();
580 prefs->AddPrefObserver(prefs::kSpellCheckDictionary, this);
581 prefs->AddPrefObserver(prefs::kEnableSpellCheck, this);
[email protected]154a4332009-06-03 20:20:58582 prefs->AddPrefObserver(prefs::kEnableAutoSpellCorrect, this);
[email protected]380ab462009-04-24 01:23:58583
[email protected]eaadd9052009-06-23 18:02:23584 if (CommandLine::ForCurrentProcess()->
585 HasSwitch(switches::kPrivacyBlacklist)) {
586 std::wstring option = CommandLine::ForCurrentProcess()->GetSwitchValue(
587 switches::kPrivacyBlacklist);
588#if defined(OS_POSIX)
589 FilePath path(WideToUTF8(option));
590#else
591 FilePath path(option);
592#endif
593 blacklist_ = new Blacklist(path);
594 }
595
[email protected]405a64b2009-09-16 21:03:44596#if defined(OS_MACOSX)
597 // If the profile directory doesn't already have a cache directory and it
598 // is under ~/Library/Application Support, use a suitable cache directory
599 // under ~/Library/Caches. For example, a profile directory of
600 // ~/Library/Application Support/Google/Chrome/MyProfileName that doesn't
601 // have a "Cache" or "MediaCache" subdirectory would use the cache directory
602 // ~/Library/Caches/Google/Chrome/MyProfileName.
603 //
604 // TODO(akalin): Come up with unit tests for this.
605 // TODO(akalin): Use for Linux, too?
606 if (!HasACacheSubdir(path_)) {
607 FilePath app_data_path, user_cache_path;
608 if (PathService::Get(base::DIR_APP_DATA, &app_data_path) &&
609 PathService::Get(base::DIR_CACHE, &user_cache_path) &&
610 app_data_path.AppendRelativePath(path_, &user_cache_path)) {
611 base_cache_path_ = user_cache_path;
612 }
613 }
614#else
[email protected]bc96d562009-08-11 18:45:13615 if (!PathService::IsOverridden(chrome::DIR_USER_DATA))
616 PathService::Get(chrome::DIR_USER_CACHE, &base_cache_path_);
[email protected]405a64b2009-09-16 21:03:44617#endif
[email protected]bc96d562009-08-11 18:45:13618 if (base_cache_path_.empty())
619 base_cache_path_ = path_;
620
[email protected]4a190632009-05-09 01:07:42621 // Listen for theme installation.
[email protected]88cf3292009-05-22 01:48:43622 registrar_.Add(this, NotificationType::THEME_INSTALLED,
623 NotificationService::AllSources());
[email protected]48352c12009-08-15 01:19:11624
625 // Listen for bookmark model load, to bootstrap the sync service.
626 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
627 Source<Profile>(this));
[email protected]db36938c2009-08-19 21:48:42628
629 ssl_config_service_manager_.reset(
630 SSLConfigServiceManager::CreateDefaultManager(this));
[email protected]2627431b2009-09-15 20:21:53631
632#if defined(OS_CHROMEOS)
[email protected]fecc1522009-10-15 21:08:29633 chromeos_preferences_.Init(prefs);
[email protected]2627431b2009-09-15 20:21:53634#endif
initial.commit09911bf2008-07-26 23:55:29635}
636
[email protected]bdbc87c2009-01-25 05:08:54637void ProfileImpl::InitExtensions() {
[email protected]8c756ac2009-01-30 23:36:41638 if (user_script_master_ || extensions_service_)
639 return; // Already initialized.
640
[email protected]bdbc87c2009-01-25 05:08:54641 const CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]f93fe782009-02-19 01:26:13642 PrefService* prefs = GetPrefs();
[email protected]bdbc87c2009-01-25 05:08:54643 bool user_scripts_enabled =
[email protected]f0a51fb52009-03-05 12:46:38644 command_line->HasSwitch(switches::kEnableUserScripts) ||
[email protected]f93fe782009-02-19 01:26:13645 prefs->GetBoolean(prefs::kEnableUserScripts);
[email protected]bdbc87c2009-01-25 05:08:54646
[email protected]f7011fcb2009-01-28 21:54:32647 FilePath script_dir;
[email protected]bdbc87c2009-01-25 05:08:54648 if (user_scripts_enabled) {
[email protected]0cd957b2009-03-06 20:13:23649 if (command_line->HasSwitch(switches::kUserScriptsDir)) {
650 std::wstring path_string =
651 command_line->GetSwitchValue(switches::kUserScriptsDir);
652 script_dir = FilePath::FromWStringHack(path_string);
653 } else {
654 script_dir = GetPath();
655 script_dir = script_dir.Append(chrome::kUserScriptsDirname);
656 }
[email protected]bdbc87c2009-01-25 05:08:54657 }
658
[email protected]bb28e062009-02-27 17:19:18659 ExtensionErrorReporter::Init(true); // allow noisy errors.
[email protected]bdbc87c2009-01-25 05:08:54660 user_script_master_ = new UserScriptMaster(
[email protected]9197f3b2009-06-02 00:49:27661 g_browser_process->file_thread()->message_loop(),
662 script_dir);
[email protected]894bb502009-05-21 22:39:57663 extensions_service_ = new ExtensionsService(
[email protected]a9b00ac2009-06-25 21:03:23664 this,
665 CommandLine::ForCurrentProcess(),
666 GetPrefs(),
667 GetPath().AppendASCII(ExtensionsService::kInstallDirectoryName),
668 MessageLoop::current(),
[email protected]93fd78f42009-07-10 16:43:17669 g_browser_process->file_thread()->message_loop(),
[email protected]c8b437d2009-07-20 21:26:36670 true);
[email protected]bdbc87c2009-01-25 05:08:54671
[email protected]9197f3b2009-06-02 00:49:27672 extensions_service_->Init();
[email protected]919ddc82009-07-15 04:30:12673
674 // Load any extensions specified with --load-extension.
675 if (command_line->HasSwitch(switches::kLoadExtension)) {
676 std::wstring path_string =
677 command_line->GetSwitchValue(switches::kLoadExtension);
678 FilePath path = FilePath::FromWStringHack(path_string);
679 extensions_service_->LoadExtension(path);
680
681 // Tell UserScriptMaser to watch this extension's directory for changes so
682 // you can live edit content scripts during development.
683 user_script_master_->AddWatchedPath(path);
684 }
[email protected]bdbc87c2009-01-25 05:08:54685}
686
[email protected]ea0c98cf2009-06-18 23:02:54687void ProfileImpl::InitWebResources() {
[email protected]0c274c202009-07-12 02:59:07688 if (web_resource_service_)
689 return; // Already initialized.
690
[email protected]ea0c98cf2009-06-18 23:02:54691 web_resource_service_ = new WebResourceService(
692 this,
693 g_browser_process->file_thread()->message_loop());
694
695 web_resource_service_->StartAfterDelay();
696}
697
initial.commit09911bf2008-07-26 23:55:29698ProfileImpl::~ProfileImpl() {
[email protected]169627b2008-12-06 19:30:19699 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29700
701 StopCreateSessionServiceTimer();
702 // TemplateURLModel schedules a task on the WebDataService from its
703 // destructor. Delete it first to ensure the task gets scheduled before we
704 // shut down the database.
705 template_url_model_.reset();
706
707 // The download manager queries the history system and should be deleted
708 // before the history is shutdown so it can properly cancel all requests.
709 download_manager_ = NULL;
710
[email protected]4a190632009-05-09 01:07:42711 // The theme provider provides bitmaps to whoever wants them.
[email protected]761962c2009-09-25 00:18:15712 theme_provider_.reset();
[email protected]4a190632009-05-09 01:07:42713
[email protected]44961292009-07-20 20:03:09714 // The ThumbnailStore saves thumbnails used by the NTP. Call Shutdown to
715 // save any new thumbnails to disk and release its reference to the
716 // HistoryService.
717 if (thumbnail_store_.get())
718 thumbnail_store_->Shutdown();
719
[email protected]e7244d82008-10-29 18:13:26720 // Remove pref observers.
721 PrefService* prefs = GetPrefs();
722 prefs->RemovePrefObserver(prefs::kSpellCheckDictionary, this);
723 prefs->RemovePrefObserver(prefs::kEnableSpellCheck, this);
[email protected]154a4332009-06-03 20:20:58724 prefs->RemovePrefObserver(prefs::kEnableAutoSpellCorrect, this);
[email protected]e7244d82008-10-29 18:13:26725
[email protected]066629f2009-10-17 00:28:13726#if defined(BROWSER_SYNC)
[email protected]48352c12009-08-15 01:19:11727 sync_service_.reset();
[email protected]3a453fa2008-08-15 18:46:34728#endif
729
initial.commit09911bf2008-07-26 23:55:29730 // Both HistoryService and WebDataService maintain threads for background
731 // processing. Its possible each thread still has tasks on it that have
732 // increased the ref count of the service. In such a situation, when we
733 // decrement the refcount, it won't be 0, and the threads/databases aren't
734 // properly shut down. By explicitly calling Cleanup/Shutdown we ensure the
735 // databases are properly closed.
736 if (web_data_service_.get())
737 web_data_service_->Shutdown();
738
739 if (history_service_.get())
740 history_service_->Cleanup();
741
[email protected]e3448ea2009-10-02 01:29:29742 DeleteSpellCheckerImpl(false);
initial.commit09911bf2008-07-26 23:55:29743
[email protected]2a0c0a52009-07-31 07:51:32744 if (default_request_context_ == request_context_) {
745#if defined(OS_LINUX)
746 // We use default_request_context_ for OCSP.
747 // Release URLRequestContext used in OCSP handlers.
748 net::SetURLRequestContextForOCSP(NULL);
749#endif
[email protected]47accfd62009-05-14 18:46:21750 default_request_context_ = NULL;
[email protected]2a0c0a52009-07-31 07:51:32751 }
[email protected]6ab9b202008-12-23 22:34:50752
[email protected]47accfd62009-05-14 18:46:21753 CleanupRequestContext(request_context_);
754 CleanupRequestContext(media_request_context_);
755 CleanupRequestContext(extensions_request_context_);
[email protected]1e744f22009-04-08 01:00:17756
[email protected]eaadd9052009-06-23 18:02:23757 // When the request contexts are gone, the blacklist wont be needed anymore.
758 delete blacklist_;
759 blacklist_ = 0;
760
[email protected]d8e41ed2008-09-11 15:22:32761 // HistoryService may call into the BookmarkModel, as such we need to
762 // delete HistoryService before the BookmarkModel. The destructor for
[email protected]90ef13132008-08-27 03:27:46763 // HistoryService will join with HistoryService's backend thread so that
764 // by the time the destructor has finished we're sure it will no longer call
[email protected]d8e41ed2008-09-11 15:22:32765 // into the BookmarkModel.
[email protected]90ef13132008-08-27 03:27:46766 history_service_ = NULL;
767 bookmark_bar_model_.reset();
768
[email protected]0189bc722009-08-28 21:56:48769 // FaviconService depends on HistoryServce so make sure we delete
770 // HistoryService first.
771 favicon_service_ = NULL;
772
[email protected]7120f132009-07-20 21:05:37773 extension_message_service_->ProfileDestroyed();
774
[email protected]6ef635e42009-07-26 06:16:12775 if (extensions_service_)
776 extensions_service_->ProfileDestroyed();
777
[email protected]66da08b2009-10-19 22:27:00778 // This causes the Preferences file to be written to disk.
initial.commit09911bf2008-07-26 23:55:29779 MarkAsCleanShutdown();
780}
781
[email protected]4bf6afd2009-10-08 14:00:11782ProfileId ProfileImpl::GetRuntimeId() {
783 return reinterpret_cast<ProfileId>(this);
784}
785
[email protected]f7011fcb2009-01-28 21:54:32786FilePath ProfileImpl::GetPath() {
initial.commit09911bf2008-07-26 23:55:29787 return path_;
788}
789
790bool ProfileImpl::IsOffTheRecord() {
791 return false;
792}
793
794Profile* ProfileImpl::GetOffTheRecordProfile() {
795 if (!off_the_record_profile_.get()) {
796 scoped_ptr<OffTheRecordProfileImpl> p(new OffTheRecordProfileImpl(this));
797 off_the_record_profile_.swap(p);
798 }
799 return off_the_record_profile_.get();
800}
801
[email protected]860f55492009-03-27 19:50:59802void ProfileImpl::DestroyOffTheRecordProfile() {
803 off_the_record_profile_.reset();
804}
805
initial.commit09911bf2008-07-26 23:55:29806Profile* ProfileImpl::GetOriginalProfile() {
807 return this;
808}
809
initial.commit09911bf2008-07-26 23:55:29810VisitedLinkMaster* ProfileImpl::GetVisitedLinkMaster() {
811 if (!visited_link_master_.get()) {
812 scoped_ptr<VisitedLinkMaster> visited_links(
813 new VisitedLinkMaster(g_browser_process->file_thread(),
[email protected]3e90d4a2009-07-03 17:38:39814 visited_link_event_listener_.get(), this));
initial.commit09911bf2008-07-26 23:55:29815 if (!visited_links->Init())
816 return NULL;
817 visited_link_master_.swap(visited_links);
818 }
819
820 return visited_link_master_.get();
821}
822
[email protected]6014d672008-12-05 00:38:25823ExtensionsService* ProfileImpl::GetExtensionsService() {
824 return extensions_service_.get();
825}
826
[email protected]0938d3c2009-01-09 20:37:35827UserScriptMaster* ProfileImpl::GetUserScriptMaster() {
[email protected]0938d3c2009-01-09 20:37:35828 return user_script_master_.get();
[email protected]04fba9a92008-10-28 17:25:25829}
830
[email protected]89ebc7e2009-08-24 22:11:07831ExtensionDevToolsManager* ProfileImpl::GetExtensionDevToolsManager() {
832 return extension_devtools_manager_.get();
833}
834
[email protected]481e1a42009-05-06 20:56:05835ExtensionProcessManager* ProfileImpl::GetExtensionProcessManager() {
[email protected]382a0702009-06-26 17:12:27836 return extension_process_manager_.get();
[email protected]481e1a42009-05-06 20:56:05837}
838
[email protected]7120f132009-07-20 21:05:37839ExtensionMessageService* ProfileImpl::GetExtensionMessageService() {
840 return extension_message_service_.get();
841}
842
[email protected]34cc84f2009-02-13 10:04:35843SSLHostState* ProfileImpl::GetSSLHostState() {
844 if (!ssl_host_state_.get())
845 ssl_host_state_.reset(new SSLHostState());
846
847 DCHECK(ssl_host_state_->CalledOnValidThread());
848 return ssl_host_state_.get();
849}
850
[email protected]77f6fb432009-09-05 14:21:09851net::StrictTransportSecurityState*
852 ProfileImpl::GetStrictTransportSecurityState() {
853 if (!strict_transport_security_state_.get()) {
854 strict_transport_security_state_ = new net::StrictTransportSecurityState();
855 strict_transport_security_persister_ = new StrictTransportSecurityPersister(
856 strict_transport_security_state_.get(),
857 g_browser_process->file_thread(), path_);
[email protected]887f80d2009-09-04 16:57:51858 }
[email protected]a9cea7542009-05-20 04:30:23859
[email protected]77f6fb432009-09-05 14:21:09860 return strict_transport_security_state_.get();
[email protected]a9cea7542009-05-20 04:30:23861}
862
initial.commit09911bf2008-07-26 23:55:29863PrefService* ProfileImpl::GetPrefs() {
864 if (!prefs_.get()) {
[email protected]6faa0e0d2009-04-28 06:50:36865 prefs_.reset(new PrefService(GetPrefFilePath(),
866 g_browser_process->file_thread()));
initial.commit09911bf2008-07-26 23:55:29867
868 // The Profile class and ProfileManager class may read some prefs so
869 // register known prefs as soon as possible.
870 Profile::RegisterUserPrefs(prefs_.get());
871 ProfileManager::RegisterUserPrefs(prefs_.get());
[email protected]2627431b2009-09-15 20:21:53872#if defined(OS_CHROMEOS)
873 // Register Touchpad prefs here instead of in browser_prefs because these
874 // prefs are used in the constructor of ProfileImpl which happens before
875 // browser_prefs' RegisterAllPrefs is called.
[email protected]fecc1522009-10-15 21:08:29876 chromeos::Preferences::RegisterUserPrefs(prefs_.get());
[email protected]2627431b2009-09-15 20:21:53877#endif
initial.commit09911bf2008-07-26 23:55:29878
879 // The last session exited cleanly if there is no pref for
880 // kSessionExitedCleanly or the value for kSessionExitedCleanly is true.
881 last_session_exited_cleanly_ =
882 prefs_->GetBoolean(prefs::kSessionExitedCleanly);
883 // Mark the session as open.
884 prefs_->SetBoolean(prefs::kSessionExitedCleanly, false);
885 // Make sure we save to disk that the session has opened.
[email protected]6faa0e0d2009-04-28 06:50:36886 prefs_->ScheduleSavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29887 }
888
889 return prefs_.get();
890}
891
[email protected]f7011fcb2009-01-28 21:54:32892FilePath ProfileImpl::GetPrefFilePath() {
893 FilePath pref_file_path = path_;
894 pref_file_path = pref_file_path.Append(chrome::kPreferencesFilename);
initial.commit09911bf2008-07-26 23:55:29895 return pref_file_path;
896}
897
[email protected]be180c802009-10-23 06:33:31898URLRequestContextGetter* ProfileImpl::GetRequestContext() {
initial.commit09911bf2008-07-26 23:55:29899 if (!request_context_) {
[email protected]f7011fcb2009-01-28 21:54:32900 FilePath cookie_path = GetPath();
901 cookie_path = cookie_path.Append(chrome::kCookieFilename);
[email protected]bc96d562009-08-11 18:45:13902 FilePath cache_path = base_cache_path_;
[email protected]e5642992009-06-26 23:06:31903 int max_size;
904 GetCacheParameters(kNormalContext, &cache_path, &max_size);
[email protected]2b2830a2009-02-07 01:58:42905
[email protected]405a64b2009-09-16 21:03:44906 cache_path = GetCachePath(cache_path);
[email protected]be180c802009-10-23 06:33:31907 request_context_ = ChromeURLRequestContextGetter::CreateOriginal(
908 this, cookie_path, cache_path, max_size);
initial.commit09911bf2008-07-26 23:55:29909 request_context_->AddRef();
910
[email protected]6ab9b202008-12-23 22:34:50911 // The first request context is always a normal (non-OTR) request context.
912 // Even when Chromium is started in OTR mode, a normal profile is always
913 // created first.
914 if (!default_request_context_) {
915 default_request_context_ = request_context_;
[email protected]be180c802009-10-23 06:33:31916 // TODO(eroman): this isn't terribly useful anymore now that the
917 // URLRequestContext is constructed by the IO thread...
[email protected]6ab9b202008-12-23 22:34:50918 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56919 NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE,
[email protected]6ab9b202008-12-23 22:34:50920 NotificationService::AllSources(), NotificationService::NoDetails());
921 }
initial.commit09911bf2008-07-26 23:55:29922 }
923
924 return request_context_;
925}
926
[email protected]be180c802009-10-23 06:33:31927URLRequestContextGetter* ProfileImpl::GetRequestContextForMedia() {
[email protected]e7f29642009-03-02 22:53:18928 if (!media_request_context_) {
[email protected]bc96d562009-08-11 18:45:13929 FilePath cache_path = base_cache_path_;
[email protected]e5642992009-06-26 23:06:31930 int max_size;
931 GetCacheParameters(kMediaContext, &cache_path, &max_size);
[email protected]e3edeba2009-03-23 18:57:14932
[email protected]405a64b2009-09-16 21:03:44933 cache_path = GetMediaCachePath(cache_path);
[email protected]be180c802009-10-23 06:33:31934 media_request_context_ =
935 ChromeURLRequestContextGetter::CreateOriginalForMedia(
936 this, cache_path, max_size);
[email protected]e7f29642009-03-02 22:53:18937 media_request_context_->AddRef();
[email protected]e7f29642009-03-02 22:53:18938 }
939
940 return media_request_context_;
941}
942
[email protected]0189bc722009-08-28 21:56:48943FaviconService* ProfileImpl::GetFaviconService(ServiceAccessType sat) {
944 if (!favicon_service_created_) {
945 favicon_service_created_ = true;
946 scoped_refptr<FaviconService> service(new FaviconService(this));
947 favicon_service_.swap(service);
948 }
949 return favicon_service_.get();
950}
951
[email protected]be180c802009-10-23 06:33:31952URLRequestContextGetter* ProfileImpl::GetRequestContextForExtensions() {
[email protected]47accfd62009-05-14 18:46:21953 if (!extensions_request_context_) {
954 FilePath cookie_path = GetPath();
955 cookie_path = cookie_path.Append(chrome::kExtensionsCookieFilename);
956
957 extensions_request_context_ =
[email protected]be180c802009-10-23 06:33:31958 ChromeURLRequestContextGetter::CreateOriginalForExtensions(
959 this, cookie_path);
[email protected]47accfd62009-05-14 18:46:21960 extensions_request_context_->AddRef();
[email protected]47accfd62009-05-14 18:46:21961 }
962
963 return extensions_request_context_;
964}
965
[email protected]db36938c2009-08-19 21:48:42966net::SSLConfigService* ProfileImpl::GetSSLConfigService() {
967 return ssl_config_service_manager_->Get();
968}
969
[email protected]eaadd9052009-06-23 18:02:23970Blacklist* ProfileImpl::GetBlacklist() {
971 return blacklist_;
972}
973
initial.commit09911bf2008-07-26 23:55:29974HistoryService* ProfileImpl::GetHistoryService(ServiceAccessType sat) {
975 if (!history_service_created_) {
[email protected]90ef13132008-08-27 03:27:46976 history_service_created_ = true;
initial.commit09911bf2008-07-26 23:55:29977 scoped_refptr<HistoryService> history(new HistoryService(this));
[email protected]d8e41ed2008-09-11 15:22:32978 if (!history->Init(GetPath(), GetBookmarkModel()))
initial.commit09911bf2008-07-26 23:55:29979 return NULL;
980 history_service_.swap(history);
initial.commit09911bf2008-07-26 23:55:29981
982 // Send out the notification that the history service was created.
983 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56984 Notify(NotificationType::HISTORY_CREATED, Source<Profile>(this),
initial.commit09911bf2008-07-26 23:55:29985 Details<HistoryService>(history_service_.get()));
986 }
987 return history_service_.get();
988}
989
[email protected]0850fa62009-10-08 22:34:29990HistoryService* ProfileImpl::GetHistoryServiceWithoutCreating() {
991 return history_service_.get();
992}
993
initial.commit09911bf2008-07-26 23:55:29994TemplateURLModel* ProfileImpl::GetTemplateURLModel() {
995 if (!template_url_model_.get())
996 template_url_model_.reset(new TemplateURLModel(this));
997 return template_url_model_.get();
998}
999
1000TemplateURLFetcher* ProfileImpl::GetTemplateURLFetcher() {
1001 if (!template_url_fetcher_.get())
1002 template_url_fetcher_.reset(new TemplateURLFetcher(this));
1003 return template_url_fetcher_.get();
1004}
1005
1006WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) {
1007 if (!created_web_data_service_)
1008 CreateWebDataService();
1009 return web_data_service_.get();
1010}
1011
1012void ProfileImpl::CreateWebDataService() {
1013 DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL);
1014 created_web_data_service_ = true;
1015 scoped_refptr<WebDataService> wds(new WebDataService());
1016 if (!wds->Init(GetPath()))
1017 return;
1018 web_data_service_.swap(wds);
1019}
1020
[email protected]e69d33952009-06-03 22:00:411021PasswordStore* ProfileImpl::GetPasswordStore(ServiceAccessType sat) {
1022 if (!created_password_store_)
1023 CreatePasswordStore();
1024 return password_store_.get();
1025}
1026
1027void ProfileImpl::CreatePasswordStore() {
1028 DCHECK(!created_password_store_ && password_store_.get() == NULL);
1029 created_password_store_ = true;
1030 scoped_refptr<PasswordStore> ps;
1031#if defined(OS_LINUX)
[email protected]d17634b2009-10-20 18:56:251032 // TODO(evanm): implement "native" password management.
1033 // This bug describes the issues.
1034 // http://code.google.com/p/chromium/issues/detail?id=12351
1035 ps = new PasswordStoreDefault(GetWebDataService(Profile::IMPLICIT_ACCESS));
1036 if (!ps->Init())
1037 return;
[email protected]e69d33952009-06-03 22:00:411038#elif defined(OS_WIN)
1039 ps = new PasswordStoreWin(GetWebDataService(Profile::IMPLICIT_ACCESS));
[email protected]034eba52009-06-03 22:50:561040#elif defined(OS_MACOSX)
[email protected]e0411ae52009-06-30 23:59:171041 FilePath login_db_file_path = GetPath();
1042 login_db_file_path = login_db_file_path.Append(chrome::kLoginDataFileName);
1043 LoginDatabaseMac* login_db = new LoginDatabaseMac();
1044 if (!login_db->Init(login_db_file_path)) {
1045 LOG(ERROR) << "Could not initialize login database.";
1046 delete login_db;
1047 return;
1048 }
1049 ps = new PasswordStoreMac(new MacKeychain(), login_db);
[email protected]e69d33952009-06-03 22:00:411050#else
1051 NOTIMPLEMENTED();
1052#endif
1053 if (!ps || !ps->Init()) {
1054 // Try falling back to the default password manager
1055 LOG(WARNING) << "Could not initialise native password manager - "
1056 "falling back to default";
1057 ps = new PasswordStoreDefault(GetWebDataService(Profile::IMPLICIT_ACCESS));
1058 if (!ps->Init())
1059 return;
1060 }
1061 password_store_.swap(ps);
1062}
1063
initial.commit09911bf2008-07-26 23:55:291064DownloadManager* ProfileImpl::GetDownloadManager() {
1065 if (!created_download_manager_) {
1066 scoped_refptr<DownloadManager> dlm(new DownloadManager);
1067 dlm->Init(this);
1068 created_download_manager_ = true;
1069 download_manager_.swap(dlm);
1070 }
1071 return download_manager_.get();
1072}
1073
1074bool ProfileImpl::HasCreatedDownloadManager() const {
1075 return created_download_manager_;
1076}
1077
[email protected]4a190632009-05-09 01:07:421078void ProfileImpl::InitThemes() {
1079 if (!created_theme_provider_) {
[email protected]0519e112009-09-29 15:49:571080#if defined(OS_LINUX)
[email protected]761962c2009-09-25 00:18:151081 theme_provider_.reset(new GtkThemeProvider);
[email protected]a5166af62009-07-03 00:42:291082#else
[email protected]761962c2009-09-25 00:18:151083 theme_provider_.reset(new BrowserThemeProvider);
[email protected]a5166af62009-07-03 00:42:291084#endif
[email protected]761962c2009-09-25 00:18:151085 theme_provider_->Init(this);
[email protected]4a190632009-05-09 01:07:421086 created_theme_provider_ = true;
[email protected]4a190632009-05-09 01:07:421087 }
1088}
1089
1090void ProfileImpl::SetTheme(Extension* extension) {
1091 InitThemes();
1092 theme_provider_.get()->SetTheme(extension);
1093}
1094
[email protected]a5166af62009-07-03 00:42:291095void ProfileImpl::SetNativeTheme() {
1096 InitThemes();
1097 theme_provider_.get()->SetNativeTheme();
1098}
1099
[email protected]4a190632009-05-09 01:07:421100void ProfileImpl::ClearTheme() {
1101 InitThemes();
1102 theme_provider_.get()->UseDefaultTheme();
1103}
1104
[email protected]51c490b2009-08-03 16:34:341105Extension* ProfileImpl::GetTheme() {
1106 InitThemes();
1107
1108 std::string id = theme_provider_.get()->GetThemeID();
1109 if (id == BrowserThemeProvider::kDefaultThemeID)
1110 return NULL;
1111
1112 return extensions_service_->GetExtensionById(id);
1113}
1114
[email protected]4a190632009-05-09 01:07:421115ThemeProvider* ProfileImpl::GetThemeProvider() {
1116 InitThemes();
1117 return theme_provider_.get();
1118}
1119
initial.commit09911bf2008-07-26 23:55:291120SessionService* ProfileImpl::GetSessionService() {
1121 if (!session_service_.get() && !shutdown_session_service_) {
1122 session_service_ = new SessionService(this);
1123 session_service_->ResetFromCurrentBrowsers();
1124 }
1125 return session_service_.get();
1126}
1127
1128void ProfileImpl::ShutdownSessionService() {
1129 if (shutdown_session_service_)
1130 return;
1131
1132 // We're about to exit, force creation of the session service if it hasn't
1133 // been created yet. We do this to ensure session state matches the point in
1134 // time the user exited.
1135 GetSessionService();
1136 shutdown_session_service_ = true;
1137 session_service_ = NULL;
1138}
1139
1140bool ProfileImpl::HasSessionService() const {
1141 return (session_service_.get() != NULL);
1142}
1143
1144std::wstring ProfileImpl::GetName() {
1145 return GetPrefs()->GetString(prefs::kProfileName);
1146}
1147void ProfileImpl::SetName(const std::wstring& name) {
1148 GetPrefs()->SetString(prefs::kProfileName, name);
1149}
1150
1151std::wstring ProfileImpl::GetID() {
1152 return GetPrefs()->GetString(prefs::kProfileID);
1153}
1154void ProfileImpl::SetID(const std::wstring& id) {
1155 GetPrefs()->SetString(prefs::kProfileID, id);
1156}
1157
initial.commit09911bf2008-07-26 23:55:291158bool ProfileImpl::DidLastSessionExitCleanly() {
1159 // last_session_exited_cleanly_ is set when the preferences are loaded. Force
1160 // it to be set by asking for the prefs.
1161 GetPrefs();
1162 return last_session_exited_cleanly_;
1163}
1164
[email protected]d8e41ed2008-09-11 15:22:321165BookmarkModel* ProfileImpl::GetBookmarkModel() {
[email protected]90ef13132008-08-27 03:27:461166 if (!bookmark_bar_model_.get()) {
[email protected]d8e41ed2008-09-11 15:22:321167 bookmark_bar_model_.reset(new BookmarkModel(this));
[email protected]90ef13132008-08-27 03:27:461168 bookmark_bar_model_->Load();
1169 }
initial.commit09911bf2008-07-26 23:55:291170 return bookmark_bar_model_.get();
1171}
1172
1173bool ProfileImpl::IsSameProfile(Profile* profile) {
1174 if (profile == static_cast<Profile*>(this))
1175 return true;
1176 OffTheRecordProfileImpl* otr_profile = off_the_record_profile_.get();
1177 return otr_profile && profile == static_cast<Profile*>(otr_profile);
1178}
1179
1180Time ProfileImpl::GetStartTime() const {
1181 return start_time_;
1182}
1183
1184TabRestoreService* ProfileImpl::GetTabRestoreService() {
1185 if (!tab_restore_service_.get())
[email protected]169627b2008-12-06 19:30:191186 tab_restore_service_ = new TabRestoreService(this);
initial.commit09911bf2008-07-26 23:55:291187 return tab_restore_service_.get();
1188}
1189
[email protected]449478302009-06-09 20:04:281190ThumbnailStore* ProfileImpl::GetThumbnailStore() {
1191 if (!thumbnail_store_.get()) {
1192 thumbnail_store_ = new ThumbnailStore;
[email protected]daa82dc2009-07-28 01:22:251193 thumbnail_store_->Init(
1194 GetPath().Append(chrome::kNewTabThumbnailsFilename), this);
[email protected]449478302009-06-09 20:04:281195 }
1196 return thumbnail_store_.get();
1197}
1198
initial.commit09911bf2008-07-26 23:55:291199void ProfileImpl::ResetTabRestoreService() {
[email protected]169627b2008-12-06 19:30:191200 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:291201}
1202
[email protected]a9afddb2009-02-12 17:49:421203// To be run in the IO thread to notify all resource message filters that the
[email protected]20930852008-10-15 19:30:411204// spellchecker has changed.
1205class NotifySpellcheckerChangeTask : public Task {
1206 public:
1207 NotifySpellcheckerChangeTask(
[email protected]e7244d82008-10-29 18:13:261208 Profile* profile,
1209 const SpellcheckerReinitializedDetails& spellchecker)
[email protected]20930852008-10-15 19:30:411210 : profile_(profile),
1211 spellchecker_(spellchecker) {
1212 }
1213
1214 private:
1215 void Run(void) {
1216 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:561217 NotificationType::SPELLCHECKER_REINITIALIZED,
[email protected]20930852008-10-15 19:30:411218 Source<Profile>(profile_),
1219 Details<SpellcheckerReinitializedDetails>(&spellchecker_));
1220 }
1221
1222 Profile* profile_;
1223 SpellcheckerReinitializedDetails spellchecker_;
1224};
1225
[email protected]e3448ea2009-10-02 01:29:291226void ProfileImpl::ReinitializeSpellChecker() {
[email protected]20930852008-10-15 19:30:411227 PrefService* prefs = GetPrefs();
[email protected]e3448ea2009-10-02 01:29:291228 if (prefs->GetBoolean(prefs::kEnableSpellCheck)) {
1229 DeleteSpellCheckerImpl(false);
[email protected]20930852008-10-15 19:30:411230
[email protected]e3448ea2009-10-02 01:29:291231 // Retrieve the (perhaps updated recently) dictionary name from preferences.
[email protected]bd17b702009-02-25 20:44:081232 FilePath dict_dir;
[email protected]e7244d82008-10-29 18:13:261233 PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
[email protected]a9afddb2009-02-12 17:49:421234 // Note that, as the object pointed to by previously by spellchecker_
[email protected]e7244d82008-10-29 18:13:261235 // is being deleted in the io thread, the spellchecker_ can be made to point
1236 // to a new object (RE-initialized) in parallel in this UI thread.
[email protected]4b4d1adc2008-12-10 22:28:581237 spellchecker_ = new SpellChecker(dict_dir,
[email protected]74c8b422009-03-11 00:34:121238 WideToASCII(prefs->GetString(prefs::kSpellCheckDictionary)),
1239 GetRequestContext(),
[email protected]bd17b702009-02-25 20:44:081240 FilePath());
[email protected]e7244d82008-10-29 18:13:261241 spellchecker_->AddRef(); // Manual refcounting.
[email protected]20930852008-10-15 19:30:411242
[email protected]e3448ea2009-10-02 01:29:291243 // Set auto spell correct status for spellchecker.
[email protected]154a4332009-06-03 20:20:581244 spellchecker_->EnableAutoSpellCorrect(
1245 prefs->GetBoolean(prefs::kEnableAutoSpellCorrect));
[email protected]154a4332009-06-03 20:20:581246
[email protected]e3448ea2009-10-02 01:29:291247 NotifySpellCheckerChanged();
1248 } else {
1249 DeleteSpellCheckerImpl(true);
[email protected]20930852008-10-15 19:30:411250 }
1251}
1252
[email protected]e3448ea2009-10-02 01:29:291253void ProfileImpl::NotifySpellCheckerChanged() {
1254 // The I/O thread may be NULL during testing.
1255 base::Thread* io_thread = g_browser_process->io_thread();
1256 if (io_thread) { // Notify resource message filters.
1257 SpellcheckerReinitializedDetails scoped_spellchecker;
1258 scoped_spellchecker.spellchecker = spellchecker_;
1259 io_thread->message_loop()->PostTask(FROM_HERE,
1260 new NotifySpellcheckerChangeTask(this, scoped_spellchecker));
1261 }
1262}
1263
1264void ProfileImpl::DeleteSpellCheckerImpl(bool notify) {
1265 if (spellchecker_) {
1266 // The spellchecker must be deleted on the I/O thread.
1267 // The I/O thread may be NULL during testing.
1268 base::Thread* io_thread = g_browser_process->io_thread();
1269 if (io_thread)
1270 io_thread->message_loop()->ReleaseSoon(FROM_HERE, spellchecker_);
1271 else // during testing, we don't have an I/O thread
1272 spellchecker_->Release();
1273
1274 spellchecker_ = NULL;
1275
1276 if (notify)
1277 NotifySpellCheckerChanged();
1278 }
[email protected]e7244d82008-10-29 18:13:261279}
1280
initial.commit09911bf2008-07-26 23:55:291281SpellChecker* ProfileImpl::GetSpellChecker() {
1282 if (!spellchecker_) {
[email protected]20930852008-10-15 19:30:411283 // This is where spellchecker gets initialized. Note that this is being
1284 // initialized in the ui_thread. However, this is not a problem as long as
1285 // it is *used* in the io thread.
[email protected]34cc84f2009-02-13 10:04:351286 // TODO(sidchat): One day, change everything so that spellchecker gets
[email protected]20930852008-10-15 19:30:411287 // initialized in the IO thread itself.
[email protected]e3448ea2009-10-02 01:29:291288 ReinitializeSpellChecker();
initial.commit09911bf2008-07-26 23:55:291289 }
[email protected]20930852008-10-15 19:30:411290
initial.commit09911bf2008-07-26 23:55:291291 return spellchecker_;
1292}
1293
[email protected]3bf335a2009-06-26 20:46:061294WebKitContext* ProfileImpl::GetWebKitContext() {
1295 if (!webkit_context_.get())
1296 webkit_context_ = new WebKitContext(path_, false);
1297 DCHECK(webkit_context_.get());
1298 return webkit_context_.get();
1299}
1300
[email protected]4bb336302009-10-12 05:44:261301DesktopNotificationService* ProfileImpl::GetDesktopNotificationService() {
1302 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
1303 if (!desktop_notification_service_.get()) {
1304 // TODO(johnnyg): hook this up with notification UI manager.
1305 desktop_notification_service_.reset(new DesktopNotificationService(
1306 this, NULL));
1307 }
1308
1309 return desktop_notification_service_.get();
1310}
1311
initial.commit09911bf2008-07-26 23:55:291312void ProfileImpl::MarkAsCleanShutdown() {
1313 if (prefs_.get()) {
1314 // The session cleanly exited, set kSessionExitedCleanly appropriately.
1315 prefs_->SetBoolean(prefs::kSessionExitedCleanly, true);
1316
1317 // NOTE: If you change what thread this writes on, be sure and update
1318 // ChromeFrame::EndSession().
[email protected]6faa0e0d2009-04-28 06:50:361319 prefs_->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:291320 }
1321}
1322
[email protected]e7244d82008-10-29 18:13:261323void ProfileImpl::Observe(NotificationType type,
1324 const NotificationSource& source,
1325 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:561326 if (NotificationType::PREF_CHANGED == type) {
[email protected]e7244d82008-10-29 18:13:261327 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
1328 PrefService* prefs = Source<PrefService>(source).ptr();
1329 DCHECK(pref_name_in && prefs);
1330 if (*pref_name_in == prefs::kSpellCheckDictionary ||
[email protected]154a4332009-06-03 20:20:581331 *pref_name_in == prefs::kEnableSpellCheck ||
1332 *pref_name_in == prefs::kEnableAutoSpellCorrect) {
[email protected]e3448ea2009-10-02 01:29:291333 ReinitializeSpellChecker();
[email protected]e7244d82008-10-29 18:13:261334 }
[email protected]4a190632009-05-09 01:07:421335 } else if (NotificationType::THEME_INSTALLED == type) {
1336 Extension* extension = Details<Extension>(details).ptr();
1337 SetTheme(extension);
[email protected]48352c12009-08-15 01:19:111338 } else if (NotificationType::BOOKMARK_MODEL_LOADED == type) {
1339 GetProfileSyncService(); // Causes lazy-load if sync is enabled.
1340 registrar_.Remove(this, NotificationType::BOOKMARK_MODEL_LOADED,
1341 Source<Profile>(this));
[email protected]e7244d82008-10-29 18:13:261342 }
1343}
1344
initial.commit09911bf2008-07-26 23:55:291345void ProfileImpl::StopCreateSessionServiceTimer() {
[email protected]2d316662008-09-03 18:18:141346 create_session_service_timer_.Stop();
initial.commit09911bf2008-07-26 23:55:291347}
[email protected]3a453fa2008-08-15 18:46:341348
[email protected]48352c12009-08-15 01:19:111349ProfileSyncService* ProfileImpl::GetProfileSyncService() {
[email protected]066629f2009-10-17 00:28:131350#if defined(BROWSER_SYNC)
[email protected]7367f1a2009-10-10 01:09:441351 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableSync)) {
1352 if (!sync_service_.get())
1353 InitSyncService();
1354 return sync_service_.get();
1355 }
1356#endif
[email protected]48352c12009-08-15 01:19:111357 return NULL;
1358}
1359
1360void ProfileImpl::InitSyncService() {
[email protected]066629f2009-10-17 00:28:131361#if defined(BROWSER_SYNC)
[email protected]48352c12009-08-15 01:19:111362 sync_service_.reset(new ProfileSyncService(this));
1363 sync_service_->Initialize();
1364#endif
1365}