blob: a234ac5a27ae437620dcf774e5da893ce6b88a66 [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]b7f05882009-02-22 01:21:5616#include "chrome/browser/download/download_manager.h"
[email protected]bb28e062009-02-27 17:19:1817#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]6014d672008-12-05 00:38:2518#include "chrome/browser/extensions/extensions_service.h"
[email protected]0938d3c2009-01-09 20:37:3519#include "chrome/browser/extensions/user_script_master.h"
[email protected]a9afddb2009-02-12 17:49:4220#include "chrome/browser/history/history.h"
[email protected]6ab9b202008-12-23 22:34:5021#include "chrome/browser/net/chrome_url_request_context.h"
initial.commit09911bf2008-07-26 23:55:2922#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2623#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]1132436e2009-04-08 20:06:3324#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]f63ae312009-02-04 17:58:4625#include "chrome/browser/search_engines/template_url_model.h"
[email protected]85e921fb82009-02-11 23:19:4426#include "chrome/browser/sessions/session_service.h"
[email protected]bd580a252009-02-12 01:16:3027#include "chrome/browser/sessions/tab_restore_service.h"
[email protected]f0a644292009-02-25 23:32:4728#include "chrome/browser/spellchecker.h"
[email protected]1132436e2009-04-08 20:06:3329#include "chrome/browser/ssl/ssl_host_state.h"
initial.commit09911bf2008-07-26 23:55:2930#include "chrome/browser/visitedlink_master.h"
31#include "chrome/browser/webdata/web_data_service.h"
32#include "chrome/common/chrome_constants.h"
33#include "chrome/common/chrome_paths.h"
34#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2935#include "chrome/common/notification_service.h"
36#include "chrome/common/pref_names.h"
[email protected]f7011fcb2009-01-28 21:54:3237#include "chrome/common/render_messages.h"
[email protected]f90f5c512009-02-18 19:10:5838#include "grit/locale_settings.h"
initial.commit09911bf2008-07-26 23:55:2939
[email protected]f7011fcb2009-01-28 21:54:3240
[email protected]e1acf6f2008-10-27 20:43:3341using base::Time;
42using base::TimeDelta;
43
initial.commit09911bf2008-07-26 23:55:2944// Delay, in milliseconds, before we explicitly create the SessionService.
45static const int kCreateSessionServiceDelayMS = 500;
46
47// A pointer to the request context for the default profile. See comments on
48// Profile::GetDefaultRequestContext.
49URLRequestContext* Profile::default_request_context_;
50
[email protected]34cc84f2009-02-13 10:04:3551// static
initial.commit09911bf2008-07-26 23:55:2952void Profile::RegisterUserPrefs(PrefService* prefs) {
[email protected]430d3f72008-10-27 17:56:5553 prefs->RegisterBooleanPref(prefs::kSearchSuggestEnabled, true);
initial.commit09911bf2008-07-26 23:55:2954 prefs->RegisterBooleanPref(prefs::kSessionExitedCleanly, true);
55 prefs->RegisterBooleanPref(prefs::kSafeBrowsingEnabled, true);
[email protected]74c8b422009-03-11 00:34:1256 // TODO(estade): IDS_SPELLCHECK_DICTIONARY should be an ASCII string.
[email protected]e7244d82008-10-29 18:13:2657 prefs->RegisterLocalizedStringPref(prefs::kSpellCheckDictionary,
58 IDS_SPELLCHECK_DICTIONARY);
[email protected]e7244d82008-10-29 18:13:2659 prefs->RegisterBooleanPref(prefs::kEnableSpellCheck, true);
[email protected]f93fe782009-02-19 01:26:1360 prefs->RegisterBooleanPref(prefs::kEnableUserScripts, false);
61 prefs->RegisterBooleanPref(prefs::kEnableExtensions, false);
initial.commit09911bf2008-07-26 23:55:2962}
63
[email protected]34cc84f2009-02-13 10:04:3564// static
[email protected]f7011fcb2009-01-28 21:54:3265Profile* Profile::CreateProfile(const FilePath& path) {
initial.commit09911bf2008-07-26 23:55:2966 return new ProfileImpl(path);
67}
68
[email protected]34cc84f2009-02-13 10:04:3569// static
initial.commit09911bf2008-07-26 23:55:2970URLRequestContext* Profile::GetDefaultRequestContext() {
71 return default_request_context_;
72}
73
74
initial.commit09911bf2008-07-26 23:55:2975////////////////////////////////////////////////////////////////////////////////
76//
77// OffTheRecordProfileImpl is a profile subclass that wraps an existing profile
78// to make it suitable for the off the record mode.
79//
80////////////////////////////////////////////////////////////////////////////////
81class OffTheRecordProfileImpl : public Profile,
82 public NotificationObserver {
83 public:
84 explicit OffTheRecordProfileImpl(Profile* real_profile)
85 : profile_(real_profile),
[email protected]363347b2009-03-13 20:06:5786 media_request_context_(NULL),
initial.commit09911bf2008-07-26 23:55:2987 start_time_(Time::Now()) {
[email protected]6ab9b202008-12-23 22:34:5088 request_context_ = ChromeURLRequestContext::CreateOffTheRecord(this);
initial.commit09911bf2008-07-26 23:55:2989 request_context_->AddRef();
90 // Register for browser close notifications so we can detect when the last
91 // off-the-record window is closed, in which case we can clean our states
92 // (cookies, downloads...).
93 NotificationService::current()->AddObserver(
[email protected]bfd04a62009-02-01 18:16:5694 this,
95 NotificationType::BROWSER_CLOSED,
96 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:2997 }
98
99 virtual ~OffTheRecordProfileImpl() {
100 if (request_context_) {
[email protected]6ab9b202008-12-23 22:34:50101 request_context_->CleanupOnUIThread();
102
initial.commit09911bf2008-07-26 23:55:29103 // Clean up request context on IO thread.
104 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]6ab9b202008-12-23 22:34:50105 NewRunnableMethod(request_context_,
106 &base::RefCountedThreadSafe<URLRequestContext>::Release));
initial.commit09911bf2008-07-26 23:55:29107 request_context_ = NULL;
108 }
109 NotificationService::current()->RemoveObserver(
[email protected]bfd04a62009-02-01 18:16:56110 this,
111 NotificationType::BROWSER_CLOSED,
112 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29113 }
114
[email protected]f7011fcb2009-01-28 21:54:32115 virtual FilePath GetPath() { return profile_->GetPath(); }
initial.commit09911bf2008-07-26 23:55:29116
117 virtual bool IsOffTheRecord() {
118 return true;
119 }
120
121 virtual Profile* GetOffTheRecordProfile() {
122 return this;
123 }
124
[email protected]860f55492009-03-27 19:50:59125 virtual void DestroyOffTheRecordProfile() {
126 // Suicide is bad!
127 NOTREACHED();
128 }
129
initial.commit09911bf2008-07-26 23:55:29130 virtual Profile* GetOriginalProfile() {
131 return profile_;
132 }
133
134 virtual VisitedLinkMaster* GetVisitedLinkMaster() {
[email protected]7fb6c862009-03-13 02:51:49135 // We don't provide access to the VisitedLinkMaster when we're OffTheRecord
136 // because we don't want to leak the sites that the user has visited before.
137 return NULL;
initial.commit09911bf2008-07-26 23:55:29138 }
139
[email protected]6014d672008-12-05 00:38:25140 virtual ExtensionsService* GetExtensionsService() {
141 return profile_->GetExtensionsService();
142 }
143
[email protected]0938d3c2009-01-09 20:37:35144 virtual UserScriptMaster* GetUserScriptMaster() {
145 return profile_->GetUserScriptMaster();
[email protected]04fba9a92008-10-28 17:25:25146 }
147
[email protected]34cc84f2009-02-13 10:04:35148 virtual SSLHostState* GetSSLHostState() {
149 if (!ssl_host_state_.get())
150 ssl_host_state_.reset(new SSLHostState());
151
152 DCHECK(ssl_host_state_->CalledOnValidThread());
153 return ssl_host_state_.get();
154 }
155
initial.commit09911bf2008-07-26 23:55:29156 virtual HistoryService* GetHistoryService(ServiceAccessType sat) {
157 if (sat == EXPLICIT_ACCESS) {
158 return profile_->GetHistoryService(sat);
159 } else {
160 NOTREACHED() << "This profile is OffTheRecord";
161 return NULL;
162 }
163 }
164
initial.commit09911bf2008-07-26 23:55:29165 virtual WebDataService* GetWebDataService(ServiceAccessType sat) {
166 if (sat == EXPLICIT_ACCESS) {
167 return profile_->GetWebDataService(sat);
168 } else {
169 NOTREACHED() << "This profile is OffTheRecord";
170 return NULL;
171 }
172 }
173
174 virtual PrefService* GetPrefs() {
175 return profile_->GetPrefs();
176 }
177
178 virtual TemplateURLModel* GetTemplateURLModel() {
179 return profile_->GetTemplateURLModel();
180 }
181
182 virtual TemplateURLFetcher* GetTemplateURLFetcher() {
183 return profile_->GetTemplateURLFetcher();
184 }
185
186 virtual DownloadManager* GetDownloadManager() {
187 if (!download_manager_.get()) {
188 scoped_refptr<DownloadManager> dlm(new DownloadManager);
189 dlm->Init(this);
190 download_manager_.swap(dlm);
191 }
192 return download_manager_.get();
193 }
194
195 virtual bool HasCreatedDownloadManager() const {
196 return (download_manager_.get() != NULL);
197 }
198
199 virtual URLRequestContext* GetRequestContext() {
200 return request_context_;
201 }
202
[email protected]e7f29642009-03-02 22:53:18203 virtual URLRequestContext* GetRequestContextForMedia() {
204 if (!media_request_context_) {
205 FilePath cache_path = GetPath();
[email protected]e3edeba2009-03-23 18:57:14206
207 // Override the cache location if specified by the user.
208 const std::wstring user_cache_dir(
209 CommandLine::ForCurrentProcess()->GetSwitchValue(
210 switches::kDiskCacheDir));
211 if (!user_cache_dir.empty()) {
212 cache_path = FilePath::FromWStringHack(user_cache_dir);
213 }
214
[email protected]4e7b5dbb2009-03-25 00:09:23215 cache_path = cache_path.Append(chrome::kOffTheRecordMediaCacheDirname);
[email protected]e7f29642009-03-02 22:53:18216 media_request_context_ =
217 ChromeURLRequestContext::CreateOffTheRecordForMedia(
218 this, cache_path);
219 media_request_context_->AddRef();
220
221 DCHECK(media_request_context_->cookie_store());
222 }
223 return media_request_context_;
224 }
225
initial.commit09911bf2008-07-26 23:55:29226 virtual SessionService* GetSessionService() {
227 // Don't save any sessions when off the record.
228 return NULL;
229 }
230
231 virtual void ShutdownSessionService() {
232 // We don't allow a session service, nothing to do.
233 }
234
235 virtual bool HasSessionService() const {
236 // We never have a session service.
237 return false;
238 }
239
240 virtual std::wstring GetName() {
241 return profile_->GetName();
242 }
243
244 virtual void SetName(const std::wstring& name) {
245 profile_->SetName(name);
246 }
247
248 virtual std::wstring GetID() {
249 return profile_->GetID();
250 }
251
252 virtual void SetID(const std::wstring& id) {
253 profile_->SetID(id);
254 }
255
initial.commit09911bf2008-07-26 23:55:29256 virtual bool DidLastSessionExitCleanly() {
257 return profile_->DidLastSessionExitCleanly();
258 }
259
[email protected]d8e41ed2008-09-11 15:22:32260 virtual BookmarkModel* GetBookmarkModel() {
261 return profile_->GetBookmarkModel();
initial.commit09911bf2008-07-26 23:55:29262 }
263
[email protected]3a453fa2008-08-15 18:46:34264#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07265 virtual ProfilePersonalization* GetProfilePersonalization() {
[email protected]3a453fa2008-08-15 18:46:34266 return profile_->GetProfilePersonalization();
267 }
268#endif
269
initial.commit09911bf2008-07-26 23:55:29270 virtual bool IsSameProfile(Profile* profile) {
271 if (profile == static_cast<Profile*>(this))
272 return true;
273 return profile == profile_;
274 }
275
276 virtual Time GetStartTime() const {
277 return start_time_;
278 }
279
280 virtual TabRestoreService* GetTabRestoreService() {
281 return NULL;
282 }
283
[email protected]e7244d82008-10-29 18:13:26284 virtual void ResetTabRestoreService() {
[email protected]20930852008-10-15 19:30:41285 }
286
[email protected]e7244d82008-10-29 18:13:26287 virtual void ReinitializeSpellChecker() {
288 profile_->ReinitializeSpellChecker();
initial.commit09911bf2008-07-26 23:55:29289 }
290
291 virtual SpellChecker* GetSpellChecker() {
292 return profile_->GetSpellChecker();
293 }
294
295 virtual void MarkAsCleanShutdown() {
296 }
297
[email protected]bdbc87c2009-01-25 05:08:54298 virtual void InitExtensions() {
299 NOTREACHED();
300 }
301
initial.commit09911bf2008-07-26 23:55:29302 virtual void ExitedOffTheRecordMode() {
303 // Drop our download manager so we forget about all the downloads made
304 // in off-the-record mode.
305 download_manager_ = NULL;
306 }
307
308 virtual void Observe(NotificationType type,
309 const NotificationSource& source,
310 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56311 DCHECK_EQ(NotificationType::BROWSER_CLOSED, type.value);
initial.commit09911bf2008-07-26 23:55:29312 // We are only interested in OTR browser closing.
313 if (Source<Browser>(source)->profile() != this)
314 return;
315
316 // Let's check if we still have an Off The Record window opened.
317 // Note that we check against 1 as this notification is sent before the
318 // browser window is actually removed from the list.
319 if (BrowserList::GetBrowserCount(this) <= 1)
320 ExitedOffTheRecordMode();
321 }
322
323 private:
324 // The real underlying profile.
325 Profile* profile_;
326
[email protected]6ab9b202008-12-23 22:34:50327 // The context to use for requests made from this OTR session.
328 ChromeURLRequestContext* request_context_;
initial.commit09911bf2008-07-26 23:55:29329
[email protected]e7f29642009-03-02 22:53:18330 // The context for requests for media resources.
331 ChromeURLRequestContext* media_request_context_;
332
initial.commit09911bf2008-07-26 23:55:29333 // The download manager that only stores downloaded items in memory.
334 scoped_refptr<DownloadManager> download_manager_;
335
[email protected]34cc84f2009-02-13 10:04:35336 // We don't want SSLHostState from the OTR profile to leak back to the main
337 // profile because then the main profile would learn some of the host names
338 // the user visited while OTR.
339 scoped_ptr<SSLHostState> ssl_host_state_;
340
initial.commit09911bf2008-07-26 23:55:29341 // Time we were started.
342 Time start_time_;
343
344 DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl);
345};
346
[email protected]f7011fcb2009-01-28 21:54:32347ProfileImpl::ProfileImpl(const FilePath& path)
initial.commit09911bf2008-07-26 23:55:29348 : path_(path),
349 off_the_record_(false),
[email protected]f7011fcb2009-01-28 21:54:32350 request_context_(NULL),
[email protected]363347b2009-03-13 20:06:57351 media_request_context_(NULL),
[email protected]f7011fcb2009-01-28 21:54:32352 history_service_created_(false),
353 created_web_data_service_(false),
354 created_download_manager_(false),
355 start_time_(Time::Now()),
356 spellchecker_(NULL),
initial.commit09911bf2008-07-26 23:55:29357 shutdown_session_service_(false) {
358 DCHECK(!path.empty()) << "Using an empty path will attempt to write " <<
359 "profile files to the root directory!";
[email protected]2d316662008-09-03 18:18:14360 create_session_service_timer_.Start(
361 TimeDelta::FromMilliseconds(kCreateSessionServiceDelayMS), this,
362 &ProfileImpl::EnsureSessionServiceCreated);
[email protected]bdbc87c2009-01-25 05:08:54363
[email protected]e7244d82008-10-29 18:13:26364 PrefService* prefs = GetPrefs();
365 prefs->AddPrefObserver(prefs::kSpellCheckDictionary, this);
366 prefs->AddPrefObserver(prefs::kEnableSpellCheck, this);
[email protected]380ab462009-04-24 01:23:58367
368#ifdef CHROME_PERSONALIZATION
369 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableP13n))
370 personalization_.reset(Personalization::CreateProfilePersonalization(this));
371#endif
372
[email protected]1351b6a2009-04-09 20:13:22373#if defined(OS_LINUX)
374 // TODO(port): Remove ifdef when the Linux splash page is not needed.
375 prefs->transient()->SetString(prefs::kHomePage, "about:linux-splash");
376 prefs->transient()->SetBoolean(prefs::kHomePageIsNewTabPage, false);
377#endif
initial.commit09911bf2008-07-26 23:55:29378}
379
[email protected]bdbc87c2009-01-25 05:08:54380void ProfileImpl::InitExtensions() {
[email protected]8c756ac2009-01-30 23:36:41381 if (user_script_master_ || extensions_service_)
382 return; // Already initialized.
383
[email protected]bdbc87c2009-01-25 05:08:54384 const CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]f93fe782009-02-19 01:26:13385 PrefService* prefs = GetPrefs();
[email protected]bdbc87c2009-01-25 05:08:54386 bool user_scripts_enabled =
[email protected]f0a51fb52009-03-05 12:46:38387 command_line->HasSwitch(switches::kEnableUserScripts) ||
[email protected]f93fe782009-02-19 01:26:13388 prefs->GetBoolean(prefs::kEnableUserScripts);
[email protected]a9afddb2009-02-12 17:49:42389 bool extensions_enabled =
[email protected]f0a51fb52009-03-05 12:46:38390 command_line->HasSwitch(switches::kEnableExtensions) ||
[email protected]f93fe782009-02-19 01:26:13391 prefs->GetBoolean(prefs::kEnableExtensions);
[email protected]bdbc87c2009-01-25 05:08:54392
[email protected]f7011fcb2009-01-28 21:54:32393 FilePath script_dir;
[email protected]bdbc87c2009-01-25 05:08:54394 if (user_scripts_enabled) {
[email protected]0cd957b2009-03-06 20:13:23395 if (command_line->HasSwitch(switches::kUserScriptsDir)) {
396 std::wstring path_string =
397 command_line->GetSwitchValue(switches::kUserScriptsDir);
398 script_dir = FilePath::FromWStringHack(path_string);
399 } else {
400 script_dir = GetPath();
401 script_dir = script_dir.Append(chrome::kUserScriptsDirname);
402 }
[email protected]bdbc87c2009-01-25 05:08:54403 }
404
[email protected]bb28e062009-02-27 17:19:18405 ExtensionErrorReporter::Init(true); // allow noisy errors.
[email protected]bdbc87c2009-01-25 05:08:54406 user_script_master_ = new UserScriptMaster(
[email protected]f7011fcb2009-01-28 21:54:32407 g_browser_process->file_thread()->message_loop(), script_dir);
[email protected]81e63782009-02-27 19:35:09408 extensions_service_ = new ExtensionsService(this, user_script_master_.get());
[email protected]bdbc87c2009-01-25 05:08:54409
410 // If we have extensions, the extension service will kick off the first scan
411 // after extensions are loaded. Otherwise, we need to do that now.
412 if (extensions_enabled)
413 extensions_service_->Init();
414 else if (user_scripts_enabled)
415 user_script_master_->StartScan();
416}
417
initial.commit09911bf2008-07-26 23:55:29418ProfileImpl::~ProfileImpl() {
[email protected]169627b2008-12-06 19:30:19419 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29420
421 StopCreateSessionServiceTimer();
422 // TemplateURLModel schedules a task on the WebDataService from its
423 // destructor. Delete it first to ensure the task gets scheduled before we
424 // shut down the database.
425 template_url_model_.reset();
426
427 // The download manager queries the history system and should be deleted
428 // before the history is shutdown so it can properly cancel all requests.
429 download_manager_ = NULL;
430
[email protected]e7244d82008-10-29 18:13:26431 // Remove pref observers.
432 PrefService* prefs = GetPrefs();
433 prefs->RemovePrefObserver(prefs::kSpellCheckDictionary, this);
434 prefs->RemovePrefObserver(prefs::kEnableSpellCheck, this);
435
[email protected]3a453fa2008-08-15 18:46:34436#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07437 personalization_.reset();
[email protected]3a453fa2008-08-15 18:46:34438#endif
439
initial.commit09911bf2008-07-26 23:55:29440 // Both HistoryService and WebDataService maintain threads for background
441 // processing. Its possible each thread still has tasks on it that have
442 // increased the ref count of the service. In such a situation, when we
443 // decrement the refcount, it won't be 0, and the threads/databases aren't
444 // properly shut down. By explicitly calling Cleanup/Shutdown we ensure the
445 // databases are properly closed.
446 if (web_data_service_.get())
447 web_data_service_->Shutdown();
448
449 if (history_service_.get())
450 history_service_->Cleanup();
451
452 // The I/O thread may be NULL during testing.
[email protected]ab820df2008-08-26 05:55:10453 base::Thread* io_thread = g_browser_process->io_thread();
initial.commit09911bf2008-07-26 23:55:29454
455 if (spellchecker_) {
456 // The spellchecker must be deleted on the I/O thread. During testing, we
457 // don't have an I/O thread.
458 if (io_thread)
459 io_thread->message_loop()->ReleaseSoon(FROM_HERE, spellchecker_);
460 else
461 spellchecker_->Release();
462 }
463
464 if (request_context_) {
[email protected]6ab9b202008-12-23 22:34:50465 request_context_->CleanupOnUIThread();
466
467 if (default_request_context_ == request_context_)
468 default_request_context_ = NULL;
469
initial.commit09911bf2008-07-26 23:55:29470 // Clean up request context on IO thread.
[email protected]6ab9b202008-12-23 22:34:50471 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
472 NewRunnableMethod(request_context_,
473 &base::RefCountedThreadSafe<URLRequestContext>::Release));
initial.commit09911bf2008-07-26 23:55:29474 request_context_ = NULL;
475 }
476
[email protected]1e744f22009-04-08 01:00:17477 if (media_request_context_) {
478 media_request_context_->CleanupOnUIThread();
479
480 // Clean up request context on IO thread.
481 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
482 NewRunnableMethod(media_request_context_,
483 &base::RefCountedThreadSafe<URLRequestContext>::Release));
484 media_request_context_ = NULL;
485 }
486
[email protected]d8e41ed2008-09-11 15:22:32487 // HistoryService may call into the BookmarkModel, as such we need to
488 // delete HistoryService before the BookmarkModel. The destructor for
[email protected]90ef13132008-08-27 03:27:46489 // HistoryService will join with HistoryService's backend thread so that
490 // by the time the destructor has finished we're sure it will no longer call
[email protected]d8e41ed2008-09-11 15:22:32491 // into the BookmarkModel.
[email protected]90ef13132008-08-27 03:27:46492 history_service_ = NULL;
493 bookmark_bar_model_.reset();
494
initial.commit09911bf2008-07-26 23:55:29495 MarkAsCleanShutdown();
496}
497
[email protected]f7011fcb2009-01-28 21:54:32498FilePath ProfileImpl::GetPath() {
initial.commit09911bf2008-07-26 23:55:29499 return path_;
500}
501
502bool ProfileImpl::IsOffTheRecord() {
503 return false;
504}
505
506Profile* ProfileImpl::GetOffTheRecordProfile() {
507 if (!off_the_record_profile_.get()) {
508 scoped_ptr<OffTheRecordProfileImpl> p(new OffTheRecordProfileImpl(this));
509 off_the_record_profile_.swap(p);
510 }
511 return off_the_record_profile_.get();
512}
513
[email protected]860f55492009-03-27 19:50:59514void ProfileImpl::DestroyOffTheRecordProfile() {
515 off_the_record_profile_.reset();
516}
517
initial.commit09911bf2008-07-26 23:55:29518Profile* ProfileImpl::GetOriginalProfile() {
519 return this;
520}
521
[email protected]176aa482008-11-14 03:25:15522static void BroadcastNewHistoryTable(base::SharedMemory* table_memory) {
initial.commit09911bf2008-07-26 23:55:29523 if (!table_memory)
524 return;
525
526 // send to all RenderProcessHosts
527 for (RenderProcessHost::iterator i = RenderProcessHost::begin();
528 i != RenderProcessHost::end(); i++) {
529 if (!i->second->channel())
530 continue;
531
[email protected]176aa482008-11-14 03:25:15532 base::SharedMemoryHandle new_table;
[email protected]f7011fcb2009-01-28 21:54:32533 base::ProcessHandle process = i->second->process().handle();
initial.commit09911bf2008-07-26 23:55:29534 if (!process) {
535 // process can be null if it's started with the --single-process flag.
[email protected]f7011fcb2009-01-28 21:54:32536 process = base::Process::Current().handle();
initial.commit09911bf2008-07-26 23:55:29537 }
538
539 table_memory->ShareToProcess(process, &new_table);
540 IPC::Message* msg = new ViewMsg_VisitedLink_NewTable(new_table);
541 i->second->channel()->Send(msg);
542 }
543}
544
545VisitedLinkMaster* ProfileImpl::GetVisitedLinkMaster() {
546 if (!visited_link_master_.get()) {
547 scoped_ptr<VisitedLinkMaster> visited_links(
548 new VisitedLinkMaster(g_browser_process->file_thread(),
549 BroadcastNewHistoryTable, this));
550 if (!visited_links->Init())
551 return NULL;
552 visited_link_master_.swap(visited_links);
553 }
554
555 return visited_link_master_.get();
556}
557
[email protected]6014d672008-12-05 00:38:25558ExtensionsService* ProfileImpl::GetExtensionsService() {
559 return extensions_service_.get();
560}
561
[email protected]0938d3c2009-01-09 20:37:35562UserScriptMaster* ProfileImpl::GetUserScriptMaster() {
[email protected]0938d3c2009-01-09 20:37:35563 return user_script_master_.get();
[email protected]04fba9a92008-10-28 17:25:25564}
565
[email protected]34cc84f2009-02-13 10:04:35566SSLHostState* ProfileImpl::GetSSLHostState() {
567 if (!ssl_host_state_.get())
568 ssl_host_state_.reset(new SSLHostState());
569
570 DCHECK(ssl_host_state_->CalledOnValidThread());
571 return ssl_host_state_.get();
572}
573
initial.commit09911bf2008-07-26 23:55:29574PrefService* ProfileImpl::GetPrefs() {
575 if (!prefs_.get()) {
[email protected]b9636002009-03-04 00:05:25576 prefs_.reset(new PrefService(GetPrefFilePath()));
initial.commit09911bf2008-07-26 23:55:29577
578 // The Profile class and ProfileManager class may read some prefs so
579 // register known prefs as soon as possible.
580 Profile::RegisterUserPrefs(prefs_.get());
581 ProfileManager::RegisterUserPrefs(prefs_.get());
582
583 // The last session exited cleanly if there is no pref for
584 // kSessionExitedCleanly or the value for kSessionExitedCleanly is true.
585 last_session_exited_cleanly_ =
586 prefs_->GetBoolean(prefs::kSessionExitedCleanly);
587 // Mark the session as open.
588 prefs_->SetBoolean(prefs::kSessionExitedCleanly, false);
589 // Make sure we save to disk that the session has opened.
590 prefs_->ScheduleSavePersistentPrefs(g_browser_process->file_thread());
591 }
592
593 return prefs_.get();
594}
595
[email protected]f7011fcb2009-01-28 21:54:32596FilePath ProfileImpl::GetPrefFilePath() {
597 FilePath pref_file_path = path_;
598 pref_file_path = pref_file_path.Append(chrome::kPreferencesFilename);
initial.commit09911bf2008-07-26 23:55:29599 return pref_file_path;
600}
601
602URLRequestContext* ProfileImpl::GetRequestContext() {
603 if (!request_context_) {
[email protected]f7011fcb2009-01-28 21:54:32604 FilePath cookie_path = GetPath();
605 cookie_path = cookie_path.Append(chrome::kCookieFilename);
606 FilePath cache_path = GetPath();
[email protected]2b2830a2009-02-07 01:58:42607
608 // Override the cache location if specified by the user.
609 const std::wstring user_cache_dir(
610 CommandLine::ForCurrentProcess()->GetSwitchValue(
611 switches::kDiskCacheDir));
612 if (!user_cache_dir.empty()) {
613 cache_path = FilePath::FromWStringHack(user_cache_dir);
614 }
615
[email protected]f7011fcb2009-01-28 21:54:32616 cache_path = cache_path.Append(chrome::kCacheDirname);
[email protected]6ab9b202008-12-23 22:34:50617 request_context_ = ChromeURLRequestContext::CreateOriginal(
618 this, cookie_path, cache_path);
initial.commit09911bf2008-07-26 23:55:29619 request_context_->AddRef();
620
[email protected]6ab9b202008-12-23 22:34:50621 // The first request context is always a normal (non-OTR) request context.
622 // Even when Chromium is started in OTR mode, a normal profile is always
623 // created first.
624 if (!default_request_context_) {
625 default_request_context_ = request_context_;
626 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56627 NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE,
[email protected]6ab9b202008-12-23 22:34:50628 NotificationService::AllSources(), NotificationService::NoDetails());
629 }
630
initial.commit09911bf2008-07-26 23:55:29631 DCHECK(request_context_->cookie_store());
632 }
633
634 return request_context_;
635}
636
[email protected]e7f29642009-03-02 22:53:18637URLRequestContext* ProfileImpl::GetRequestContextForMedia() {
638 if (!media_request_context_) {
639 FilePath cache_path = GetPath();
[email protected]e3edeba2009-03-23 18:57:14640
641 // Override the cache location if specified by the user.
642 const std::wstring user_cache_dir(
643 CommandLine::ForCurrentProcess()->GetSwitchValue(
644 switches::kDiskCacheDir));
645 if (!user_cache_dir.empty()) {
646 cache_path = FilePath::FromWStringHack(user_cache_dir);
647 }
648
[email protected]4e7b5dbb2009-03-25 00:09:23649 cache_path = cache_path.Append(chrome::kMediaCacheDirname);
[email protected]e7f29642009-03-02 22:53:18650 media_request_context_ = ChromeURLRequestContext::CreateOriginalForMedia(
651 this, cache_path);
652 media_request_context_->AddRef();
653
654 DCHECK(media_request_context_->cookie_store());
655 }
656
657 return media_request_context_;
658}
659
initial.commit09911bf2008-07-26 23:55:29660HistoryService* ProfileImpl::GetHistoryService(ServiceAccessType sat) {
661 if (!history_service_created_) {
[email protected]90ef13132008-08-27 03:27:46662 history_service_created_ = true;
initial.commit09911bf2008-07-26 23:55:29663 scoped_refptr<HistoryService> history(new HistoryService(this));
[email protected]d8e41ed2008-09-11 15:22:32664 if (!history->Init(GetPath(), GetBookmarkModel()))
initial.commit09911bf2008-07-26 23:55:29665 return NULL;
666 history_service_.swap(history);
initial.commit09911bf2008-07-26 23:55:29667
668 // Send out the notification that the history service was created.
669 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56670 Notify(NotificationType::HISTORY_CREATED, Source<Profile>(this),
initial.commit09911bf2008-07-26 23:55:29671 Details<HistoryService>(history_service_.get()));
672 }
673 return history_service_.get();
674}
675
initial.commit09911bf2008-07-26 23:55:29676TemplateURLModel* ProfileImpl::GetTemplateURLModel() {
677 if (!template_url_model_.get())
678 template_url_model_.reset(new TemplateURLModel(this));
679 return template_url_model_.get();
680}
681
682TemplateURLFetcher* ProfileImpl::GetTemplateURLFetcher() {
683 if (!template_url_fetcher_.get())
684 template_url_fetcher_.reset(new TemplateURLFetcher(this));
685 return template_url_fetcher_.get();
686}
687
688WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) {
689 if (!created_web_data_service_)
690 CreateWebDataService();
691 return web_data_service_.get();
692}
693
694void ProfileImpl::CreateWebDataService() {
695 DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL);
696 created_web_data_service_ = true;
697 scoped_refptr<WebDataService> wds(new WebDataService());
698 if (!wds->Init(GetPath()))
699 return;
700 web_data_service_.swap(wds);
701}
702
703DownloadManager* ProfileImpl::GetDownloadManager() {
704 if (!created_download_manager_) {
705 scoped_refptr<DownloadManager> dlm(new DownloadManager);
706 dlm->Init(this);
707 created_download_manager_ = true;
708 download_manager_.swap(dlm);
709 }
710 return download_manager_.get();
711}
712
713bool ProfileImpl::HasCreatedDownloadManager() const {
714 return created_download_manager_;
715}
716
717SessionService* ProfileImpl::GetSessionService() {
718 if (!session_service_.get() && !shutdown_session_service_) {
719 session_service_ = new SessionService(this);
720 session_service_->ResetFromCurrentBrowsers();
721 }
722 return session_service_.get();
723}
724
725void ProfileImpl::ShutdownSessionService() {
726 if (shutdown_session_service_)
727 return;
728
729 // We're about to exit, force creation of the session service if it hasn't
730 // been created yet. We do this to ensure session state matches the point in
731 // time the user exited.
732 GetSessionService();
733 shutdown_session_service_ = true;
734 session_service_ = NULL;
735}
736
737bool ProfileImpl::HasSessionService() const {
738 return (session_service_.get() != NULL);
739}
740
741std::wstring ProfileImpl::GetName() {
742 return GetPrefs()->GetString(prefs::kProfileName);
743}
744void ProfileImpl::SetName(const std::wstring& name) {
745 GetPrefs()->SetString(prefs::kProfileName, name);
746}
747
748std::wstring ProfileImpl::GetID() {
749 return GetPrefs()->GetString(prefs::kProfileID);
750}
751void ProfileImpl::SetID(const std::wstring& id) {
752 GetPrefs()->SetString(prefs::kProfileID, id);
753}
754
initial.commit09911bf2008-07-26 23:55:29755bool ProfileImpl::DidLastSessionExitCleanly() {
756 // last_session_exited_cleanly_ is set when the preferences are loaded. Force
757 // it to be set by asking for the prefs.
758 GetPrefs();
759 return last_session_exited_cleanly_;
760}
761
[email protected]d8e41ed2008-09-11 15:22:32762BookmarkModel* ProfileImpl::GetBookmarkModel() {
[email protected]90ef13132008-08-27 03:27:46763 if (!bookmark_bar_model_.get()) {
[email protected]d8e41ed2008-09-11 15:22:32764 bookmark_bar_model_.reset(new BookmarkModel(this));
[email protected]90ef13132008-08-27 03:27:46765 bookmark_bar_model_->Load();
766 }
initial.commit09911bf2008-07-26 23:55:29767 return bookmark_bar_model_.get();
768}
769
770bool ProfileImpl::IsSameProfile(Profile* profile) {
771 if (profile == static_cast<Profile*>(this))
772 return true;
773 OffTheRecordProfileImpl* otr_profile = off_the_record_profile_.get();
774 return otr_profile && profile == static_cast<Profile*>(otr_profile);
775}
776
777Time ProfileImpl::GetStartTime() const {
778 return start_time_;
779}
780
781TabRestoreService* ProfileImpl::GetTabRestoreService() {
782 if (!tab_restore_service_.get())
[email protected]169627b2008-12-06 19:30:19783 tab_restore_service_ = new TabRestoreService(this);
initial.commit09911bf2008-07-26 23:55:29784 return tab_restore_service_.get();
785}
786
787void ProfileImpl::ResetTabRestoreService() {
[email protected]169627b2008-12-06 19:30:19788 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29789}
790
[email protected]a9afddb2009-02-12 17:49:42791// To be run in the IO thread to notify all resource message filters that the
[email protected]20930852008-10-15 19:30:41792// spellchecker has changed.
793class NotifySpellcheckerChangeTask : public Task {
794 public:
795 NotifySpellcheckerChangeTask(
[email protected]e7244d82008-10-29 18:13:26796 Profile* profile,
797 const SpellcheckerReinitializedDetails& spellchecker)
[email protected]20930852008-10-15 19:30:41798 : profile_(profile),
799 spellchecker_(spellchecker) {
800 }
801
802 private:
803 void Run(void) {
804 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56805 NotificationType::SPELLCHECKER_REINITIALIZED,
[email protected]20930852008-10-15 19:30:41806 Source<Profile>(profile_),
807 Details<SpellcheckerReinitializedDetails>(&spellchecker_));
808 }
809
810 Profile* profile_;
811 SpellcheckerReinitializedDetails spellchecker_;
812};
813
[email protected]e7244d82008-10-29 18:13:26814void ProfileImpl::InitializeSpellChecker(bool need_to_broadcast) {
[email protected]20930852008-10-15 19:30:41815 // The I/O thread may be NULL during testing.
816 base::Thread* io_thread = g_browser_process->io_thread();
817 if (spellchecker_) {
818 // The spellchecker must be deleted on the I/O thread.
819 // A dummy variable to aid in logical clarity.
820 SpellChecker* last_spellchecker = spellchecker_;
821
822 if (io_thread)
823 io_thread->message_loop()->ReleaseSoon(FROM_HERE, last_spellchecker);
824 else // during testing, we don't have an I/O thread
825 last_spellchecker->Release();
[email protected]20930852008-10-15 19:30:41826 }
827
[email protected]20930852008-10-15 19:30:41828 // Retrieve the (perhaps updated recently) dictionary name from preferences.
829 PrefService* prefs = GetPrefs();
[email protected]e7244d82008-10-29 18:13:26830 bool enable_spellcheck = prefs->GetBoolean(prefs::kEnableSpellCheck);
[email protected]20930852008-10-15 19:30:41831
[email protected]e7244d82008-10-29 18:13:26832 if (enable_spellcheck) {
[email protected]bd17b702009-02-25 20:44:08833 FilePath dict_dir;
[email protected]e7244d82008-10-29 18:13:26834 PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
[email protected]a9afddb2009-02-12 17:49:42835 // Note that, as the object pointed to by previously by spellchecker_
[email protected]e7244d82008-10-29 18:13:26836 // is being deleted in the io thread, the spellchecker_ can be made to point
837 // to a new object (RE-initialized) in parallel in this UI thread.
[email protected]4b4d1adc2008-12-10 22:28:58838 spellchecker_ = new SpellChecker(dict_dir,
[email protected]74c8b422009-03-11 00:34:12839 WideToASCII(prefs->GetString(prefs::kSpellCheckDictionary)),
840 GetRequestContext(),
[email protected]bd17b702009-02-25 20:44:08841 FilePath());
[email protected]e7244d82008-10-29 18:13:26842 spellchecker_->AddRef(); // Manual refcounting.
843 } else {
844 spellchecker_ = NULL;
845 }
[email protected]20930852008-10-15 19:30:41846
847 if (need_to_broadcast && io_thread) { // Notify resource message filters.
848 SpellcheckerReinitializedDetails scoped_spellchecker;
849 scoped_spellchecker.spellchecker = spellchecker_;
[email protected]e7244d82008-10-29 18:13:26850 if (io_thread) {
851 io_thread->message_loop()->PostTask(
[email protected]a9afddb2009-02-12 17:49:42852 FROM_HERE,
[email protected]e7244d82008-10-29 18:13:26853 new NotifySpellcheckerChangeTask(this, scoped_spellchecker));
854 }
[email protected]20930852008-10-15 19:30:41855 }
856}
857
[email protected]e7244d82008-10-29 18:13:26858void ProfileImpl::ReinitializeSpellChecker() {
859 InitializeSpellChecker(true);
860}
861
initial.commit09911bf2008-07-26 23:55:29862SpellChecker* ProfileImpl::GetSpellChecker() {
863 if (!spellchecker_) {
[email protected]20930852008-10-15 19:30:41864 // This is where spellchecker gets initialized. Note that this is being
865 // initialized in the ui_thread. However, this is not a problem as long as
866 // it is *used* in the io thread.
[email protected]34cc84f2009-02-13 10:04:35867 // TODO(sidchat): One day, change everything so that spellchecker gets
[email protected]20930852008-10-15 19:30:41868 // initialized in the IO thread itself.
[email protected]e7244d82008-10-29 18:13:26869 InitializeSpellChecker(false);
initial.commit09911bf2008-07-26 23:55:29870 }
[email protected]20930852008-10-15 19:30:41871
initial.commit09911bf2008-07-26 23:55:29872 return spellchecker_;
873}
874
875void ProfileImpl::MarkAsCleanShutdown() {
876 if (prefs_.get()) {
877 // The session cleanly exited, set kSessionExitedCleanly appropriately.
878 prefs_->SetBoolean(prefs::kSessionExitedCleanly, true);
879
880 // NOTE: If you change what thread this writes on, be sure and update
881 // ChromeFrame::EndSession().
882 prefs_->SavePersistentPrefs(g_browser_process->file_thread());
883 }
884}
885
[email protected]e7244d82008-10-29 18:13:26886void ProfileImpl::Observe(NotificationType type,
887 const NotificationSource& source,
888 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56889 if (NotificationType::PREF_CHANGED == type) {
[email protected]e7244d82008-10-29 18:13:26890 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
891 PrefService* prefs = Source<PrefService>(source).ptr();
892 DCHECK(pref_name_in && prefs);
893 if (*pref_name_in == prefs::kSpellCheckDictionary ||
894 *pref_name_in == prefs::kEnableSpellCheck) {
895 InitializeSpellChecker(true);
896 }
897 }
898}
899
initial.commit09911bf2008-07-26 23:55:29900void ProfileImpl::StopCreateSessionServiceTimer() {
[email protected]2d316662008-09-03 18:18:14901 create_session_service_timer_.Stop();
initial.commit09911bf2008-07-26 23:55:29902}
[email protected]3a453fa2008-08-15 18:46:34903
904#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07905ProfilePersonalization* ProfileImpl::GetProfilePersonalization() {
[email protected]57d3d0a2008-09-24 00:50:07906 return personalization_.get();
[email protected]3a453fa2008-08-15 18:46:34907}
license.botbf09a502008-08-24 00:55:55908#endif