blob: 5f6bb1e1536350fff11041f50dd6efb442d2a217 [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]f63ae312009-02-04 17:58:4624#include "chrome/browser/search_engines/template_url_model.h"
[email protected]34cc84f2009-02-13 10:04:3525#include "chrome/browser/ssl/ssl_host_state.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"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/browser/visitedlink_master.h"
30#include "chrome/browser/webdata/web_data_service.h"
31#include "chrome/common/chrome_constants.h"
32#include "chrome/common/chrome_paths.h"
33#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2934#include "chrome/common/notification_service.h"
35#include "chrome/common/pref_names.h"
[email protected]f7011fcb2009-01-28 21:54:3236#include "chrome/common/render_messages.h"
[email protected]f90f5c512009-02-18 19:10:5837#include "grit/locale_settings.h"
initial.commit09911bf2008-07-26 23:55:2938
[email protected]f7011fcb2009-01-28 21:54:3239#if defined(OS_POSIX)
40// TODO(port): get rid of this include. It's used just to provide declarations
41// and stub definitions for classes we encouter during the porting effort.
42#include "chrome/common/temp_scaffolding_stubs.h"
43#endif
44
45// TODO(port): Get rid of this section and finish porting.
46#if defined(OS_WIN)
[email protected]f7011fcb2009-01-28 21:54:3247#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]f7011fcb2009-01-28 21:54:3248#endif
49
[email protected]e1acf6f2008-10-27 20:43:3350using base::Time;
51using base::TimeDelta;
52
initial.commit09911bf2008-07-26 23:55:2953// Delay, in milliseconds, before we explicitly create the SessionService.
54static const int kCreateSessionServiceDelayMS = 500;
55
56// A pointer to the request context for the default profile. See comments on
57// Profile::GetDefaultRequestContext.
58URLRequestContext* Profile::default_request_context_;
59
[email protected]34cc84f2009-02-13 10:04:3560// static
initial.commit09911bf2008-07-26 23:55:2961void Profile::RegisterUserPrefs(PrefService* prefs) {
[email protected]430d3f72008-10-27 17:56:5562 prefs->RegisterBooleanPref(prefs::kSearchSuggestEnabled, true);
initial.commit09911bf2008-07-26 23:55:2963 prefs->RegisterBooleanPref(prefs::kSessionExitedCleanly, true);
64 prefs->RegisterBooleanPref(prefs::kSafeBrowsingEnabled, true);
[email protected]74c8b422009-03-11 00:34:1265 // TODO(estade): IDS_SPELLCHECK_DICTIONARY should be an ASCII string.
[email protected]f7011fcb2009-01-28 21:54:3266#if defined(OS_MACOSX)
67 // MASSIVE HACK!!! We don't have localization working yet. Undo this once we
68 // do. TODO(port): take this out
69 prefs->RegisterStringPref(prefs::kSpellCheckDictionary,
70 L"IDS_SPELLCHECK_DICTIONARY");
71#elif defined(OS_WIN) || defined(OS_LINUX)
[email protected]e7244d82008-10-29 18:13:2672 prefs->RegisterLocalizedStringPref(prefs::kSpellCheckDictionary,
73 IDS_SPELLCHECK_DICTIONARY);
[email protected]f7011fcb2009-01-28 21:54:3274#endif
[email protected]e7244d82008-10-29 18:13:2675 prefs->RegisterBooleanPref(prefs::kEnableSpellCheck, true);
[email protected]f93fe782009-02-19 01:26:1376 prefs->RegisterBooleanPref(prefs::kEnableUserScripts, false);
77 prefs->RegisterBooleanPref(prefs::kEnableExtensions, false);
initial.commit09911bf2008-07-26 23:55:2978}
79
[email protected]34cc84f2009-02-13 10:04:3580// static
[email protected]f7011fcb2009-01-28 21:54:3281Profile* Profile::CreateProfile(const FilePath& path) {
initial.commit09911bf2008-07-26 23:55:2982 return new ProfileImpl(path);
83}
84
[email protected]34cc84f2009-02-13 10:04:3585// static
initial.commit09911bf2008-07-26 23:55:2986URLRequestContext* Profile::GetDefaultRequestContext() {
87 return default_request_context_;
88}
89
90
initial.commit09911bf2008-07-26 23:55:2991////////////////////////////////////////////////////////////////////////////////
92//
93// OffTheRecordProfileImpl is a profile subclass that wraps an existing profile
94// to make it suitable for the off the record mode.
95//
96////////////////////////////////////////////////////////////////////////////////
97class OffTheRecordProfileImpl : public Profile,
98 public NotificationObserver {
99 public:
100 explicit OffTheRecordProfileImpl(Profile* real_profile)
101 : profile_(real_profile),
[email protected]363347b2009-03-13 20:06:57102 media_request_context_(NULL),
initial.commit09911bf2008-07-26 23:55:29103 start_time_(Time::Now()) {
[email protected]6ab9b202008-12-23 22:34:50104 request_context_ = ChromeURLRequestContext::CreateOffTheRecord(this);
initial.commit09911bf2008-07-26 23:55:29105 request_context_->AddRef();
106 // Register for browser close notifications so we can detect when the last
107 // off-the-record window is closed, in which case we can clean our states
108 // (cookies, downloads...).
109 NotificationService::current()->AddObserver(
[email protected]bfd04a62009-02-01 18:16:56110 this,
111 NotificationType::BROWSER_CLOSED,
112 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29113 }
114
115 virtual ~OffTheRecordProfileImpl() {
116 if (request_context_) {
[email protected]6ab9b202008-12-23 22:34:50117 request_context_->CleanupOnUIThread();
118
initial.commit09911bf2008-07-26 23:55:29119 // Clean up request context on IO thread.
120 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]6ab9b202008-12-23 22:34:50121 NewRunnableMethod(request_context_,
122 &base::RefCountedThreadSafe<URLRequestContext>::Release));
initial.commit09911bf2008-07-26 23:55:29123 request_context_ = NULL;
124 }
125 NotificationService::current()->RemoveObserver(
[email protected]bfd04a62009-02-01 18:16:56126 this,
127 NotificationType::BROWSER_CLOSED,
128 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29129 }
130
[email protected]f7011fcb2009-01-28 21:54:32131 virtual FilePath GetPath() { return profile_->GetPath(); }
initial.commit09911bf2008-07-26 23:55:29132
133 virtual bool IsOffTheRecord() {
134 return true;
135 }
136
137 virtual Profile* GetOffTheRecordProfile() {
138 return this;
139 }
140
[email protected]860f55492009-03-27 19:50:59141 virtual void DestroyOffTheRecordProfile() {
142 // Suicide is bad!
143 NOTREACHED();
144 }
145
initial.commit09911bf2008-07-26 23:55:29146 virtual Profile* GetOriginalProfile() {
147 return profile_;
148 }
149
150 virtual VisitedLinkMaster* GetVisitedLinkMaster() {
[email protected]7fb6c862009-03-13 02:51:49151 // We don't provide access to the VisitedLinkMaster when we're OffTheRecord
152 // because we don't want to leak the sites that the user has visited before.
153 return NULL;
initial.commit09911bf2008-07-26 23:55:29154 }
155
[email protected]6014d672008-12-05 00:38:25156 virtual ExtensionsService* GetExtensionsService() {
157 return profile_->GetExtensionsService();
158 }
159
[email protected]0938d3c2009-01-09 20:37:35160 virtual UserScriptMaster* GetUserScriptMaster() {
161 return profile_->GetUserScriptMaster();
[email protected]04fba9a92008-10-28 17:25:25162 }
163
[email protected]34cc84f2009-02-13 10:04:35164 virtual SSLHostState* GetSSLHostState() {
165 if (!ssl_host_state_.get())
166 ssl_host_state_.reset(new SSLHostState());
167
168 DCHECK(ssl_host_state_->CalledOnValidThread());
169 return ssl_host_state_.get();
170 }
171
initial.commit09911bf2008-07-26 23:55:29172 virtual HistoryService* GetHistoryService(ServiceAccessType sat) {
173 if (sat == EXPLICIT_ACCESS) {
174 return profile_->GetHistoryService(sat);
175 } else {
176 NOTREACHED() << "This profile is OffTheRecord";
177 return NULL;
178 }
179 }
180
initial.commit09911bf2008-07-26 23:55:29181 virtual WebDataService* GetWebDataService(ServiceAccessType sat) {
182 if (sat == EXPLICIT_ACCESS) {
183 return profile_->GetWebDataService(sat);
184 } else {
185 NOTREACHED() << "This profile is OffTheRecord";
186 return NULL;
187 }
188 }
189
190 virtual PrefService* GetPrefs() {
191 return profile_->GetPrefs();
192 }
193
194 virtual TemplateURLModel* GetTemplateURLModel() {
195 return profile_->GetTemplateURLModel();
196 }
197
198 virtual TemplateURLFetcher* GetTemplateURLFetcher() {
199 return profile_->GetTemplateURLFetcher();
200 }
201
202 virtual DownloadManager* GetDownloadManager() {
203 if (!download_manager_.get()) {
204 scoped_refptr<DownloadManager> dlm(new DownloadManager);
205 dlm->Init(this);
206 download_manager_.swap(dlm);
207 }
208 return download_manager_.get();
209 }
210
211 virtual bool HasCreatedDownloadManager() const {
212 return (download_manager_.get() != NULL);
213 }
214
215 virtual URLRequestContext* GetRequestContext() {
216 return request_context_;
217 }
218
[email protected]e7f29642009-03-02 22:53:18219 virtual URLRequestContext* GetRequestContextForMedia() {
220 if (!media_request_context_) {
221 FilePath cache_path = GetPath();
[email protected]e3edeba2009-03-23 18:57:14222
223 // Override the cache location if specified by the user.
224 const std::wstring user_cache_dir(
225 CommandLine::ForCurrentProcess()->GetSwitchValue(
226 switches::kDiskCacheDir));
227 if (!user_cache_dir.empty()) {
228 cache_path = FilePath::FromWStringHack(user_cache_dir);
229 }
230
[email protected]4e7b5dbb2009-03-25 00:09:23231 cache_path = cache_path.Append(chrome::kOffTheRecordMediaCacheDirname);
[email protected]e7f29642009-03-02 22:53:18232 media_request_context_ =
233 ChromeURLRequestContext::CreateOffTheRecordForMedia(
234 this, cache_path);
235 media_request_context_->AddRef();
236
237 DCHECK(media_request_context_->cookie_store());
238 }
239 return media_request_context_;
240 }
241
initial.commit09911bf2008-07-26 23:55:29242 virtual SessionService* GetSessionService() {
243 // Don't save any sessions when off the record.
244 return NULL;
245 }
246
247 virtual void ShutdownSessionService() {
248 // We don't allow a session service, nothing to do.
249 }
250
251 virtual bool HasSessionService() const {
252 // We never have a session service.
253 return false;
254 }
255
256 virtual std::wstring GetName() {
257 return profile_->GetName();
258 }
259
260 virtual void SetName(const std::wstring& name) {
261 profile_->SetName(name);
262 }
263
264 virtual std::wstring GetID() {
265 return profile_->GetID();
266 }
267
268 virtual void SetID(const std::wstring& id) {
269 profile_->SetID(id);
270 }
271
initial.commit09911bf2008-07-26 23:55:29272 virtual bool DidLastSessionExitCleanly() {
273 return profile_->DidLastSessionExitCleanly();
274 }
275
[email protected]d8e41ed2008-09-11 15:22:32276 virtual BookmarkModel* GetBookmarkModel() {
277 return profile_->GetBookmarkModel();
initial.commit09911bf2008-07-26 23:55:29278 }
279
[email protected]3a453fa2008-08-15 18:46:34280#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07281 virtual ProfilePersonalization* GetProfilePersonalization() {
[email protected]3a453fa2008-08-15 18:46:34282 return profile_->GetProfilePersonalization();
283 }
284#endif
285
initial.commit09911bf2008-07-26 23:55:29286 virtual bool IsSameProfile(Profile* profile) {
287 if (profile == static_cast<Profile*>(this))
288 return true;
289 return profile == profile_;
290 }
291
292 virtual Time GetStartTime() const {
293 return start_time_;
294 }
295
296 virtual TabRestoreService* GetTabRestoreService() {
297 return NULL;
298 }
299
[email protected]e7244d82008-10-29 18:13:26300 virtual void ResetTabRestoreService() {
[email protected]20930852008-10-15 19:30:41301 }
302
[email protected]e7244d82008-10-29 18:13:26303 virtual void ReinitializeSpellChecker() {
304 profile_->ReinitializeSpellChecker();
initial.commit09911bf2008-07-26 23:55:29305 }
306
307 virtual SpellChecker* GetSpellChecker() {
308 return profile_->GetSpellChecker();
309 }
310
311 virtual void MarkAsCleanShutdown() {
312 }
313
[email protected]bdbc87c2009-01-25 05:08:54314 virtual void InitExtensions() {
315 NOTREACHED();
316 }
317
initial.commit09911bf2008-07-26 23:55:29318 virtual void ExitedOffTheRecordMode() {
319 // Drop our download manager so we forget about all the downloads made
320 // in off-the-record mode.
321 download_manager_ = NULL;
322 }
323
324 virtual void Observe(NotificationType type,
325 const NotificationSource& source,
326 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56327 DCHECK_EQ(NotificationType::BROWSER_CLOSED, type.value);
initial.commit09911bf2008-07-26 23:55:29328 // We are only interested in OTR browser closing.
329 if (Source<Browser>(source)->profile() != this)
330 return;
331
332 // Let's check if we still have an Off The Record window opened.
333 // Note that we check against 1 as this notification is sent before the
334 // browser window is actually removed from the list.
335 if (BrowserList::GetBrowserCount(this) <= 1)
336 ExitedOffTheRecordMode();
337 }
338
339 private:
340 // The real underlying profile.
341 Profile* profile_;
342
[email protected]6ab9b202008-12-23 22:34:50343 // The context to use for requests made from this OTR session.
344 ChromeURLRequestContext* request_context_;
initial.commit09911bf2008-07-26 23:55:29345
[email protected]e7f29642009-03-02 22:53:18346 // The context for requests for media resources.
347 ChromeURLRequestContext* media_request_context_;
348
initial.commit09911bf2008-07-26 23:55:29349 // The download manager that only stores downloaded items in memory.
350 scoped_refptr<DownloadManager> download_manager_;
351
[email protected]34cc84f2009-02-13 10:04:35352 // We don't want SSLHostState from the OTR profile to leak back to the main
353 // profile because then the main profile would learn some of the host names
354 // the user visited while OTR.
355 scoped_ptr<SSLHostState> ssl_host_state_;
356
initial.commit09911bf2008-07-26 23:55:29357 // Time we were started.
358 Time start_time_;
359
360 DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl);
361};
362
[email protected]f7011fcb2009-01-28 21:54:32363ProfileImpl::ProfileImpl(const FilePath& path)
initial.commit09911bf2008-07-26 23:55:29364 : path_(path),
365 off_the_record_(false),
[email protected]3a453fa2008-08-15 18:46:34366#ifdef CHROME_PERSONALIZATION
367 personalization_(NULL),
368#endif
[email protected]f7011fcb2009-01-28 21:54:32369 request_context_(NULL),
[email protected]363347b2009-03-13 20:06:57370 media_request_context_(NULL),
[email protected]f7011fcb2009-01-28 21:54:32371 history_service_created_(false),
372 created_web_data_service_(false),
373 created_download_manager_(false),
374 start_time_(Time::Now()),
375 spellchecker_(NULL),
initial.commit09911bf2008-07-26 23:55:29376 shutdown_session_service_(false) {
377 DCHECK(!path.empty()) << "Using an empty path will attempt to write " <<
378 "profile files to the root directory!";
[email protected]2d316662008-09-03 18:18:14379 create_session_service_timer_.Start(
380 TimeDelta::FromMilliseconds(kCreateSessionServiceDelayMS), this,
381 &ProfileImpl::EnsureSessionServiceCreated);
[email protected]bdbc87c2009-01-25 05:08:54382
[email protected]e7244d82008-10-29 18:13:26383 PrefService* prefs = GetPrefs();
384 prefs->AddPrefObserver(prefs::kSpellCheckDictionary, this);
385 prefs->AddPrefObserver(prefs::kEnableSpellCheck, this);
initial.commit09911bf2008-07-26 23:55:29386}
387
[email protected]bdbc87c2009-01-25 05:08:54388void ProfileImpl::InitExtensions() {
[email protected]8c756ac2009-01-30 23:36:41389 if (user_script_master_ || extensions_service_)
390 return; // Already initialized.
391
[email protected]bdbc87c2009-01-25 05:08:54392 const CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]f93fe782009-02-19 01:26:13393 PrefService* prefs = GetPrefs();
[email protected]bdbc87c2009-01-25 05:08:54394 bool user_scripts_enabled =
[email protected]f0a51fb52009-03-05 12:46:38395 command_line->HasSwitch(switches::kEnableUserScripts) ||
[email protected]f93fe782009-02-19 01:26:13396 prefs->GetBoolean(prefs::kEnableUserScripts);
[email protected]a9afddb2009-02-12 17:49:42397 bool extensions_enabled =
[email protected]f0a51fb52009-03-05 12:46:38398 command_line->HasSwitch(switches::kEnableExtensions) ||
[email protected]f93fe782009-02-19 01:26:13399 prefs->GetBoolean(prefs::kEnableExtensions);
[email protected]bdbc87c2009-01-25 05:08:54400
[email protected]f7011fcb2009-01-28 21:54:32401 FilePath script_dir;
[email protected]bdbc87c2009-01-25 05:08:54402 if (user_scripts_enabled) {
[email protected]0cd957b2009-03-06 20:13:23403 if (command_line->HasSwitch(switches::kUserScriptsDir)) {
404 std::wstring path_string =
405 command_line->GetSwitchValue(switches::kUserScriptsDir);
406 script_dir = FilePath::FromWStringHack(path_string);
407 } else {
408 script_dir = GetPath();
409 script_dir = script_dir.Append(chrome::kUserScriptsDirname);
410 }
[email protected]bdbc87c2009-01-25 05:08:54411 }
412
[email protected]bb28e062009-02-27 17:19:18413 ExtensionErrorReporter::Init(true); // allow noisy errors.
[email protected]bdbc87c2009-01-25 05:08:54414 user_script_master_ = new UserScriptMaster(
[email protected]f7011fcb2009-01-28 21:54:32415 g_browser_process->file_thread()->message_loop(), script_dir);
[email protected]81e63782009-02-27 19:35:09416 extensions_service_ = new ExtensionsService(this, user_script_master_.get());
[email protected]bdbc87c2009-01-25 05:08:54417
418 // If we have extensions, the extension service will kick off the first scan
419 // after extensions are loaded. Otherwise, we need to do that now.
420 if (extensions_enabled)
421 extensions_service_->Init();
422 else if (user_scripts_enabled)
423 user_script_master_->StartScan();
424}
425
initial.commit09911bf2008-07-26 23:55:29426ProfileImpl::~ProfileImpl() {
[email protected]169627b2008-12-06 19:30:19427 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29428
429 StopCreateSessionServiceTimer();
430 // TemplateURLModel schedules a task on the WebDataService from its
431 // destructor. Delete it first to ensure the task gets scheduled before we
432 // shut down the database.
433 template_url_model_.reset();
434
435 // The download manager queries the history system and should be deleted
436 // before the history is shutdown so it can properly cancel all requests.
437 download_manager_ = NULL;
438
[email protected]e7244d82008-10-29 18:13:26439 // Remove pref observers.
440 PrefService* prefs = GetPrefs();
441 prefs->RemovePrefObserver(prefs::kSpellCheckDictionary, this);
442 prefs->RemovePrefObserver(prefs::kEnableSpellCheck, this);
443
[email protected]3a453fa2008-08-15 18:46:34444#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07445 personalization_.reset();
[email protected]3a453fa2008-08-15 18:46:34446#endif
447
initial.commit09911bf2008-07-26 23:55:29448 // Both HistoryService and WebDataService maintain threads for background
449 // processing. Its possible each thread still has tasks on it that have
450 // increased the ref count of the service. In such a situation, when we
451 // decrement the refcount, it won't be 0, and the threads/databases aren't
452 // properly shut down. By explicitly calling Cleanup/Shutdown we ensure the
453 // databases are properly closed.
454 if (web_data_service_.get())
455 web_data_service_->Shutdown();
456
457 if (history_service_.get())
458 history_service_->Cleanup();
459
460 // The I/O thread may be NULL during testing.
[email protected]ab820df2008-08-26 05:55:10461 base::Thread* io_thread = g_browser_process->io_thread();
initial.commit09911bf2008-07-26 23:55:29462
463 if (spellchecker_) {
464 // The spellchecker must be deleted on the I/O thread. During testing, we
465 // don't have an I/O thread.
466 if (io_thread)
467 io_thread->message_loop()->ReleaseSoon(FROM_HERE, spellchecker_);
468 else
469 spellchecker_->Release();
470 }
471
472 if (request_context_) {
[email protected]6ab9b202008-12-23 22:34:50473 request_context_->CleanupOnUIThread();
474
475 if (default_request_context_ == request_context_)
476 default_request_context_ = NULL;
477
initial.commit09911bf2008-07-26 23:55:29478 // Clean up request context on IO thread.
[email protected]6ab9b202008-12-23 22:34:50479 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
480 NewRunnableMethod(request_context_,
481 &base::RefCountedThreadSafe<URLRequestContext>::Release));
initial.commit09911bf2008-07-26 23:55:29482 request_context_ = NULL;
483 }
484
[email protected]1e744f22009-04-08 01:00:17485 if (media_request_context_) {
486 media_request_context_->CleanupOnUIThread();
487
488 // Clean up request context on IO thread.
489 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
490 NewRunnableMethod(media_request_context_,
491 &base::RefCountedThreadSafe<URLRequestContext>::Release));
492 media_request_context_ = NULL;
493 }
494
[email protected]d8e41ed2008-09-11 15:22:32495 // HistoryService may call into the BookmarkModel, as such we need to
496 // delete HistoryService before the BookmarkModel. The destructor for
[email protected]90ef13132008-08-27 03:27:46497 // HistoryService will join with HistoryService's backend thread so that
498 // by the time the destructor has finished we're sure it will no longer call
[email protected]d8e41ed2008-09-11 15:22:32499 // into the BookmarkModel.
[email protected]90ef13132008-08-27 03:27:46500 history_service_ = NULL;
501 bookmark_bar_model_.reset();
502
initial.commit09911bf2008-07-26 23:55:29503 MarkAsCleanShutdown();
504}
505
[email protected]f7011fcb2009-01-28 21:54:32506FilePath ProfileImpl::GetPath() {
initial.commit09911bf2008-07-26 23:55:29507 return path_;
508}
509
510bool ProfileImpl::IsOffTheRecord() {
511 return false;
512}
513
514Profile* ProfileImpl::GetOffTheRecordProfile() {
515 if (!off_the_record_profile_.get()) {
516 scoped_ptr<OffTheRecordProfileImpl> p(new OffTheRecordProfileImpl(this));
517 off_the_record_profile_.swap(p);
518 }
519 return off_the_record_profile_.get();
520}
521
[email protected]860f55492009-03-27 19:50:59522void ProfileImpl::DestroyOffTheRecordProfile() {
523 off_the_record_profile_.reset();
524}
525
initial.commit09911bf2008-07-26 23:55:29526Profile* ProfileImpl::GetOriginalProfile() {
527 return this;
528}
529
[email protected]176aa482008-11-14 03:25:15530static void BroadcastNewHistoryTable(base::SharedMemory* table_memory) {
initial.commit09911bf2008-07-26 23:55:29531 if (!table_memory)
532 return;
533
534 // send to all RenderProcessHosts
535 for (RenderProcessHost::iterator i = RenderProcessHost::begin();
536 i != RenderProcessHost::end(); i++) {
537 if (!i->second->channel())
538 continue;
539
[email protected]176aa482008-11-14 03:25:15540 base::SharedMemoryHandle new_table;
[email protected]f7011fcb2009-01-28 21:54:32541 base::ProcessHandle process = i->second->process().handle();
initial.commit09911bf2008-07-26 23:55:29542 if (!process) {
543 // process can be null if it's started with the --single-process flag.
[email protected]f7011fcb2009-01-28 21:54:32544 process = base::Process::Current().handle();
initial.commit09911bf2008-07-26 23:55:29545 }
546
547 table_memory->ShareToProcess(process, &new_table);
548 IPC::Message* msg = new ViewMsg_VisitedLink_NewTable(new_table);
549 i->second->channel()->Send(msg);
550 }
551}
552
553VisitedLinkMaster* ProfileImpl::GetVisitedLinkMaster() {
554 if (!visited_link_master_.get()) {
555 scoped_ptr<VisitedLinkMaster> visited_links(
556 new VisitedLinkMaster(g_browser_process->file_thread(),
557 BroadcastNewHistoryTable, this));
558 if (!visited_links->Init())
559 return NULL;
560 visited_link_master_.swap(visited_links);
561 }
562
563 return visited_link_master_.get();
564}
565
[email protected]6014d672008-12-05 00:38:25566ExtensionsService* ProfileImpl::GetExtensionsService() {
567 return extensions_service_.get();
568}
569
[email protected]0938d3c2009-01-09 20:37:35570UserScriptMaster* ProfileImpl::GetUserScriptMaster() {
[email protected]0938d3c2009-01-09 20:37:35571 return user_script_master_.get();
[email protected]04fba9a92008-10-28 17:25:25572}
573
[email protected]34cc84f2009-02-13 10:04:35574SSLHostState* ProfileImpl::GetSSLHostState() {
575 if (!ssl_host_state_.get())
576 ssl_host_state_.reset(new SSLHostState());
577
578 DCHECK(ssl_host_state_->CalledOnValidThread());
579 return ssl_host_state_.get();
580}
581
initial.commit09911bf2008-07-26 23:55:29582PrefService* ProfileImpl::GetPrefs() {
583 if (!prefs_.get()) {
[email protected]b9636002009-03-04 00:05:25584 prefs_.reset(new PrefService(GetPrefFilePath()));
initial.commit09911bf2008-07-26 23:55:29585
586 // The Profile class and ProfileManager class may read some prefs so
587 // register known prefs as soon as possible.
588 Profile::RegisterUserPrefs(prefs_.get());
589 ProfileManager::RegisterUserPrefs(prefs_.get());
590
591 // The last session exited cleanly if there is no pref for
592 // kSessionExitedCleanly or the value for kSessionExitedCleanly is true.
593 last_session_exited_cleanly_ =
594 prefs_->GetBoolean(prefs::kSessionExitedCleanly);
595 // Mark the session as open.
596 prefs_->SetBoolean(prefs::kSessionExitedCleanly, false);
597 // Make sure we save to disk that the session has opened.
598 prefs_->ScheduleSavePersistentPrefs(g_browser_process->file_thread());
599 }
600
601 return prefs_.get();
602}
603
[email protected]f7011fcb2009-01-28 21:54:32604FilePath ProfileImpl::GetPrefFilePath() {
605 FilePath pref_file_path = path_;
606 pref_file_path = pref_file_path.Append(chrome::kPreferencesFilename);
initial.commit09911bf2008-07-26 23:55:29607 return pref_file_path;
608}
609
610URLRequestContext* ProfileImpl::GetRequestContext() {
611 if (!request_context_) {
[email protected]f7011fcb2009-01-28 21:54:32612 FilePath cookie_path = GetPath();
613 cookie_path = cookie_path.Append(chrome::kCookieFilename);
614 FilePath cache_path = GetPath();
[email protected]2b2830a2009-02-07 01:58:42615
616 // Override the cache location if specified by the user.
617 const std::wstring user_cache_dir(
618 CommandLine::ForCurrentProcess()->GetSwitchValue(
619 switches::kDiskCacheDir));
620 if (!user_cache_dir.empty()) {
621 cache_path = FilePath::FromWStringHack(user_cache_dir);
622 }
623
[email protected]f7011fcb2009-01-28 21:54:32624 cache_path = cache_path.Append(chrome::kCacheDirname);
[email protected]6ab9b202008-12-23 22:34:50625 request_context_ = ChromeURLRequestContext::CreateOriginal(
626 this, cookie_path, cache_path);
initial.commit09911bf2008-07-26 23:55:29627 request_context_->AddRef();
628
[email protected]6ab9b202008-12-23 22:34:50629 // The first request context is always a normal (non-OTR) request context.
630 // Even when Chromium is started in OTR mode, a normal profile is always
631 // created first.
632 if (!default_request_context_) {
633 default_request_context_ = request_context_;
634 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56635 NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE,
[email protected]6ab9b202008-12-23 22:34:50636 NotificationService::AllSources(), NotificationService::NoDetails());
637 }
638
initial.commit09911bf2008-07-26 23:55:29639 DCHECK(request_context_->cookie_store());
640 }
641
642 return request_context_;
643}
644
[email protected]e7f29642009-03-02 22:53:18645URLRequestContext* ProfileImpl::GetRequestContextForMedia() {
646 if (!media_request_context_) {
647 FilePath cache_path = GetPath();
[email protected]e3edeba2009-03-23 18:57:14648
649 // Override the cache location if specified by the user.
650 const std::wstring user_cache_dir(
651 CommandLine::ForCurrentProcess()->GetSwitchValue(
652 switches::kDiskCacheDir));
653 if (!user_cache_dir.empty()) {
654 cache_path = FilePath::FromWStringHack(user_cache_dir);
655 }
656
[email protected]4e7b5dbb2009-03-25 00:09:23657 cache_path = cache_path.Append(chrome::kMediaCacheDirname);
[email protected]e7f29642009-03-02 22:53:18658 media_request_context_ = ChromeURLRequestContext::CreateOriginalForMedia(
659 this, cache_path);
660 media_request_context_->AddRef();
661
662 DCHECK(media_request_context_->cookie_store());
663 }
664
665 return media_request_context_;
666}
667
initial.commit09911bf2008-07-26 23:55:29668HistoryService* ProfileImpl::GetHistoryService(ServiceAccessType sat) {
669 if (!history_service_created_) {
[email protected]90ef13132008-08-27 03:27:46670 history_service_created_ = true;
initial.commit09911bf2008-07-26 23:55:29671 scoped_refptr<HistoryService> history(new HistoryService(this));
[email protected]d8e41ed2008-09-11 15:22:32672 if (!history->Init(GetPath(), GetBookmarkModel()))
initial.commit09911bf2008-07-26 23:55:29673 return NULL;
674 history_service_.swap(history);
initial.commit09911bf2008-07-26 23:55:29675
676 // Send out the notification that the history service was created.
677 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56678 Notify(NotificationType::HISTORY_CREATED, Source<Profile>(this),
initial.commit09911bf2008-07-26 23:55:29679 Details<HistoryService>(history_service_.get()));
680 }
681 return history_service_.get();
682}
683
initial.commit09911bf2008-07-26 23:55:29684TemplateURLModel* ProfileImpl::GetTemplateURLModel() {
685 if (!template_url_model_.get())
686 template_url_model_.reset(new TemplateURLModel(this));
687 return template_url_model_.get();
688}
689
690TemplateURLFetcher* ProfileImpl::GetTemplateURLFetcher() {
691 if (!template_url_fetcher_.get())
692 template_url_fetcher_.reset(new TemplateURLFetcher(this));
693 return template_url_fetcher_.get();
694}
695
696WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) {
697 if (!created_web_data_service_)
698 CreateWebDataService();
699 return web_data_service_.get();
700}
701
702void ProfileImpl::CreateWebDataService() {
703 DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL);
704 created_web_data_service_ = true;
705 scoped_refptr<WebDataService> wds(new WebDataService());
706 if (!wds->Init(GetPath()))
707 return;
708 web_data_service_.swap(wds);
709}
710
711DownloadManager* ProfileImpl::GetDownloadManager() {
712 if (!created_download_manager_) {
713 scoped_refptr<DownloadManager> dlm(new DownloadManager);
714 dlm->Init(this);
715 created_download_manager_ = true;
716 download_manager_.swap(dlm);
717 }
718 return download_manager_.get();
719}
720
721bool ProfileImpl::HasCreatedDownloadManager() const {
722 return created_download_manager_;
723}
724
725SessionService* ProfileImpl::GetSessionService() {
726 if (!session_service_.get() && !shutdown_session_service_) {
727 session_service_ = new SessionService(this);
728 session_service_->ResetFromCurrentBrowsers();
729 }
730 return session_service_.get();
731}
732
733void ProfileImpl::ShutdownSessionService() {
734 if (shutdown_session_service_)
735 return;
736
737 // We're about to exit, force creation of the session service if it hasn't
738 // been created yet. We do this to ensure session state matches the point in
739 // time the user exited.
740 GetSessionService();
741 shutdown_session_service_ = true;
742 session_service_ = NULL;
743}
744
745bool ProfileImpl::HasSessionService() const {
746 return (session_service_.get() != NULL);
747}
748
749std::wstring ProfileImpl::GetName() {
750 return GetPrefs()->GetString(prefs::kProfileName);
751}
752void ProfileImpl::SetName(const std::wstring& name) {
753 GetPrefs()->SetString(prefs::kProfileName, name);
754}
755
756std::wstring ProfileImpl::GetID() {
757 return GetPrefs()->GetString(prefs::kProfileID);
758}
759void ProfileImpl::SetID(const std::wstring& id) {
760 GetPrefs()->SetString(prefs::kProfileID, id);
761}
762
initial.commit09911bf2008-07-26 23:55:29763bool ProfileImpl::DidLastSessionExitCleanly() {
764 // last_session_exited_cleanly_ is set when the preferences are loaded. Force
765 // it to be set by asking for the prefs.
766 GetPrefs();
767 return last_session_exited_cleanly_;
768}
769
[email protected]d8e41ed2008-09-11 15:22:32770BookmarkModel* ProfileImpl::GetBookmarkModel() {
[email protected]90ef13132008-08-27 03:27:46771 if (!bookmark_bar_model_.get()) {
[email protected]d8e41ed2008-09-11 15:22:32772 bookmark_bar_model_.reset(new BookmarkModel(this));
[email protected]90ef13132008-08-27 03:27:46773 bookmark_bar_model_->Load();
774 }
initial.commit09911bf2008-07-26 23:55:29775 return bookmark_bar_model_.get();
776}
777
778bool ProfileImpl::IsSameProfile(Profile* profile) {
779 if (profile == static_cast<Profile*>(this))
780 return true;
781 OffTheRecordProfileImpl* otr_profile = off_the_record_profile_.get();
782 return otr_profile && profile == static_cast<Profile*>(otr_profile);
783}
784
785Time ProfileImpl::GetStartTime() const {
786 return start_time_;
787}
788
789TabRestoreService* ProfileImpl::GetTabRestoreService() {
790 if (!tab_restore_service_.get())
[email protected]169627b2008-12-06 19:30:19791 tab_restore_service_ = new TabRestoreService(this);
initial.commit09911bf2008-07-26 23:55:29792 return tab_restore_service_.get();
793}
794
795void ProfileImpl::ResetTabRestoreService() {
[email protected]169627b2008-12-06 19:30:19796 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29797}
798
[email protected]a9afddb2009-02-12 17:49:42799// To be run in the IO thread to notify all resource message filters that the
[email protected]20930852008-10-15 19:30:41800// spellchecker has changed.
801class NotifySpellcheckerChangeTask : public Task {
802 public:
803 NotifySpellcheckerChangeTask(
[email protected]e7244d82008-10-29 18:13:26804 Profile* profile,
805 const SpellcheckerReinitializedDetails& spellchecker)
[email protected]20930852008-10-15 19:30:41806 : profile_(profile),
807 spellchecker_(spellchecker) {
808 }
809
810 private:
811 void Run(void) {
812 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56813 NotificationType::SPELLCHECKER_REINITIALIZED,
[email protected]20930852008-10-15 19:30:41814 Source<Profile>(profile_),
815 Details<SpellcheckerReinitializedDetails>(&spellchecker_));
816 }
817
818 Profile* profile_;
819 SpellcheckerReinitializedDetails spellchecker_;
820};
821
[email protected]e7244d82008-10-29 18:13:26822void ProfileImpl::InitializeSpellChecker(bool need_to_broadcast) {
[email protected]20930852008-10-15 19:30:41823 // The I/O thread may be NULL during testing.
824 base::Thread* io_thread = g_browser_process->io_thread();
825 if (spellchecker_) {
826 // The spellchecker must be deleted on the I/O thread.
827 // A dummy variable to aid in logical clarity.
828 SpellChecker* last_spellchecker = spellchecker_;
829
830 if (io_thread)
831 io_thread->message_loop()->ReleaseSoon(FROM_HERE, last_spellchecker);
832 else // during testing, we don't have an I/O thread
833 last_spellchecker->Release();
[email protected]20930852008-10-15 19:30:41834 }
835
[email protected]20930852008-10-15 19:30:41836 // Retrieve the (perhaps updated recently) dictionary name from preferences.
837 PrefService* prefs = GetPrefs();
[email protected]e7244d82008-10-29 18:13:26838 bool enable_spellcheck = prefs->GetBoolean(prefs::kEnableSpellCheck);
[email protected]20930852008-10-15 19:30:41839
[email protected]e7244d82008-10-29 18:13:26840 if (enable_spellcheck) {
[email protected]bd17b702009-02-25 20:44:08841 FilePath dict_dir;
[email protected]e7244d82008-10-29 18:13:26842 PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
[email protected]a9afddb2009-02-12 17:49:42843 // Note that, as the object pointed to by previously by spellchecker_
[email protected]e7244d82008-10-29 18:13:26844 // is being deleted in the io thread, the spellchecker_ can be made to point
845 // to a new object (RE-initialized) in parallel in this UI thread.
[email protected]4b4d1adc2008-12-10 22:28:58846 spellchecker_ = new SpellChecker(dict_dir,
[email protected]74c8b422009-03-11 00:34:12847 WideToASCII(prefs->GetString(prefs::kSpellCheckDictionary)),
848 GetRequestContext(),
[email protected]bd17b702009-02-25 20:44:08849 FilePath());
[email protected]e7244d82008-10-29 18:13:26850 spellchecker_->AddRef(); // Manual refcounting.
851 } else {
852 spellchecker_ = NULL;
853 }
[email protected]20930852008-10-15 19:30:41854
855 if (need_to_broadcast && io_thread) { // Notify resource message filters.
856 SpellcheckerReinitializedDetails scoped_spellchecker;
857 scoped_spellchecker.spellchecker = spellchecker_;
[email protected]e7244d82008-10-29 18:13:26858 if (io_thread) {
859 io_thread->message_loop()->PostTask(
[email protected]a9afddb2009-02-12 17:49:42860 FROM_HERE,
[email protected]e7244d82008-10-29 18:13:26861 new NotifySpellcheckerChangeTask(this, scoped_spellchecker));
862 }
[email protected]20930852008-10-15 19:30:41863 }
864}
865
[email protected]e7244d82008-10-29 18:13:26866void ProfileImpl::ReinitializeSpellChecker() {
867 InitializeSpellChecker(true);
868}
869
initial.commit09911bf2008-07-26 23:55:29870SpellChecker* ProfileImpl::GetSpellChecker() {
871 if (!spellchecker_) {
[email protected]20930852008-10-15 19:30:41872 // This is where spellchecker gets initialized. Note that this is being
873 // initialized in the ui_thread. However, this is not a problem as long as
874 // it is *used* in the io thread.
[email protected]34cc84f2009-02-13 10:04:35875 // TODO(sidchat): One day, change everything so that spellchecker gets
[email protected]20930852008-10-15 19:30:41876 // initialized in the IO thread itself.
[email protected]e7244d82008-10-29 18:13:26877 InitializeSpellChecker(false);
initial.commit09911bf2008-07-26 23:55:29878 }
[email protected]20930852008-10-15 19:30:41879
initial.commit09911bf2008-07-26 23:55:29880 return spellchecker_;
881}
882
883void ProfileImpl::MarkAsCleanShutdown() {
884 if (prefs_.get()) {
885 // The session cleanly exited, set kSessionExitedCleanly appropriately.
886 prefs_->SetBoolean(prefs::kSessionExitedCleanly, true);
887
888 // NOTE: If you change what thread this writes on, be sure and update
889 // ChromeFrame::EndSession().
890 prefs_->SavePersistentPrefs(g_browser_process->file_thread());
891 }
892}
893
[email protected]e7244d82008-10-29 18:13:26894void ProfileImpl::Observe(NotificationType type,
895 const NotificationSource& source,
896 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56897 if (NotificationType::PREF_CHANGED == type) {
[email protected]e7244d82008-10-29 18:13:26898 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
899 PrefService* prefs = Source<PrefService>(source).ptr();
900 DCHECK(pref_name_in && prefs);
901 if (*pref_name_in == prefs::kSpellCheckDictionary ||
902 *pref_name_in == prefs::kEnableSpellCheck) {
903 InitializeSpellChecker(true);
904 }
905 }
906}
907
initial.commit09911bf2008-07-26 23:55:29908void ProfileImpl::StopCreateSessionServiceTimer() {
[email protected]2d316662008-09-03 18:18:14909 create_session_service_timer_.Stop();
initial.commit09911bf2008-07-26 23:55:29910}
[email protected]3a453fa2008-08-15 18:46:34911
912#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07913ProfilePersonalization* ProfileImpl::GetProfilePersonalization() {
[email protected]bb95ef9e2009-03-05 23:20:29914 DCHECK(!Personalization::IsP13NDisabled());
[email protected]828e46f2009-02-27 02:43:51915 if (!personalization_.get())
916 personalization_.reset(
917 Personalization::CreateProfilePersonalization(this));
[email protected]57d3d0a2008-09-24 00:50:07918 return personalization_.get();
[email protected]3a453fa2008-08-15 18:46:34919}
license.botbf09a502008-08-24 00:55:55920#endif