blob: 9017c5f0595f1952c673ce07a069f080d40372ae [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/file_util.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/path_service.h"
11#include "base/scoped_ptr.h"
12#include "base/string_util.h"
[email protected]a9afddb2009-02-12 17:49:4213#include "chrome/browser/bookmarks/bookmark_model.h"
initial.commit09911bf2008-07-26 23:55:2914#include "chrome/browser/browser_list.h"
15#include "chrome/browser/browser_process.h"
[email protected]67a46b7f2009-06-16 21:41:0216#include "chrome/browser/browser_theme_provider.h"
[email protected]b7f05882009-02-22 01:21:5617#include "chrome/browser/download/download_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]a9afddb2009-02-12 17:49:4222#include "chrome/browser/history/history.h"
[email protected]3bf335a2009-06-26 20:46:0623#include "chrome/browser/in_process_webkit/webkit_context.h"
[email protected]6ab9b202008-12-23 22:34:5024#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]e69d33952009-06-03 22:00:4125#include "chrome/browser/password_manager/password_store_default.h"
[email protected]eaadd9052009-06-23 18:02:2326#include "chrome/browser/privacy_blacklist/blacklist.h"
initial.commit09911bf2008-07-26 23:55:2927#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2628#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]1132436e2009-04-08 20:06:3329#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]f63ae312009-02-04 17:58:4630#include "chrome/browser/search_engines/template_url_model.h"
[email protected]85e921fb82009-02-11 23:19:4431#include "chrome/browser/sessions/session_service.h"
[email protected]bd580a252009-02-12 01:16:3032#include "chrome/browser/sessions/tab_restore_service.h"
[email protected]f0a644292009-02-25 23:32:4733#include "chrome/browser/spellchecker.h"
[email protected]1132436e2009-04-08 20:06:3334#include "chrome/browser/ssl/ssl_host_state.h"
[email protected]449478302009-06-09 20:04:2835#include "chrome/browser/thumbnail_store.h"
initial.commit09911bf2008-07-26 23:55:2936#include "chrome/browser/visitedlink_master.h"
[email protected]3e90d4a2009-07-03 17:38:3937#include "chrome/browser/visitedlink_event_listener.h"
initial.commit09911bf2008-07-26 23:55:2938#include "chrome/browser/webdata/web_data_service.h"
39#include "chrome/common/chrome_constants.h"
40#include "chrome/common/chrome_paths.h"
41#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5842#include "chrome/common/extensions/extension_error_reporter.h"
[email protected]47accfd62009-05-14 18:46:2143#include "chrome/common/net/cookie_monster_sqlite.h"
initial.commit09911bf2008-07-26 23:55:2944#include "chrome/common/notification_service.h"
45#include "chrome/common/pref_names.h"
[email protected]f7011fcb2009-01-28 21:54:3246#include "chrome/common/render_messages.h"
[email protected]f90f5c512009-02-18 19:10:5847#include "grit/locale_settings.h"
[email protected]a9cea7542009-05-20 04:30:2348#include "net/base/force_tls_state.h"
initial.commit09911bf2008-07-26 23:55:2949
[email protected]a5166af62009-07-03 00:42:2950#if defined(OS_LINUX)
51#include "chrome/browser/gtk/gtk_theme_provider.h"
52#endif
53
[email protected]e1acf6f2008-10-27 20:43:3354using base::Time;
55using base::TimeDelta;
56
[email protected]e5642992009-06-26 23:06:3157namespace {
58
initial.commit09911bf2008-07-26 23:55:2959// Delay, in milliseconds, before we explicitly create the SessionService.
60static const int kCreateSessionServiceDelayMS = 500;
61
[email protected]e5642992009-06-26 23:06:3162enum ContextType {
63 kNormalContext,
64 kMediaContext
65};
66
67// Gets the cache parameters from the command line. |type| is the type of
68// request context that we need, |cache_path| will be set to the user provided
69// path, or will not be touched if there is not an argument. |max_size| will
70// be the user provided value or zero by default.
71void GetCacheParameters(ContextType type, FilePath* cache_path,
72 int* max_size) {
73 DCHECK(cache_path);
74 DCHECK(max_size);
75
76 // Override the cache location if specified by the user.
77 std::wstring user_path(CommandLine::ForCurrentProcess()->GetSwitchValue(
78 switches::kDiskCacheDir));
79
80 if (!user_path.empty()) {
81 *cache_path = FilePath::FromWStringHack(user_path);
82 }
83
84 const wchar_t* arg = kNormalContext == type ? switches::kDiskCacheSize :
85 switches::kMediaCacheSize;
86 std::string value =
87 WideToASCII(CommandLine::ForCurrentProcess()->GetSwitchValue(arg));
88
89 // By default we let the cache determine the right size.
90 *max_size = 0;
91 if (!StringToInt(value, max_size)) {
92 *max_size = 0;
93 } else if (max_size < 0) {
94 *max_size = 0;
95 }
96}
97
98} // namespace
99
initial.commit09911bf2008-07-26 23:55:29100// A pointer to the request context for the default profile. See comments on
101// Profile::GetDefaultRequestContext.
102URLRequestContext* Profile::default_request_context_;
103
[email protected]47accfd62009-05-14 18:46:21104static void CleanupRequestContext(ChromeURLRequestContext* context) {
105 if (context) {
106 context->CleanupOnUIThread();
107
108 // Clean up request context on IO thread.
109 g_browser_process->io_thread()->message_loop()->ReleaseSoon(FROM_HERE,
110 context);
111 }
112}
113
[email protected]34cc84f2009-02-13 10:04:35114// static
initial.commit09911bf2008-07-26 23:55:29115void Profile::RegisterUserPrefs(PrefService* prefs) {
[email protected]430d3f72008-10-27 17:56:55116 prefs->RegisterBooleanPref(prefs::kSearchSuggestEnabled, true);
initial.commit09911bf2008-07-26 23:55:29117 prefs->RegisterBooleanPref(prefs::kSessionExitedCleanly, true);
118 prefs->RegisterBooleanPref(prefs::kSafeBrowsingEnabled, true);
[email protected]74c8b422009-03-11 00:34:12119 // TODO(estade): IDS_SPELLCHECK_DICTIONARY should be an ASCII string.
[email protected]e7244d82008-10-29 18:13:26120 prefs->RegisterLocalizedStringPref(prefs::kSpellCheckDictionary,
121 IDS_SPELLCHECK_DICTIONARY);
122 prefs->RegisterBooleanPref(prefs::kEnableSpellCheck, true);
[email protected]154a4332009-06-03 20:20:58123 prefs->RegisterBooleanPref(prefs::kEnableAutoSpellCorrect, true);
[email protected]f93fe782009-02-19 01:26:13124 prefs->RegisterBooleanPref(prefs::kEnableUserScripts, false);
[email protected]a5166af62009-07-03 00:42:29125#if defined(OS_LINUX)
126 prefs->RegisterBooleanPref(prefs::kUsesSystemTheme, false);
127#endif
[email protected]4a190632009-05-09 01:07:42128 prefs->RegisterStringPref(prefs::kCurrentThemeID, L"");
129 prefs->RegisterDictionaryPref(prefs::kCurrentThemeImages);
130 prefs->RegisterDictionaryPref(prefs::kCurrentThemeColors);
131 prefs->RegisterDictionaryPref(prefs::kCurrentThemeTints);
[email protected]7895ea22009-06-02 20:53:50132 prefs->RegisterDictionaryPref(prefs::kCurrentThemeDisplayProperties);
[email protected]f93fe782009-02-19 01:26:13133 prefs->RegisterBooleanPref(prefs::kEnableExtensions, false);
initial.commit09911bf2008-07-26 23:55:29134}
135
[email protected]34cc84f2009-02-13 10:04:35136// static
[email protected]f7011fcb2009-01-28 21:54:32137Profile* Profile::CreateProfile(const FilePath& path) {
initial.commit09911bf2008-07-26 23:55:29138 return new ProfileImpl(path);
139}
140
[email protected]34cc84f2009-02-13 10:04:35141// static
initial.commit09911bf2008-07-26 23:55:29142URLRequestContext* Profile::GetDefaultRequestContext() {
143 return default_request_context_;
144}
145
[email protected]e69d33952009-06-03 22:00:41146#if defined(OS_LINUX)
147// Temporarily disabled while we figure some stuff out.
148// http://code.google.com/p/chromium/issues/detail?id=12351
149// #include "chrome/browser/password_manager/password_store_gnome.h"
150// #include "chrome/browser/password_manager/password_store_kwallet.h"
151#elif defined(OS_WIN)
152#include "chrome/browser/password_manager/password_store_win.h"
[email protected]034eba52009-06-03 22:50:56153#elif defined(OS_MACOSX)
154#include "chrome/browser/keychain_mac.h"
[email protected]e0411ae52009-06-30 23:59:17155#include "chrome/browser/password_manager/login_database_mac.h"
[email protected]034eba52009-06-03 22:50:56156#include "chrome/browser/password_manager/password_store_mac.h"
[email protected]e69d33952009-06-03 22:00:41157#endif
initial.commit09911bf2008-07-26 23:55:29158
initial.commit09911bf2008-07-26 23:55:29159////////////////////////////////////////////////////////////////////////////////
160//
161// OffTheRecordProfileImpl is a profile subclass that wraps an existing profile
[email protected]4a190632009-05-09 01:07:42162// to make it suitable for the off the record mode.
initial.commit09911bf2008-07-26 23:55:29163//
164////////////////////////////////////////////////////////////////////////////////
165class OffTheRecordProfileImpl : public Profile,
166 public NotificationObserver {
167 public:
168 explicit OffTheRecordProfileImpl(Profile* real_profile)
169 : profile_(real_profile),
[email protected]47accfd62009-05-14 18:46:21170 extensions_request_context_(NULL),
initial.commit09911bf2008-07-26 23:55:29171 start_time_(Time::Now()) {
[email protected]6ab9b202008-12-23 22:34:50172 request_context_ = ChromeURLRequestContext::CreateOffTheRecord(this);
initial.commit09911bf2008-07-26 23:55:29173 request_context_->AddRef();
[email protected]481e1a42009-05-06 20:56:05174
initial.commit09911bf2008-07-26 23:55:29175 // Register for browser close notifications so we can detect when the last
176 // off-the-record window is closed, in which case we can clean our states
177 // (cookies, downloads...).
[email protected]88cf3292009-05-22 01:48:43178 registrar_.Add(this, NotificationType::BROWSER_CLOSED,
179 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29180 }
181
182 virtual ~OffTheRecordProfileImpl() {
[email protected]47accfd62009-05-14 18:46:21183 CleanupRequestContext(request_context_);
[email protected]47accfd62009-05-14 18:46:21184 CleanupRequestContext(extensions_request_context_);
initial.commit09911bf2008-07-26 23:55:29185 }
186
[email protected]f7011fcb2009-01-28 21:54:32187 virtual FilePath GetPath() { return profile_->GetPath(); }
initial.commit09911bf2008-07-26 23:55:29188
189 virtual bool IsOffTheRecord() {
190 return true;
191 }
192
193 virtual Profile* GetOffTheRecordProfile() {
194 return this;
195 }
196
[email protected]860f55492009-03-27 19:50:59197 virtual void DestroyOffTheRecordProfile() {
198 // Suicide is bad!
199 NOTREACHED();
200 }
201
initial.commit09911bf2008-07-26 23:55:29202 virtual Profile* GetOriginalProfile() {
203 return profile_;
204 }
205
206 virtual VisitedLinkMaster* GetVisitedLinkMaster() {
[email protected]7fb6c862009-03-13 02:51:49207 // We don't provide access to the VisitedLinkMaster when we're OffTheRecord
208 // because we don't want to leak the sites that the user has visited before.
209 return NULL;
initial.commit09911bf2008-07-26 23:55:29210 }
211
[email protected]6014d672008-12-05 00:38:25212 virtual ExtensionsService* GetExtensionsService() {
[email protected]1bd54132009-06-11 00:05:34213 return NULL;
[email protected]6014d672008-12-05 00:38:25214 }
215
[email protected]0938d3c2009-01-09 20:37:35216 virtual UserScriptMaster* GetUserScriptMaster() {
217 return profile_->GetUserScriptMaster();
[email protected]04fba9a92008-10-28 17:25:25218 }
219
[email protected]481e1a42009-05-06 20:56:05220 virtual ExtensionProcessManager* GetExtensionProcessManager() {
[email protected]1bd54132009-06-11 00:05:34221 return NULL;
[email protected]481e1a42009-05-06 20:56:05222 }
223
[email protected]7120f132009-07-20 21:05:37224 virtual ExtensionMessageService* GetExtensionMessageService() {
225 return NULL;
226 }
227
[email protected]34cc84f2009-02-13 10:04:35228 virtual SSLHostState* GetSSLHostState() {
229 if (!ssl_host_state_.get())
230 ssl_host_state_.reset(new SSLHostState());
231
232 DCHECK(ssl_host_state_->CalledOnValidThread());
233 return ssl_host_state_.get();
234 }
235
[email protected]a9cea7542009-05-20 04:30:23236 virtual net::ForceTLSState* GetForceTLSState() {
237 if (!force_tls_state_.get())
238 force_tls_state_.reset(new net::ForceTLSState());
239
240 return force_tls_state_.get();
241 }
242
initial.commit09911bf2008-07-26 23:55:29243 virtual HistoryService* GetHistoryService(ServiceAccessType sat) {
244 if (sat == EXPLICIT_ACCESS) {
245 return profile_->GetHistoryService(sat);
246 } else {
247 NOTREACHED() << "This profile is OffTheRecord";
248 return NULL;
249 }
250 }
251
initial.commit09911bf2008-07-26 23:55:29252 virtual WebDataService* GetWebDataService(ServiceAccessType sat) {
253 if (sat == EXPLICIT_ACCESS) {
254 return profile_->GetWebDataService(sat);
255 } else {
256 NOTREACHED() << "This profile is OffTheRecord";
257 return NULL;
258 }
259 }
260
[email protected]e69d33952009-06-03 22:00:41261 virtual PasswordStore* GetPasswordStore(ServiceAccessType sat) {
262 if (sat == EXPLICIT_ACCESS) {
263 return profile_->GetPasswordStore(sat);
264 } else {
265 NOTREACHED() << "This profile is OffTheRecord";
266 return NULL;
267 }
268 }
269
initial.commit09911bf2008-07-26 23:55:29270 virtual PrefService* GetPrefs() {
271 return profile_->GetPrefs();
272 }
273
274 virtual TemplateURLModel* GetTemplateURLModel() {
275 return profile_->GetTemplateURLModel();
276 }
277
278 virtual TemplateURLFetcher* GetTemplateURLFetcher() {
279 return profile_->GetTemplateURLFetcher();
280 }
281
282 virtual DownloadManager* GetDownloadManager() {
283 if (!download_manager_.get()) {
284 scoped_refptr<DownloadManager> dlm(new DownloadManager);
285 dlm->Init(this);
286 download_manager_.swap(dlm);
287 }
288 return download_manager_.get();
289 }
290
291 virtual bool HasCreatedDownloadManager() const {
292 return (download_manager_.get() != NULL);
293 }
294
[email protected]4a190632009-05-09 01:07:42295 virtual void InitThemes() {
296 GetOriginalProfile()->InitThemes();
297 }
298
299 virtual void SetTheme(Extension* extension) {
300 GetOriginalProfile()->SetTheme(extension);
301 }
302
[email protected]a5166af62009-07-03 00:42:29303 virtual void SetNativeTheme() {
304 GetOriginalProfile()->SetNativeTheme();
305 }
306
[email protected]4a190632009-05-09 01:07:42307 virtual void ClearTheme() {
308 GetOriginalProfile()->ClearTheme();
309 }
310
311 virtual ThemeProvider* GetThemeProvider() {
312 return GetOriginalProfile()->GetThemeProvider();
313 }
314
initial.commit09911bf2008-07-26 23:55:29315 virtual URLRequestContext* GetRequestContext() {
316 return request_context_;
317 }
318
[email protected]e7f29642009-03-02 22:53:18319 virtual URLRequestContext* GetRequestContextForMedia() {
[email protected]d14c7ac2009-05-29 20:38:11320 // In OTR mode, media request context is the same as the original one.
321 return request_context_;
[email protected]e7f29642009-03-02 22:53:18322 }
323
[email protected]47accfd62009-05-14 18:46:21324 URLRequestContext* GetRequestContextForExtensions() {
325 if (!extensions_request_context_) {
326 extensions_request_context_ =
327 ChromeURLRequestContext::CreateOffTheRecordForExtensions(this);
328 extensions_request_context_->AddRef();
329
330 DCHECK(extensions_request_context_->cookie_store());
331 }
332
333 return extensions_request_context_;
334 }
335
[email protected]eaadd9052009-06-23 18:02:23336 virtual Blacklist* GetBlacklist() {
337 return GetOriginalProfile()->GetBlacklist();
338 }
339
initial.commit09911bf2008-07-26 23:55:29340 virtual SessionService* GetSessionService() {
341 // Don't save any sessions when off the record.
342 return NULL;
343 }
344
345 virtual void ShutdownSessionService() {
346 // We don't allow a session service, nothing to do.
347 }
348
349 virtual bool HasSessionService() const {
350 // We never have a session service.
351 return false;
352 }
353
354 virtual std::wstring GetName() {
355 return profile_->GetName();
356 }
357
358 virtual void SetName(const std::wstring& name) {
359 profile_->SetName(name);
360 }
361
362 virtual std::wstring GetID() {
363 return profile_->GetID();
364 }
365
366 virtual void SetID(const std::wstring& id) {
367 profile_->SetID(id);
368 }
369
initial.commit09911bf2008-07-26 23:55:29370 virtual bool DidLastSessionExitCleanly() {
371 return profile_->DidLastSessionExitCleanly();
372 }
373
[email protected]d8e41ed2008-09-11 15:22:32374 virtual BookmarkModel* GetBookmarkModel() {
375 return profile_->GetBookmarkModel();
initial.commit09911bf2008-07-26 23:55:29376 }
377
[email protected]3a453fa2008-08-15 18:46:34378#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07379 virtual ProfilePersonalization* GetProfilePersonalization() {
[email protected]3a453fa2008-08-15 18:46:34380 return profile_->GetProfilePersonalization();
381 }
382#endif
383
initial.commit09911bf2008-07-26 23:55:29384 virtual bool IsSameProfile(Profile* profile) {
385 if (profile == static_cast<Profile*>(this))
386 return true;
387 return profile == profile_;
388 }
389
390 virtual Time GetStartTime() const {
391 return start_time_;
392 }
393
394 virtual TabRestoreService* GetTabRestoreService() {
395 return NULL;
396 }
397
[email protected]e7244d82008-10-29 18:13:26398 virtual void ResetTabRestoreService() {
[email protected]20930852008-10-15 19:30:41399 }
400
[email protected]e7244d82008-10-29 18:13:26401 virtual void ReinitializeSpellChecker() {
402 profile_->ReinitializeSpellChecker();
initial.commit09911bf2008-07-26 23:55:29403 }
404
405 virtual SpellChecker* GetSpellChecker() {
406 return profile_->GetSpellChecker();
407 }
408
[email protected]3bf335a2009-06-26 20:46:06409 virtual WebKitContext* GetWebKitContext() {
410 if (!webkit_context_.get())
411 webkit_context_ = new WebKitContext(GetPath(), true);
412 DCHECK(webkit_context_.get());
413 return webkit_context_.get();
414}
415
[email protected]449478302009-06-09 20:04:28416 virtual ThumbnailStore* GetThumbnailStore() {
417 return NULL;
418 }
419
initial.commit09911bf2008-07-26 23:55:29420 virtual void MarkAsCleanShutdown() {
421 }
422
[email protected]bdbc87c2009-01-25 05:08:54423 virtual void InitExtensions() {
424 NOTREACHED();
425 }
426
[email protected]ea0c98cf2009-06-18 23:02:54427 virtual void InitWebResources() {
428 NOTREACHED();
429 }
430
initial.commit09911bf2008-07-26 23:55:29431 virtual void ExitedOffTheRecordMode() {
432 // Drop our download manager so we forget about all the downloads made
433 // in off-the-record mode.
434 download_manager_ = NULL;
435 }
436
437 virtual void Observe(NotificationType type,
438 const NotificationSource& source,
439 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56440 DCHECK_EQ(NotificationType::BROWSER_CLOSED, type.value);
initial.commit09911bf2008-07-26 23:55:29441 // We are only interested in OTR browser closing.
442 if (Source<Browser>(source)->profile() != this)
443 return;
444
445 // Let's check if we still have an Off The Record window opened.
446 // Note that we check against 1 as this notification is sent before the
447 // browser window is actually removed from the list.
448 if (BrowserList::GetBrowserCount(this) <= 1)
449 ExitedOffTheRecordMode();
450 }
451
452 private:
[email protected]88cf3292009-05-22 01:48:43453 NotificationRegistrar registrar_;
454
initial.commit09911bf2008-07-26 23:55:29455 // The real underlying profile.
456 Profile* profile_;
457
[email protected]6ab9b202008-12-23 22:34:50458 // The context to use for requests made from this OTR session.
459 ChromeURLRequestContext* request_context_;
initial.commit09911bf2008-07-26 23:55:29460
[email protected]47accfd62009-05-14 18:46:21461 ChromeURLRequestContext* extensions_request_context_;
462
initial.commit09911bf2008-07-26 23:55:29463 // The download manager that only stores downloaded items in memory.
464 scoped_refptr<DownloadManager> download_manager_;
465
[email protected]4a190632009-05-09 01:07:42466 scoped_refptr<BrowserThemeProvider> theme_provider_;
467
[email protected]3bf335a2009-06-26 20:46:06468 // Use a special WebKit context for OTR browsing.
469 scoped_refptr<WebKitContext> webkit_context_;
470
[email protected]34cc84f2009-02-13 10:04:35471 // We don't want SSLHostState from the OTR profile to leak back to the main
472 // profile because then the main profile would learn some of the host names
473 // the user visited while OTR.
474 scoped_ptr<SSLHostState> ssl_host_state_;
475
[email protected]a9cea7542009-05-20 04:30:23476 // The ForceTLSState that only stores enabled sites in memory.
477 scoped_ptr<net::ForceTLSState> force_tls_state_;
478
initial.commit09911bf2008-07-26 23:55:29479 // Time we were started.
480 Time start_time_;
481
482 DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl);
483};
484
[email protected]f7011fcb2009-01-28 21:54:32485ProfileImpl::ProfileImpl(const FilePath& path)
initial.commit09911bf2008-07-26 23:55:29486 : path_(path),
[email protected]3e90d4a2009-07-03 17:38:39487 visited_link_event_listener_(new VisitedLinkEventListener()),
[email protected]f7011fcb2009-01-28 21:54:32488 request_context_(NULL),
[email protected]363347b2009-03-13 20:06:57489 media_request_context_(NULL),
[email protected]47accfd62009-05-14 18:46:21490 extensions_request_context_(NULL),
[email protected]eaadd9052009-06-23 18:02:23491 blacklist_(NULL),
[email protected]f7011fcb2009-01-28 21:54:32492 history_service_created_(false),
493 created_web_data_service_(false),
[email protected]e69d33952009-06-03 22:00:41494 created_password_store_(false),
[email protected]f7011fcb2009-01-28 21:54:32495 created_download_manager_(false),
[email protected]4a190632009-05-09 01:07:42496 created_theme_provider_(false),
[email protected]f7011fcb2009-01-28 21:54:32497 start_time_(Time::Now()),
498 spellchecker_(NULL),
initial.commit09911bf2008-07-26 23:55:29499 shutdown_session_service_(false) {
500 DCHECK(!path.empty()) << "Using an empty path will attempt to write " <<
501 "profile files to the root directory!";
[email protected]2d316662008-09-03 18:18:14502 create_session_service_timer_.Start(
503 TimeDelta::FromMilliseconds(kCreateSessionServiceDelayMS), this,
504 &ProfileImpl::EnsureSessionServiceCreated);
[email protected]bdbc87c2009-01-25 05:08:54505
[email protected]382a0702009-06-26 17:12:27506 extension_process_manager_.reset(new ExtensionProcessManager(this));
[email protected]7120f132009-07-20 21:05:37507 extension_message_service_ = new ExtensionMessageService(this);
[email protected]382a0702009-06-26 17:12:27508
[email protected]e7244d82008-10-29 18:13:26509 PrefService* prefs = GetPrefs();
510 prefs->AddPrefObserver(prefs::kSpellCheckDictionary, this);
511 prefs->AddPrefObserver(prefs::kEnableSpellCheck, this);
[email protected]154a4332009-06-03 20:20:58512 prefs->AddPrefObserver(prefs::kEnableAutoSpellCorrect, this);
[email protected]380ab462009-04-24 01:23:58513
514#ifdef CHROME_PERSONALIZATION
515 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableP13n))
516 personalization_.reset(Personalization::CreateProfilePersonalization(this));
517#endif
518
[email protected]eaadd9052009-06-23 18:02:23519 if (CommandLine::ForCurrentProcess()->
520 HasSwitch(switches::kPrivacyBlacklist)) {
521 std::wstring option = CommandLine::ForCurrentProcess()->GetSwitchValue(
522 switches::kPrivacyBlacklist);
523#if defined(OS_POSIX)
524 FilePath path(WideToUTF8(option));
525#else
526 FilePath path(option);
527#endif
528 blacklist_ = new Blacklist(path);
529 }
530
[email protected]4a190632009-05-09 01:07:42531 // Listen for theme installation.
[email protected]88cf3292009-05-22 01:48:43532 registrar_.Add(this, NotificationType::THEME_INSTALLED,
533 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29534}
535
[email protected]bdbc87c2009-01-25 05:08:54536void ProfileImpl::InitExtensions() {
[email protected]8c756ac2009-01-30 23:36:41537 if (user_script_master_ || extensions_service_)
538 return; // Already initialized.
539
[email protected]bdbc87c2009-01-25 05:08:54540 const CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]f93fe782009-02-19 01:26:13541 PrefService* prefs = GetPrefs();
[email protected]bdbc87c2009-01-25 05:08:54542 bool user_scripts_enabled =
[email protected]f0a51fb52009-03-05 12:46:38543 command_line->HasSwitch(switches::kEnableUserScripts) ||
[email protected]f93fe782009-02-19 01:26:13544 prefs->GetBoolean(prefs::kEnableUserScripts);
[email protected]bdbc87c2009-01-25 05:08:54545
[email protected]f7011fcb2009-01-28 21:54:32546 FilePath script_dir;
[email protected]bdbc87c2009-01-25 05:08:54547 if (user_scripts_enabled) {
[email protected]0cd957b2009-03-06 20:13:23548 if (command_line->HasSwitch(switches::kUserScriptsDir)) {
549 std::wstring path_string =
550 command_line->GetSwitchValue(switches::kUserScriptsDir);
551 script_dir = FilePath::FromWStringHack(path_string);
552 } else {
553 script_dir = GetPath();
554 script_dir = script_dir.Append(chrome::kUserScriptsDirname);
555 }
[email protected]bdbc87c2009-01-25 05:08:54556 }
557
[email protected]bb28e062009-02-27 17:19:18558 ExtensionErrorReporter::Init(true); // allow noisy errors.
[email protected]bdbc87c2009-01-25 05:08:54559 user_script_master_ = new UserScriptMaster(
[email protected]9197f3b2009-06-02 00:49:27560 g_browser_process->file_thread()->message_loop(),
561 script_dir);
[email protected]894bb502009-05-21 22:39:57562 extensions_service_ = new ExtensionsService(
[email protected]a9b00ac2009-06-25 21:03:23563 this,
564 CommandLine::ForCurrentProcess(),
565 GetPrefs(),
566 GetPath().AppendASCII(ExtensionsService::kInstallDirectoryName),
567 MessageLoop::current(),
[email protected]93fd78f42009-07-10 16:43:17568 g_browser_process->file_thread()->message_loop(),
[email protected]b40cd872009-07-10 22:58:19569 false);
[email protected]bdbc87c2009-01-25 05:08:54570
[email protected]9197f3b2009-06-02 00:49:27571 extensions_service_->Init();
[email protected]919ddc82009-07-15 04:30:12572
573 // Load any extensions specified with --load-extension.
574 if (command_line->HasSwitch(switches::kLoadExtension)) {
575 std::wstring path_string =
576 command_line->GetSwitchValue(switches::kLoadExtension);
577 FilePath path = FilePath::FromWStringHack(path_string);
578 extensions_service_->LoadExtension(path);
579
580 // Tell UserScriptMaser to watch this extension's directory for changes so
581 // you can live edit content scripts during development.
582 user_script_master_->AddWatchedPath(path);
583 }
[email protected]bdbc87c2009-01-25 05:08:54584}
585
[email protected]ea0c98cf2009-06-18 23:02:54586void ProfileImpl::InitWebResources() {
[email protected]0c274c202009-07-12 02:59:07587 if (web_resource_service_)
588 return; // Already initialized.
589
[email protected]ea0c98cf2009-06-18 23:02:54590 web_resource_service_ = new WebResourceService(
591 this,
592 g_browser_process->file_thread()->message_loop());
593
594 web_resource_service_->StartAfterDelay();
595}
596
initial.commit09911bf2008-07-26 23:55:29597ProfileImpl::~ProfileImpl() {
[email protected]169627b2008-12-06 19:30:19598 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29599
600 StopCreateSessionServiceTimer();
601 // TemplateURLModel schedules a task on the WebDataService from its
602 // destructor. Delete it first to ensure the task gets scheduled before we
603 // shut down the database.
604 template_url_model_.reset();
605
606 // The download manager queries the history system and should be deleted
607 // before the history is shutdown so it can properly cancel all requests.
608 download_manager_ = NULL;
609
[email protected]4a190632009-05-09 01:07:42610 // The theme provider provides bitmaps to whoever wants them.
611 theme_provider_ = NULL;
612
[email protected]44961292009-07-20 20:03:09613 // The ThumbnailStore saves thumbnails used by the NTP. Call Shutdown to
614 // save any new thumbnails to disk and release its reference to the
615 // HistoryService.
616 if (thumbnail_store_.get())
617 thumbnail_store_->Shutdown();
618
[email protected]e7244d82008-10-29 18:13:26619 // Remove pref observers.
620 PrefService* prefs = GetPrefs();
621 prefs->RemovePrefObserver(prefs::kSpellCheckDictionary, this);
622 prefs->RemovePrefObserver(prefs::kEnableSpellCheck, this);
[email protected]154a4332009-06-03 20:20:58623 prefs->RemovePrefObserver(prefs::kEnableAutoSpellCorrect, this);
[email protected]e7244d82008-10-29 18:13:26624
[email protected]3a453fa2008-08-15 18:46:34625#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07626 personalization_.reset();
[email protected]3a453fa2008-08-15 18:46:34627#endif
628
initial.commit09911bf2008-07-26 23:55:29629 // Both HistoryService and WebDataService maintain threads for background
630 // processing. Its possible each thread still has tasks on it that have
631 // increased the ref count of the service. In such a situation, when we
632 // decrement the refcount, it won't be 0, and the threads/databases aren't
633 // properly shut down. By explicitly calling Cleanup/Shutdown we ensure the
634 // databases are properly closed.
635 if (web_data_service_.get())
636 web_data_service_->Shutdown();
637
638 if (history_service_.get())
639 history_service_->Cleanup();
640
641 // The I/O thread may be NULL during testing.
[email protected]ab820df2008-08-26 05:55:10642 base::Thread* io_thread = g_browser_process->io_thread();
initial.commit09911bf2008-07-26 23:55:29643
644 if (spellchecker_) {
645 // The spellchecker must be deleted on the I/O thread. During testing, we
646 // don't have an I/O thread.
647 if (io_thread)
648 io_thread->message_loop()->ReleaseSoon(FROM_HERE, spellchecker_);
649 else
650 spellchecker_->Release();
651 }
652
[email protected]47accfd62009-05-14 18:46:21653 if (default_request_context_ == request_context_)
654 default_request_context_ = NULL;
[email protected]6ab9b202008-12-23 22:34:50655
[email protected]47accfd62009-05-14 18:46:21656 CleanupRequestContext(request_context_);
657 CleanupRequestContext(media_request_context_);
658 CleanupRequestContext(extensions_request_context_);
[email protected]1e744f22009-04-08 01:00:17659
[email protected]eaadd9052009-06-23 18:02:23660 // When the request contexts are gone, the blacklist wont be needed anymore.
661 delete blacklist_;
662 blacklist_ = 0;
663
[email protected]d8e41ed2008-09-11 15:22:32664 // HistoryService may call into the BookmarkModel, as such we need to
665 // delete HistoryService before the BookmarkModel. The destructor for
[email protected]90ef13132008-08-27 03:27:46666 // HistoryService will join with HistoryService's backend thread so that
667 // by the time the destructor has finished we're sure it will no longer call
[email protected]d8e41ed2008-09-11 15:22:32668 // into the BookmarkModel.
[email protected]90ef13132008-08-27 03:27:46669 history_service_ = NULL;
670 bookmark_bar_model_.reset();
671
[email protected]7120f132009-07-20 21:05:37672 extension_message_service_->ProfileDestroyed();
673
initial.commit09911bf2008-07-26 23:55:29674 MarkAsCleanShutdown();
675}
676
[email protected]f7011fcb2009-01-28 21:54:32677FilePath ProfileImpl::GetPath() {
initial.commit09911bf2008-07-26 23:55:29678 return path_;
679}
680
681bool ProfileImpl::IsOffTheRecord() {
682 return false;
683}
684
685Profile* ProfileImpl::GetOffTheRecordProfile() {
686 if (!off_the_record_profile_.get()) {
687 scoped_ptr<OffTheRecordProfileImpl> p(new OffTheRecordProfileImpl(this));
688 off_the_record_profile_.swap(p);
689 }
690 return off_the_record_profile_.get();
691}
692
[email protected]860f55492009-03-27 19:50:59693void ProfileImpl::DestroyOffTheRecordProfile() {
694 off_the_record_profile_.reset();
695}
696
initial.commit09911bf2008-07-26 23:55:29697Profile* ProfileImpl::GetOriginalProfile() {
698 return this;
699}
700
initial.commit09911bf2008-07-26 23:55:29701VisitedLinkMaster* ProfileImpl::GetVisitedLinkMaster() {
702 if (!visited_link_master_.get()) {
703 scoped_ptr<VisitedLinkMaster> visited_links(
704 new VisitedLinkMaster(g_browser_process->file_thread(),
[email protected]3e90d4a2009-07-03 17:38:39705 visited_link_event_listener_.get(), this));
initial.commit09911bf2008-07-26 23:55:29706 if (!visited_links->Init())
707 return NULL;
708 visited_link_master_.swap(visited_links);
709 }
710
711 return visited_link_master_.get();
712}
713
[email protected]6014d672008-12-05 00:38:25714ExtensionsService* ProfileImpl::GetExtensionsService() {
715 return extensions_service_.get();
716}
717
[email protected]0938d3c2009-01-09 20:37:35718UserScriptMaster* ProfileImpl::GetUserScriptMaster() {
[email protected]0938d3c2009-01-09 20:37:35719 return user_script_master_.get();
[email protected]04fba9a92008-10-28 17:25:25720}
721
[email protected]481e1a42009-05-06 20:56:05722ExtensionProcessManager* ProfileImpl::GetExtensionProcessManager() {
[email protected]382a0702009-06-26 17:12:27723 return extension_process_manager_.get();
[email protected]481e1a42009-05-06 20:56:05724}
725
[email protected]7120f132009-07-20 21:05:37726ExtensionMessageService* ProfileImpl::GetExtensionMessageService() {
727 return extension_message_service_.get();
728}
729
[email protected]34cc84f2009-02-13 10:04:35730SSLHostState* ProfileImpl::GetSSLHostState() {
731 if (!ssl_host_state_.get())
732 ssl_host_state_.reset(new SSLHostState());
733
734 DCHECK(ssl_host_state_->CalledOnValidThread());
735 return ssl_host_state_.get();
736}
737
[email protected]a9cea7542009-05-20 04:30:23738net::ForceTLSState* ProfileImpl::GetForceTLSState() {
739 if (!force_tls_state_.get())
740 force_tls_state_.reset(new net::ForceTLSState());
741
742 return force_tls_state_.get();
743}
744
initial.commit09911bf2008-07-26 23:55:29745PrefService* ProfileImpl::GetPrefs() {
746 if (!prefs_.get()) {
[email protected]6faa0e0d2009-04-28 06:50:36747 prefs_.reset(new PrefService(GetPrefFilePath(),
748 g_browser_process->file_thread()));
initial.commit09911bf2008-07-26 23:55:29749
750 // The Profile class and ProfileManager class may read some prefs so
751 // register known prefs as soon as possible.
752 Profile::RegisterUserPrefs(prefs_.get());
753 ProfileManager::RegisterUserPrefs(prefs_.get());
754
755 // The last session exited cleanly if there is no pref for
756 // kSessionExitedCleanly or the value for kSessionExitedCleanly is true.
757 last_session_exited_cleanly_ =
758 prefs_->GetBoolean(prefs::kSessionExitedCleanly);
759 // Mark the session as open.
760 prefs_->SetBoolean(prefs::kSessionExitedCleanly, false);
761 // Make sure we save to disk that the session has opened.
[email protected]6faa0e0d2009-04-28 06:50:36762 prefs_->ScheduleSavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29763 }
764
765 return prefs_.get();
766}
767
[email protected]f7011fcb2009-01-28 21:54:32768FilePath ProfileImpl::GetPrefFilePath() {
769 FilePath pref_file_path = path_;
770 pref_file_path = pref_file_path.Append(chrome::kPreferencesFilename);
initial.commit09911bf2008-07-26 23:55:29771 return pref_file_path;
772}
773
774URLRequestContext* ProfileImpl::GetRequestContext() {
775 if (!request_context_) {
[email protected]f7011fcb2009-01-28 21:54:32776 FilePath cookie_path = GetPath();
777 cookie_path = cookie_path.Append(chrome::kCookieFilename);
778 FilePath cache_path = GetPath();
[email protected]e5642992009-06-26 23:06:31779 int max_size;
780 GetCacheParameters(kNormalContext, &cache_path, &max_size);
[email protected]2b2830a2009-02-07 01:58:42781
[email protected]f7011fcb2009-01-28 21:54:32782 cache_path = cache_path.Append(chrome::kCacheDirname);
[email protected]6ab9b202008-12-23 22:34:50783 request_context_ = ChromeURLRequestContext::CreateOriginal(
[email protected]e5642992009-06-26 23:06:31784 this, cookie_path, cache_path, max_size);
initial.commit09911bf2008-07-26 23:55:29785 request_context_->AddRef();
786
[email protected]6ab9b202008-12-23 22:34:50787 // The first request context is always a normal (non-OTR) request context.
788 // Even when Chromium is started in OTR mode, a normal profile is always
789 // created first.
790 if (!default_request_context_) {
791 default_request_context_ = request_context_;
792 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56793 NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE,
[email protected]6ab9b202008-12-23 22:34:50794 NotificationService::AllSources(), NotificationService::NoDetails());
795 }
796
initial.commit09911bf2008-07-26 23:55:29797 DCHECK(request_context_->cookie_store());
798 }
799
800 return request_context_;
801}
802
[email protected]e7f29642009-03-02 22:53:18803URLRequestContext* ProfileImpl::GetRequestContextForMedia() {
804 if (!media_request_context_) {
805 FilePath cache_path = GetPath();
[email protected]e5642992009-06-26 23:06:31806 int max_size;
807 GetCacheParameters(kMediaContext, &cache_path, &max_size);
[email protected]e3edeba2009-03-23 18:57:14808
[email protected]4e7b5dbb2009-03-25 00:09:23809 cache_path = cache_path.Append(chrome::kMediaCacheDirname);
[email protected]e7f29642009-03-02 22:53:18810 media_request_context_ = ChromeURLRequestContext::CreateOriginalForMedia(
[email protected]e5642992009-06-26 23:06:31811 this, cache_path, max_size);
[email protected]e7f29642009-03-02 22:53:18812 media_request_context_->AddRef();
813
814 DCHECK(media_request_context_->cookie_store());
815 }
816
817 return media_request_context_;
818}
819
[email protected]47accfd62009-05-14 18:46:21820URLRequestContext* ProfileImpl::GetRequestContextForExtensions() {
821 if (!extensions_request_context_) {
822 FilePath cookie_path = GetPath();
823 cookie_path = cookie_path.Append(chrome::kExtensionsCookieFilename);
824
825 extensions_request_context_ =
826 ChromeURLRequestContext::CreateOriginalForExtensions(this, cookie_path);
827 extensions_request_context_->AddRef();
828
829 DCHECK(extensions_request_context_->cookie_store());
830 }
831
832 return extensions_request_context_;
833}
834
[email protected]eaadd9052009-06-23 18:02:23835Blacklist* ProfileImpl::GetBlacklist() {
836 return blacklist_;
837}
838
initial.commit09911bf2008-07-26 23:55:29839HistoryService* ProfileImpl::GetHistoryService(ServiceAccessType sat) {
840 if (!history_service_created_) {
[email protected]90ef13132008-08-27 03:27:46841 history_service_created_ = true;
initial.commit09911bf2008-07-26 23:55:29842 scoped_refptr<HistoryService> history(new HistoryService(this));
[email protected]d8e41ed2008-09-11 15:22:32843 if (!history->Init(GetPath(), GetBookmarkModel()))
initial.commit09911bf2008-07-26 23:55:29844 return NULL;
845 history_service_.swap(history);
initial.commit09911bf2008-07-26 23:55:29846
847 // Send out the notification that the history service was created.
848 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56849 Notify(NotificationType::HISTORY_CREATED, Source<Profile>(this),
initial.commit09911bf2008-07-26 23:55:29850 Details<HistoryService>(history_service_.get()));
851 }
852 return history_service_.get();
853}
854
initial.commit09911bf2008-07-26 23:55:29855TemplateURLModel* ProfileImpl::GetTemplateURLModel() {
856 if (!template_url_model_.get())
857 template_url_model_.reset(new TemplateURLModel(this));
858 return template_url_model_.get();
859}
860
861TemplateURLFetcher* ProfileImpl::GetTemplateURLFetcher() {
862 if (!template_url_fetcher_.get())
863 template_url_fetcher_.reset(new TemplateURLFetcher(this));
864 return template_url_fetcher_.get();
865}
866
867WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) {
868 if (!created_web_data_service_)
869 CreateWebDataService();
870 return web_data_service_.get();
871}
872
873void ProfileImpl::CreateWebDataService() {
874 DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL);
875 created_web_data_service_ = true;
876 scoped_refptr<WebDataService> wds(new WebDataService());
877 if (!wds->Init(GetPath()))
878 return;
879 web_data_service_.swap(wds);
880}
881
[email protected]e69d33952009-06-03 22:00:41882PasswordStore* ProfileImpl::GetPasswordStore(ServiceAccessType sat) {
883 if (!created_password_store_)
884 CreatePasswordStore();
885 return password_store_.get();
886}
887
888void ProfileImpl::CreatePasswordStore() {
889 DCHECK(!created_password_store_ && password_store_.get() == NULL);
890 created_password_store_ = true;
891 scoped_refptr<PasswordStore> ps;
892#if defined(OS_LINUX)
893// Temporarily disabled while we figure some stuff out.
894// http://code.google.com/p/chromium/issues/detail?id=12351
895// if (getenv("KDE_FULL_SESSION")) {
896// ps = new PasswordStoreKWallet();
897// } else {
898// ps = new PasswordStoreGnome();
899// }
900 NOTIMPLEMENTED();
901#elif defined(OS_WIN)
902 ps = new PasswordStoreWin(GetWebDataService(Profile::IMPLICIT_ACCESS));
[email protected]034eba52009-06-03 22:50:56903#elif defined(OS_MACOSX)
[email protected]e0411ae52009-06-30 23:59:17904 FilePath login_db_file_path = GetPath();
905 login_db_file_path = login_db_file_path.Append(chrome::kLoginDataFileName);
906 LoginDatabaseMac* login_db = new LoginDatabaseMac();
907 if (!login_db->Init(login_db_file_path)) {
908 LOG(ERROR) << "Could not initialize login database.";
909 delete login_db;
910 return;
911 }
912 ps = new PasswordStoreMac(new MacKeychain(), login_db);
[email protected]e69d33952009-06-03 22:00:41913#else
914 NOTIMPLEMENTED();
915#endif
916 if (!ps || !ps->Init()) {
917 // Try falling back to the default password manager
918 LOG(WARNING) << "Could not initialise native password manager - "
919 "falling back to default";
920 ps = new PasswordStoreDefault(GetWebDataService(Profile::IMPLICIT_ACCESS));
921 if (!ps->Init())
922 return;
923 }
924 password_store_.swap(ps);
925}
926
initial.commit09911bf2008-07-26 23:55:29927DownloadManager* ProfileImpl::GetDownloadManager() {
928 if (!created_download_manager_) {
929 scoped_refptr<DownloadManager> dlm(new DownloadManager);
930 dlm->Init(this);
931 created_download_manager_ = true;
932 download_manager_.swap(dlm);
933 }
934 return download_manager_.get();
935}
936
937bool ProfileImpl::HasCreatedDownloadManager() const {
938 return created_download_manager_;
939}
940
[email protected]4a190632009-05-09 01:07:42941void ProfileImpl::InitThemes() {
942 if (!created_theme_provider_) {
[email protected]0ef42ff92009-07-06 20:17:35943#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
[email protected]a5166af62009-07-03 00:42:29944 scoped_refptr<BrowserThemeProvider> themes(new GtkThemeProvider);
945#else
[email protected]4a190632009-05-09 01:07:42946 scoped_refptr<BrowserThemeProvider> themes(new BrowserThemeProvider);
[email protected]a5166af62009-07-03 00:42:29947#endif
[email protected]4a190632009-05-09 01:07:42948 themes->Init(this);
949 created_theme_provider_ = true;
950 theme_provider_.swap(themes);
951 }
952}
953
954void ProfileImpl::SetTheme(Extension* extension) {
955 InitThemes();
956 theme_provider_.get()->SetTheme(extension);
957}
958
[email protected]a5166af62009-07-03 00:42:29959void ProfileImpl::SetNativeTheme() {
960 InitThemes();
961 theme_provider_.get()->SetNativeTheme();
962}
963
[email protected]4a190632009-05-09 01:07:42964void ProfileImpl::ClearTheme() {
965 InitThemes();
966 theme_provider_.get()->UseDefaultTheme();
967}
968
969ThemeProvider* ProfileImpl::GetThemeProvider() {
970 InitThemes();
971 return theme_provider_.get();
972}
973
initial.commit09911bf2008-07-26 23:55:29974SessionService* ProfileImpl::GetSessionService() {
975 if (!session_service_.get() && !shutdown_session_service_) {
976 session_service_ = new SessionService(this);
977 session_service_->ResetFromCurrentBrowsers();
978 }
979 return session_service_.get();
980}
981
982void ProfileImpl::ShutdownSessionService() {
983 if (shutdown_session_service_)
984 return;
985
986 // We're about to exit, force creation of the session service if it hasn't
987 // been created yet. We do this to ensure session state matches the point in
988 // time the user exited.
989 GetSessionService();
990 shutdown_session_service_ = true;
991 session_service_ = NULL;
992}
993
994bool ProfileImpl::HasSessionService() const {
995 return (session_service_.get() != NULL);
996}
997
998std::wstring ProfileImpl::GetName() {
999 return GetPrefs()->GetString(prefs::kProfileName);
1000}
1001void ProfileImpl::SetName(const std::wstring& name) {
1002 GetPrefs()->SetString(prefs::kProfileName, name);
1003}
1004
1005std::wstring ProfileImpl::GetID() {
1006 return GetPrefs()->GetString(prefs::kProfileID);
1007}
1008void ProfileImpl::SetID(const std::wstring& id) {
1009 GetPrefs()->SetString(prefs::kProfileID, id);
1010}
1011
initial.commit09911bf2008-07-26 23:55:291012bool ProfileImpl::DidLastSessionExitCleanly() {
1013 // last_session_exited_cleanly_ is set when the preferences are loaded. Force
1014 // it to be set by asking for the prefs.
1015 GetPrefs();
1016 return last_session_exited_cleanly_;
1017}
1018
[email protected]d8e41ed2008-09-11 15:22:321019BookmarkModel* ProfileImpl::GetBookmarkModel() {
[email protected]90ef13132008-08-27 03:27:461020 if (!bookmark_bar_model_.get()) {
[email protected]d8e41ed2008-09-11 15:22:321021 bookmark_bar_model_.reset(new BookmarkModel(this));
[email protected]90ef13132008-08-27 03:27:461022 bookmark_bar_model_->Load();
1023 }
initial.commit09911bf2008-07-26 23:55:291024 return bookmark_bar_model_.get();
1025}
1026
1027bool ProfileImpl::IsSameProfile(Profile* profile) {
1028 if (profile == static_cast<Profile*>(this))
1029 return true;
1030 OffTheRecordProfileImpl* otr_profile = off_the_record_profile_.get();
1031 return otr_profile && profile == static_cast<Profile*>(otr_profile);
1032}
1033
1034Time ProfileImpl::GetStartTime() const {
1035 return start_time_;
1036}
1037
1038TabRestoreService* ProfileImpl::GetTabRestoreService() {
1039 if (!tab_restore_service_.get())
[email protected]169627b2008-12-06 19:30:191040 tab_restore_service_ = new TabRestoreService(this);
initial.commit09911bf2008-07-26 23:55:291041 return tab_restore_service_.get();
1042}
1043
[email protected]449478302009-06-09 20:04:281044ThumbnailStore* ProfileImpl::GetThumbnailStore() {
1045 if (!thumbnail_store_.get()) {
1046 thumbnail_store_ = new ThumbnailStore;
[email protected]72d0fc72009-07-09 17:30:491047 thumbnail_store_->Init(GetPath().AppendASCII("Top Thumbnails"), this);
[email protected]449478302009-06-09 20:04:281048 }
1049 return thumbnail_store_.get();
1050}
1051
initial.commit09911bf2008-07-26 23:55:291052void ProfileImpl::ResetTabRestoreService() {
[email protected]169627b2008-12-06 19:30:191053 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:291054}
1055
[email protected]a9afddb2009-02-12 17:49:421056// To be run in the IO thread to notify all resource message filters that the
[email protected]20930852008-10-15 19:30:411057// spellchecker has changed.
1058class NotifySpellcheckerChangeTask : public Task {
1059 public:
1060 NotifySpellcheckerChangeTask(
[email protected]e7244d82008-10-29 18:13:261061 Profile* profile,
1062 const SpellcheckerReinitializedDetails& spellchecker)
[email protected]20930852008-10-15 19:30:411063 : profile_(profile),
1064 spellchecker_(spellchecker) {
1065 }
1066
1067 private:
1068 void Run(void) {
1069 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:561070 NotificationType::SPELLCHECKER_REINITIALIZED,
[email protected]20930852008-10-15 19:30:411071 Source<Profile>(profile_),
1072 Details<SpellcheckerReinitializedDetails>(&spellchecker_));
1073 }
1074
1075 Profile* profile_;
1076 SpellcheckerReinitializedDetails spellchecker_;
1077};
1078
[email protected]e7244d82008-10-29 18:13:261079void ProfileImpl::InitializeSpellChecker(bool need_to_broadcast) {
[email protected]20930852008-10-15 19:30:411080 // The I/O thread may be NULL during testing.
1081 base::Thread* io_thread = g_browser_process->io_thread();
1082 if (spellchecker_) {
1083 // The spellchecker must be deleted on the I/O thread.
1084 // A dummy variable to aid in logical clarity.
1085 SpellChecker* last_spellchecker = spellchecker_;
1086
1087 if (io_thread)
1088 io_thread->message_loop()->ReleaseSoon(FROM_HERE, last_spellchecker);
1089 else // during testing, we don't have an I/O thread
1090 last_spellchecker->Release();
[email protected]20930852008-10-15 19:30:411091 }
1092
[email protected]20930852008-10-15 19:30:411093 // Retrieve the (perhaps updated recently) dictionary name from preferences.
1094 PrefService* prefs = GetPrefs();
[email protected]e7244d82008-10-29 18:13:261095 bool enable_spellcheck = prefs->GetBoolean(prefs::kEnableSpellCheck);
[email protected]20930852008-10-15 19:30:411096
[email protected]e7244d82008-10-29 18:13:261097 if (enable_spellcheck) {
[email protected]bd17b702009-02-25 20:44:081098 FilePath dict_dir;
[email protected]e7244d82008-10-29 18:13:261099 PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
[email protected]a9afddb2009-02-12 17:49:421100 // Note that, as the object pointed to by previously by spellchecker_
[email protected]e7244d82008-10-29 18:13:261101 // is being deleted in the io thread, the spellchecker_ can be made to point
1102 // to a new object (RE-initialized) in parallel in this UI thread.
[email protected]4b4d1adc2008-12-10 22:28:581103 spellchecker_ = new SpellChecker(dict_dir,
[email protected]74c8b422009-03-11 00:34:121104 WideToASCII(prefs->GetString(prefs::kSpellCheckDictionary)),
1105 GetRequestContext(),
[email protected]bd17b702009-02-25 20:44:081106 FilePath());
[email protected]e7244d82008-10-29 18:13:261107 spellchecker_->AddRef(); // Manual refcounting.
1108 } else {
1109 spellchecker_ = NULL;
1110 }
[email protected]20930852008-10-15 19:30:411111
[email protected]154a4332009-06-03 20:20:581112 // Set auto spell correct status for spellchecker.
1113 if (spellchecker_) {
1114 spellchecker_->EnableAutoSpellCorrect(
1115 prefs->GetBoolean(prefs::kEnableAutoSpellCorrect));
1116 }
1117
[email protected]20930852008-10-15 19:30:411118 if (need_to_broadcast && io_thread) { // Notify resource message filters.
1119 SpellcheckerReinitializedDetails scoped_spellchecker;
1120 scoped_spellchecker.spellchecker = spellchecker_;
[email protected]e7244d82008-10-29 18:13:261121 if (io_thread) {
1122 io_thread->message_loop()->PostTask(
[email protected]a9afddb2009-02-12 17:49:421123 FROM_HERE,
[email protected]e7244d82008-10-29 18:13:261124 new NotifySpellcheckerChangeTask(this, scoped_spellchecker));
1125 }
[email protected]20930852008-10-15 19:30:411126 }
1127}
1128
[email protected]e7244d82008-10-29 18:13:261129void ProfileImpl::ReinitializeSpellChecker() {
1130 InitializeSpellChecker(true);
1131}
1132
initial.commit09911bf2008-07-26 23:55:291133SpellChecker* ProfileImpl::GetSpellChecker() {
1134 if (!spellchecker_) {
[email protected]20930852008-10-15 19:30:411135 // This is where spellchecker gets initialized. Note that this is being
1136 // initialized in the ui_thread. However, this is not a problem as long as
1137 // it is *used* in the io thread.
[email protected]34cc84f2009-02-13 10:04:351138 // TODO(sidchat): One day, change everything so that spellchecker gets
[email protected]20930852008-10-15 19:30:411139 // initialized in the IO thread itself.
[email protected]e7244d82008-10-29 18:13:261140 InitializeSpellChecker(false);
initial.commit09911bf2008-07-26 23:55:291141 }
[email protected]20930852008-10-15 19:30:411142
initial.commit09911bf2008-07-26 23:55:291143 return spellchecker_;
1144}
1145
[email protected]3bf335a2009-06-26 20:46:061146WebKitContext* ProfileImpl::GetWebKitContext() {
1147 if (!webkit_context_.get())
1148 webkit_context_ = new WebKitContext(path_, false);
1149 DCHECK(webkit_context_.get());
1150 return webkit_context_.get();
1151}
1152
initial.commit09911bf2008-07-26 23:55:291153void ProfileImpl::MarkAsCleanShutdown() {
1154 if (prefs_.get()) {
1155 // The session cleanly exited, set kSessionExitedCleanly appropriately.
1156 prefs_->SetBoolean(prefs::kSessionExitedCleanly, true);
1157
1158 // NOTE: If you change what thread this writes on, be sure and update
1159 // ChromeFrame::EndSession().
[email protected]6faa0e0d2009-04-28 06:50:361160 prefs_->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:291161 }
1162}
1163
[email protected]e7244d82008-10-29 18:13:261164void ProfileImpl::Observe(NotificationType type,
1165 const NotificationSource& source,
1166 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:561167 if (NotificationType::PREF_CHANGED == type) {
[email protected]e7244d82008-10-29 18:13:261168 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
1169 PrefService* prefs = Source<PrefService>(source).ptr();
1170 DCHECK(pref_name_in && prefs);
1171 if (*pref_name_in == prefs::kSpellCheckDictionary ||
[email protected]154a4332009-06-03 20:20:581172 *pref_name_in == prefs::kEnableSpellCheck ||
1173 *pref_name_in == prefs::kEnableAutoSpellCorrect) {
[email protected]e7244d82008-10-29 18:13:261174 InitializeSpellChecker(true);
1175 }
[email protected]4a190632009-05-09 01:07:421176 } else if (NotificationType::THEME_INSTALLED == type) {
1177 Extension* extension = Details<Extension>(details).ptr();
1178 SetTheme(extension);
[email protected]e7244d82008-10-29 18:13:261179 }
1180}
1181
initial.commit09911bf2008-07-26 23:55:291182void ProfileImpl::StopCreateSessionServiceTimer() {
[email protected]2d316662008-09-03 18:18:141183 create_session_service_timer_.Stop();
initial.commit09911bf2008-07-26 23:55:291184}
[email protected]3a453fa2008-08-15 18:46:341185
1186#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:071187ProfilePersonalization* ProfileImpl::GetProfilePersonalization() {
[email protected]57d3d0a2008-09-24 00:50:071188 return personalization_.get();
[email protected]3a453fa2008-08-15 18:46:341189}
license.botbf09a502008-08-24 00:55:551190#endif