blob: 88bc07206861f4c65e726d18ed7cebdcb85bc217 [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]f7011fcb2009-01-28 21:54:3265#if defined(OS_MACOSX)
66 // MASSIVE HACK!!! We don't have localization working yet. Undo this once we
67 // do. TODO(port): take this out
68 prefs->RegisterStringPref(prefs::kSpellCheckDictionary,
69 L"IDS_SPELLCHECK_DICTIONARY");
70#elif defined(OS_WIN) || defined(OS_LINUX)
[email protected]e7244d82008-10-29 18:13:2671 prefs->RegisterLocalizedStringPref(prefs::kSpellCheckDictionary,
72 IDS_SPELLCHECK_DICTIONARY);
[email protected]f7011fcb2009-01-28 21:54:3273#endif
[email protected]e7244d82008-10-29 18:13:2674 prefs->RegisterBooleanPref(prefs::kEnableSpellCheck, true);
[email protected]f93fe782009-02-19 01:26:1375 prefs->RegisterBooleanPref(prefs::kEnableUserScripts, false);
76 prefs->RegisterBooleanPref(prefs::kEnableExtensions, false);
initial.commit09911bf2008-07-26 23:55:2977}
78
[email protected]34cc84f2009-02-13 10:04:3579// static
[email protected]f7011fcb2009-01-28 21:54:3280Profile* Profile::CreateProfile(const FilePath& path) {
initial.commit09911bf2008-07-26 23:55:2981 return new ProfileImpl(path);
82}
83
[email protected]34cc84f2009-02-13 10:04:3584// static
initial.commit09911bf2008-07-26 23:55:2985URLRequestContext* Profile::GetDefaultRequestContext() {
86 return default_request_context_;
87}
88
89
initial.commit09911bf2008-07-26 23:55:2990////////////////////////////////////////////////////////////////////////////////
91//
92// OffTheRecordProfileImpl is a profile subclass that wraps an existing profile
93// to make it suitable for the off the record mode.
94//
95////////////////////////////////////////////////////////////////////////////////
96class OffTheRecordProfileImpl : public Profile,
97 public NotificationObserver {
98 public:
99 explicit OffTheRecordProfileImpl(Profile* real_profile)
100 : profile_(real_profile),
101 start_time_(Time::Now()) {
[email protected]6ab9b202008-12-23 22:34:50102 request_context_ = ChromeURLRequestContext::CreateOffTheRecord(this);
initial.commit09911bf2008-07-26 23:55:29103 request_context_->AddRef();
104 // Register for browser close notifications so we can detect when the last
105 // off-the-record window is closed, in which case we can clean our states
106 // (cookies, downloads...).
107 NotificationService::current()->AddObserver(
[email protected]bfd04a62009-02-01 18:16:56108 this,
109 NotificationType::BROWSER_CLOSED,
110 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29111 }
112
113 virtual ~OffTheRecordProfileImpl() {
114 if (request_context_) {
[email protected]6ab9b202008-12-23 22:34:50115 request_context_->CleanupOnUIThread();
116
initial.commit09911bf2008-07-26 23:55:29117 // Clean up request context on IO thread.
118 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]6ab9b202008-12-23 22:34:50119 NewRunnableMethod(request_context_,
120 &base::RefCountedThreadSafe<URLRequestContext>::Release));
initial.commit09911bf2008-07-26 23:55:29121 request_context_ = NULL;
122 }
123 NotificationService::current()->RemoveObserver(
[email protected]bfd04a62009-02-01 18:16:56124 this,
125 NotificationType::BROWSER_CLOSED,
126 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29127 }
128
[email protected]f7011fcb2009-01-28 21:54:32129 virtual FilePath GetPath() { return profile_->GetPath(); }
initial.commit09911bf2008-07-26 23:55:29130
131 virtual bool IsOffTheRecord() {
132 return true;
133 }
134
135 virtual Profile* GetOffTheRecordProfile() {
136 return this;
137 }
138
139 virtual Profile* GetOriginalProfile() {
140 return profile_;
141 }
142
143 virtual VisitedLinkMaster* GetVisitedLinkMaster() {
144 return profile_->GetVisitedLinkMaster();
145 }
146
[email protected]6014d672008-12-05 00:38:25147 virtual ExtensionsService* GetExtensionsService() {
148 return profile_->GetExtensionsService();
149 }
150
[email protected]0938d3c2009-01-09 20:37:35151 virtual UserScriptMaster* GetUserScriptMaster() {
152 return profile_->GetUserScriptMaster();
[email protected]04fba9a92008-10-28 17:25:25153 }
154
[email protected]34cc84f2009-02-13 10:04:35155 virtual SSLHostState* GetSSLHostState() {
156 if (!ssl_host_state_.get())
157 ssl_host_state_.reset(new SSLHostState());
158
159 DCHECK(ssl_host_state_->CalledOnValidThread());
160 return ssl_host_state_.get();
161 }
162
initial.commit09911bf2008-07-26 23:55:29163 virtual HistoryService* GetHistoryService(ServiceAccessType sat) {
164 if (sat == EXPLICIT_ACCESS) {
165 return profile_->GetHistoryService(sat);
166 } else {
167 NOTREACHED() << "This profile is OffTheRecord";
168 return NULL;
169 }
170 }
171
initial.commit09911bf2008-07-26 23:55:29172 virtual WebDataService* GetWebDataService(ServiceAccessType sat) {
173 if (sat == EXPLICIT_ACCESS) {
174 return profile_->GetWebDataService(sat);
175 } else {
176 NOTREACHED() << "This profile is OffTheRecord";
177 return NULL;
178 }
179 }
180
181 virtual PrefService* GetPrefs() {
182 return profile_->GetPrefs();
183 }
184
185 virtual TemplateURLModel* GetTemplateURLModel() {
186 return profile_->GetTemplateURLModel();
187 }
188
189 virtual TemplateURLFetcher* GetTemplateURLFetcher() {
190 return profile_->GetTemplateURLFetcher();
191 }
192
193 virtual DownloadManager* GetDownloadManager() {
194 if (!download_manager_.get()) {
195 scoped_refptr<DownloadManager> dlm(new DownloadManager);
196 dlm->Init(this);
197 download_manager_.swap(dlm);
198 }
199 return download_manager_.get();
200 }
201
202 virtual bool HasCreatedDownloadManager() const {
203 return (download_manager_.get() != NULL);
204 }
205
206 virtual URLRequestContext* GetRequestContext() {
207 return request_context_;
208 }
209
[email protected]e7f29642009-03-02 22:53:18210 virtual URLRequestContext* GetRequestContextForMedia() {
211 if (!media_request_context_) {
212 FilePath cache_path = GetPath();
213 cache_path.Append(chrome::kOffTheRecordMediaCacheDirname);
214 media_request_context_ =
215 ChromeURLRequestContext::CreateOffTheRecordForMedia(
216 this, cache_path);
217 media_request_context_->AddRef();
218
219 DCHECK(media_request_context_->cookie_store());
220 }
221 return media_request_context_;
222 }
223
initial.commit09911bf2008-07-26 23:55:29224 virtual SessionService* GetSessionService() {
225 // Don't save any sessions when off the record.
226 return NULL;
227 }
228
229 virtual void ShutdownSessionService() {
230 // We don't allow a session service, nothing to do.
231 }
232
233 virtual bool HasSessionService() const {
234 // We never have a session service.
235 return false;
236 }
237
238 virtual std::wstring GetName() {
239 return profile_->GetName();
240 }
241
242 virtual void SetName(const std::wstring& name) {
243 profile_->SetName(name);
244 }
245
246 virtual std::wstring GetID() {
247 return profile_->GetID();
248 }
249
250 virtual void SetID(const std::wstring& id) {
251 profile_->SetID(id);
252 }
253
initial.commit09911bf2008-07-26 23:55:29254 virtual bool DidLastSessionExitCleanly() {
255 return profile_->DidLastSessionExitCleanly();
256 }
257
[email protected]d8e41ed2008-09-11 15:22:32258 virtual BookmarkModel* GetBookmarkModel() {
259 return profile_->GetBookmarkModel();
initial.commit09911bf2008-07-26 23:55:29260 }
261
[email protected]3a453fa2008-08-15 18:46:34262#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07263 virtual ProfilePersonalization* GetProfilePersonalization() {
[email protected]3a453fa2008-08-15 18:46:34264 return profile_->GetProfilePersonalization();
265 }
266#endif
267
initial.commit09911bf2008-07-26 23:55:29268 virtual bool IsSameProfile(Profile* profile) {
269 if (profile == static_cast<Profile*>(this))
270 return true;
271 return profile == profile_;
272 }
273
274 virtual Time GetStartTime() const {
275 return start_time_;
276 }
277
278 virtual TabRestoreService* GetTabRestoreService() {
279 return NULL;
280 }
281
[email protected]e7244d82008-10-29 18:13:26282 virtual void ResetTabRestoreService() {
[email protected]20930852008-10-15 19:30:41283 }
284
[email protected]e7244d82008-10-29 18:13:26285 virtual void ReinitializeSpellChecker() {
286 profile_->ReinitializeSpellChecker();
initial.commit09911bf2008-07-26 23:55:29287 }
288
289 virtual SpellChecker* GetSpellChecker() {
290 return profile_->GetSpellChecker();
291 }
292
293 virtual void MarkAsCleanShutdown() {
294 }
295
[email protected]bdbc87c2009-01-25 05:08:54296 virtual void InitExtensions() {
297 NOTREACHED();
298 }
299
initial.commit09911bf2008-07-26 23:55:29300 virtual void ExitedOffTheRecordMode() {
301 // Drop our download manager so we forget about all the downloads made
302 // in off-the-record mode.
303 download_manager_ = NULL;
304 }
305
306 virtual void Observe(NotificationType type,
307 const NotificationSource& source,
308 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56309 DCHECK_EQ(NotificationType::BROWSER_CLOSED, type.value);
initial.commit09911bf2008-07-26 23:55:29310 // We are only interested in OTR browser closing.
311 if (Source<Browser>(source)->profile() != this)
312 return;
313
314 // Let's check if we still have an Off The Record window opened.
315 // Note that we check against 1 as this notification is sent before the
316 // browser window is actually removed from the list.
317 if (BrowserList::GetBrowserCount(this) <= 1)
318 ExitedOffTheRecordMode();
319 }
320
321 private:
322 // The real underlying profile.
323 Profile* profile_;
324
[email protected]6ab9b202008-12-23 22:34:50325 // The context to use for requests made from this OTR session.
326 ChromeURLRequestContext* request_context_;
initial.commit09911bf2008-07-26 23:55:29327
[email protected]e7f29642009-03-02 22:53:18328 // The context for requests for media resources.
329 ChromeURLRequestContext* media_request_context_;
330
initial.commit09911bf2008-07-26 23:55:29331 // The download manager that only stores downloaded items in memory.
332 scoped_refptr<DownloadManager> download_manager_;
333
[email protected]34cc84f2009-02-13 10:04:35334 // We don't want SSLHostState from the OTR profile to leak back to the main
335 // profile because then the main profile would learn some of the host names
336 // the user visited while OTR.
337 scoped_ptr<SSLHostState> ssl_host_state_;
338
initial.commit09911bf2008-07-26 23:55:29339 // Time we were started.
340 Time start_time_;
341
342 DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl);
343};
344
[email protected]f7011fcb2009-01-28 21:54:32345ProfileImpl::ProfileImpl(const FilePath& path)
initial.commit09911bf2008-07-26 23:55:29346 : path_(path),
347 off_the_record_(false),
[email protected]3a453fa2008-08-15 18:46:34348#ifdef CHROME_PERSONALIZATION
349 personalization_(NULL),
350#endif
[email protected]f7011fcb2009-01-28 21:54:32351 request_context_(NULL),
352 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);
initial.commit09911bf2008-07-26 23:55:29367}
368
[email protected]bdbc87c2009-01-25 05:08:54369void ProfileImpl::InitExtensions() {
[email protected]8c756ac2009-01-30 23:36:41370 if (user_script_master_ || extensions_service_)
371 return; // Already initialized.
372
[email protected]bdbc87c2009-01-25 05:08:54373 const CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]f93fe782009-02-19 01:26:13374 PrefService* prefs = GetPrefs();
[email protected]bdbc87c2009-01-25 05:08:54375 bool user_scripts_enabled =
[email protected]f0a51fb52009-03-05 12:46:38376 command_line->HasSwitch(switches::kEnableUserScripts) ||
[email protected]f93fe782009-02-19 01:26:13377 prefs->GetBoolean(prefs::kEnableUserScripts);
[email protected]a9afddb2009-02-12 17:49:42378 bool extensions_enabled =
[email protected]f0a51fb52009-03-05 12:46:38379 command_line->HasSwitch(switches::kEnableExtensions) ||
[email protected]f93fe782009-02-19 01:26:13380 prefs->GetBoolean(prefs::kEnableExtensions);
[email protected]bdbc87c2009-01-25 05:08:54381
[email protected]f7011fcb2009-01-28 21:54:32382 FilePath script_dir;
[email protected]bdbc87c2009-01-25 05:08:54383 if (user_scripts_enabled) {
[email protected]0cd957b2009-03-06 20:13:23384
385 if (command_line->HasSwitch(switches::kUserScriptsDir)) {
386 std::wstring path_string =
387 command_line->GetSwitchValue(switches::kUserScriptsDir);
388 script_dir = FilePath::FromWStringHack(path_string);
389 } else {
390 script_dir = GetPath();
391 script_dir = script_dir.Append(chrome::kUserScriptsDirname);
392 }
[email protected]bdbc87c2009-01-25 05:08:54393 }
394
[email protected]bb28e062009-02-27 17:19:18395 ExtensionErrorReporter::Init(true); // allow noisy errors.
[email protected]bdbc87c2009-01-25 05:08:54396 user_script_master_ = new UserScriptMaster(
[email protected]f7011fcb2009-01-28 21:54:32397 g_browser_process->file_thread()->message_loop(), script_dir);
[email protected]81e63782009-02-27 19:35:09398 extensions_service_ = new ExtensionsService(this, user_script_master_.get());
[email protected]bdbc87c2009-01-25 05:08:54399
400 // If we have extensions, the extension service will kick off the first scan
401 // after extensions are loaded. Otherwise, we need to do that now.
402 if (extensions_enabled)
403 extensions_service_->Init();
404 else if (user_scripts_enabled)
405 user_script_master_->StartScan();
406}
407
initial.commit09911bf2008-07-26 23:55:29408ProfileImpl::~ProfileImpl() {
[email protected]169627b2008-12-06 19:30:19409 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29410
411 StopCreateSessionServiceTimer();
412 // TemplateURLModel schedules a task on the WebDataService from its
413 // destructor. Delete it first to ensure the task gets scheduled before we
414 // shut down the database.
415 template_url_model_.reset();
416
417 // The download manager queries the history system and should be deleted
418 // before the history is shutdown so it can properly cancel all requests.
419 download_manager_ = NULL;
420
[email protected]e7244d82008-10-29 18:13:26421 // Remove pref observers.
422 PrefService* prefs = GetPrefs();
423 prefs->RemovePrefObserver(prefs::kSpellCheckDictionary, this);
424 prefs->RemovePrefObserver(prefs::kEnableSpellCheck, this);
425
[email protected]3a453fa2008-08-15 18:46:34426#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07427 personalization_.reset();
[email protected]3a453fa2008-08-15 18:46:34428#endif
429
initial.commit09911bf2008-07-26 23:55:29430 // Both HistoryService and WebDataService maintain threads for background
431 // processing. Its possible each thread still has tasks on it that have
432 // increased the ref count of the service. In such a situation, when we
433 // decrement the refcount, it won't be 0, and the threads/databases aren't
434 // properly shut down. By explicitly calling Cleanup/Shutdown we ensure the
435 // databases are properly closed.
436 if (web_data_service_.get())
437 web_data_service_->Shutdown();
438
439 if (history_service_.get())
440 history_service_->Cleanup();
441
442 // The I/O thread may be NULL during testing.
[email protected]ab820df2008-08-26 05:55:10443 base::Thread* io_thread = g_browser_process->io_thread();
initial.commit09911bf2008-07-26 23:55:29444
445 if (spellchecker_) {
446 // The spellchecker must be deleted on the I/O thread. During testing, we
447 // don't have an I/O thread.
448 if (io_thread)
449 io_thread->message_loop()->ReleaseSoon(FROM_HERE, spellchecker_);
450 else
451 spellchecker_->Release();
452 }
453
454 if (request_context_) {
[email protected]6ab9b202008-12-23 22:34:50455 request_context_->CleanupOnUIThread();
456
457 if (default_request_context_ == request_context_)
458 default_request_context_ = NULL;
459
initial.commit09911bf2008-07-26 23:55:29460 // Clean up request context on IO thread.
[email protected]6ab9b202008-12-23 22:34:50461 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
462 NewRunnableMethod(request_context_,
463 &base::RefCountedThreadSafe<URLRequestContext>::Release));
initial.commit09911bf2008-07-26 23:55:29464 request_context_ = NULL;
465 }
466
[email protected]d8e41ed2008-09-11 15:22:32467 // HistoryService may call into the BookmarkModel, as such we need to
468 // delete HistoryService before the BookmarkModel. The destructor for
[email protected]90ef13132008-08-27 03:27:46469 // HistoryService will join with HistoryService's backend thread so that
470 // by the time the destructor has finished we're sure it will no longer call
[email protected]d8e41ed2008-09-11 15:22:32471 // into the BookmarkModel.
[email protected]90ef13132008-08-27 03:27:46472 history_service_ = NULL;
473 bookmark_bar_model_.reset();
474
initial.commit09911bf2008-07-26 23:55:29475 MarkAsCleanShutdown();
476}
477
[email protected]f7011fcb2009-01-28 21:54:32478FilePath ProfileImpl::GetPath() {
initial.commit09911bf2008-07-26 23:55:29479 return path_;
480}
481
482bool ProfileImpl::IsOffTheRecord() {
483 return false;
484}
485
486Profile* ProfileImpl::GetOffTheRecordProfile() {
487 if (!off_the_record_profile_.get()) {
488 scoped_ptr<OffTheRecordProfileImpl> p(new OffTheRecordProfileImpl(this));
489 off_the_record_profile_.swap(p);
490 }
491 return off_the_record_profile_.get();
492}
493
494Profile* ProfileImpl::GetOriginalProfile() {
495 return this;
496}
497
[email protected]176aa482008-11-14 03:25:15498static void BroadcastNewHistoryTable(base::SharedMemory* table_memory) {
initial.commit09911bf2008-07-26 23:55:29499 if (!table_memory)
500 return;
501
502 // send to all RenderProcessHosts
503 for (RenderProcessHost::iterator i = RenderProcessHost::begin();
504 i != RenderProcessHost::end(); i++) {
505 if (!i->second->channel())
506 continue;
507
[email protected]176aa482008-11-14 03:25:15508 base::SharedMemoryHandle new_table;
[email protected]f7011fcb2009-01-28 21:54:32509 base::ProcessHandle process = i->second->process().handle();
initial.commit09911bf2008-07-26 23:55:29510 if (!process) {
511 // process can be null if it's started with the --single-process flag.
[email protected]f7011fcb2009-01-28 21:54:32512 process = base::Process::Current().handle();
initial.commit09911bf2008-07-26 23:55:29513 }
514
515 table_memory->ShareToProcess(process, &new_table);
516 IPC::Message* msg = new ViewMsg_VisitedLink_NewTable(new_table);
517 i->second->channel()->Send(msg);
518 }
519}
520
521VisitedLinkMaster* ProfileImpl::GetVisitedLinkMaster() {
522 if (!visited_link_master_.get()) {
523 scoped_ptr<VisitedLinkMaster> visited_links(
524 new VisitedLinkMaster(g_browser_process->file_thread(),
525 BroadcastNewHistoryTable, this));
526 if (!visited_links->Init())
527 return NULL;
528 visited_link_master_.swap(visited_links);
529 }
530
531 return visited_link_master_.get();
532}
533
[email protected]6014d672008-12-05 00:38:25534ExtensionsService* ProfileImpl::GetExtensionsService() {
535 return extensions_service_.get();
536}
537
[email protected]0938d3c2009-01-09 20:37:35538UserScriptMaster* ProfileImpl::GetUserScriptMaster() {
[email protected]0938d3c2009-01-09 20:37:35539 return user_script_master_.get();
[email protected]04fba9a92008-10-28 17:25:25540}
541
[email protected]34cc84f2009-02-13 10:04:35542SSLHostState* ProfileImpl::GetSSLHostState() {
543 if (!ssl_host_state_.get())
544 ssl_host_state_.reset(new SSLHostState());
545
546 DCHECK(ssl_host_state_->CalledOnValidThread());
547 return ssl_host_state_.get();
548}
549
initial.commit09911bf2008-07-26 23:55:29550PrefService* ProfileImpl::GetPrefs() {
551 if (!prefs_.get()) {
[email protected]b9636002009-03-04 00:05:25552 prefs_.reset(new PrefService(GetPrefFilePath()));
initial.commit09911bf2008-07-26 23:55:29553
554 // The Profile class and ProfileManager class may read some prefs so
555 // register known prefs as soon as possible.
556 Profile::RegisterUserPrefs(prefs_.get());
557 ProfileManager::RegisterUserPrefs(prefs_.get());
558
559 // The last session exited cleanly if there is no pref for
560 // kSessionExitedCleanly or the value for kSessionExitedCleanly is true.
561 last_session_exited_cleanly_ =
562 prefs_->GetBoolean(prefs::kSessionExitedCleanly);
563 // Mark the session as open.
564 prefs_->SetBoolean(prefs::kSessionExitedCleanly, false);
565 // Make sure we save to disk that the session has opened.
566 prefs_->ScheduleSavePersistentPrefs(g_browser_process->file_thread());
567 }
568
569 return prefs_.get();
570}
571
[email protected]f7011fcb2009-01-28 21:54:32572FilePath ProfileImpl::GetPrefFilePath() {
573 FilePath pref_file_path = path_;
574 pref_file_path = pref_file_path.Append(chrome::kPreferencesFilename);
initial.commit09911bf2008-07-26 23:55:29575 return pref_file_path;
576}
577
578URLRequestContext* ProfileImpl::GetRequestContext() {
579 if (!request_context_) {
[email protected]f7011fcb2009-01-28 21:54:32580 FilePath cookie_path = GetPath();
581 cookie_path = cookie_path.Append(chrome::kCookieFilename);
582 FilePath cache_path = GetPath();
[email protected]2b2830a2009-02-07 01:58:42583
584 // Override the cache location if specified by the user.
585 const std::wstring user_cache_dir(
586 CommandLine::ForCurrentProcess()->GetSwitchValue(
587 switches::kDiskCacheDir));
588 if (!user_cache_dir.empty()) {
589 cache_path = FilePath::FromWStringHack(user_cache_dir);
590 }
591
[email protected]f7011fcb2009-01-28 21:54:32592 cache_path = cache_path.Append(chrome::kCacheDirname);
[email protected]6ab9b202008-12-23 22:34:50593 request_context_ = ChromeURLRequestContext::CreateOriginal(
594 this, cookie_path, cache_path);
initial.commit09911bf2008-07-26 23:55:29595 request_context_->AddRef();
596
[email protected]6ab9b202008-12-23 22:34:50597 // The first request context is always a normal (non-OTR) request context.
598 // Even when Chromium is started in OTR mode, a normal profile is always
599 // created first.
600 if (!default_request_context_) {
601 default_request_context_ = request_context_;
602 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56603 NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE,
[email protected]6ab9b202008-12-23 22:34:50604 NotificationService::AllSources(), NotificationService::NoDetails());
605 }
606
initial.commit09911bf2008-07-26 23:55:29607 DCHECK(request_context_->cookie_store());
608 }
609
610 return request_context_;
611}
612
[email protected]e7f29642009-03-02 22:53:18613URLRequestContext* ProfileImpl::GetRequestContextForMedia() {
614 if (!media_request_context_) {
615 FilePath cache_path = GetPath();
616 cache_path.Append(chrome::kMediaCacheDirname);
617 media_request_context_ = ChromeURLRequestContext::CreateOriginalForMedia(
618 this, cache_path);
619 media_request_context_->AddRef();
620
621 DCHECK(media_request_context_->cookie_store());
622 }
623
624 return media_request_context_;
625}
626
initial.commit09911bf2008-07-26 23:55:29627HistoryService* ProfileImpl::GetHistoryService(ServiceAccessType sat) {
628 if (!history_service_created_) {
[email protected]90ef13132008-08-27 03:27:46629 history_service_created_ = true;
initial.commit09911bf2008-07-26 23:55:29630 scoped_refptr<HistoryService> history(new HistoryService(this));
[email protected]d8e41ed2008-09-11 15:22:32631 if (!history->Init(GetPath(), GetBookmarkModel()))
initial.commit09911bf2008-07-26 23:55:29632 return NULL;
633 history_service_.swap(history);
initial.commit09911bf2008-07-26 23:55:29634
635 // Send out the notification that the history service was created.
636 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56637 Notify(NotificationType::HISTORY_CREATED, Source<Profile>(this),
initial.commit09911bf2008-07-26 23:55:29638 Details<HistoryService>(history_service_.get()));
639 }
640 return history_service_.get();
641}
642
initial.commit09911bf2008-07-26 23:55:29643TemplateURLModel* ProfileImpl::GetTemplateURLModel() {
644 if (!template_url_model_.get())
645 template_url_model_.reset(new TemplateURLModel(this));
646 return template_url_model_.get();
647}
648
649TemplateURLFetcher* ProfileImpl::GetTemplateURLFetcher() {
650 if (!template_url_fetcher_.get())
651 template_url_fetcher_.reset(new TemplateURLFetcher(this));
652 return template_url_fetcher_.get();
653}
654
655WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) {
656 if (!created_web_data_service_)
657 CreateWebDataService();
658 return web_data_service_.get();
659}
660
661void ProfileImpl::CreateWebDataService() {
662 DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL);
663 created_web_data_service_ = true;
664 scoped_refptr<WebDataService> wds(new WebDataService());
665 if (!wds->Init(GetPath()))
666 return;
667 web_data_service_.swap(wds);
668}
669
670DownloadManager* ProfileImpl::GetDownloadManager() {
671 if (!created_download_manager_) {
672 scoped_refptr<DownloadManager> dlm(new DownloadManager);
673 dlm->Init(this);
674 created_download_manager_ = true;
675 download_manager_.swap(dlm);
676 }
677 return download_manager_.get();
678}
679
680bool ProfileImpl::HasCreatedDownloadManager() const {
681 return created_download_manager_;
682}
683
684SessionService* ProfileImpl::GetSessionService() {
685 if (!session_service_.get() && !shutdown_session_service_) {
686 session_service_ = new SessionService(this);
687 session_service_->ResetFromCurrentBrowsers();
688 }
689 return session_service_.get();
690}
691
692void ProfileImpl::ShutdownSessionService() {
693 if (shutdown_session_service_)
694 return;
695
696 // We're about to exit, force creation of the session service if it hasn't
697 // been created yet. We do this to ensure session state matches the point in
698 // time the user exited.
699 GetSessionService();
700 shutdown_session_service_ = true;
701 session_service_ = NULL;
702}
703
704bool ProfileImpl::HasSessionService() const {
705 return (session_service_.get() != NULL);
706}
707
708std::wstring ProfileImpl::GetName() {
709 return GetPrefs()->GetString(prefs::kProfileName);
710}
711void ProfileImpl::SetName(const std::wstring& name) {
712 GetPrefs()->SetString(prefs::kProfileName, name);
713}
714
715std::wstring ProfileImpl::GetID() {
716 return GetPrefs()->GetString(prefs::kProfileID);
717}
718void ProfileImpl::SetID(const std::wstring& id) {
719 GetPrefs()->SetString(prefs::kProfileID, id);
720}
721
initial.commit09911bf2008-07-26 23:55:29722bool ProfileImpl::DidLastSessionExitCleanly() {
723 // last_session_exited_cleanly_ is set when the preferences are loaded. Force
724 // it to be set by asking for the prefs.
725 GetPrefs();
726 return last_session_exited_cleanly_;
727}
728
[email protected]d8e41ed2008-09-11 15:22:32729BookmarkModel* ProfileImpl::GetBookmarkModel() {
[email protected]90ef13132008-08-27 03:27:46730 if (!bookmark_bar_model_.get()) {
[email protected]d8e41ed2008-09-11 15:22:32731 bookmark_bar_model_.reset(new BookmarkModel(this));
[email protected]90ef13132008-08-27 03:27:46732 bookmark_bar_model_->Load();
733 }
initial.commit09911bf2008-07-26 23:55:29734 return bookmark_bar_model_.get();
735}
736
737bool ProfileImpl::IsSameProfile(Profile* profile) {
738 if (profile == static_cast<Profile*>(this))
739 return true;
740 OffTheRecordProfileImpl* otr_profile = off_the_record_profile_.get();
741 return otr_profile && profile == static_cast<Profile*>(otr_profile);
742}
743
744Time ProfileImpl::GetStartTime() const {
745 return start_time_;
746}
747
748TabRestoreService* ProfileImpl::GetTabRestoreService() {
749 if (!tab_restore_service_.get())
[email protected]169627b2008-12-06 19:30:19750 tab_restore_service_ = new TabRestoreService(this);
initial.commit09911bf2008-07-26 23:55:29751 return tab_restore_service_.get();
752}
753
754void ProfileImpl::ResetTabRestoreService() {
[email protected]169627b2008-12-06 19:30:19755 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29756}
757
[email protected]a9afddb2009-02-12 17:49:42758// To be run in the IO thread to notify all resource message filters that the
[email protected]20930852008-10-15 19:30:41759// spellchecker has changed.
760class NotifySpellcheckerChangeTask : public Task {
761 public:
762 NotifySpellcheckerChangeTask(
[email protected]e7244d82008-10-29 18:13:26763 Profile* profile,
764 const SpellcheckerReinitializedDetails& spellchecker)
[email protected]20930852008-10-15 19:30:41765 : profile_(profile),
766 spellchecker_(spellchecker) {
767 }
768
769 private:
770 void Run(void) {
771 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56772 NotificationType::SPELLCHECKER_REINITIALIZED,
[email protected]20930852008-10-15 19:30:41773 Source<Profile>(profile_),
774 Details<SpellcheckerReinitializedDetails>(&spellchecker_));
775 }
776
777 Profile* profile_;
778 SpellcheckerReinitializedDetails spellchecker_;
779};
780
[email protected]e7244d82008-10-29 18:13:26781void ProfileImpl::InitializeSpellChecker(bool need_to_broadcast) {
[email protected]20930852008-10-15 19:30:41782 // The I/O thread may be NULL during testing.
783 base::Thread* io_thread = g_browser_process->io_thread();
784 if (spellchecker_) {
785 // The spellchecker must be deleted on the I/O thread.
786 // A dummy variable to aid in logical clarity.
787 SpellChecker* last_spellchecker = spellchecker_;
788
789 if (io_thread)
790 io_thread->message_loop()->ReleaseSoon(FROM_HERE, last_spellchecker);
791 else // during testing, we don't have an I/O thread
792 last_spellchecker->Release();
[email protected]20930852008-10-15 19:30:41793 }
794
[email protected]20930852008-10-15 19:30:41795 // Retrieve the (perhaps updated recently) dictionary name from preferences.
796 PrefService* prefs = GetPrefs();
[email protected]e7244d82008-10-29 18:13:26797 bool enable_spellcheck = prefs->GetBoolean(prefs::kEnableSpellCheck);
[email protected]20930852008-10-15 19:30:41798
[email protected]e7244d82008-10-29 18:13:26799 if (enable_spellcheck) {
[email protected]bd17b702009-02-25 20:44:08800 FilePath dict_dir;
[email protected]e7244d82008-10-29 18:13:26801 PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
[email protected]a9afddb2009-02-12 17:49:42802 // Note that, as the object pointed to by previously by spellchecker_
[email protected]e7244d82008-10-29 18:13:26803 // is being deleted in the io thread, the spellchecker_ can be made to point
804 // to a new object (RE-initialized) in parallel in this UI thread.
[email protected]4b4d1adc2008-12-10 22:28:58805 spellchecker_ = new SpellChecker(dict_dir,
806 prefs->GetString(prefs::kSpellCheckDictionary), GetRequestContext(),
[email protected]bd17b702009-02-25 20:44:08807 FilePath());
[email protected]e7244d82008-10-29 18:13:26808 spellchecker_->AddRef(); // Manual refcounting.
809 } else {
810 spellchecker_ = NULL;
811 }
[email protected]20930852008-10-15 19:30:41812
813 if (need_to_broadcast && io_thread) { // Notify resource message filters.
814 SpellcheckerReinitializedDetails scoped_spellchecker;
815 scoped_spellchecker.spellchecker = spellchecker_;
[email protected]e7244d82008-10-29 18:13:26816 if (io_thread) {
817 io_thread->message_loop()->PostTask(
[email protected]a9afddb2009-02-12 17:49:42818 FROM_HERE,
[email protected]e7244d82008-10-29 18:13:26819 new NotifySpellcheckerChangeTask(this, scoped_spellchecker));
820 }
[email protected]20930852008-10-15 19:30:41821 }
822}
823
[email protected]e7244d82008-10-29 18:13:26824void ProfileImpl::ReinitializeSpellChecker() {
825 InitializeSpellChecker(true);
826}
827
initial.commit09911bf2008-07-26 23:55:29828SpellChecker* ProfileImpl::GetSpellChecker() {
829 if (!spellchecker_) {
[email protected]20930852008-10-15 19:30:41830 // This is where spellchecker gets initialized. Note that this is being
831 // initialized in the ui_thread. However, this is not a problem as long as
832 // it is *used* in the io thread.
[email protected]34cc84f2009-02-13 10:04:35833 // TODO(sidchat): One day, change everything so that spellchecker gets
[email protected]20930852008-10-15 19:30:41834 // initialized in the IO thread itself.
[email protected]e7244d82008-10-29 18:13:26835 InitializeSpellChecker(false);
initial.commit09911bf2008-07-26 23:55:29836 }
[email protected]20930852008-10-15 19:30:41837
initial.commit09911bf2008-07-26 23:55:29838 return spellchecker_;
839}
840
841void ProfileImpl::MarkAsCleanShutdown() {
842 if (prefs_.get()) {
843 // The session cleanly exited, set kSessionExitedCleanly appropriately.
844 prefs_->SetBoolean(prefs::kSessionExitedCleanly, true);
845
846 // NOTE: If you change what thread this writes on, be sure and update
847 // ChromeFrame::EndSession().
848 prefs_->SavePersistentPrefs(g_browser_process->file_thread());
849 }
850}
851
[email protected]e7244d82008-10-29 18:13:26852void ProfileImpl::Observe(NotificationType type,
853 const NotificationSource& source,
854 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56855 if (NotificationType::PREF_CHANGED == type) {
[email protected]e7244d82008-10-29 18:13:26856 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
857 PrefService* prefs = Source<PrefService>(source).ptr();
858 DCHECK(pref_name_in && prefs);
859 if (*pref_name_in == prefs::kSpellCheckDictionary ||
860 *pref_name_in == prefs::kEnableSpellCheck) {
861 InitializeSpellChecker(true);
862 }
863 }
864}
865
initial.commit09911bf2008-07-26 23:55:29866void ProfileImpl::StopCreateSessionServiceTimer() {
[email protected]2d316662008-09-03 18:18:14867 create_session_service_timer_.Stop();
initial.commit09911bf2008-07-26 23:55:29868}
[email protected]3a453fa2008-08-15 18:46:34869
870#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07871ProfilePersonalization* ProfileImpl::GetProfilePersonalization() {
[email protected]bb95ef9e2009-03-05 23:20:29872 DCHECK(!Personalization::IsP13NDisabled());
[email protected]828e46f2009-02-27 02:43:51873 if (!personalization_.get())
874 personalization_.reset(
875 Personalization::CreateProfilePersonalization(this));
[email protected]57d3d0a2008-09-24 00:50:07876 return personalization_.get();
[email protected]3a453fa2008-08-15 18:46:34877}
license.botbf09a502008-08-24 00:55:55878#endif