blob: 18cd5effbb82da5382302b2c1ebb058c0954991e [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]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]6ab9b202008-12-23 22:34:5022#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]e69d33952009-06-03 22:00:4123#include "chrome/browser/password_manager/password_store_default.h"
[email protected]eaadd9052009-06-23 18:02:2324#include "chrome/browser/privacy_blacklist/blacklist.h"
initial.commit09911bf2008-07-26 23:55:2925#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2626#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]1132436e2009-04-08 20:06:3327#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]f63ae312009-02-04 17:58:4628#include "chrome/browser/search_engines/template_url_model.h"
[email protected]85e921fb82009-02-11 23:19:4429#include "chrome/browser/sessions/session_service.h"
[email protected]bd580a252009-02-12 01:16:3030#include "chrome/browser/sessions/tab_restore_service.h"
[email protected]f0a644292009-02-25 23:32:4731#include "chrome/browser/spellchecker.h"
[email protected]1132436e2009-04-08 20:06:3332#include "chrome/browser/ssl/ssl_host_state.h"
[email protected]449478302009-06-09 20:04:2833#include "chrome/browser/thumbnail_store.h"
initial.commit09911bf2008-07-26 23:55:2934#include "chrome/browser/visitedlink_master.h"
35#include "chrome/browser/webdata/web_data_service.h"
36#include "chrome/common/chrome_constants.h"
37#include "chrome/common/chrome_paths.h"
38#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5839#include "chrome/common/extensions/extension_error_reporter.h"
[email protected]47accfd62009-05-14 18:46:2140#include "chrome/common/net/cookie_monster_sqlite.h"
initial.commit09911bf2008-07-26 23:55:2941#include "chrome/common/notification_service.h"
42#include "chrome/common/pref_names.h"
[email protected]f7011fcb2009-01-28 21:54:3243#include "chrome/common/render_messages.h"
[email protected]f90f5c512009-02-18 19:10:5844#include "grit/locale_settings.h"
[email protected]a9cea7542009-05-20 04:30:2345#include "net/base/force_tls_state.h"
initial.commit09911bf2008-07-26 23:55:2946
[email protected]e1acf6f2008-10-27 20:43:3347using base::Time;
48using base::TimeDelta;
49
initial.commit09911bf2008-07-26 23:55:2950// Delay, in milliseconds, before we explicitly create the SessionService.
51static const int kCreateSessionServiceDelayMS = 500;
52
53// A pointer to the request context for the default profile. See comments on
54// Profile::GetDefaultRequestContext.
55URLRequestContext* Profile::default_request_context_;
56
[email protected]47accfd62009-05-14 18:46:2157static void CleanupRequestContext(ChromeURLRequestContext* context) {
58 if (context) {
59 context->CleanupOnUIThread();
60
61 // Clean up request context on IO thread.
62 g_browser_process->io_thread()->message_loop()->ReleaseSoon(FROM_HERE,
63 context);
64 }
65}
66
[email protected]34cc84f2009-02-13 10:04:3567// static
initial.commit09911bf2008-07-26 23:55:2968void Profile::RegisterUserPrefs(PrefService* prefs) {
[email protected]430d3f72008-10-27 17:56:5569 prefs->RegisterBooleanPref(prefs::kSearchSuggestEnabled, true);
initial.commit09911bf2008-07-26 23:55:2970 prefs->RegisterBooleanPref(prefs::kSessionExitedCleanly, true);
71 prefs->RegisterBooleanPref(prefs::kSafeBrowsingEnabled, true);
[email protected]74c8b422009-03-11 00:34:1272 // TODO(estade): IDS_SPELLCHECK_DICTIONARY should be an ASCII string.
[email protected]e7244d82008-10-29 18:13:2673 prefs->RegisterLocalizedStringPref(prefs::kSpellCheckDictionary,
74 IDS_SPELLCHECK_DICTIONARY);
75 prefs->RegisterBooleanPref(prefs::kEnableSpellCheck, true);
[email protected]154a4332009-06-03 20:20:5876 prefs->RegisterBooleanPref(prefs::kEnableAutoSpellCorrect, true);
[email protected]f93fe782009-02-19 01:26:1377 prefs->RegisterBooleanPref(prefs::kEnableUserScripts, false);
[email protected]4a190632009-05-09 01:07:4278 prefs->RegisterStringPref(prefs::kCurrentThemeID, L"");
79 prefs->RegisterDictionaryPref(prefs::kCurrentThemeImages);
80 prefs->RegisterDictionaryPref(prefs::kCurrentThemeColors);
81 prefs->RegisterDictionaryPref(prefs::kCurrentThemeTints);
[email protected]7895ea22009-06-02 20:53:5082 prefs->RegisterDictionaryPref(prefs::kCurrentThemeDisplayProperties);
[email protected]f93fe782009-02-19 01:26:1383 prefs->RegisterBooleanPref(prefs::kEnableExtensions, false);
initial.commit09911bf2008-07-26 23:55:2984}
85
[email protected]34cc84f2009-02-13 10:04:3586// static
[email protected]f7011fcb2009-01-28 21:54:3287Profile* Profile::CreateProfile(const FilePath& path) {
initial.commit09911bf2008-07-26 23:55:2988 return new ProfileImpl(path);
89}
90
[email protected]34cc84f2009-02-13 10:04:3591// static
initial.commit09911bf2008-07-26 23:55:2992URLRequestContext* Profile::GetDefaultRequestContext() {
93 return default_request_context_;
94}
95
[email protected]e69d33952009-06-03 22:00:4196#if defined(OS_LINUX)
97// Temporarily disabled while we figure some stuff out.
98// http://code.google.com/p/chromium/issues/detail?id=12351
99// #include "chrome/browser/password_manager/password_store_gnome.h"
100// #include "chrome/browser/password_manager/password_store_kwallet.h"
101#elif defined(OS_WIN)
102#include "chrome/browser/password_manager/password_store_win.h"
[email protected]034eba52009-06-03 22:50:56103#elif defined(OS_MACOSX)
104#include "chrome/browser/keychain_mac.h"
105#include "chrome/browser/password_manager/password_store_mac.h"
[email protected]e69d33952009-06-03 22:00:41106#endif
initial.commit09911bf2008-07-26 23:55:29107
initial.commit09911bf2008-07-26 23:55:29108////////////////////////////////////////////////////////////////////////////////
109//
110// OffTheRecordProfileImpl is a profile subclass that wraps an existing profile
[email protected]4a190632009-05-09 01:07:42111// to make it suitable for the off the record mode.
initial.commit09911bf2008-07-26 23:55:29112//
113////////////////////////////////////////////////////////////////////////////////
114class OffTheRecordProfileImpl : public Profile,
115 public NotificationObserver {
116 public:
117 explicit OffTheRecordProfileImpl(Profile* real_profile)
118 : profile_(real_profile),
[email protected]47accfd62009-05-14 18:46:21119 extensions_request_context_(NULL),
initial.commit09911bf2008-07-26 23:55:29120 start_time_(Time::Now()) {
[email protected]6ab9b202008-12-23 22:34:50121 request_context_ = ChromeURLRequestContext::CreateOffTheRecord(this);
initial.commit09911bf2008-07-26 23:55:29122 request_context_->AddRef();
[email protected]481e1a42009-05-06 20:56:05123
initial.commit09911bf2008-07-26 23:55:29124 // Register for browser close notifications so we can detect when the last
125 // off-the-record window is closed, in which case we can clean our states
126 // (cookies, downloads...).
[email protected]88cf3292009-05-22 01:48:43127 registrar_.Add(this, NotificationType::BROWSER_CLOSED,
128 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29129 }
130
131 virtual ~OffTheRecordProfileImpl() {
[email protected]47accfd62009-05-14 18:46:21132 CleanupRequestContext(request_context_);
[email protected]47accfd62009-05-14 18:46:21133 CleanupRequestContext(extensions_request_context_);
initial.commit09911bf2008-07-26 23:55:29134 }
135
[email protected]f7011fcb2009-01-28 21:54:32136 virtual FilePath GetPath() { return profile_->GetPath(); }
initial.commit09911bf2008-07-26 23:55:29137
138 virtual bool IsOffTheRecord() {
139 return true;
140 }
141
142 virtual Profile* GetOffTheRecordProfile() {
143 return this;
144 }
145
[email protected]860f55492009-03-27 19:50:59146 virtual void DestroyOffTheRecordProfile() {
147 // Suicide is bad!
148 NOTREACHED();
149 }
150
initial.commit09911bf2008-07-26 23:55:29151 virtual Profile* GetOriginalProfile() {
152 return profile_;
153 }
154
155 virtual VisitedLinkMaster* GetVisitedLinkMaster() {
[email protected]7fb6c862009-03-13 02:51:49156 // We don't provide access to the VisitedLinkMaster when we're OffTheRecord
157 // because we don't want to leak the sites that the user has visited before.
158 return NULL;
initial.commit09911bf2008-07-26 23:55:29159 }
160
[email protected]6014d672008-12-05 00:38:25161 virtual ExtensionsService* GetExtensionsService() {
[email protected]1bd54132009-06-11 00:05:34162 return NULL;
[email protected]6014d672008-12-05 00:38:25163 }
164
[email protected]0938d3c2009-01-09 20:37:35165 virtual UserScriptMaster* GetUserScriptMaster() {
166 return profile_->GetUserScriptMaster();
[email protected]04fba9a92008-10-28 17:25:25167 }
168
[email protected]481e1a42009-05-06 20:56:05169 virtual ExtensionProcessManager* GetExtensionProcessManager() {
[email protected]1bd54132009-06-11 00:05:34170 return NULL;
[email protected]481e1a42009-05-06 20:56:05171 }
172
[email protected]34cc84f2009-02-13 10:04:35173 virtual SSLHostState* GetSSLHostState() {
174 if (!ssl_host_state_.get())
175 ssl_host_state_.reset(new SSLHostState());
176
177 DCHECK(ssl_host_state_->CalledOnValidThread());
178 return ssl_host_state_.get();
179 }
180
[email protected]a9cea7542009-05-20 04:30:23181 virtual net::ForceTLSState* GetForceTLSState() {
182 if (!force_tls_state_.get())
183 force_tls_state_.reset(new net::ForceTLSState());
184
185 return force_tls_state_.get();
186 }
187
initial.commit09911bf2008-07-26 23:55:29188 virtual HistoryService* GetHistoryService(ServiceAccessType sat) {
189 if (sat == EXPLICIT_ACCESS) {
190 return profile_->GetHistoryService(sat);
191 } else {
192 NOTREACHED() << "This profile is OffTheRecord";
193 return NULL;
194 }
195 }
196
initial.commit09911bf2008-07-26 23:55:29197 virtual WebDataService* GetWebDataService(ServiceAccessType sat) {
198 if (sat == EXPLICIT_ACCESS) {
199 return profile_->GetWebDataService(sat);
200 } else {
201 NOTREACHED() << "This profile is OffTheRecord";
202 return NULL;
203 }
204 }
205
[email protected]e69d33952009-06-03 22:00:41206 virtual PasswordStore* GetPasswordStore(ServiceAccessType sat) {
207 if (sat == EXPLICIT_ACCESS) {
208 return profile_->GetPasswordStore(sat);
209 } else {
210 NOTREACHED() << "This profile is OffTheRecord";
211 return NULL;
212 }
213 }
214
initial.commit09911bf2008-07-26 23:55:29215 virtual PrefService* GetPrefs() {
216 return profile_->GetPrefs();
217 }
218
219 virtual TemplateURLModel* GetTemplateURLModel() {
220 return profile_->GetTemplateURLModel();
221 }
222
223 virtual TemplateURLFetcher* GetTemplateURLFetcher() {
224 return profile_->GetTemplateURLFetcher();
225 }
226
227 virtual DownloadManager* GetDownloadManager() {
228 if (!download_manager_.get()) {
229 scoped_refptr<DownloadManager> dlm(new DownloadManager);
230 dlm->Init(this);
231 download_manager_.swap(dlm);
232 }
233 return download_manager_.get();
234 }
235
236 virtual bool HasCreatedDownloadManager() const {
237 return (download_manager_.get() != NULL);
238 }
239
[email protected]4a190632009-05-09 01:07:42240 virtual void InitThemes() {
241 GetOriginalProfile()->InitThemes();
242 }
243
244 virtual void SetTheme(Extension* extension) {
245 GetOriginalProfile()->SetTheme(extension);
246 }
247
248 virtual void ClearTheme() {
249 GetOriginalProfile()->ClearTheme();
250 }
251
252 virtual ThemeProvider* GetThemeProvider() {
253 return GetOriginalProfile()->GetThemeProvider();
254 }
255
initial.commit09911bf2008-07-26 23:55:29256 virtual URLRequestContext* GetRequestContext() {
257 return request_context_;
258 }
259
[email protected]e7f29642009-03-02 22:53:18260 virtual URLRequestContext* GetRequestContextForMedia() {
[email protected]d14c7ac2009-05-29 20:38:11261 // In OTR mode, media request context is the same as the original one.
262 return request_context_;
[email protected]e7f29642009-03-02 22:53:18263 }
264
[email protected]47accfd62009-05-14 18:46:21265 URLRequestContext* GetRequestContextForExtensions() {
266 if (!extensions_request_context_) {
267 extensions_request_context_ =
268 ChromeURLRequestContext::CreateOffTheRecordForExtensions(this);
269 extensions_request_context_->AddRef();
270
271 DCHECK(extensions_request_context_->cookie_store());
272 }
273
274 return extensions_request_context_;
275 }
276
[email protected]eaadd9052009-06-23 18:02:23277 virtual Blacklist* GetBlacklist() {
278 return GetOriginalProfile()->GetBlacklist();
279 }
280
initial.commit09911bf2008-07-26 23:55:29281 virtual SessionService* GetSessionService() {
282 // Don't save any sessions when off the record.
283 return NULL;
284 }
285
286 virtual void ShutdownSessionService() {
287 // We don't allow a session service, nothing to do.
288 }
289
290 virtual bool HasSessionService() const {
291 // We never have a session service.
292 return false;
293 }
294
295 virtual std::wstring GetName() {
296 return profile_->GetName();
297 }
298
299 virtual void SetName(const std::wstring& name) {
300 profile_->SetName(name);
301 }
302
303 virtual std::wstring GetID() {
304 return profile_->GetID();
305 }
306
307 virtual void SetID(const std::wstring& id) {
308 profile_->SetID(id);
309 }
310
initial.commit09911bf2008-07-26 23:55:29311 virtual bool DidLastSessionExitCleanly() {
312 return profile_->DidLastSessionExitCleanly();
313 }
314
[email protected]d8e41ed2008-09-11 15:22:32315 virtual BookmarkModel* GetBookmarkModel() {
316 return profile_->GetBookmarkModel();
initial.commit09911bf2008-07-26 23:55:29317 }
318
[email protected]3a453fa2008-08-15 18:46:34319#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07320 virtual ProfilePersonalization* GetProfilePersonalization() {
[email protected]3a453fa2008-08-15 18:46:34321 return profile_->GetProfilePersonalization();
322 }
323#endif
324
initial.commit09911bf2008-07-26 23:55:29325 virtual bool IsSameProfile(Profile* profile) {
326 if (profile == static_cast<Profile*>(this))
327 return true;
328 return profile == profile_;
329 }
330
331 virtual Time GetStartTime() const {
332 return start_time_;
333 }
334
335 virtual TabRestoreService* GetTabRestoreService() {
336 return NULL;
337 }
338
[email protected]e7244d82008-10-29 18:13:26339 virtual void ResetTabRestoreService() {
[email protected]20930852008-10-15 19:30:41340 }
341
[email protected]e7244d82008-10-29 18:13:26342 virtual void ReinitializeSpellChecker() {
343 profile_->ReinitializeSpellChecker();
initial.commit09911bf2008-07-26 23:55:29344 }
345
346 virtual SpellChecker* GetSpellChecker() {
347 return profile_->GetSpellChecker();
348 }
349
[email protected]449478302009-06-09 20:04:28350 virtual ThumbnailStore* GetThumbnailStore() {
351 return NULL;
352 }
353
initial.commit09911bf2008-07-26 23:55:29354 virtual void MarkAsCleanShutdown() {
355 }
356
[email protected]bdbc87c2009-01-25 05:08:54357 virtual void InitExtensions() {
358 NOTREACHED();
359 }
360
[email protected]ea0c98cf2009-06-18 23:02:54361 virtual void InitWebResources() {
362 NOTREACHED();
363 }
364
initial.commit09911bf2008-07-26 23:55:29365 virtual void ExitedOffTheRecordMode() {
366 // Drop our download manager so we forget about all the downloads made
367 // in off-the-record mode.
368 download_manager_ = NULL;
369 }
370
371 virtual void Observe(NotificationType type,
372 const NotificationSource& source,
373 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56374 DCHECK_EQ(NotificationType::BROWSER_CLOSED, type.value);
initial.commit09911bf2008-07-26 23:55:29375 // We are only interested in OTR browser closing.
376 if (Source<Browser>(source)->profile() != this)
377 return;
378
379 // Let's check if we still have an Off The Record window opened.
380 // Note that we check against 1 as this notification is sent before the
381 // browser window is actually removed from the list.
382 if (BrowserList::GetBrowserCount(this) <= 1)
383 ExitedOffTheRecordMode();
384 }
385
386 private:
[email protected]88cf3292009-05-22 01:48:43387 NotificationRegistrar registrar_;
388
initial.commit09911bf2008-07-26 23:55:29389 // The real underlying profile.
390 Profile* profile_;
391
[email protected]6ab9b202008-12-23 22:34:50392 // The context to use for requests made from this OTR session.
393 ChromeURLRequestContext* request_context_;
initial.commit09911bf2008-07-26 23:55:29394
[email protected]47accfd62009-05-14 18:46:21395 ChromeURLRequestContext* extensions_request_context_;
396
initial.commit09911bf2008-07-26 23:55:29397 // The download manager that only stores downloaded items in memory.
398 scoped_refptr<DownloadManager> download_manager_;
399
[email protected]4a190632009-05-09 01:07:42400 // The download manager that only stores downloaded items in memory.
401 scoped_refptr<BrowserThemeProvider> theme_provider_;
402
[email protected]34cc84f2009-02-13 10:04:35403 // We don't want SSLHostState from the OTR profile to leak back to the main
404 // profile because then the main profile would learn some of the host names
405 // the user visited while OTR.
406 scoped_ptr<SSLHostState> ssl_host_state_;
407
[email protected]a9cea7542009-05-20 04:30:23408 // The ForceTLSState that only stores enabled sites in memory.
409 scoped_ptr<net::ForceTLSState> force_tls_state_;
410
initial.commit09911bf2008-07-26 23:55:29411 // Time we were started.
412 Time start_time_;
413
414 DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl);
415};
416
[email protected]f7011fcb2009-01-28 21:54:32417ProfileImpl::ProfileImpl(const FilePath& path)
initial.commit09911bf2008-07-26 23:55:29418 : path_(path),
[email protected]f7011fcb2009-01-28 21:54:32419 request_context_(NULL),
[email protected]363347b2009-03-13 20:06:57420 media_request_context_(NULL),
[email protected]47accfd62009-05-14 18:46:21421 extensions_request_context_(NULL),
[email protected]eaadd9052009-06-23 18:02:23422 blacklist_(NULL),
[email protected]f7011fcb2009-01-28 21:54:32423 history_service_created_(false),
424 created_web_data_service_(false),
[email protected]e69d33952009-06-03 22:00:41425 created_password_store_(false),
[email protected]f7011fcb2009-01-28 21:54:32426 created_download_manager_(false),
[email protected]4a190632009-05-09 01:07:42427 created_theme_provider_(false),
[email protected]f7011fcb2009-01-28 21:54:32428 start_time_(Time::Now()),
429 spellchecker_(NULL),
initial.commit09911bf2008-07-26 23:55:29430 shutdown_session_service_(false) {
431 DCHECK(!path.empty()) << "Using an empty path will attempt to write " <<
432 "profile files to the root directory!";
[email protected]2d316662008-09-03 18:18:14433 create_session_service_timer_.Start(
434 TimeDelta::FromMilliseconds(kCreateSessionServiceDelayMS), this,
435 &ProfileImpl::EnsureSessionServiceCreated);
[email protected]bdbc87c2009-01-25 05:08:54436
[email protected]e7244d82008-10-29 18:13:26437 PrefService* prefs = GetPrefs();
438 prefs->AddPrefObserver(prefs::kSpellCheckDictionary, this);
439 prefs->AddPrefObserver(prefs::kEnableSpellCheck, this);
[email protected]154a4332009-06-03 20:20:58440 prefs->AddPrefObserver(prefs::kEnableAutoSpellCorrect, this);
[email protected]380ab462009-04-24 01:23:58441
442#ifdef CHROME_PERSONALIZATION
443 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableP13n))
444 personalization_.reset(Personalization::CreateProfilePersonalization(this));
445#endif
446
[email protected]eaadd9052009-06-23 18:02:23447 if (CommandLine::ForCurrentProcess()->
448 HasSwitch(switches::kPrivacyBlacklist)) {
449 std::wstring option = CommandLine::ForCurrentProcess()->GetSwitchValue(
450 switches::kPrivacyBlacklist);
451#if defined(OS_POSIX)
452 FilePath path(WideToUTF8(option));
453#else
454 FilePath path(option);
455#endif
456 blacklist_ = new Blacklist(path);
457 }
458
[email protected]1351b6a2009-04-09 20:13:22459#if defined(OS_LINUX)
460 // TODO(port): Remove ifdef when the Linux splash page is not needed.
461 prefs->transient()->SetString(prefs::kHomePage, "about:linux-splash");
462 prefs->transient()->SetBoolean(prefs::kHomePageIsNewTabPage, false);
463#endif
[email protected]4a190632009-05-09 01:07:42464
465 // Listen for theme installation.
[email protected]88cf3292009-05-22 01:48:43466 registrar_.Add(this, NotificationType::THEME_INSTALLED,
467 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29468}
469
[email protected]bdbc87c2009-01-25 05:08:54470void ProfileImpl::InitExtensions() {
[email protected]8c756ac2009-01-30 23:36:41471 if (user_script_master_ || extensions_service_)
472 return; // Already initialized.
473
[email protected]bdbc87c2009-01-25 05:08:54474 const CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]f93fe782009-02-19 01:26:13475 PrefService* prefs = GetPrefs();
[email protected]bdbc87c2009-01-25 05:08:54476 bool user_scripts_enabled =
[email protected]f0a51fb52009-03-05 12:46:38477 command_line->HasSwitch(switches::kEnableUserScripts) ||
[email protected]f93fe782009-02-19 01:26:13478 prefs->GetBoolean(prefs::kEnableUserScripts);
[email protected]bdbc87c2009-01-25 05:08:54479
[email protected]f7011fcb2009-01-28 21:54:32480 FilePath script_dir;
[email protected]bdbc87c2009-01-25 05:08:54481 if (user_scripts_enabled) {
[email protected]0cd957b2009-03-06 20:13:23482 if (command_line->HasSwitch(switches::kUserScriptsDir)) {
483 std::wstring path_string =
484 command_line->GetSwitchValue(switches::kUserScriptsDir);
485 script_dir = FilePath::FromWStringHack(path_string);
486 } else {
487 script_dir = GetPath();
488 script_dir = script_dir.Append(chrome::kUserScriptsDirname);
489 }
[email protected]bdbc87c2009-01-25 05:08:54490 }
491
[email protected]bb28e062009-02-27 17:19:18492 ExtensionErrorReporter::Init(true); // allow noisy errors.
[email protected]bdbc87c2009-01-25 05:08:54493 user_script_master_ = new UserScriptMaster(
[email protected]9197f3b2009-06-02 00:49:27494 g_browser_process->file_thread()->message_loop(),
495 script_dir);
[email protected]894bb502009-05-21 22:39:57496 extensions_service_ = new ExtensionsService(
[email protected]a9b00ac2009-06-25 21:03:23497 this,
498 CommandLine::ForCurrentProcess(),
499 GetPrefs(),
500 GetPath().AppendASCII(ExtensionsService::kInstallDirectoryName),
501 MessageLoop::current(),
[email protected]a1257b12009-06-12 02:51:34502 g_browser_process->file_thread()->message_loop());
[email protected]bdbc87c2009-01-25 05:08:54503
[email protected]9197f3b2009-06-02 00:49:27504 extensions_service_->Init();
[email protected]bdbc87c2009-01-25 05:08:54505}
506
[email protected]ea0c98cf2009-06-18 23:02:54507void ProfileImpl::InitWebResources() {
508 web_resource_service_ = new WebResourceService(
509 this,
510 g_browser_process->file_thread()->message_loop());
511
512 web_resource_service_->StartAfterDelay();
513}
514
initial.commit09911bf2008-07-26 23:55:29515ProfileImpl::~ProfileImpl() {
[email protected]169627b2008-12-06 19:30:19516 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29517
518 StopCreateSessionServiceTimer();
519 // TemplateURLModel schedules a task on the WebDataService from its
520 // destructor. Delete it first to ensure the task gets scheduled before we
521 // shut down the database.
522 template_url_model_.reset();
523
524 // The download manager queries the history system and should be deleted
525 // before the history is shutdown so it can properly cancel all requests.
526 download_manager_ = NULL;
527
[email protected]4a190632009-05-09 01:07:42528 // The theme provider provides bitmaps to whoever wants them.
529 theme_provider_ = NULL;
530
[email protected]e7244d82008-10-29 18:13:26531 // Remove pref observers.
532 PrefService* prefs = GetPrefs();
533 prefs->RemovePrefObserver(prefs::kSpellCheckDictionary, this);
534 prefs->RemovePrefObserver(prefs::kEnableSpellCheck, this);
[email protected]154a4332009-06-03 20:20:58535 prefs->RemovePrefObserver(prefs::kEnableAutoSpellCorrect, this);
[email protected]e7244d82008-10-29 18:13:26536
[email protected]3a453fa2008-08-15 18:46:34537#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07538 personalization_.reset();
[email protected]3a453fa2008-08-15 18:46:34539#endif
540
initial.commit09911bf2008-07-26 23:55:29541 // Both HistoryService and WebDataService maintain threads for background
542 // processing. Its possible each thread still has tasks on it that have
543 // increased the ref count of the service. In such a situation, when we
544 // decrement the refcount, it won't be 0, and the threads/databases aren't
545 // properly shut down. By explicitly calling Cleanup/Shutdown we ensure the
546 // databases are properly closed.
547 if (web_data_service_.get())
548 web_data_service_->Shutdown();
549
550 if (history_service_.get())
551 history_service_->Cleanup();
552
553 // The I/O thread may be NULL during testing.
[email protected]ab820df2008-08-26 05:55:10554 base::Thread* io_thread = g_browser_process->io_thread();
initial.commit09911bf2008-07-26 23:55:29555
556 if (spellchecker_) {
557 // The spellchecker must be deleted on the I/O thread. During testing, we
558 // don't have an I/O thread.
559 if (io_thread)
560 io_thread->message_loop()->ReleaseSoon(FROM_HERE, spellchecker_);
561 else
562 spellchecker_->Release();
563 }
564
[email protected]47accfd62009-05-14 18:46:21565 if (default_request_context_ == request_context_)
566 default_request_context_ = NULL;
[email protected]6ab9b202008-12-23 22:34:50567
[email protected]47accfd62009-05-14 18:46:21568 CleanupRequestContext(request_context_);
569 CleanupRequestContext(media_request_context_);
570 CleanupRequestContext(extensions_request_context_);
[email protected]1e744f22009-04-08 01:00:17571
[email protected]eaadd9052009-06-23 18:02:23572 // When the request contexts are gone, the blacklist wont be needed anymore.
573 delete blacklist_;
574 blacklist_ = 0;
575
[email protected]d8e41ed2008-09-11 15:22:32576 // HistoryService may call into the BookmarkModel, as such we need to
577 // delete HistoryService before the BookmarkModel. The destructor for
[email protected]90ef13132008-08-27 03:27:46578 // HistoryService will join with HistoryService's backend thread so that
579 // by the time the destructor has finished we're sure it will no longer call
[email protected]d8e41ed2008-09-11 15:22:32580 // into the BookmarkModel.
[email protected]90ef13132008-08-27 03:27:46581 history_service_ = NULL;
582 bookmark_bar_model_.reset();
583
initial.commit09911bf2008-07-26 23:55:29584 MarkAsCleanShutdown();
585}
586
[email protected]f7011fcb2009-01-28 21:54:32587FilePath ProfileImpl::GetPath() {
initial.commit09911bf2008-07-26 23:55:29588 return path_;
589}
590
591bool ProfileImpl::IsOffTheRecord() {
592 return false;
593}
594
595Profile* ProfileImpl::GetOffTheRecordProfile() {
596 if (!off_the_record_profile_.get()) {
597 scoped_ptr<OffTheRecordProfileImpl> p(new OffTheRecordProfileImpl(this));
598 off_the_record_profile_.swap(p);
599 }
600 return off_the_record_profile_.get();
601}
602
[email protected]860f55492009-03-27 19:50:59603void ProfileImpl::DestroyOffTheRecordProfile() {
604 off_the_record_profile_.reset();
605}
606
initial.commit09911bf2008-07-26 23:55:29607Profile* ProfileImpl::GetOriginalProfile() {
608 return this;
609}
610
[email protected]176aa482008-11-14 03:25:15611static void BroadcastNewHistoryTable(base::SharedMemory* table_memory) {
initial.commit09911bf2008-07-26 23:55:29612 if (!table_memory)
613 return;
614
615 // send to all RenderProcessHosts
616 for (RenderProcessHost::iterator i = RenderProcessHost::begin();
617 i != RenderProcessHost::end(); i++) {
618 if (!i->second->channel())
619 continue;
620
[email protected]176aa482008-11-14 03:25:15621 base::SharedMemoryHandle new_table;
[email protected]f7011fcb2009-01-28 21:54:32622 base::ProcessHandle process = i->second->process().handle();
initial.commit09911bf2008-07-26 23:55:29623 if (!process) {
624 // process can be null if it's started with the --single-process flag.
[email protected]f7011fcb2009-01-28 21:54:32625 process = base::Process::Current().handle();
initial.commit09911bf2008-07-26 23:55:29626 }
627
628 table_memory->ShareToProcess(process, &new_table);
629 IPC::Message* msg = new ViewMsg_VisitedLink_NewTable(new_table);
630 i->second->channel()->Send(msg);
631 }
632}
633
634VisitedLinkMaster* ProfileImpl::GetVisitedLinkMaster() {
635 if (!visited_link_master_.get()) {
636 scoped_ptr<VisitedLinkMaster> visited_links(
637 new VisitedLinkMaster(g_browser_process->file_thread(),
638 BroadcastNewHistoryTable, this));
639 if (!visited_links->Init())
640 return NULL;
641 visited_link_master_.swap(visited_links);
642 }
643
644 return visited_link_master_.get();
645}
646
[email protected]6014d672008-12-05 00:38:25647ExtensionsService* ProfileImpl::GetExtensionsService() {
648 return extensions_service_.get();
649}
650
[email protected]0938d3c2009-01-09 20:37:35651UserScriptMaster* ProfileImpl::GetUserScriptMaster() {
[email protected]0938d3c2009-01-09 20:37:35652 return user_script_master_.get();
[email protected]04fba9a92008-10-28 17:25:25653}
654
[email protected]481e1a42009-05-06 20:56:05655ExtensionProcessManager* ProfileImpl::GetExtensionProcessManager() {
[email protected]c6e4a3412009-06-24 15:45:29656 return extensions_service_->extension_process_manager();
[email protected]481e1a42009-05-06 20:56:05657}
658
[email protected]34cc84f2009-02-13 10:04:35659SSLHostState* ProfileImpl::GetSSLHostState() {
660 if (!ssl_host_state_.get())
661 ssl_host_state_.reset(new SSLHostState());
662
663 DCHECK(ssl_host_state_->CalledOnValidThread());
664 return ssl_host_state_.get();
665}
666
[email protected]a9cea7542009-05-20 04:30:23667net::ForceTLSState* ProfileImpl::GetForceTLSState() {
668 if (!force_tls_state_.get())
669 force_tls_state_.reset(new net::ForceTLSState());
670
671 return force_tls_state_.get();
672}
673
initial.commit09911bf2008-07-26 23:55:29674PrefService* ProfileImpl::GetPrefs() {
675 if (!prefs_.get()) {
[email protected]6faa0e0d2009-04-28 06:50:36676 prefs_.reset(new PrefService(GetPrefFilePath(),
677 g_browser_process->file_thread()));
initial.commit09911bf2008-07-26 23:55:29678
679 // The Profile class and ProfileManager class may read some prefs so
680 // register known prefs as soon as possible.
681 Profile::RegisterUserPrefs(prefs_.get());
682 ProfileManager::RegisterUserPrefs(prefs_.get());
683
684 // The last session exited cleanly if there is no pref for
685 // kSessionExitedCleanly or the value for kSessionExitedCleanly is true.
686 last_session_exited_cleanly_ =
687 prefs_->GetBoolean(prefs::kSessionExitedCleanly);
688 // Mark the session as open.
689 prefs_->SetBoolean(prefs::kSessionExitedCleanly, false);
690 // Make sure we save to disk that the session has opened.
[email protected]6faa0e0d2009-04-28 06:50:36691 prefs_->ScheduleSavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29692 }
693
694 return prefs_.get();
695}
696
[email protected]f7011fcb2009-01-28 21:54:32697FilePath ProfileImpl::GetPrefFilePath() {
698 FilePath pref_file_path = path_;
699 pref_file_path = pref_file_path.Append(chrome::kPreferencesFilename);
initial.commit09911bf2008-07-26 23:55:29700 return pref_file_path;
701}
702
703URLRequestContext* ProfileImpl::GetRequestContext() {
704 if (!request_context_) {
[email protected]f7011fcb2009-01-28 21:54:32705 FilePath cookie_path = GetPath();
706 cookie_path = cookie_path.Append(chrome::kCookieFilename);
707 FilePath cache_path = GetPath();
[email protected]2b2830a2009-02-07 01:58:42708
709 // Override the cache location if specified by the user.
710 const std::wstring user_cache_dir(
711 CommandLine::ForCurrentProcess()->GetSwitchValue(
712 switches::kDiskCacheDir));
713 if (!user_cache_dir.empty()) {
714 cache_path = FilePath::FromWStringHack(user_cache_dir);
715 }
716
[email protected]f7011fcb2009-01-28 21:54:32717 cache_path = cache_path.Append(chrome::kCacheDirname);
[email protected]6ab9b202008-12-23 22:34:50718 request_context_ = ChromeURLRequestContext::CreateOriginal(
719 this, cookie_path, cache_path);
initial.commit09911bf2008-07-26 23:55:29720 request_context_->AddRef();
721
[email protected]6ab9b202008-12-23 22:34:50722 // The first request context is always a normal (non-OTR) request context.
723 // Even when Chromium is started in OTR mode, a normal profile is always
724 // created first.
725 if (!default_request_context_) {
726 default_request_context_ = request_context_;
727 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56728 NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE,
[email protected]6ab9b202008-12-23 22:34:50729 NotificationService::AllSources(), NotificationService::NoDetails());
730 }
731
initial.commit09911bf2008-07-26 23:55:29732 DCHECK(request_context_->cookie_store());
733 }
734
735 return request_context_;
736}
737
[email protected]e7f29642009-03-02 22:53:18738URLRequestContext* ProfileImpl::GetRequestContextForMedia() {
739 if (!media_request_context_) {
740 FilePath cache_path = GetPath();
[email protected]e3edeba2009-03-23 18:57:14741
742 // Override the cache location if specified by the user.
743 const std::wstring user_cache_dir(
744 CommandLine::ForCurrentProcess()->GetSwitchValue(
745 switches::kDiskCacheDir));
746 if (!user_cache_dir.empty()) {
747 cache_path = FilePath::FromWStringHack(user_cache_dir);
748 }
749
[email protected]4e7b5dbb2009-03-25 00:09:23750 cache_path = cache_path.Append(chrome::kMediaCacheDirname);
[email protected]e7f29642009-03-02 22:53:18751 media_request_context_ = ChromeURLRequestContext::CreateOriginalForMedia(
752 this, cache_path);
753 media_request_context_->AddRef();
754
755 DCHECK(media_request_context_->cookie_store());
756 }
757
758 return media_request_context_;
759}
760
[email protected]47accfd62009-05-14 18:46:21761URLRequestContext* ProfileImpl::GetRequestContextForExtensions() {
762 if (!extensions_request_context_) {
763 FilePath cookie_path = GetPath();
764 cookie_path = cookie_path.Append(chrome::kExtensionsCookieFilename);
765
766 extensions_request_context_ =
767 ChromeURLRequestContext::CreateOriginalForExtensions(this, cookie_path);
768 extensions_request_context_->AddRef();
769
770 DCHECK(extensions_request_context_->cookie_store());
771 }
772
773 return extensions_request_context_;
774}
775
[email protected]eaadd9052009-06-23 18:02:23776Blacklist* ProfileImpl::GetBlacklist() {
777 return blacklist_;
778}
779
initial.commit09911bf2008-07-26 23:55:29780HistoryService* ProfileImpl::GetHistoryService(ServiceAccessType sat) {
781 if (!history_service_created_) {
[email protected]90ef13132008-08-27 03:27:46782 history_service_created_ = true;
initial.commit09911bf2008-07-26 23:55:29783 scoped_refptr<HistoryService> history(new HistoryService(this));
[email protected]d8e41ed2008-09-11 15:22:32784 if (!history->Init(GetPath(), GetBookmarkModel()))
initial.commit09911bf2008-07-26 23:55:29785 return NULL;
786 history_service_.swap(history);
initial.commit09911bf2008-07-26 23:55:29787
788 // Send out the notification that the history service was created.
789 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56790 Notify(NotificationType::HISTORY_CREATED, Source<Profile>(this),
initial.commit09911bf2008-07-26 23:55:29791 Details<HistoryService>(history_service_.get()));
792 }
793 return history_service_.get();
794}
795
initial.commit09911bf2008-07-26 23:55:29796TemplateURLModel* ProfileImpl::GetTemplateURLModel() {
797 if (!template_url_model_.get())
798 template_url_model_.reset(new TemplateURLModel(this));
799 return template_url_model_.get();
800}
801
802TemplateURLFetcher* ProfileImpl::GetTemplateURLFetcher() {
803 if (!template_url_fetcher_.get())
804 template_url_fetcher_.reset(new TemplateURLFetcher(this));
805 return template_url_fetcher_.get();
806}
807
808WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) {
809 if (!created_web_data_service_)
810 CreateWebDataService();
811 return web_data_service_.get();
812}
813
814void ProfileImpl::CreateWebDataService() {
815 DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL);
816 created_web_data_service_ = true;
817 scoped_refptr<WebDataService> wds(new WebDataService());
818 if (!wds->Init(GetPath()))
819 return;
820 web_data_service_.swap(wds);
821}
822
[email protected]e69d33952009-06-03 22:00:41823PasswordStore* ProfileImpl::GetPasswordStore(ServiceAccessType sat) {
824 if (!created_password_store_)
825 CreatePasswordStore();
826 return password_store_.get();
827}
828
829void ProfileImpl::CreatePasswordStore() {
830 DCHECK(!created_password_store_ && password_store_.get() == NULL);
831 created_password_store_ = true;
832 scoped_refptr<PasswordStore> ps;
833#if defined(OS_LINUX)
834// Temporarily disabled while we figure some stuff out.
835// http://code.google.com/p/chromium/issues/detail?id=12351
836// if (getenv("KDE_FULL_SESSION")) {
837// ps = new PasswordStoreKWallet();
838// } else {
839// ps = new PasswordStoreGnome();
840// }
841 NOTIMPLEMENTED();
842#elif defined(OS_WIN)
843 ps = new PasswordStoreWin(GetWebDataService(Profile::IMPLICIT_ACCESS));
[email protected]034eba52009-06-03 22:50:56844#elif defined(OS_MACOSX)
845 ps = new PasswordStoreMac(new MacKeychain());
[email protected]e69d33952009-06-03 22:00:41846#else
847 NOTIMPLEMENTED();
848#endif
849 if (!ps || !ps->Init()) {
850 // Try falling back to the default password manager
851 LOG(WARNING) << "Could not initialise native password manager - "
852 "falling back to default";
853 ps = new PasswordStoreDefault(GetWebDataService(Profile::IMPLICIT_ACCESS));
854 if (!ps->Init())
855 return;
856 }
857 password_store_.swap(ps);
858}
859
initial.commit09911bf2008-07-26 23:55:29860DownloadManager* ProfileImpl::GetDownloadManager() {
861 if (!created_download_manager_) {
862 scoped_refptr<DownloadManager> dlm(new DownloadManager);
863 dlm->Init(this);
864 created_download_manager_ = true;
865 download_manager_.swap(dlm);
866 }
867 return download_manager_.get();
868}
869
870bool ProfileImpl::HasCreatedDownloadManager() const {
871 return created_download_manager_;
872}
873
[email protected]4a190632009-05-09 01:07:42874void ProfileImpl::InitThemes() {
875 if (!created_theme_provider_) {
876 scoped_refptr<BrowserThemeProvider> themes(new BrowserThemeProvider);
877 themes->Init(this);
878 created_theme_provider_ = true;
879 theme_provider_.swap(themes);
880 }
881}
882
883void ProfileImpl::SetTheme(Extension* extension) {
884 InitThemes();
885 theme_provider_.get()->SetTheme(extension);
886}
887
888void ProfileImpl::ClearTheme() {
889 InitThemes();
890 theme_provider_.get()->UseDefaultTheme();
891}
892
893ThemeProvider* ProfileImpl::GetThemeProvider() {
894 InitThemes();
895 return theme_provider_.get();
896}
897
initial.commit09911bf2008-07-26 23:55:29898SessionService* ProfileImpl::GetSessionService() {
899 if (!session_service_.get() && !shutdown_session_service_) {
900 session_service_ = new SessionService(this);
901 session_service_->ResetFromCurrentBrowsers();
902 }
903 return session_service_.get();
904}
905
906void ProfileImpl::ShutdownSessionService() {
907 if (shutdown_session_service_)
908 return;
909
910 // We're about to exit, force creation of the session service if it hasn't
911 // been created yet. We do this to ensure session state matches the point in
912 // time the user exited.
913 GetSessionService();
914 shutdown_session_service_ = true;
915 session_service_ = NULL;
916}
917
918bool ProfileImpl::HasSessionService() const {
919 return (session_service_.get() != NULL);
920}
921
922std::wstring ProfileImpl::GetName() {
923 return GetPrefs()->GetString(prefs::kProfileName);
924}
925void ProfileImpl::SetName(const std::wstring& name) {
926 GetPrefs()->SetString(prefs::kProfileName, name);
927}
928
929std::wstring ProfileImpl::GetID() {
930 return GetPrefs()->GetString(prefs::kProfileID);
931}
932void ProfileImpl::SetID(const std::wstring& id) {
933 GetPrefs()->SetString(prefs::kProfileID, id);
934}
935
initial.commit09911bf2008-07-26 23:55:29936bool ProfileImpl::DidLastSessionExitCleanly() {
937 // last_session_exited_cleanly_ is set when the preferences are loaded. Force
938 // it to be set by asking for the prefs.
939 GetPrefs();
940 return last_session_exited_cleanly_;
941}
942
[email protected]d8e41ed2008-09-11 15:22:32943BookmarkModel* ProfileImpl::GetBookmarkModel() {
[email protected]90ef13132008-08-27 03:27:46944 if (!bookmark_bar_model_.get()) {
[email protected]d8e41ed2008-09-11 15:22:32945 bookmark_bar_model_.reset(new BookmarkModel(this));
[email protected]90ef13132008-08-27 03:27:46946 bookmark_bar_model_->Load();
947 }
initial.commit09911bf2008-07-26 23:55:29948 return bookmark_bar_model_.get();
949}
950
951bool ProfileImpl::IsSameProfile(Profile* profile) {
952 if (profile == static_cast<Profile*>(this))
953 return true;
954 OffTheRecordProfileImpl* otr_profile = off_the_record_profile_.get();
955 return otr_profile && profile == static_cast<Profile*>(otr_profile);
956}
957
958Time ProfileImpl::GetStartTime() const {
959 return start_time_;
960}
961
962TabRestoreService* ProfileImpl::GetTabRestoreService() {
963 if (!tab_restore_service_.get())
[email protected]169627b2008-12-06 19:30:19964 tab_restore_service_ = new TabRestoreService(this);
initial.commit09911bf2008-07-26 23:55:29965 return tab_restore_service_.get();
966}
967
[email protected]449478302009-06-09 20:04:28968ThumbnailStore* ProfileImpl::GetThumbnailStore() {
969 if (!thumbnail_store_.get()) {
970 thumbnail_store_ = new ThumbnailStore;
[email protected]f2458dc2009-06-24 22:38:58971 thumbnail_store_->Init(GetPath().AppendASCII("thumbnailstore"), this);
[email protected]449478302009-06-09 20:04:28972 }
973 return thumbnail_store_.get();
974}
975
initial.commit09911bf2008-07-26 23:55:29976void ProfileImpl::ResetTabRestoreService() {
[email protected]169627b2008-12-06 19:30:19977 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29978}
979
[email protected]a9afddb2009-02-12 17:49:42980// To be run in the IO thread to notify all resource message filters that the
[email protected]20930852008-10-15 19:30:41981// spellchecker has changed.
982class NotifySpellcheckerChangeTask : public Task {
983 public:
984 NotifySpellcheckerChangeTask(
[email protected]e7244d82008-10-29 18:13:26985 Profile* profile,
986 const SpellcheckerReinitializedDetails& spellchecker)
[email protected]20930852008-10-15 19:30:41987 : profile_(profile),
988 spellchecker_(spellchecker) {
989 }
990
991 private:
992 void Run(void) {
993 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56994 NotificationType::SPELLCHECKER_REINITIALIZED,
[email protected]20930852008-10-15 19:30:41995 Source<Profile>(profile_),
996 Details<SpellcheckerReinitializedDetails>(&spellchecker_));
997 }
998
999 Profile* profile_;
1000 SpellcheckerReinitializedDetails spellchecker_;
1001};
1002
[email protected]e7244d82008-10-29 18:13:261003void ProfileImpl::InitializeSpellChecker(bool need_to_broadcast) {
[email protected]20930852008-10-15 19:30:411004 // The I/O thread may be NULL during testing.
1005 base::Thread* io_thread = g_browser_process->io_thread();
1006 if (spellchecker_) {
1007 // The spellchecker must be deleted on the I/O thread.
1008 // A dummy variable to aid in logical clarity.
1009 SpellChecker* last_spellchecker = spellchecker_;
1010
1011 if (io_thread)
1012 io_thread->message_loop()->ReleaseSoon(FROM_HERE, last_spellchecker);
1013 else // during testing, we don't have an I/O thread
1014 last_spellchecker->Release();
[email protected]20930852008-10-15 19:30:411015 }
1016
[email protected]20930852008-10-15 19:30:411017 // Retrieve the (perhaps updated recently) dictionary name from preferences.
1018 PrefService* prefs = GetPrefs();
[email protected]e7244d82008-10-29 18:13:261019 bool enable_spellcheck = prefs->GetBoolean(prefs::kEnableSpellCheck);
[email protected]20930852008-10-15 19:30:411020
[email protected]e7244d82008-10-29 18:13:261021 if (enable_spellcheck) {
[email protected]bd17b702009-02-25 20:44:081022 FilePath dict_dir;
[email protected]e7244d82008-10-29 18:13:261023 PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
[email protected]a9afddb2009-02-12 17:49:421024 // Note that, as the object pointed to by previously by spellchecker_
[email protected]e7244d82008-10-29 18:13:261025 // is being deleted in the io thread, the spellchecker_ can be made to point
1026 // to a new object (RE-initialized) in parallel in this UI thread.
[email protected]4b4d1adc2008-12-10 22:28:581027 spellchecker_ = new SpellChecker(dict_dir,
[email protected]74c8b422009-03-11 00:34:121028 WideToASCII(prefs->GetString(prefs::kSpellCheckDictionary)),
1029 GetRequestContext(),
[email protected]bd17b702009-02-25 20:44:081030 FilePath());
[email protected]e7244d82008-10-29 18:13:261031 spellchecker_->AddRef(); // Manual refcounting.
1032 } else {
1033 spellchecker_ = NULL;
1034 }
[email protected]20930852008-10-15 19:30:411035
[email protected]154a4332009-06-03 20:20:581036 // Set auto spell correct status for spellchecker.
1037 if (spellchecker_) {
1038 spellchecker_->EnableAutoSpellCorrect(
1039 prefs->GetBoolean(prefs::kEnableAutoSpellCorrect));
1040 }
1041
[email protected]20930852008-10-15 19:30:411042 if (need_to_broadcast && io_thread) { // Notify resource message filters.
1043 SpellcheckerReinitializedDetails scoped_spellchecker;
1044 scoped_spellchecker.spellchecker = spellchecker_;
[email protected]e7244d82008-10-29 18:13:261045 if (io_thread) {
1046 io_thread->message_loop()->PostTask(
[email protected]a9afddb2009-02-12 17:49:421047 FROM_HERE,
[email protected]e7244d82008-10-29 18:13:261048 new NotifySpellcheckerChangeTask(this, scoped_spellchecker));
1049 }
[email protected]20930852008-10-15 19:30:411050 }
1051}
1052
[email protected]e7244d82008-10-29 18:13:261053void ProfileImpl::ReinitializeSpellChecker() {
1054 InitializeSpellChecker(true);
1055}
1056
initial.commit09911bf2008-07-26 23:55:291057SpellChecker* ProfileImpl::GetSpellChecker() {
1058 if (!spellchecker_) {
[email protected]20930852008-10-15 19:30:411059 // This is where spellchecker gets initialized. Note that this is being
1060 // initialized in the ui_thread. However, this is not a problem as long as
1061 // it is *used* in the io thread.
[email protected]34cc84f2009-02-13 10:04:351062 // TODO(sidchat): One day, change everything so that spellchecker gets
[email protected]20930852008-10-15 19:30:411063 // initialized in the IO thread itself.
[email protected]e7244d82008-10-29 18:13:261064 InitializeSpellChecker(false);
initial.commit09911bf2008-07-26 23:55:291065 }
[email protected]20930852008-10-15 19:30:411066
initial.commit09911bf2008-07-26 23:55:291067 return spellchecker_;
1068}
1069
1070void ProfileImpl::MarkAsCleanShutdown() {
1071 if (prefs_.get()) {
1072 // The session cleanly exited, set kSessionExitedCleanly appropriately.
1073 prefs_->SetBoolean(prefs::kSessionExitedCleanly, true);
1074
1075 // NOTE: If you change what thread this writes on, be sure and update
1076 // ChromeFrame::EndSession().
[email protected]6faa0e0d2009-04-28 06:50:361077 prefs_->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:291078 }
1079}
1080
[email protected]e7244d82008-10-29 18:13:261081void ProfileImpl::Observe(NotificationType type,
1082 const NotificationSource& source,
1083 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:561084 if (NotificationType::PREF_CHANGED == type) {
[email protected]e7244d82008-10-29 18:13:261085 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
1086 PrefService* prefs = Source<PrefService>(source).ptr();
1087 DCHECK(pref_name_in && prefs);
1088 if (*pref_name_in == prefs::kSpellCheckDictionary ||
[email protected]154a4332009-06-03 20:20:581089 *pref_name_in == prefs::kEnableSpellCheck ||
1090 *pref_name_in == prefs::kEnableAutoSpellCorrect) {
[email protected]e7244d82008-10-29 18:13:261091 InitializeSpellChecker(true);
1092 }
[email protected]4a190632009-05-09 01:07:421093 } else if (NotificationType::THEME_INSTALLED == type) {
1094 Extension* extension = Details<Extension>(details).ptr();
1095 SetTheme(extension);
[email protected]e7244d82008-10-29 18:13:261096 }
1097}
1098
initial.commit09911bf2008-07-26 23:55:291099void ProfileImpl::StopCreateSessionServiceTimer() {
[email protected]2d316662008-09-03 18:18:141100 create_session_service_timer_.Stop();
initial.commit09911bf2008-07-26 23:55:291101}
[email protected]3a453fa2008-08-15 18:46:341102
1103#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:071104ProfilePersonalization* ProfileImpl::GetProfilePersonalization() {
[email protected]57d3d0a2008-09-24 00:50:071105 return personalization_.get();
[email protected]3a453fa2008-08-15 18:46:341106}
license.botbf09a502008-08-24 00:55:551107#endif