blob: 6186394e4960b882128b13b5d384f8f97be79a6f [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/profile.h"
6
7#include "base/command_line.h"
[email protected]04fba9a92008-10-28 17:25:258#include "base/file_path.h"
initial.commit09911bf2008-07-26 23:55:299#include "base/path_service.h"
10#include "base/scoped_ptr.h"
11#include "base/string_util.h"
[email protected]a9afddb2009-02-12 17:49:4212#include "chrome/browser/bookmarks/bookmark_model.h"
initial.commit09911bf2008-07-26 23:55:2913#include "chrome/browser/browser_list.h"
14#include "chrome/browser/browser_process.h"
[email protected]67a46b7f2009-06-16 21:41:0215#include "chrome/browser/browser_theme_provider.h"
[email protected]b7f05882009-02-22 01:21:5616#include "chrome/browser/download/download_manager.h"
[email protected]7120f132009-07-20 21:05:3717#include "chrome/browser/extensions/extension_message_service.h"
[email protected]481e1a42009-05-06 20:56:0518#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]6014d672008-12-05 00:38:2519#include "chrome/browser/extensions/extensions_service.h"
[email protected]0938d3c2009-01-09 20:37:3520#include "chrome/browser/extensions/user_script_master.h"
[email protected]a9afddb2009-02-12 17:49:4221#include "chrome/browser/history/history.h"
[email protected]3bf335a2009-06-26 20:46:0622#include "chrome/browser/in_process_webkit/webkit_context.h"
[email protected]6ab9b202008-12-23 22:34:5023#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]e69d33952009-06-03 22:00:4124#include "chrome/browser/password_manager/password_store_default.h"
[email protected]eaadd9052009-06-23 18:02:2325#include "chrome/browser/privacy_blacklist/blacklist.h"
initial.commit09911bf2008-07-26 23:55:2926#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2627#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]1132436e2009-04-08 20:06:3328#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]f63ae312009-02-04 17:58:4629#include "chrome/browser/search_engines/template_url_model.h"
[email protected]85e921fb82009-02-11 23:19:4430#include "chrome/browser/sessions/session_service.h"
[email protected]bd580a252009-02-12 01:16:3031#include "chrome/browser/sessions/tab_restore_service.h"
[email protected]f0a644292009-02-25 23:32:4732#include "chrome/browser/spellchecker.h"
[email protected]1132436e2009-04-08 20:06:3333#include "chrome/browser/ssl/ssl_host_state.h"
[email protected]449478302009-06-09 20:04:2834#include "chrome/browser/thumbnail_store.h"
initial.commit09911bf2008-07-26 23:55:2935#include "chrome/browser/visitedlink_master.h"
[email protected]3e90d4a2009-07-03 17:38:3936#include "chrome/browser/visitedlink_event_listener.h"
initial.commit09911bf2008-07-26 23:55:2937#include "chrome/browser/webdata/web_data_service.h"
38#include "chrome/common/chrome_constants.h"
39#include "chrome/common/chrome_paths.h"
40#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5841#include "chrome/common/extensions/extension_error_reporter.h"
[email protected]47accfd62009-05-14 18:46:2142#include "chrome/common/net/cookie_monster_sqlite.h"
initial.commit09911bf2008-07-26 23:55:2943#include "chrome/common/notification_service.h"
44#include "chrome/common/pref_names.h"
[email protected]f7011fcb2009-01-28 21:54:3245#include "chrome/common/render_messages.h"
[email protected]f90f5c512009-02-18 19:10:5846#include "grit/locale_settings.h"
[email protected]a9cea7542009-05-20 04:30:2347#include "net/base/force_tls_state.h"
initial.commit09911bf2008-07-26 23:55:2948
[email protected]a5166af62009-07-03 00:42:2949#if defined(OS_LINUX)
[email protected]2a0c0a52009-07-31 07:51:3250#include "net/ocsp/nss_ocsp.h"
[email protected]a5166af62009-07-03 00:42:2951#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]51c490b2009-08-03 16:34:34128 prefs->RegisterStringPref(prefs::kCurrentThemeID,
129 UTF8ToWide(BrowserThemeProvider::kDefaultThemeID));
[email protected]4a190632009-05-09 01:07:42130 prefs->RegisterDictionaryPref(prefs::kCurrentThemeImages);
131 prefs->RegisterDictionaryPref(prefs::kCurrentThemeColors);
132 prefs->RegisterDictionaryPref(prefs::kCurrentThemeTints);
[email protected]7895ea22009-06-02 20:53:50133 prefs->RegisterDictionaryPref(prefs::kCurrentThemeDisplayProperties);
[email protected]f93fe782009-02-19 01:26:13134 prefs->RegisterBooleanPref(prefs::kEnableExtensions, false);
initial.commit09911bf2008-07-26 23:55:29135}
136
[email protected]34cc84f2009-02-13 10:04:35137// static
[email protected]f7011fcb2009-01-28 21:54:32138Profile* Profile::CreateProfile(const FilePath& path) {
initial.commit09911bf2008-07-26 23:55:29139 return new ProfileImpl(path);
140}
141
[email protected]34cc84f2009-02-13 10:04:35142// static
initial.commit09911bf2008-07-26 23:55:29143URLRequestContext* Profile::GetDefaultRequestContext() {
144 return default_request_context_;
145}
146
[email protected]e69d33952009-06-03 22:00:41147#if defined(OS_LINUX)
148// Temporarily disabled while we figure some stuff out.
149// http://code.google.com/p/chromium/issues/detail?id=12351
150// #include "chrome/browser/password_manager/password_store_gnome.h"
151// #include "chrome/browser/password_manager/password_store_kwallet.h"
152#elif defined(OS_WIN)
153#include "chrome/browser/password_manager/password_store_win.h"
[email protected]034eba52009-06-03 22:50:56154#elif defined(OS_MACOSX)
155#include "chrome/browser/keychain_mac.h"
[email protected]e0411ae52009-06-30 23:59:17156#include "chrome/browser/password_manager/login_database_mac.h"
[email protected]034eba52009-06-03 22:50:56157#include "chrome/browser/password_manager/password_store_mac.h"
[email protected]e69d33952009-06-03 22:00:41158#endif
initial.commit09911bf2008-07-26 23:55:29159
initial.commit09911bf2008-07-26 23:55:29160////////////////////////////////////////////////////////////////////////////////
161//
162// OffTheRecordProfileImpl is a profile subclass that wraps an existing profile
[email protected]4a190632009-05-09 01:07:42163// to make it suitable for the off the record mode.
initial.commit09911bf2008-07-26 23:55:29164//
165////////////////////////////////////////////////////////////////////////////////
166class OffTheRecordProfileImpl : public Profile,
167 public NotificationObserver {
168 public:
169 explicit OffTheRecordProfileImpl(Profile* real_profile)
170 : profile_(real_profile),
[email protected]47accfd62009-05-14 18:46:21171 extensions_request_context_(NULL),
initial.commit09911bf2008-07-26 23:55:29172 start_time_(Time::Now()) {
[email protected]6ab9b202008-12-23 22:34:50173 request_context_ = ChromeURLRequestContext::CreateOffTheRecord(this);
initial.commit09911bf2008-07-26 23:55:29174 request_context_->AddRef();
[email protected]481e1a42009-05-06 20:56:05175
initial.commit09911bf2008-07-26 23:55:29176 // Register for browser close notifications so we can detect when the last
177 // off-the-record window is closed, in which case we can clean our states
178 // (cookies, downloads...).
[email protected]88cf3292009-05-22 01:48:43179 registrar_.Add(this, NotificationType::BROWSER_CLOSED,
180 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29181 }
182
183 virtual ~OffTheRecordProfileImpl() {
[email protected]47accfd62009-05-14 18:46:21184 CleanupRequestContext(request_context_);
[email protected]47accfd62009-05-14 18:46:21185 CleanupRequestContext(extensions_request_context_);
initial.commit09911bf2008-07-26 23:55:29186 }
187
[email protected]f7011fcb2009-01-28 21:54:32188 virtual FilePath GetPath() { return profile_->GetPath(); }
initial.commit09911bf2008-07-26 23:55:29189
190 virtual bool IsOffTheRecord() {
191 return true;
192 }
193
194 virtual Profile* GetOffTheRecordProfile() {
195 return this;
196 }
197
[email protected]860f55492009-03-27 19:50:59198 virtual void DestroyOffTheRecordProfile() {
199 // Suicide is bad!
200 NOTREACHED();
201 }
202
initial.commit09911bf2008-07-26 23:55:29203 virtual Profile* GetOriginalProfile() {
204 return profile_;
205 }
206
207 virtual VisitedLinkMaster* GetVisitedLinkMaster() {
[email protected]7fb6c862009-03-13 02:51:49208 // We don't provide access to the VisitedLinkMaster when we're OffTheRecord
209 // because we don't want to leak the sites that the user has visited before.
210 return NULL;
initial.commit09911bf2008-07-26 23:55:29211 }
212
[email protected]6014d672008-12-05 00:38:25213 virtual ExtensionsService* GetExtensionsService() {
[email protected]1bd54132009-06-11 00:05:34214 return NULL;
[email protected]6014d672008-12-05 00:38:25215 }
216
[email protected]0938d3c2009-01-09 20:37:35217 virtual UserScriptMaster* GetUserScriptMaster() {
218 return profile_->GetUserScriptMaster();
[email protected]04fba9a92008-10-28 17:25:25219 }
220
[email protected]481e1a42009-05-06 20:56:05221 virtual ExtensionProcessManager* GetExtensionProcessManager() {
[email protected]1bd54132009-06-11 00:05:34222 return NULL;
[email protected]481e1a42009-05-06 20:56:05223 }
224
[email protected]7120f132009-07-20 21:05:37225 virtual ExtensionMessageService* GetExtensionMessageService() {
226 return NULL;
227 }
228
[email protected]34cc84f2009-02-13 10:04:35229 virtual SSLHostState* GetSSLHostState() {
230 if (!ssl_host_state_.get())
231 ssl_host_state_.reset(new SSLHostState());
232
233 DCHECK(ssl_host_state_->CalledOnValidThread());
234 return ssl_host_state_.get();
235 }
236
[email protected]a9cea7542009-05-20 04:30:23237 virtual net::ForceTLSState* GetForceTLSState() {
238 if (!force_tls_state_.get())
239 force_tls_state_.reset(new net::ForceTLSState());
240
241 return force_tls_state_.get();
242 }
243
initial.commit09911bf2008-07-26 23:55:29244 virtual HistoryService* GetHistoryService(ServiceAccessType sat) {
245 if (sat == EXPLICIT_ACCESS) {
246 return profile_->GetHistoryService(sat);
247 } else {
248 NOTREACHED() << "This profile is OffTheRecord";
249 return NULL;
250 }
251 }
252
initial.commit09911bf2008-07-26 23:55:29253 virtual WebDataService* GetWebDataService(ServiceAccessType sat) {
254 if (sat == EXPLICIT_ACCESS) {
255 return profile_->GetWebDataService(sat);
256 } else {
257 NOTREACHED() << "This profile is OffTheRecord";
258 return NULL;
259 }
260 }
261
[email protected]e69d33952009-06-03 22:00:41262 virtual PasswordStore* GetPasswordStore(ServiceAccessType sat) {
263 if (sat == EXPLICIT_ACCESS) {
264 return profile_->GetPasswordStore(sat);
265 } else {
266 NOTREACHED() << "This profile is OffTheRecord";
267 return NULL;
268 }
269 }
270
initial.commit09911bf2008-07-26 23:55:29271 virtual PrefService* GetPrefs() {
272 return profile_->GetPrefs();
273 }
274
275 virtual TemplateURLModel* GetTemplateURLModel() {
276 return profile_->GetTemplateURLModel();
277 }
278
279 virtual TemplateURLFetcher* GetTemplateURLFetcher() {
280 return profile_->GetTemplateURLFetcher();
281 }
282
283 virtual DownloadManager* GetDownloadManager() {
284 if (!download_manager_.get()) {
285 scoped_refptr<DownloadManager> dlm(new DownloadManager);
286 dlm->Init(this);
287 download_manager_.swap(dlm);
288 }
289 return download_manager_.get();
290 }
291
292 virtual bool HasCreatedDownloadManager() const {
293 return (download_manager_.get() != NULL);
294 }
295
[email protected]4a190632009-05-09 01:07:42296 virtual void InitThemes() {
297 GetOriginalProfile()->InitThemes();
298 }
299
300 virtual void SetTheme(Extension* extension) {
301 GetOriginalProfile()->SetTheme(extension);
302 }
303
[email protected]a5166af62009-07-03 00:42:29304 virtual void SetNativeTheme() {
305 GetOriginalProfile()->SetNativeTheme();
306 }
307
[email protected]4a190632009-05-09 01:07:42308 virtual void ClearTheme() {
309 GetOriginalProfile()->ClearTheme();
310 }
311
[email protected]51c490b2009-08-03 16:34:34312 virtual Extension* GetTheme() {
313 return GetOriginalProfile()->GetTheme();
314 }
315
[email protected]4a190632009-05-09 01:07:42316 virtual ThemeProvider* GetThemeProvider() {
317 return GetOriginalProfile()->GetThemeProvider();
318 }
319
initial.commit09911bf2008-07-26 23:55:29320 virtual URLRequestContext* GetRequestContext() {
321 return request_context_;
322 }
323
[email protected]e7f29642009-03-02 22:53:18324 virtual URLRequestContext* GetRequestContextForMedia() {
[email protected]d14c7ac2009-05-29 20:38:11325 // In OTR mode, media request context is the same as the original one.
326 return request_context_;
[email protected]e7f29642009-03-02 22:53:18327 }
328
[email protected]47accfd62009-05-14 18:46:21329 URLRequestContext* GetRequestContextForExtensions() {
330 if (!extensions_request_context_) {
331 extensions_request_context_ =
332 ChromeURLRequestContext::CreateOffTheRecordForExtensions(this);
333 extensions_request_context_->AddRef();
334
335 DCHECK(extensions_request_context_->cookie_store());
336 }
337
338 return extensions_request_context_;
339 }
340
[email protected]eaadd9052009-06-23 18:02:23341 virtual Blacklist* GetBlacklist() {
342 return GetOriginalProfile()->GetBlacklist();
343 }
344
initial.commit09911bf2008-07-26 23:55:29345 virtual SessionService* GetSessionService() {
346 // Don't save any sessions when off the record.
347 return NULL;
348 }
349
350 virtual void ShutdownSessionService() {
351 // We don't allow a session service, nothing to do.
352 }
353
354 virtual bool HasSessionService() const {
355 // We never have a session service.
356 return false;
357 }
358
359 virtual std::wstring GetName() {
360 return profile_->GetName();
361 }
362
363 virtual void SetName(const std::wstring& name) {
364 profile_->SetName(name);
365 }
366
367 virtual std::wstring GetID() {
368 return profile_->GetID();
369 }
370
371 virtual void SetID(const std::wstring& id) {
372 profile_->SetID(id);
373 }
374
initial.commit09911bf2008-07-26 23:55:29375 virtual bool DidLastSessionExitCleanly() {
376 return profile_->DidLastSessionExitCleanly();
377 }
378
[email protected]d8e41ed2008-09-11 15:22:32379 virtual BookmarkModel* GetBookmarkModel() {
380 return profile_->GetBookmarkModel();
initial.commit09911bf2008-07-26 23:55:29381 }
382
[email protected]3a453fa2008-08-15 18:46:34383#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07384 virtual ProfilePersonalization* GetProfilePersonalization() {
[email protected]3a453fa2008-08-15 18:46:34385 return profile_->GetProfilePersonalization();
386 }
387#endif
388
initial.commit09911bf2008-07-26 23:55:29389 virtual bool IsSameProfile(Profile* profile) {
390 if (profile == static_cast<Profile*>(this))
391 return true;
392 return profile == profile_;
393 }
394
395 virtual Time GetStartTime() const {
396 return start_time_;
397 }
398
399 virtual TabRestoreService* GetTabRestoreService() {
400 return NULL;
401 }
402
[email protected]e7244d82008-10-29 18:13:26403 virtual void ResetTabRestoreService() {
[email protected]20930852008-10-15 19:30:41404 }
405
[email protected]e7244d82008-10-29 18:13:26406 virtual void ReinitializeSpellChecker() {
407 profile_->ReinitializeSpellChecker();
initial.commit09911bf2008-07-26 23:55:29408 }
409
410 virtual SpellChecker* GetSpellChecker() {
411 return profile_->GetSpellChecker();
412 }
413
[email protected]3bf335a2009-06-26 20:46:06414 virtual WebKitContext* GetWebKitContext() {
415 if (!webkit_context_.get())
416 webkit_context_ = new WebKitContext(GetPath(), true);
417 DCHECK(webkit_context_.get());
418 return webkit_context_.get();
419}
420
[email protected]449478302009-06-09 20:04:28421 virtual ThumbnailStore* GetThumbnailStore() {
422 return NULL;
423 }
424
initial.commit09911bf2008-07-26 23:55:29425 virtual void MarkAsCleanShutdown() {
426 }
427
[email protected]bdbc87c2009-01-25 05:08:54428 virtual void InitExtensions() {
429 NOTREACHED();
430 }
431
[email protected]ea0c98cf2009-06-18 23:02:54432 virtual void InitWebResources() {
433 NOTREACHED();
434 }
435
initial.commit09911bf2008-07-26 23:55:29436 virtual void ExitedOffTheRecordMode() {
437 // Drop our download manager so we forget about all the downloads made
438 // in off-the-record mode.
439 download_manager_ = NULL;
440 }
441
442 virtual void Observe(NotificationType type,
443 const NotificationSource& source,
444 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56445 DCHECK_EQ(NotificationType::BROWSER_CLOSED, type.value);
initial.commit09911bf2008-07-26 23:55:29446 // We are only interested in OTR browser closing.
447 if (Source<Browser>(source)->profile() != this)
448 return;
449
450 // Let's check if we still have an Off The Record window opened.
451 // Note that we check against 1 as this notification is sent before the
452 // browser window is actually removed from the list.
453 if (BrowserList::GetBrowserCount(this) <= 1)
454 ExitedOffTheRecordMode();
455 }
456
457 private:
[email protected]88cf3292009-05-22 01:48:43458 NotificationRegistrar registrar_;
459
initial.commit09911bf2008-07-26 23:55:29460 // The real underlying profile.
461 Profile* profile_;
462
[email protected]6ab9b202008-12-23 22:34:50463 // The context to use for requests made from this OTR session.
464 ChromeURLRequestContext* request_context_;
initial.commit09911bf2008-07-26 23:55:29465
[email protected]47accfd62009-05-14 18:46:21466 ChromeURLRequestContext* extensions_request_context_;
467
initial.commit09911bf2008-07-26 23:55:29468 // The download manager that only stores downloaded items in memory.
469 scoped_refptr<DownloadManager> download_manager_;
470
[email protected]4a190632009-05-09 01:07:42471 scoped_refptr<BrowserThemeProvider> theme_provider_;
472
[email protected]3bf335a2009-06-26 20:46:06473 // Use a special WebKit context for OTR browsing.
474 scoped_refptr<WebKitContext> webkit_context_;
475
[email protected]34cc84f2009-02-13 10:04:35476 // We don't want SSLHostState from the OTR profile to leak back to the main
477 // profile because then the main profile would learn some of the host names
478 // the user visited while OTR.
479 scoped_ptr<SSLHostState> ssl_host_state_;
480
[email protected]a9cea7542009-05-20 04:30:23481 // The ForceTLSState that only stores enabled sites in memory.
482 scoped_ptr<net::ForceTLSState> force_tls_state_;
483
initial.commit09911bf2008-07-26 23:55:29484 // Time we were started.
485 Time start_time_;
486
487 DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl);
488};
489
[email protected]f7011fcb2009-01-28 21:54:32490ProfileImpl::ProfileImpl(const FilePath& path)
initial.commit09911bf2008-07-26 23:55:29491 : path_(path),
[email protected]3e90d4a2009-07-03 17:38:39492 visited_link_event_listener_(new VisitedLinkEventListener()),
[email protected]f7011fcb2009-01-28 21:54:32493 request_context_(NULL),
[email protected]363347b2009-03-13 20:06:57494 media_request_context_(NULL),
[email protected]47accfd62009-05-14 18:46:21495 extensions_request_context_(NULL),
[email protected]eaadd9052009-06-23 18:02:23496 blacklist_(NULL),
[email protected]f7011fcb2009-01-28 21:54:32497 history_service_created_(false),
498 created_web_data_service_(false),
[email protected]e69d33952009-06-03 22:00:41499 created_password_store_(false),
[email protected]f7011fcb2009-01-28 21:54:32500 created_download_manager_(false),
[email protected]4a190632009-05-09 01:07:42501 created_theme_provider_(false),
[email protected]f7011fcb2009-01-28 21:54:32502 start_time_(Time::Now()),
503 spellchecker_(NULL),
initial.commit09911bf2008-07-26 23:55:29504 shutdown_session_service_(false) {
505 DCHECK(!path.empty()) << "Using an empty path will attempt to write " <<
506 "profile files to the root directory!";
[email protected]2d316662008-09-03 18:18:14507 create_session_service_timer_.Start(
508 TimeDelta::FromMilliseconds(kCreateSessionServiceDelayMS), this,
509 &ProfileImpl::EnsureSessionServiceCreated);
[email protected]bdbc87c2009-01-25 05:08:54510
[email protected]382a0702009-06-26 17:12:27511 extension_process_manager_.reset(new ExtensionProcessManager(this));
[email protected]7120f132009-07-20 21:05:37512 extension_message_service_ = new ExtensionMessageService(this);
[email protected]382a0702009-06-26 17:12:27513
[email protected]e7244d82008-10-29 18:13:26514 PrefService* prefs = GetPrefs();
515 prefs->AddPrefObserver(prefs::kSpellCheckDictionary, this);
516 prefs->AddPrefObserver(prefs::kEnableSpellCheck, this);
[email protected]154a4332009-06-03 20:20:58517 prefs->AddPrefObserver(prefs::kEnableAutoSpellCorrect, this);
[email protected]380ab462009-04-24 01:23:58518
519#ifdef CHROME_PERSONALIZATION
[email protected]92ad2cd2009-08-11 02:36:34520 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableSync))
[email protected]380ab462009-04-24 01:23:58521 personalization_.reset(Personalization::CreateProfilePersonalization(this));
522#endif
523
[email protected]eaadd9052009-06-23 18:02:23524 if (CommandLine::ForCurrentProcess()->
525 HasSwitch(switches::kPrivacyBlacklist)) {
526 std::wstring option = CommandLine::ForCurrentProcess()->GetSwitchValue(
527 switches::kPrivacyBlacklist);
528#if defined(OS_POSIX)
529 FilePath path(WideToUTF8(option));
530#else
531 FilePath path(option);
532#endif
533 blacklist_ = new Blacklist(path);
534 }
535
[email protected]bc96d562009-08-11 18:45:13536 if (!PathService::IsOverridden(chrome::DIR_USER_DATA))
537 PathService::Get(chrome::DIR_USER_CACHE, &base_cache_path_);
538 if (base_cache_path_.empty())
539 base_cache_path_ = path_;
540
[email protected]4a190632009-05-09 01:07:42541 // Listen for theme installation.
[email protected]88cf3292009-05-22 01:48:43542 registrar_.Add(this, NotificationType::THEME_INSTALLED,
543 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29544}
545
[email protected]bdbc87c2009-01-25 05:08:54546void ProfileImpl::InitExtensions() {
[email protected]8c756ac2009-01-30 23:36:41547 if (user_script_master_ || extensions_service_)
548 return; // Already initialized.
549
[email protected]bdbc87c2009-01-25 05:08:54550 const CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]f93fe782009-02-19 01:26:13551 PrefService* prefs = GetPrefs();
[email protected]bdbc87c2009-01-25 05:08:54552 bool user_scripts_enabled =
[email protected]f0a51fb52009-03-05 12:46:38553 command_line->HasSwitch(switches::kEnableUserScripts) ||
[email protected]f93fe782009-02-19 01:26:13554 prefs->GetBoolean(prefs::kEnableUserScripts);
[email protected]bdbc87c2009-01-25 05:08:54555
[email protected]f7011fcb2009-01-28 21:54:32556 FilePath script_dir;
[email protected]bdbc87c2009-01-25 05:08:54557 if (user_scripts_enabled) {
[email protected]0cd957b2009-03-06 20:13:23558 if (command_line->HasSwitch(switches::kUserScriptsDir)) {
559 std::wstring path_string =
560 command_line->GetSwitchValue(switches::kUserScriptsDir);
561 script_dir = FilePath::FromWStringHack(path_string);
562 } else {
563 script_dir = GetPath();
564 script_dir = script_dir.Append(chrome::kUserScriptsDirname);
565 }
[email protected]bdbc87c2009-01-25 05:08:54566 }
567
[email protected]bb28e062009-02-27 17:19:18568 ExtensionErrorReporter::Init(true); // allow noisy errors.
[email protected]bdbc87c2009-01-25 05:08:54569 user_script_master_ = new UserScriptMaster(
[email protected]9197f3b2009-06-02 00:49:27570 g_browser_process->file_thread()->message_loop(),
571 script_dir);
[email protected]894bb502009-05-21 22:39:57572 extensions_service_ = new ExtensionsService(
[email protected]a9b00ac2009-06-25 21:03:23573 this,
574 CommandLine::ForCurrentProcess(),
575 GetPrefs(),
576 GetPath().AppendASCII(ExtensionsService::kInstallDirectoryName),
577 MessageLoop::current(),
[email protected]93fd78f42009-07-10 16:43:17578 g_browser_process->file_thread()->message_loop(),
[email protected]c8b437d2009-07-20 21:26:36579 true);
[email protected]bdbc87c2009-01-25 05:08:54580
[email protected]9197f3b2009-06-02 00:49:27581 extensions_service_->Init();
[email protected]919ddc82009-07-15 04:30:12582
583 // Load any extensions specified with --load-extension.
584 if (command_line->HasSwitch(switches::kLoadExtension)) {
585 std::wstring path_string =
586 command_line->GetSwitchValue(switches::kLoadExtension);
587 FilePath path = FilePath::FromWStringHack(path_string);
588 extensions_service_->LoadExtension(path);
589
590 // Tell UserScriptMaser to watch this extension's directory for changes so
591 // you can live edit content scripts during development.
592 user_script_master_->AddWatchedPath(path);
593 }
[email protected]bdbc87c2009-01-25 05:08:54594}
595
[email protected]ea0c98cf2009-06-18 23:02:54596void ProfileImpl::InitWebResources() {
[email protected]0c274c202009-07-12 02:59:07597 if (web_resource_service_)
598 return; // Already initialized.
599
[email protected]ea0c98cf2009-06-18 23:02:54600 web_resource_service_ = new WebResourceService(
601 this,
602 g_browser_process->file_thread()->message_loop());
603
604 web_resource_service_->StartAfterDelay();
605}
606
initial.commit09911bf2008-07-26 23:55:29607ProfileImpl::~ProfileImpl() {
[email protected]169627b2008-12-06 19:30:19608 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29609
610 StopCreateSessionServiceTimer();
611 // TemplateURLModel schedules a task on the WebDataService from its
612 // destructor. Delete it first to ensure the task gets scheduled before we
613 // shut down the database.
614 template_url_model_.reset();
615
616 // The download manager queries the history system and should be deleted
617 // before the history is shutdown so it can properly cancel all requests.
618 download_manager_ = NULL;
619
[email protected]4a190632009-05-09 01:07:42620 // The theme provider provides bitmaps to whoever wants them.
621 theme_provider_ = NULL;
622
[email protected]44961292009-07-20 20:03:09623 // The ThumbnailStore saves thumbnails used by the NTP. Call Shutdown to
624 // save any new thumbnails to disk and release its reference to the
625 // HistoryService.
626 if (thumbnail_store_.get())
627 thumbnail_store_->Shutdown();
628
[email protected]e7244d82008-10-29 18:13:26629 // Remove pref observers.
630 PrefService* prefs = GetPrefs();
631 prefs->RemovePrefObserver(prefs::kSpellCheckDictionary, this);
632 prefs->RemovePrefObserver(prefs::kEnableSpellCheck, this);
[email protected]154a4332009-06-03 20:20:58633 prefs->RemovePrefObserver(prefs::kEnableAutoSpellCorrect, this);
[email protected]e7244d82008-10-29 18:13:26634
[email protected]3a453fa2008-08-15 18:46:34635#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07636 personalization_.reset();
[email protected]3a453fa2008-08-15 18:46:34637#endif
638
initial.commit09911bf2008-07-26 23:55:29639 // Both HistoryService and WebDataService maintain threads for background
640 // processing. Its possible each thread still has tasks on it that have
641 // increased the ref count of the service. In such a situation, when we
642 // decrement the refcount, it won't be 0, and the threads/databases aren't
643 // properly shut down. By explicitly calling Cleanup/Shutdown we ensure the
644 // databases are properly closed.
645 if (web_data_service_.get())
646 web_data_service_->Shutdown();
647
648 if (history_service_.get())
649 history_service_->Cleanup();
650
651 // The I/O thread may be NULL during testing.
[email protected]ab820df2008-08-26 05:55:10652 base::Thread* io_thread = g_browser_process->io_thread();
initial.commit09911bf2008-07-26 23:55:29653
654 if (spellchecker_) {
655 // The spellchecker must be deleted on the I/O thread. During testing, we
656 // don't have an I/O thread.
657 if (io_thread)
658 io_thread->message_loop()->ReleaseSoon(FROM_HERE, spellchecker_);
659 else
660 spellchecker_->Release();
661 }
662
[email protected]2a0c0a52009-07-31 07:51:32663 if (default_request_context_ == request_context_) {
664#if defined(OS_LINUX)
665 // We use default_request_context_ for OCSP.
666 // Release URLRequestContext used in OCSP handlers.
667 net::SetURLRequestContextForOCSP(NULL);
668#endif
[email protected]47accfd62009-05-14 18:46:21669 default_request_context_ = NULL;
[email protected]2a0c0a52009-07-31 07:51:32670 }
[email protected]6ab9b202008-12-23 22:34:50671
[email protected]47accfd62009-05-14 18:46:21672 CleanupRequestContext(request_context_);
673 CleanupRequestContext(media_request_context_);
674 CleanupRequestContext(extensions_request_context_);
[email protected]1e744f22009-04-08 01:00:17675
[email protected]eaadd9052009-06-23 18:02:23676 // When the request contexts are gone, the blacklist wont be needed anymore.
677 delete blacklist_;
678 blacklist_ = 0;
679
[email protected]d8e41ed2008-09-11 15:22:32680 // HistoryService may call into the BookmarkModel, as such we need to
681 // delete HistoryService before the BookmarkModel. The destructor for
[email protected]90ef13132008-08-27 03:27:46682 // HistoryService will join with HistoryService's backend thread so that
683 // by the time the destructor has finished we're sure it will no longer call
[email protected]d8e41ed2008-09-11 15:22:32684 // into the BookmarkModel.
[email protected]90ef13132008-08-27 03:27:46685 history_service_ = NULL;
686 bookmark_bar_model_.reset();
687
[email protected]7120f132009-07-20 21:05:37688 extension_message_service_->ProfileDestroyed();
[email protected]7120f132009-07-20 21:05:37689
[email protected]6ef635e42009-07-26 06:16:12690 if (extensions_service_)
691 extensions_service_->ProfileDestroyed();
692
initial.commit09911bf2008-07-26 23:55:29693 MarkAsCleanShutdown();
694}
695
[email protected]f7011fcb2009-01-28 21:54:32696FilePath ProfileImpl::GetPath() {
initial.commit09911bf2008-07-26 23:55:29697 return path_;
698}
699
700bool ProfileImpl::IsOffTheRecord() {
701 return false;
702}
703
704Profile* ProfileImpl::GetOffTheRecordProfile() {
705 if (!off_the_record_profile_.get()) {
706 scoped_ptr<OffTheRecordProfileImpl> p(new OffTheRecordProfileImpl(this));
707 off_the_record_profile_.swap(p);
708 }
709 return off_the_record_profile_.get();
710}
711
[email protected]860f55492009-03-27 19:50:59712void ProfileImpl::DestroyOffTheRecordProfile() {
713 off_the_record_profile_.reset();
714}
715
initial.commit09911bf2008-07-26 23:55:29716Profile* ProfileImpl::GetOriginalProfile() {
717 return this;
718}
719
initial.commit09911bf2008-07-26 23:55:29720VisitedLinkMaster* ProfileImpl::GetVisitedLinkMaster() {
721 if (!visited_link_master_.get()) {
722 scoped_ptr<VisitedLinkMaster> visited_links(
723 new VisitedLinkMaster(g_browser_process->file_thread(),
[email protected]3e90d4a2009-07-03 17:38:39724 visited_link_event_listener_.get(), this));
initial.commit09911bf2008-07-26 23:55:29725 if (!visited_links->Init())
726 return NULL;
727 visited_link_master_.swap(visited_links);
728 }
729
730 return visited_link_master_.get();
731}
732
[email protected]6014d672008-12-05 00:38:25733ExtensionsService* ProfileImpl::GetExtensionsService() {
734 return extensions_service_.get();
735}
736
[email protected]0938d3c2009-01-09 20:37:35737UserScriptMaster* ProfileImpl::GetUserScriptMaster() {
[email protected]0938d3c2009-01-09 20:37:35738 return user_script_master_.get();
[email protected]04fba9a92008-10-28 17:25:25739}
740
[email protected]481e1a42009-05-06 20:56:05741ExtensionProcessManager* ProfileImpl::GetExtensionProcessManager() {
[email protected]382a0702009-06-26 17:12:27742 return extension_process_manager_.get();
[email protected]481e1a42009-05-06 20:56:05743}
744
[email protected]7120f132009-07-20 21:05:37745ExtensionMessageService* ProfileImpl::GetExtensionMessageService() {
746 return extension_message_service_.get();
747}
748
[email protected]34cc84f2009-02-13 10:04:35749SSLHostState* ProfileImpl::GetSSLHostState() {
750 if (!ssl_host_state_.get())
751 ssl_host_state_.reset(new SSLHostState());
752
753 DCHECK(ssl_host_state_->CalledOnValidThread());
754 return ssl_host_state_.get();
755}
756
[email protected]a9cea7542009-05-20 04:30:23757net::ForceTLSState* ProfileImpl::GetForceTLSState() {
758 if (!force_tls_state_.get())
759 force_tls_state_.reset(new net::ForceTLSState());
760
761 return force_tls_state_.get();
762}
763
initial.commit09911bf2008-07-26 23:55:29764PrefService* ProfileImpl::GetPrefs() {
765 if (!prefs_.get()) {
[email protected]6faa0e0d2009-04-28 06:50:36766 prefs_.reset(new PrefService(GetPrefFilePath(),
767 g_browser_process->file_thread()));
initial.commit09911bf2008-07-26 23:55:29768
769 // The Profile class and ProfileManager class may read some prefs so
770 // register known prefs as soon as possible.
771 Profile::RegisterUserPrefs(prefs_.get());
772 ProfileManager::RegisterUserPrefs(prefs_.get());
773
774 // The last session exited cleanly if there is no pref for
775 // kSessionExitedCleanly or the value for kSessionExitedCleanly is true.
776 last_session_exited_cleanly_ =
777 prefs_->GetBoolean(prefs::kSessionExitedCleanly);
778 // Mark the session as open.
779 prefs_->SetBoolean(prefs::kSessionExitedCleanly, false);
780 // Make sure we save to disk that the session has opened.
[email protected]6faa0e0d2009-04-28 06:50:36781 prefs_->ScheduleSavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29782 }
783
784 return prefs_.get();
785}
786
[email protected]f7011fcb2009-01-28 21:54:32787FilePath ProfileImpl::GetPrefFilePath() {
788 FilePath pref_file_path = path_;
789 pref_file_path = pref_file_path.Append(chrome::kPreferencesFilename);
initial.commit09911bf2008-07-26 23:55:29790 return pref_file_path;
791}
792
793URLRequestContext* ProfileImpl::GetRequestContext() {
794 if (!request_context_) {
[email protected]f7011fcb2009-01-28 21:54:32795 FilePath cookie_path = GetPath();
796 cookie_path = cookie_path.Append(chrome::kCookieFilename);
[email protected]bc96d562009-08-11 18:45:13797 FilePath cache_path = base_cache_path_;
[email protected]e5642992009-06-26 23:06:31798 int max_size;
799 GetCacheParameters(kNormalContext, &cache_path, &max_size);
[email protected]2b2830a2009-02-07 01:58:42800
[email protected]f7011fcb2009-01-28 21:54:32801 cache_path = cache_path.Append(chrome::kCacheDirname);
[email protected]6ab9b202008-12-23 22:34:50802 request_context_ = ChromeURLRequestContext::CreateOriginal(
[email protected]e5642992009-06-26 23:06:31803 this, cookie_path, cache_path, max_size);
initial.commit09911bf2008-07-26 23:55:29804 request_context_->AddRef();
805
[email protected]6ab9b202008-12-23 22:34:50806 // The first request context is always a normal (non-OTR) request context.
807 // Even when Chromium is started in OTR mode, a normal profile is always
808 // created first.
809 if (!default_request_context_) {
810 default_request_context_ = request_context_;
811 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56812 NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE,
[email protected]6ab9b202008-12-23 22:34:50813 NotificationService::AllSources(), NotificationService::NoDetails());
[email protected]2a0c0a52009-07-31 07:51:32814#if defined(OS_LINUX)
815 // TODO(ukai): find a better way to set the URLRequestContext for OCSP.
816 net::SetURLRequestContextForOCSP(default_request_context_);
817#endif
[email protected]6ab9b202008-12-23 22:34:50818 }
819
initial.commit09911bf2008-07-26 23:55:29820 DCHECK(request_context_->cookie_store());
821 }
822
823 return request_context_;
824}
825
[email protected]e7f29642009-03-02 22:53:18826URLRequestContext* ProfileImpl::GetRequestContextForMedia() {
827 if (!media_request_context_) {
[email protected]bc96d562009-08-11 18:45:13828 FilePath cache_path = base_cache_path_;
[email protected]e5642992009-06-26 23:06:31829 int max_size;
830 GetCacheParameters(kMediaContext, &cache_path, &max_size);
[email protected]e3edeba2009-03-23 18:57:14831
[email protected]4e7b5dbb2009-03-25 00:09:23832 cache_path = cache_path.Append(chrome::kMediaCacheDirname);
[email protected]e7f29642009-03-02 22:53:18833 media_request_context_ = ChromeURLRequestContext::CreateOriginalForMedia(
[email protected]e5642992009-06-26 23:06:31834 this, cache_path, max_size);
[email protected]e7f29642009-03-02 22:53:18835 media_request_context_->AddRef();
836
837 DCHECK(media_request_context_->cookie_store());
838 }
839
840 return media_request_context_;
841}
842
[email protected]47accfd62009-05-14 18:46:21843URLRequestContext* ProfileImpl::GetRequestContextForExtensions() {
844 if (!extensions_request_context_) {
845 FilePath cookie_path = GetPath();
846 cookie_path = cookie_path.Append(chrome::kExtensionsCookieFilename);
847
848 extensions_request_context_ =
849 ChromeURLRequestContext::CreateOriginalForExtensions(this, cookie_path);
850 extensions_request_context_->AddRef();
851
852 DCHECK(extensions_request_context_->cookie_store());
853 }
854
855 return extensions_request_context_;
856}
857
[email protected]eaadd9052009-06-23 18:02:23858Blacklist* ProfileImpl::GetBlacklist() {
859 return blacklist_;
860}
861
initial.commit09911bf2008-07-26 23:55:29862HistoryService* ProfileImpl::GetHistoryService(ServiceAccessType sat) {
863 if (!history_service_created_) {
[email protected]90ef13132008-08-27 03:27:46864 history_service_created_ = true;
initial.commit09911bf2008-07-26 23:55:29865 scoped_refptr<HistoryService> history(new HistoryService(this));
[email protected]d8e41ed2008-09-11 15:22:32866 if (!history->Init(GetPath(), GetBookmarkModel()))
initial.commit09911bf2008-07-26 23:55:29867 return NULL;
868 history_service_.swap(history);
initial.commit09911bf2008-07-26 23:55:29869
870 // Send out the notification that the history service was created.
871 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56872 Notify(NotificationType::HISTORY_CREATED, Source<Profile>(this),
initial.commit09911bf2008-07-26 23:55:29873 Details<HistoryService>(history_service_.get()));
874 }
875 return history_service_.get();
876}
877
initial.commit09911bf2008-07-26 23:55:29878TemplateURLModel* ProfileImpl::GetTemplateURLModel() {
879 if (!template_url_model_.get())
880 template_url_model_.reset(new TemplateURLModel(this));
881 return template_url_model_.get();
882}
883
884TemplateURLFetcher* ProfileImpl::GetTemplateURLFetcher() {
885 if (!template_url_fetcher_.get())
886 template_url_fetcher_.reset(new TemplateURLFetcher(this));
887 return template_url_fetcher_.get();
888}
889
890WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) {
891 if (!created_web_data_service_)
892 CreateWebDataService();
893 return web_data_service_.get();
894}
895
896void ProfileImpl::CreateWebDataService() {
897 DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL);
898 created_web_data_service_ = true;
899 scoped_refptr<WebDataService> wds(new WebDataService());
900 if (!wds->Init(GetPath()))
901 return;
902 web_data_service_.swap(wds);
903}
904
[email protected]e69d33952009-06-03 22:00:41905PasswordStore* ProfileImpl::GetPasswordStore(ServiceAccessType sat) {
906 if (!created_password_store_)
907 CreatePasswordStore();
908 return password_store_.get();
909}
910
911void ProfileImpl::CreatePasswordStore() {
912 DCHECK(!created_password_store_ && password_store_.get() == NULL);
913 created_password_store_ = true;
914 scoped_refptr<PasswordStore> ps;
915#if defined(OS_LINUX)
916// Temporarily disabled while we figure some stuff out.
917// http://code.google.com/p/chromium/issues/detail?id=12351
918// if (getenv("KDE_FULL_SESSION")) {
919// ps = new PasswordStoreKWallet();
920// } else {
921// ps = new PasswordStoreGnome();
922// }
923 NOTIMPLEMENTED();
924#elif defined(OS_WIN)
925 ps = new PasswordStoreWin(GetWebDataService(Profile::IMPLICIT_ACCESS));
[email protected]034eba52009-06-03 22:50:56926#elif defined(OS_MACOSX)
[email protected]e0411ae52009-06-30 23:59:17927 FilePath login_db_file_path = GetPath();
928 login_db_file_path = login_db_file_path.Append(chrome::kLoginDataFileName);
929 LoginDatabaseMac* login_db = new LoginDatabaseMac();
930 if (!login_db->Init(login_db_file_path)) {
931 LOG(ERROR) << "Could not initialize login database.";
932 delete login_db;
933 return;
934 }
935 ps = new PasswordStoreMac(new MacKeychain(), login_db);
[email protected]e69d33952009-06-03 22:00:41936#else
937 NOTIMPLEMENTED();
938#endif
939 if (!ps || !ps->Init()) {
940 // Try falling back to the default password manager
941 LOG(WARNING) << "Could not initialise native password manager - "
942 "falling back to default";
943 ps = new PasswordStoreDefault(GetWebDataService(Profile::IMPLICIT_ACCESS));
944 if (!ps->Init())
945 return;
946 }
947 password_store_.swap(ps);
948}
949
initial.commit09911bf2008-07-26 23:55:29950DownloadManager* ProfileImpl::GetDownloadManager() {
951 if (!created_download_manager_) {
952 scoped_refptr<DownloadManager> dlm(new DownloadManager);
953 dlm->Init(this);
954 created_download_manager_ = true;
955 download_manager_.swap(dlm);
956 }
957 return download_manager_.get();
958}
959
960bool ProfileImpl::HasCreatedDownloadManager() const {
961 return created_download_manager_;
962}
963
[email protected]4a190632009-05-09 01:07:42964void ProfileImpl::InitThemes() {
965 if (!created_theme_provider_) {
[email protected]0ef42ff92009-07-06 20:17:35966#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
[email protected]a5166af62009-07-03 00:42:29967 scoped_refptr<BrowserThemeProvider> themes(new GtkThemeProvider);
968#else
[email protected]4a190632009-05-09 01:07:42969 scoped_refptr<BrowserThemeProvider> themes(new BrowserThemeProvider);
[email protected]a5166af62009-07-03 00:42:29970#endif
[email protected]4a190632009-05-09 01:07:42971 themes->Init(this);
972 created_theme_provider_ = true;
973 theme_provider_.swap(themes);
974 }
975}
976
977void ProfileImpl::SetTheme(Extension* extension) {
978 InitThemes();
979 theme_provider_.get()->SetTheme(extension);
980}
981
[email protected]a5166af62009-07-03 00:42:29982void ProfileImpl::SetNativeTheme() {
983 InitThemes();
984 theme_provider_.get()->SetNativeTheme();
985}
986
[email protected]4a190632009-05-09 01:07:42987void ProfileImpl::ClearTheme() {
988 InitThemes();
989 theme_provider_.get()->UseDefaultTheme();
990}
991
[email protected]51c490b2009-08-03 16:34:34992Extension* ProfileImpl::GetTheme() {
993 InitThemes();
994
995 std::string id = theme_provider_.get()->GetThemeID();
996 if (id == BrowserThemeProvider::kDefaultThemeID)
997 return NULL;
998
999 return extensions_service_->GetExtensionById(id);
1000}
1001
[email protected]4a190632009-05-09 01:07:421002ThemeProvider* ProfileImpl::GetThemeProvider() {
1003 InitThemes();
1004 return theme_provider_.get();
1005}
1006
initial.commit09911bf2008-07-26 23:55:291007SessionService* ProfileImpl::GetSessionService() {
1008 if (!session_service_.get() && !shutdown_session_service_) {
1009 session_service_ = new SessionService(this);
1010 session_service_->ResetFromCurrentBrowsers();
1011 }
1012 return session_service_.get();
1013}
1014
1015void ProfileImpl::ShutdownSessionService() {
1016 if (shutdown_session_service_)
1017 return;
1018
1019 // We're about to exit, force creation of the session service if it hasn't
1020 // been created yet. We do this to ensure session state matches the point in
1021 // time the user exited.
1022 GetSessionService();
1023 shutdown_session_service_ = true;
1024 session_service_ = NULL;
1025}
1026
1027bool ProfileImpl::HasSessionService() const {
1028 return (session_service_.get() != NULL);
1029}
1030
1031std::wstring ProfileImpl::GetName() {
1032 return GetPrefs()->GetString(prefs::kProfileName);
1033}
1034void ProfileImpl::SetName(const std::wstring& name) {
1035 GetPrefs()->SetString(prefs::kProfileName, name);
1036}
1037
1038std::wstring ProfileImpl::GetID() {
1039 return GetPrefs()->GetString(prefs::kProfileID);
1040}
1041void ProfileImpl::SetID(const std::wstring& id) {
1042 GetPrefs()->SetString(prefs::kProfileID, id);
1043}
1044
initial.commit09911bf2008-07-26 23:55:291045bool ProfileImpl::DidLastSessionExitCleanly() {
1046 // last_session_exited_cleanly_ is set when the preferences are loaded. Force
1047 // it to be set by asking for the prefs.
1048 GetPrefs();
1049 return last_session_exited_cleanly_;
1050}
1051
[email protected]d8e41ed2008-09-11 15:22:321052BookmarkModel* ProfileImpl::GetBookmarkModel() {
[email protected]90ef13132008-08-27 03:27:461053 if (!bookmark_bar_model_.get()) {
[email protected]d8e41ed2008-09-11 15:22:321054 bookmark_bar_model_.reset(new BookmarkModel(this));
[email protected]90ef13132008-08-27 03:27:461055 bookmark_bar_model_->Load();
1056 }
initial.commit09911bf2008-07-26 23:55:291057 return bookmark_bar_model_.get();
1058}
1059
1060bool ProfileImpl::IsSameProfile(Profile* profile) {
1061 if (profile == static_cast<Profile*>(this))
1062 return true;
1063 OffTheRecordProfileImpl* otr_profile = off_the_record_profile_.get();
1064 return otr_profile && profile == static_cast<Profile*>(otr_profile);
1065}
1066
1067Time ProfileImpl::GetStartTime() const {
1068 return start_time_;
1069}
1070
1071TabRestoreService* ProfileImpl::GetTabRestoreService() {
1072 if (!tab_restore_service_.get())
[email protected]169627b2008-12-06 19:30:191073 tab_restore_service_ = new TabRestoreService(this);
initial.commit09911bf2008-07-26 23:55:291074 return tab_restore_service_.get();
1075}
1076
[email protected]449478302009-06-09 20:04:281077ThumbnailStore* ProfileImpl::GetThumbnailStore() {
1078 if (!thumbnail_store_.get()) {
1079 thumbnail_store_ = new ThumbnailStore;
[email protected]daa82dc2009-07-28 01:22:251080 thumbnail_store_->Init(
1081 GetPath().Append(chrome::kNewTabThumbnailsFilename), this);
[email protected]449478302009-06-09 20:04:281082 }
1083 return thumbnail_store_.get();
1084}
1085
initial.commit09911bf2008-07-26 23:55:291086void ProfileImpl::ResetTabRestoreService() {
[email protected]169627b2008-12-06 19:30:191087 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:291088}
1089
[email protected]a9afddb2009-02-12 17:49:421090// To be run in the IO thread to notify all resource message filters that the
[email protected]20930852008-10-15 19:30:411091// spellchecker has changed.
1092class NotifySpellcheckerChangeTask : public Task {
1093 public:
1094 NotifySpellcheckerChangeTask(
[email protected]e7244d82008-10-29 18:13:261095 Profile* profile,
1096 const SpellcheckerReinitializedDetails& spellchecker)
[email protected]20930852008-10-15 19:30:411097 : profile_(profile),
1098 spellchecker_(spellchecker) {
1099 }
1100
1101 private:
1102 void Run(void) {
1103 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:561104 NotificationType::SPELLCHECKER_REINITIALIZED,
[email protected]20930852008-10-15 19:30:411105 Source<Profile>(profile_),
1106 Details<SpellcheckerReinitializedDetails>(&spellchecker_));
1107 }
1108
1109 Profile* profile_;
1110 SpellcheckerReinitializedDetails spellchecker_;
1111};
1112
[email protected]e7244d82008-10-29 18:13:261113void ProfileImpl::InitializeSpellChecker(bool need_to_broadcast) {
[email protected]20930852008-10-15 19:30:411114 // The I/O thread may be NULL during testing.
1115 base::Thread* io_thread = g_browser_process->io_thread();
1116 if (spellchecker_) {
1117 // The spellchecker must be deleted on the I/O thread.
1118 // A dummy variable to aid in logical clarity.
1119 SpellChecker* last_spellchecker = spellchecker_;
1120
1121 if (io_thread)
1122 io_thread->message_loop()->ReleaseSoon(FROM_HERE, last_spellchecker);
1123 else // during testing, we don't have an I/O thread
1124 last_spellchecker->Release();
[email protected]20930852008-10-15 19:30:411125 }
1126
[email protected]20930852008-10-15 19:30:411127 // Retrieve the (perhaps updated recently) dictionary name from preferences.
1128 PrefService* prefs = GetPrefs();
[email protected]e7244d82008-10-29 18:13:261129 bool enable_spellcheck = prefs->GetBoolean(prefs::kEnableSpellCheck);
[email protected]20930852008-10-15 19:30:411130
[email protected]e7244d82008-10-29 18:13:261131 if (enable_spellcheck) {
[email protected]bd17b702009-02-25 20:44:081132 FilePath dict_dir;
[email protected]e7244d82008-10-29 18:13:261133 PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
[email protected]a9afddb2009-02-12 17:49:421134 // Note that, as the object pointed to by previously by spellchecker_
[email protected]e7244d82008-10-29 18:13:261135 // is being deleted in the io thread, the spellchecker_ can be made to point
1136 // to a new object (RE-initialized) in parallel in this UI thread.
[email protected]4b4d1adc2008-12-10 22:28:581137 spellchecker_ = new SpellChecker(dict_dir,
[email protected]74c8b422009-03-11 00:34:121138 WideToASCII(prefs->GetString(prefs::kSpellCheckDictionary)),
1139 GetRequestContext(),
[email protected]bd17b702009-02-25 20:44:081140 FilePath());
[email protected]e7244d82008-10-29 18:13:261141 spellchecker_->AddRef(); // Manual refcounting.
1142 } else {
1143 spellchecker_ = NULL;
1144 }
[email protected]20930852008-10-15 19:30:411145
[email protected]154a4332009-06-03 20:20:581146 // Set auto spell correct status for spellchecker.
1147 if (spellchecker_) {
1148 spellchecker_->EnableAutoSpellCorrect(
1149 prefs->GetBoolean(prefs::kEnableAutoSpellCorrect));
1150 }
1151
[email protected]20930852008-10-15 19:30:411152 if (need_to_broadcast && io_thread) { // Notify resource message filters.
1153 SpellcheckerReinitializedDetails scoped_spellchecker;
1154 scoped_spellchecker.spellchecker = spellchecker_;
[email protected]e7244d82008-10-29 18:13:261155 if (io_thread) {
1156 io_thread->message_loop()->PostTask(
[email protected]a9afddb2009-02-12 17:49:421157 FROM_HERE,
[email protected]e7244d82008-10-29 18:13:261158 new NotifySpellcheckerChangeTask(this, scoped_spellchecker));
1159 }
[email protected]20930852008-10-15 19:30:411160 }
1161}
1162
[email protected]e7244d82008-10-29 18:13:261163void ProfileImpl::ReinitializeSpellChecker() {
1164 InitializeSpellChecker(true);
1165}
1166
initial.commit09911bf2008-07-26 23:55:291167SpellChecker* ProfileImpl::GetSpellChecker() {
1168 if (!spellchecker_) {
[email protected]20930852008-10-15 19:30:411169 // This is where spellchecker gets initialized. Note that this is being
1170 // initialized in the ui_thread. However, this is not a problem as long as
1171 // it is *used* in the io thread.
[email protected]34cc84f2009-02-13 10:04:351172 // TODO(sidchat): One day, change everything so that spellchecker gets
[email protected]20930852008-10-15 19:30:411173 // initialized in the IO thread itself.
[email protected]e7244d82008-10-29 18:13:261174 InitializeSpellChecker(false);
initial.commit09911bf2008-07-26 23:55:291175 }
[email protected]20930852008-10-15 19:30:411176
initial.commit09911bf2008-07-26 23:55:291177 return spellchecker_;
1178}
1179
[email protected]3bf335a2009-06-26 20:46:061180WebKitContext* ProfileImpl::GetWebKitContext() {
1181 if (!webkit_context_.get())
1182 webkit_context_ = new WebKitContext(path_, false);
1183 DCHECK(webkit_context_.get());
1184 return webkit_context_.get();
1185}
1186
initial.commit09911bf2008-07-26 23:55:291187void ProfileImpl::MarkAsCleanShutdown() {
1188 if (prefs_.get()) {
1189 // The session cleanly exited, set kSessionExitedCleanly appropriately.
1190 prefs_->SetBoolean(prefs::kSessionExitedCleanly, true);
1191
1192 // NOTE: If you change what thread this writes on, be sure and update
1193 // ChromeFrame::EndSession().
[email protected]6faa0e0d2009-04-28 06:50:361194 prefs_->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:291195 }
1196}
1197
[email protected]e7244d82008-10-29 18:13:261198void ProfileImpl::Observe(NotificationType type,
1199 const NotificationSource& source,
1200 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:561201 if (NotificationType::PREF_CHANGED == type) {
[email protected]e7244d82008-10-29 18:13:261202 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
1203 PrefService* prefs = Source<PrefService>(source).ptr();
1204 DCHECK(pref_name_in && prefs);
1205 if (*pref_name_in == prefs::kSpellCheckDictionary ||
[email protected]154a4332009-06-03 20:20:581206 *pref_name_in == prefs::kEnableSpellCheck ||
1207 *pref_name_in == prefs::kEnableAutoSpellCorrect) {
[email protected]e7244d82008-10-29 18:13:261208 InitializeSpellChecker(true);
1209 }
[email protected]4a190632009-05-09 01:07:421210 } else if (NotificationType::THEME_INSTALLED == type) {
1211 Extension* extension = Details<Extension>(details).ptr();
1212 SetTheme(extension);
[email protected]e7244d82008-10-29 18:13:261213 }
1214}
1215
initial.commit09911bf2008-07-26 23:55:291216void ProfileImpl::StopCreateSessionServiceTimer() {
[email protected]2d316662008-09-03 18:18:141217 create_session_service_timer_.Stop();
initial.commit09911bf2008-07-26 23:55:291218}
[email protected]3a453fa2008-08-15 18:46:341219
1220#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:071221ProfilePersonalization* ProfileImpl::GetProfilePersonalization() {
[email protected]57d3d0a2008-09-24 00:50:071222 return personalization_.get();
[email protected]3a453fa2008-08-15 18:46:341223}
license.botbf09a502008-08-24 00:55:551224#endif