blob: 1bc69566d35ed895bb97d171057b8b08c3815e9e [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),
102 start_time_(Time::Now()) {
[email protected]6ab9b202008-12-23 22:34:50103 request_context_ = ChromeURLRequestContext::CreateOffTheRecord(this);
initial.commit09911bf2008-07-26 23:55:29104 request_context_->AddRef();
105 // Register for browser close notifications so we can detect when the last
106 // off-the-record window is closed, in which case we can clean our states
107 // (cookies, downloads...).
108 NotificationService::current()->AddObserver(
[email protected]bfd04a62009-02-01 18:16:56109 this,
110 NotificationType::BROWSER_CLOSED,
111 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29112 }
113
114 virtual ~OffTheRecordProfileImpl() {
115 if (request_context_) {
[email protected]6ab9b202008-12-23 22:34:50116 request_context_->CleanupOnUIThread();
117
initial.commit09911bf2008-07-26 23:55:29118 // Clean up request context on IO thread.
119 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]6ab9b202008-12-23 22:34:50120 NewRunnableMethod(request_context_,
121 &base::RefCountedThreadSafe<URLRequestContext>::Release));
initial.commit09911bf2008-07-26 23:55:29122 request_context_ = NULL;
123 }
124 NotificationService::current()->RemoveObserver(
[email protected]bfd04a62009-02-01 18:16:56125 this,
126 NotificationType::BROWSER_CLOSED,
127 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29128 }
129
[email protected]f7011fcb2009-01-28 21:54:32130 virtual FilePath GetPath() { return profile_->GetPath(); }
initial.commit09911bf2008-07-26 23:55:29131
132 virtual bool IsOffTheRecord() {
133 return true;
134 }
135
136 virtual Profile* GetOffTheRecordProfile() {
137 return this;
138 }
139
140 virtual Profile* GetOriginalProfile() {
141 return profile_;
142 }
143
144 virtual VisitedLinkMaster* GetVisitedLinkMaster() {
145 return profile_->GetVisitedLinkMaster();
146 }
147
[email protected]6014d672008-12-05 00:38:25148 virtual ExtensionsService* GetExtensionsService() {
149 return profile_->GetExtensionsService();
150 }
151
[email protected]0938d3c2009-01-09 20:37:35152 virtual UserScriptMaster* GetUserScriptMaster() {
153 return profile_->GetUserScriptMaster();
[email protected]04fba9a92008-10-28 17:25:25154 }
155
[email protected]34cc84f2009-02-13 10:04:35156 virtual SSLHostState* GetSSLHostState() {
157 if (!ssl_host_state_.get())
158 ssl_host_state_.reset(new SSLHostState());
159
160 DCHECK(ssl_host_state_->CalledOnValidThread());
161 return ssl_host_state_.get();
162 }
163
initial.commit09911bf2008-07-26 23:55:29164 virtual HistoryService* GetHistoryService(ServiceAccessType sat) {
165 if (sat == EXPLICIT_ACCESS) {
166 return profile_->GetHistoryService(sat);
167 } else {
168 NOTREACHED() << "This profile is OffTheRecord";
169 return NULL;
170 }
171 }
172
initial.commit09911bf2008-07-26 23:55:29173 virtual WebDataService* GetWebDataService(ServiceAccessType sat) {
174 if (sat == EXPLICIT_ACCESS) {
175 return profile_->GetWebDataService(sat);
176 } else {
177 NOTREACHED() << "This profile is OffTheRecord";
178 return NULL;
179 }
180 }
181
182 virtual PrefService* GetPrefs() {
183 return profile_->GetPrefs();
184 }
185
186 virtual TemplateURLModel* GetTemplateURLModel() {
187 return profile_->GetTemplateURLModel();
188 }
189
190 virtual TemplateURLFetcher* GetTemplateURLFetcher() {
191 return profile_->GetTemplateURLFetcher();
192 }
193
194 virtual DownloadManager* GetDownloadManager() {
195 if (!download_manager_.get()) {
196 scoped_refptr<DownloadManager> dlm(new DownloadManager);
197 dlm->Init(this);
198 download_manager_.swap(dlm);
199 }
200 return download_manager_.get();
201 }
202
203 virtual bool HasCreatedDownloadManager() const {
204 return (download_manager_.get() != NULL);
205 }
206
207 virtual URLRequestContext* GetRequestContext() {
208 return request_context_;
209 }
210
[email protected]e7f29642009-03-02 22:53:18211 virtual URLRequestContext* GetRequestContextForMedia() {
212 if (!media_request_context_) {
213 FilePath cache_path = GetPath();
214 cache_path.Append(chrome::kOffTheRecordMediaCacheDirname);
215 media_request_context_ =
216 ChromeURLRequestContext::CreateOffTheRecordForMedia(
217 this, cache_path);
218 media_request_context_->AddRef();
219
220 DCHECK(media_request_context_->cookie_store());
221 }
222 return media_request_context_;
223 }
224
initial.commit09911bf2008-07-26 23:55:29225 virtual SessionService* GetSessionService() {
226 // Don't save any sessions when off the record.
227 return NULL;
228 }
229
230 virtual void ShutdownSessionService() {
231 // We don't allow a session service, nothing to do.
232 }
233
234 virtual bool HasSessionService() const {
235 // We never have a session service.
236 return false;
237 }
238
239 virtual std::wstring GetName() {
240 return profile_->GetName();
241 }
242
243 virtual void SetName(const std::wstring& name) {
244 profile_->SetName(name);
245 }
246
247 virtual std::wstring GetID() {
248 return profile_->GetID();
249 }
250
251 virtual void SetID(const std::wstring& id) {
252 profile_->SetID(id);
253 }
254
initial.commit09911bf2008-07-26 23:55:29255 virtual bool DidLastSessionExitCleanly() {
256 return profile_->DidLastSessionExitCleanly();
257 }
258
[email protected]d8e41ed2008-09-11 15:22:32259 virtual BookmarkModel* GetBookmarkModel() {
260 return profile_->GetBookmarkModel();
initial.commit09911bf2008-07-26 23:55:29261 }
262
[email protected]3a453fa2008-08-15 18:46:34263#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07264 virtual ProfilePersonalization* GetProfilePersonalization() {
[email protected]3a453fa2008-08-15 18:46:34265 return profile_->GetProfilePersonalization();
266 }
267#endif
268
initial.commit09911bf2008-07-26 23:55:29269 virtual bool IsSameProfile(Profile* profile) {
270 if (profile == static_cast<Profile*>(this))
271 return true;
272 return profile == profile_;
273 }
274
275 virtual Time GetStartTime() const {
276 return start_time_;
277 }
278
279 virtual TabRestoreService* GetTabRestoreService() {
280 return NULL;
281 }
282
[email protected]e7244d82008-10-29 18:13:26283 virtual void ResetTabRestoreService() {
[email protected]20930852008-10-15 19:30:41284 }
285
[email protected]e7244d82008-10-29 18:13:26286 virtual void ReinitializeSpellChecker() {
287 profile_->ReinitializeSpellChecker();
initial.commit09911bf2008-07-26 23:55:29288 }
289
290 virtual SpellChecker* GetSpellChecker() {
291 return profile_->GetSpellChecker();
292 }
293
294 virtual void MarkAsCleanShutdown() {
295 }
296
[email protected]bdbc87c2009-01-25 05:08:54297 virtual void InitExtensions() {
298 NOTREACHED();
299 }
300
initial.commit09911bf2008-07-26 23:55:29301 virtual void ExitedOffTheRecordMode() {
302 // Drop our download manager so we forget about all the downloads made
303 // in off-the-record mode.
304 download_manager_ = NULL;
305 }
306
307 virtual void Observe(NotificationType type,
308 const NotificationSource& source,
309 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56310 DCHECK_EQ(NotificationType::BROWSER_CLOSED, type.value);
initial.commit09911bf2008-07-26 23:55:29311 // We are only interested in OTR browser closing.
312 if (Source<Browser>(source)->profile() != this)
313 return;
314
315 // Let's check if we still have an Off The Record window opened.
316 // Note that we check against 1 as this notification is sent before the
317 // browser window is actually removed from the list.
318 if (BrowserList::GetBrowserCount(this) <= 1)
319 ExitedOffTheRecordMode();
320 }
321
322 private:
323 // The real underlying profile.
324 Profile* profile_;
325
[email protected]6ab9b202008-12-23 22:34:50326 // The context to use for requests made from this OTR session.
327 ChromeURLRequestContext* request_context_;
initial.commit09911bf2008-07-26 23:55:29328
[email protected]e7f29642009-03-02 22:53:18329 // The context for requests for media resources.
330 ChromeURLRequestContext* media_request_context_;
331
initial.commit09911bf2008-07-26 23:55:29332 // The download manager that only stores downloaded items in memory.
333 scoped_refptr<DownloadManager> download_manager_;
334
[email protected]34cc84f2009-02-13 10:04:35335 // We don't want SSLHostState from the OTR profile to leak back to the main
336 // profile because then the main profile would learn some of the host names
337 // the user visited while OTR.
338 scoped_ptr<SSLHostState> ssl_host_state_;
339
initial.commit09911bf2008-07-26 23:55:29340 // Time we were started.
341 Time start_time_;
342
343 DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl);
344};
345
[email protected]f7011fcb2009-01-28 21:54:32346ProfileImpl::ProfileImpl(const FilePath& path)
initial.commit09911bf2008-07-26 23:55:29347 : path_(path),
348 off_the_record_(false),
[email protected]3a453fa2008-08-15 18:46:34349#ifdef CHROME_PERSONALIZATION
350 personalization_(NULL),
351#endif
[email protected]f7011fcb2009-01-28 21:54:32352 request_context_(NULL),
353 history_service_created_(false),
354 created_web_data_service_(false),
355 created_download_manager_(false),
356 start_time_(Time::Now()),
357 spellchecker_(NULL),
initial.commit09911bf2008-07-26 23:55:29358 shutdown_session_service_(false) {
359 DCHECK(!path.empty()) << "Using an empty path will attempt to write " <<
360 "profile files to the root directory!";
[email protected]2d316662008-09-03 18:18:14361 create_session_service_timer_.Start(
362 TimeDelta::FromMilliseconds(kCreateSessionServiceDelayMS), this,
363 &ProfileImpl::EnsureSessionServiceCreated);
[email protected]bdbc87c2009-01-25 05:08:54364
[email protected]e7244d82008-10-29 18:13:26365 PrefService* prefs = GetPrefs();
366 prefs->AddPrefObserver(prefs::kSpellCheckDictionary, this);
367 prefs->AddPrefObserver(prefs::kEnableSpellCheck, this);
initial.commit09911bf2008-07-26 23:55:29368}
369
[email protected]bdbc87c2009-01-25 05:08:54370void ProfileImpl::InitExtensions() {
[email protected]8c756ac2009-01-30 23:36:41371 if (user_script_master_ || extensions_service_)
372 return; // Already initialized.
373
[email protected]bdbc87c2009-01-25 05:08:54374 const CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]f93fe782009-02-19 01:26:13375 PrefService* prefs = GetPrefs();
[email protected]bdbc87c2009-01-25 05:08:54376 bool user_scripts_enabled =
[email protected]f0a51fb52009-03-05 12:46:38377 command_line->HasSwitch(switches::kEnableUserScripts) ||
[email protected]f93fe782009-02-19 01:26:13378 prefs->GetBoolean(prefs::kEnableUserScripts);
[email protected]a9afddb2009-02-12 17:49:42379 bool extensions_enabled =
[email protected]f0a51fb52009-03-05 12:46:38380 command_line->HasSwitch(switches::kEnableExtensions) ||
[email protected]f93fe782009-02-19 01:26:13381 prefs->GetBoolean(prefs::kEnableExtensions);
[email protected]bdbc87c2009-01-25 05:08:54382
[email protected]f7011fcb2009-01-28 21:54:32383 FilePath script_dir;
[email protected]bdbc87c2009-01-25 05:08:54384 if (user_scripts_enabled) {
[email protected]0cd957b2009-03-06 20:13:23385 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,
[email protected]74c8b422009-03-11 00:34:12806 WideToASCII(prefs->GetString(prefs::kSpellCheckDictionary)),
807 GetRequestContext(),
[email protected]bd17b702009-02-25 20:44:08808 FilePath());
[email protected]e7244d82008-10-29 18:13:26809 spellchecker_->AddRef(); // Manual refcounting.
810 } else {
811 spellchecker_ = NULL;
812 }
[email protected]20930852008-10-15 19:30:41813
814 if (need_to_broadcast && io_thread) { // Notify resource message filters.
815 SpellcheckerReinitializedDetails scoped_spellchecker;
816 scoped_spellchecker.spellchecker = spellchecker_;
[email protected]e7244d82008-10-29 18:13:26817 if (io_thread) {
818 io_thread->message_loop()->PostTask(
[email protected]a9afddb2009-02-12 17:49:42819 FROM_HERE,
[email protected]e7244d82008-10-29 18:13:26820 new NotifySpellcheckerChangeTask(this, scoped_spellchecker));
821 }
[email protected]20930852008-10-15 19:30:41822 }
823}
824
[email protected]e7244d82008-10-29 18:13:26825void ProfileImpl::ReinitializeSpellChecker() {
826 InitializeSpellChecker(true);
827}
828
initial.commit09911bf2008-07-26 23:55:29829SpellChecker* ProfileImpl::GetSpellChecker() {
830 if (!spellchecker_) {
[email protected]20930852008-10-15 19:30:41831 // This is where spellchecker gets initialized. Note that this is being
832 // initialized in the ui_thread. However, this is not a problem as long as
833 // it is *used* in the io thread.
[email protected]34cc84f2009-02-13 10:04:35834 // TODO(sidchat): One day, change everything so that spellchecker gets
[email protected]20930852008-10-15 19:30:41835 // initialized in the IO thread itself.
[email protected]e7244d82008-10-29 18:13:26836 InitializeSpellChecker(false);
initial.commit09911bf2008-07-26 23:55:29837 }
[email protected]20930852008-10-15 19:30:41838
initial.commit09911bf2008-07-26 23:55:29839 return spellchecker_;
840}
841
842void ProfileImpl::MarkAsCleanShutdown() {
843 if (prefs_.get()) {
844 // The session cleanly exited, set kSessionExitedCleanly appropriately.
845 prefs_->SetBoolean(prefs::kSessionExitedCleanly, true);
846
847 // NOTE: If you change what thread this writes on, be sure and update
848 // ChromeFrame::EndSession().
849 prefs_->SavePersistentPrefs(g_browser_process->file_thread());
850 }
851}
852
[email protected]e7244d82008-10-29 18:13:26853void ProfileImpl::Observe(NotificationType type,
854 const NotificationSource& source,
855 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56856 if (NotificationType::PREF_CHANGED == type) {
[email protected]e7244d82008-10-29 18:13:26857 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
858 PrefService* prefs = Source<PrefService>(source).ptr();
859 DCHECK(pref_name_in && prefs);
860 if (*pref_name_in == prefs::kSpellCheckDictionary ||
861 *pref_name_in == prefs::kEnableSpellCheck) {
862 InitializeSpellChecker(true);
863 }
864 }
865}
866
initial.commit09911bf2008-07-26 23:55:29867void ProfileImpl::StopCreateSessionServiceTimer() {
[email protected]2d316662008-09-03 18:18:14868 create_session_service_timer_.Stop();
initial.commit09911bf2008-07-26 23:55:29869}
[email protected]3a453fa2008-08-15 18:46:34870
871#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07872ProfilePersonalization* ProfileImpl::GetProfilePersonalization() {
[email protected]bb95ef9e2009-03-05 23:20:29873 DCHECK(!Personalization::IsP13NDisabled());
[email protected]828e46f2009-02-27 02:43:51874 if (!personalization_.get())
875 personalization_.reset(
876 Personalization::CreateProfilePersonalization(this));
[email protected]57d3d0a2008-09-24 00:50:07877 return personalization_.get();
[email protected]3a453fa2008-08-15 18:46:34878}
license.botbf09a502008-08-24 00:55:55879#endif