blob: 9600c6cef599c585257a4844391ccd2e024c5daf [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
210 virtual SessionService* GetSessionService() {
211 // Don't save any sessions when off the record.
212 return NULL;
213 }
214
215 virtual void ShutdownSessionService() {
216 // We don't allow a session service, nothing to do.
217 }
218
219 virtual bool HasSessionService() const {
220 // We never have a session service.
221 return false;
222 }
223
224 virtual std::wstring GetName() {
225 return profile_->GetName();
226 }
227
228 virtual void SetName(const std::wstring& name) {
229 profile_->SetName(name);
230 }
231
232 virtual std::wstring GetID() {
233 return profile_->GetID();
234 }
235
236 virtual void SetID(const std::wstring& id) {
237 profile_->SetID(id);
238 }
239
initial.commit09911bf2008-07-26 23:55:29240 virtual bool DidLastSessionExitCleanly() {
241 return profile_->DidLastSessionExitCleanly();
242 }
243
[email protected]d8e41ed2008-09-11 15:22:32244 virtual BookmarkModel* GetBookmarkModel() {
245 return profile_->GetBookmarkModel();
initial.commit09911bf2008-07-26 23:55:29246 }
247
[email protected]3a453fa2008-08-15 18:46:34248#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07249 virtual ProfilePersonalization* GetProfilePersonalization() {
[email protected]3a453fa2008-08-15 18:46:34250 return profile_->GetProfilePersonalization();
251 }
252#endif
253
initial.commit09911bf2008-07-26 23:55:29254 virtual bool IsSameProfile(Profile* profile) {
255 if (profile == static_cast<Profile*>(this))
256 return true;
257 return profile == profile_;
258 }
259
260 virtual Time GetStartTime() const {
261 return start_time_;
262 }
263
264 virtual TabRestoreService* GetTabRestoreService() {
265 return NULL;
266 }
267
[email protected]e7244d82008-10-29 18:13:26268 virtual void ResetTabRestoreService() {
[email protected]20930852008-10-15 19:30:41269 }
270
[email protected]e7244d82008-10-29 18:13:26271 virtual void ReinitializeSpellChecker() {
272 profile_->ReinitializeSpellChecker();
initial.commit09911bf2008-07-26 23:55:29273 }
274
275 virtual SpellChecker* GetSpellChecker() {
276 return profile_->GetSpellChecker();
277 }
278
279 virtual void MarkAsCleanShutdown() {
280 }
281
[email protected]bdbc87c2009-01-25 05:08:54282 virtual void InitExtensions() {
283 NOTREACHED();
284 }
285
initial.commit09911bf2008-07-26 23:55:29286 virtual void ExitedOffTheRecordMode() {
287 // Drop our download manager so we forget about all the downloads made
288 // in off-the-record mode.
289 download_manager_ = NULL;
290 }
291
292 virtual void Observe(NotificationType type,
293 const NotificationSource& source,
294 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56295 DCHECK_EQ(NotificationType::BROWSER_CLOSED, type.value);
initial.commit09911bf2008-07-26 23:55:29296 // We are only interested in OTR browser closing.
297 if (Source<Browser>(source)->profile() != this)
298 return;
299
300 // Let's check if we still have an Off The Record window opened.
301 // Note that we check against 1 as this notification is sent before the
302 // browser window is actually removed from the list.
303 if (BrowserList::GetBrowserCount(this) <= 1)
304 ExitedOffTheRecordMode();
305 }
306
307 private:
308 // The real underlying profile.
309 Profile* profile_;
310
[email protected]6ab9b202008-12-23 22:34:50311 // The context to use for requests made from this OTR session.
312 ChromeURLRequestContext* request_context_;
initial.commit09911bf2008-07-26 23:55:29313
314 // The download manager that only stores downloaded items in memory.
315 scoped_refptr<DownloadManager> download_manager_;
316
[email protected]34cc84f2009-02-13 10:04:35317 // We don't want SSLHostState from the OTR profile to leak back to the main
318 // profile because then the main profile would learn some of the host names
319 // the user visited while OTR.
320 scoped_ptr<SSLHostState> ssl_host_state_;
321
initial.commit09911bf2008-07-26 23:55:29322 // Time we were started.
323 Time start_time_;
324
325 DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl);
326};
327
[email protected]f7011fcb2009-01-28 21:54:32328ProfileImpl::ProfileImpl(const FilePath& path)
initial.commit09911bf2008-07-26 23:55:29329 : path_(path),
330 off_the_record_(false),
[email protected]3a453fa2008-08-15 18:46:34331#ifdef CHROME_PERSONALIZATION
332 personalization_(NULL),
333#endif
[email protected]f7011fcb2009-01-28 21:54:32334 request_context_(NULL),
335 history_service_created_(false),
336 created_web_data_service_(false),
337 created_download_manager_(false),
338 start_time_(Time::Now()),
339 spellchecker_(NULL),
initial.commit09911bf2008-07-26 23:55:29340 shutdown_session_service_(false) {
341 DCHECK(!path.empty()) << "Using an empty path will attempt to write " <<
342 "profile files to the root directory!";
[email protected]2d316662008-09-03 18:18:14343 create_session_service_timer_.Start(
344 TimeDelta::FromMilliseconds(kCreateSessionServiceDelayMS), this,
345 &ProfileImpl::EnsureSessionServiceCreated);
[email protected]bdbc87c2009-01-25 05:08:54346
[email protected]e7244d82008-10-29 18:13:26347 PrefService* prefs = GetPrefs();
348 prefs->AddPrefObserver(prefs::kSpellCheckDictionary, this);
349 prefs->AddPrefObserver(prefs::kEnableSpellCheck, this);
initial.commit09911bf2008-07-26 23:55:29350}
351
[email protected]bdbc87c2009-01-25 05:08:54352void ProfileImpl::InitExtensions() {
[email protected]8c756ac2009-01-30 23:36:41353 if (user_script_master_ || extensions_service_)
354 return; // Already initialized.
355
[email protected]bdbc87c2009-01-25 05:08:54356 const CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]f93fe782009-02-19 01:26:13357 PrefService* prefs = GetPrefs();
[email protected]bdbc87c2009-01-25 05:08:54358 bool user_scripts_enabled =
[email protected]f93fe782009-02-19 01:26:13359 command_line->HasSwitch(switches::kEnableUserScripts) ||
360 prefs->GetBoolean(prefs::kEnableUserScripts);
[email protected]a9afddb2009-02-12 17:49:42361 bool extensions_enabled =
[email protected]f93fe782009-02-19 01:26:13362 command_line->HasSwitch(switches::kEnableExtensions) ||
363 prefs->GetBoolean(prefs::kEnableExtensions);
[email protected]bdbc87c2009-01-25 05:08:54364
[email protected]f7011fcb2009-01-28 21:54:32365 FilePath script_dir;
[email protected]bdbc87c2009-01-25 05:08:54366 if (user_scripts_enabled) {
367 script_dir = GetPath();
[email protected]f7011fcb2009-01-28 21:54:32368 script_dir = script_dir.Append(chrome::kUserScriptsDirname);
[email protected]bdbc87c2009-01-25 05:08:54369 }
370
[email protected]bb28e062009-02-27 17:19:18371 ExtensionErrorReporter::Init(true); // allow noisy errors.
[email protected]bdbc87c2009-01-25 05:08:54372 user_script_master_ = new UserScriptMaster(
[email protected]f7011fcb2009-01-28 21:54:32373 g_browser_process->file_thread()->message_loop(), script_dir);
[email protected]bdbc87c2009-01-25 05:08:54374 extensions_service_ = new ExtensionsService(
375 FilePath(GetPath()), user_script_master_.get());
376
377 // If we have extensions, the extension service will kick off the first scan
378 // after extensions are loaded. Otherwise, we need to do that now.
379 if (extensions_enabled)
380 extensions_service_->Init();
381 else if (user_scripts_enabled)
382 user_script_master_->StartScan();
383}
384
initial.commit09911bf2008-07-26 23:55:29385ProfileImpl::~ProfileImpl() {
[email protected]169627b2008-12-06 19:30:19386 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29387
388 StopCreateSessionServiceTimer();
389 // TemplateURLModel schedules a task on the WebDataService from its
390 // destructor. Delete it first to ensure the task gets scheduled before we
391 // shut down the database.
392 template_url_model_.reset();
393
394 // The download manager queries the history system and should be deleted
395 // before the history is shutdown so it can properly cancel all requests.
396 download_manager_ = NULL;
397
[email protected]e7244d82008-10-29 18:13:26398 // Remove pref observers.
399 PrefService* prefs = GetPrefs();
400 prefs->RemovePrefObserver(prefs::kSpellCheckDictionary, this);
401 prefs->RemovePrefObserver(prefs::kEnableSpellCheck, this);
402
[email protected]3a453fa2008-08-15 18:46:34403#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07404 personalization_.reset();
[email protected]3a453fa2008-08-15 18:46:34405#endif
406
initial.commit09911bf2008-07-26 23:55:29407 // Both HistoryService and WebDataService maintain threads for background
408 // processing. Its possible each thread still has tasks on it that have
409 // increased the ref count of the service. In such a situation, when we
410 // decrement the refcount, it won't be 0, and the threads/databases aren't
411 // properly shut down. By explicitly calling Cleanup/Shutdown we ensure the
412 // databases are properly closed.
413 if (web_data_service_.get())
414 web_data_service_->Shutdown();
415
416 if (history_service_.get())
417 history_service_->Cleanup();
418
419 // The I/O thread may be NULL during testing.
[email protected]ab820df2008-08-26 05:55:10420 base::Thread* io_thread = g_browser_process->io_thread();
initial.commit09911bf2008-07-26 23:55:29421
422 if (spellchecker_) {
423 // The spellchecker must be deleted on the I/O thread. During testing, we
424 // don't have an I/O thread.
425 if (io_thread)
426 io_thread->message_loop()->ReleaseSoon(FROM_HERE, spellchecker_);
427 else
428 spellchecker_->Release();
429 }
430
431 if (request_context_) {
[email protected]6ab9b202008-12-23 22:34:50432 request_context_->CleanupOnUIThread();
433
434 if (default_request_context_ == request_context_)
435 default_request_context_ = NULL;
436
initial.commit09911bf2008-07-26 23:55:29437 // Clean up request context on IO thread.
[email protected]6ab9b202008-12-23 22:34:50438 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
439 NewRunnableMethod(request_context_,
440 &base::RefCountedThreadSafe<URLRequestContext>::Release));
initial.commit09911bf2008-07-26 23:55:29441 request_context_ = NULL;
442 }
443
[email protected]d8e41ed2008-09-11 15:22:32444 // HistoryService may call into the BookmarkModel, as such we need to
445 // delete HistoryService before the BookmarkModel. The destructor for
[email protected]90ef13132008-08-27 03:27:46446 // HistoryService will join with HistoryService's backend thread so that
447 // by the time the destructor has finished we're sure it will no longer call
[email protected]d8e41ed2008-09-11 15:22:32448 // into the BookmarkModel.
[email protected]90ef13132008-08-27 03:27:46449 history_service_ = NULL;
450 bookmark_bar_model_.reset();
451
initial.commit09911bf2008-07-26 23:55:29452 MarkAsCleanShutdown();
453}
454
[email protected]f7011fcb2009-01-28 21:54:32455FilePath ProfileImpl::GetPath() {
initial.commit09911bf2008-07-26 23:55:29456 return path_;
457}
458
459bool ProfileImpl::IsOffTheRecord() {
460 return false;
461}
462
463Profile* ProfileImpl::GetOffTheRecordProfile() {
464 if (!off_the_record_profile_.get()) {
465 scoped_ptr<OffTheRecordProfileImpl> p(new OffTheRecordProfileImpl(this));
466 off_the_record_profile_.swap(p);
467 }
468 return off_the_record_profile_.get();
469}
470
471Profile* ProfileImpl::GetOriginalProfile() {
472 return this;
473}
474
[email protected]176aa482008-11-14 03:25:15475static void BroadcastNewHistoryTable(base::SharedMemory* table_memory) {
initial.commit09911bf2008-07-26 23:55:29476 if (!table_memory)
477 return;
478
479 // send to all RenderProcessHosts
480 for (RenderProcessHost::iterator i = RenderProcessHost::begin();
481 i != RenderProcessHost::end(); i++) {
482 if (!i->second->channel())
483 continue;
484
[email protected]176aa482008-11-14 03:25:15485 base::SharedMemoryHandle new_table;
[email protected]f7011fcb2009-01-28 21:54:32486 base::ProcessHandle process = i->second->process().handle();
initial.commit09911bf2008-07-26 23:55:29487 if (!process) {
488 // process can be null if it's started with the --single-process flag.
[email protected]f7011fcb2009-01-28 21:54:32489 process = base::Process::Current().handle();
initial.commit09911bf2008-07-26 23:55:29490 }
491
492 table_memory->ShareToProcess(process, &new_table);
493 IPC::Message* msg = new ViewMsg_VisitedLink_NewTable(new_table);
494 i->second->channel()->Send(msg);
495 }
496}
497
498VisitedLinkMaster* ProfileImpl::GetVisitedLinkMaster() {
499 if (!visited_link_master_.get()) {
500 scoped_ptr<VisitedLinkMaster> visited_links(
501 new VisitedLinkMaster(g_browser_process->file_thread(),
502 BroadcastNewHistoryTable, this));
503 if (!visited_links->Init())
504 return NULL;
505 visited_link_master_.swap(visited_links);
506 }
507
508 return visited_link_master_.get();
509}
510
[email protected]6014d672008-12-05 00:38:25511ExtensionsService* ProfileImpl::GetExtensionsService() {
512 return extensions_service_.get();
513}
514
[email protected]0938d3c2009-01-09 20:37:35515UserScriptMaster* ProfileImpl::GetUserScriptMaster() {
[email protected]0938d3c2009-01-09 20:37:35516 return user_script_master_.get();
[email protected]04fba9a92008-10-28 17:25:25517}
518
[email protected]34cc84f2009-02-13 10:04:35519SSLHostState* ProfileImpl::GetSSLHostState() {
520 if (!ssl_host_state_.get())
521 ssl_host_state_.reset(new SSLHostState());
522
523 DCHECK(ssl_host_state_->CalledOnValidThread());
524 return ssl_host_state_.get();
525}
526
initial.commit09911bf2008-07-26 23:55:29527PrefService* ProfileImpl::GetPrefs() {
528 if (!prefs_.get()) {
[email protected]f7011fcb2009-01-28 21:54:32529 prefs_.reset(new PrefService(GetPrefFilePath().ToWStringHack()));
initial.commit09911bf2008-07-26 23:55:29530
531 // The Profile class and ProfileManager class may read some prefs so
532 // register known prefs as soon as possible.
533 Profile::RegisterUserPrefs(prefs_.get());
534 ProfileManager::RegisterUserPrefs(prefs_.get());
535
536 // The last session exited cleanly if there is no pref for
537 // kSessionExitedCleanly or the value for kSessionExitedCleanly is true.
538 last_session_exited_cleanly_ =
539 prefs_->GetBoolean(prefs::kSessionExitedCleanly);
540 // Mark the session as open.
541 prefs_->SetBoolean(prefs::kSessionExitedCleanly, false);
542 // Make sure we save to disk that the session has opened.
543 prefs_->ScheduleSavePersistentPrefs(g_browser_process->file_thread());
544 }
545
546 return prefs_.get();
547}
548
[email protected]f7011fcb2009-01-28 21:54:32549FilePath ProfileImpl::GetPrefFilePath() {
550 FilePath pref_file_path = path_;
551 pref_file_path = pref_file_path.Append(chrome::kPreferencesFilename);
initial.commit09911bf2008-07-26 23:55:29552 return pref_file_path;
553}
554
555URLRequestContext* ProfileImpl::GetRequestContext() {
556 if (!request_context_) {
[email protected]f7011fcb2009-01-28 21:54:32557 FilePath cookie_path = GetPath();
558 cookie_path = cookie_path.Append(chrome::kCookieFilename);
559 FilePath cache_path = GetPath();
[email protected]2b2830a2009-02-07 01:58:42560
561 // Override the cache location if specified by the user.
562 const std::wstring user_cache_dir(
563 CommandLine::ForCurrentProcess()->GetSwitchValue(
564 switches::kDiskCacheDir));
565 if (!user_cache_dir.empty()) {
566 cache_path = FilePath::FromWStringHack(user_cache_dir);
567 }
568
[email protected]f7011fcb2009-01-28 21:54:32569 cache_path = cache_path.Append(chrome::kCacheDirname);
[email protected]6ab9b202008-12-23 22:34:50570 request_context_ = ChromeURLRequestContext::CreateOriginal(
571 this, cookie_path, cache_path);
initial.commit09911bf2008-07-26 23:55:29572 request_context_->AddRef();
573
[email protected]6ab9b202008-12-23 22:34:50574 // The first request context is always a normal (non-OTR) request context.
575 // Even when Chromium is started in OTR mode, a normal profile is always
576 // created first.
577 if (!default_request_context_) {
578 default_request_context_ = request_context_;
579 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56580 NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE,
[email protected]6ab9b202008-12-23 22:34:50581 NotificationService::AllSources(), NotificationService::NoDetails());
582 }
583
initial.commit09911bf2008-07-26 23:55:29584 DCHECK(request_context_->cookie_store());
585 }
586
587 return request_context_;
588}
589
590HistoryService* ProfileImpl::GetHistoryService(ServiceAccessType sat) {
591 if (!history_service_created_) {
[email protected]90ef13132008-08-27 03:27:46592 history_service_created_ = true;
initial.commit09911bf2008-07-26 23:55:29593 scoped_refptr<HistoryService> history(new HistoryService(this));
[email protected]d8e41ed2008-09-11 15:22:32594 if (!history->Init(GetPath(), GetBookmarkModel()))
initial.commit09911bf2008-07-26 23:55:29595 return NULL;
596 history_service_.swap(history);
initial.commit09911bf2008-07-26 23:55:29597
598 // Send out the notification that the history service was created.
599 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56600 Notify(NotificationType::HISTORY_CREATED, Source<Profile>(this),
initial.commit09911bf2008-07-26 23:55:29601 Details<HistoryService>(history_service_.get()));
602 }
603 return history_service_.get();
604}
605
initial.commit09911bf2008-07-26 23:55:29606TemplateURLModel* ProfileImpl::GetTemplateURLModel() {
607 if (!template_url_model_.get())
608 template_url_model_.reset(new TemplateURLModel(this));
609 return template_url_model_.get();
610}
611
612TemplateURLFetcher* ProfileImpl::GetTemplateURLFetcher() {
613 if (!template_url_fetcher_.get())
614 template_url_fetcher_.reset(new TemplateURLFetcher(this));
615 return template_url_fetcher_.get();
616}
617
618WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) {
619 if (!created_web_data_service_)
620 CreateWebDataService();
621 return web_data_service_.get();
622}
623
624void ProfileImpl::CreateWebDataService() {
625 DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL);
626 created_web_data_service_ = true;
627 scoped_refptr<WebDataService> wds(new WebDataService());
628 if (!wds->Init(GetPath()))
629 return;
630 web_data_service_.swap(wds);
631}
632
633DownloadManager* ProfileImpl::GetDownloadManager() {
634 if (!created_download_manager_) {
635 scoped_refptr<DownloadManager> dlm(new DownloadManager);
636 dlm->Init(this);
637 created_download_manager_ = true;
638 download_manager_.swap(dlm);
639 }
640 return download_manager_.get();
641}
642
643bool ProfileImpl::HasCreatedDownloadManager() const {
644 return created_download_manager_;
645}
646
647SessionService* ProfileImpl::GetSessionService() {
648 if (!session_service_.get() && !shutdown_session_service_) {
649 session_service_ = new SessionService(this);
650 session_service_->ResetFromCurrentBrowsers();
651 }
652 return session_service_.get();
653}
654
655void ProfileImpl::ShutdownSessionService() {
656 if (shutdown_session_service_)
657 return;
658
659 // We're about to exit, force creation of the session service if it hasn't
660 // been created yet. We do this to ensure session state matches the point in
661 // time the user exited.
662 GetSessionService();
663 shutdown_session_service_ = true;
664 session_service_ = NULL;
665}
666
667bool ProfileImpl::HasSessionService() const {
668 return (session_service_.get() != NULL);
669}
670
671std::wstring ProfileImpl::GetName() {
672 return GetPrefs()->GetString(prefs::kProfileName);
673}
674void ProfileImpl::SetName(const std::wstring& name) {
675 GetPrefs()->SetString(prefs::kProfileName, name);
676}
677
678std::wstring ProfileImpl::GetID() {
679 return GetPrefs()->GetString(prefs::kProfileID);
680}
681void ProfileImpl::SetID(const std::wstring& id) {
682 GetPrefs()->SetString(prefs::kProfileID, id);
683}
684
initial.commit09911bf2008-07-26 23:55:29685bool ProfileImpl::DidLastSessionExitCleanly() {
686 // last_session_exited_cleanly_ is set when the preferences are loaded. Force
687 // it to be set by asking for the prefs.
688 GetPrefs();
689 return last_session_exited_cleanly_;
690}
691
[email protected]d8e41ed2008-09-11 15:22:32692BookmarkModel* ProfileImpl::GetBookmarkModel() {
[email protected]90ef13132008-08-27 03:27:46693 if (!bookmark_bar_model_.get()) {
[email protected]d8e41ed2008-09-11 15:22:32694 bookmark_bar_model_.reset(new BookmarkModel(this));
[email protected]90ef13132008-08-27 03:27:46695 bookmark_bar_model_->Load();
696 }
initial.commit09911bf2008-07-26 23:55:29697 return bookmark_bar_model_.get();
698}
699
700bool ProfileImpl::IsSameProfile(Profile* profile) {
701 if (profile == static_cast<Profile*>(this))
702 return true;
703 OffTheRecordProfileImpl* otr_profile = off_the_record_profile_.get();
704 return otr_profile && profile == static_cast<Profile*>(otr_profile);
705}
706
707Time ProfileImpl::GetStartTime() const {
708 return start_time_;
709}
710
711TabRestoreService* ProfileImpl::GetTabRestoreService() {
712 if (!tab_restore_service_.get())
[email protected]169627b2008-12-06 19:30:19713 tab_restore_service_ = new TabRestoreService(this);
initial.commit09911bf2008-07-26 23:55:29714 return tab_restore_service_.get();
715}
716
717void ProfileImpl::ResetTabRestoreService() {
[email protected]169627b2008-12-06 19:30:19718 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29719}
720
[email protected]a9afddb2009-02-12 17:49:42721// To be run in the IO thread to notify all resource message filters that the
[email protected]20930852008-10-15 19:30:41722// spellchecker has changed.
723class NotifySpellcheckerChangeTask : public Task {
724 public:
725 NotifySpellcheckerChangeTask(
[email protected]e7244d82008-10-29 18:13:26726 Profile* profile,
727 const SpellcheckerReinitializedDetails& spellchecker)
[email protected]20930852008-10-15 19:30:41728 : profile_(profile),
729 spellchecker_(spellchecker) {
730 }
731
732 private:
733 void Run(void) {
734 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56735 NotificationType::SPELLCHECKER_REINITIALIZED,
[email protected]20930852008-10-15 19:30:41736 Source<Profile>(profile_),
737 Details<SpellcheckerReinitializedDetails>(&spellchecker_));
738 }
739
740 Profile* profile_;
741 SpellcheckerReinitializedDetails spellchecker_;
742};
743
[email protected]e7244d82008-10-29 18:13:26744void ProfileImpl::InitializeSpellChecker(bool need_to_broadcast) {
[email protected]20930852008-10-15 19:30:41745 // The I/O thread may be NULL during testing.
746 base::Thread* io_thread = g_browser_process->io_thread();
747 if (spellchecker_) {
748 // The spellchecker must be deleted on the I/O thread.
749 // A dummy variable to aid in logical clarity.
750 SpellChecker* last_spellchecker = spellchecker_;
751
752 if (io_thread)
753 io_thread->message_loop()->ReleaseSoon(FROM_HERE, last_spellchecker);
754 else // during testing, we don't have an I/O thread
755 last_spellchecker->Release();
[email protected]20930852008-10-15 19:30:41756 }
757
[email protected]20930852008-10-15 19:30:41758 // Retrieve the (perhaps updated recently) dictionary name from preferences.
759 PrefService* prefs = GetPrefs();
[email protected]e7244d82008-10-29 18:13:26760 bool enable_spellcheck = prefs->GetBoolean(prefs::kEnableSpellCheck);
[email protected]20930852008-10-15 19:30:41761
[email protected]e7244d82008-10-29 18:13:26762 if (enable_spellcheck) {
[email protected]bd17b702009-02-25 20:44:08763 FilePath dict_dir;
[email protected]e7244d82008-10-29 18:13:26764 PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
[email protected]a9afddb2009-02-12 17:49:42765 // Note that, as the object pointed to by previously by spellchecker_
[email protected]e7244d82008-10-29 18:13:26766 // is being deleted in the io thread, the spellchecker_ can be made to point
767 // to a new object (RE-initialized) in parallel in this UI thread.
[email protected]4b4d1adc2008-12-10 22:28:58768 spellchecker_ = new SpellChecker(dict_dir,
769 prefs->GetString(prefs::kSpellCheckDictionary), GetRequestContext(),
[email protected]bd17b702009-02-25 20:44:08770 FilePath());
[email protected]e7244d82008-10-29 18:13:26771 spellchecker_->AddRef(); // Manual refcounting.
772 } else {
773 spellchecker_ = NULL;
774 }
[email protected]20930852008-10-15 19:30:41775
776 if (need_to_broadcast && io_thread) { // Notify resource message filters.
777 SpellcheckerReinitializedDetails scoped_spellchecker;
778 scoped_spellchecker.spellchecker = spellchecker_;
[email protected]e7244d82008-10-29 18:13:26779 if (io_thread) {
780 io_thread->message_loop()->PostTask(
[email protected]a9afddb2009-02-12 17:49:42781 FROM_HERE,
[email protected]e7244d82008-10-29 18:13:26782 new NotifySpellcheckerChangeTask(this, scoped_spellchecker));
783 }
[email protected]20930852008-10-15 19:30:41784 }
785}
786
[email protected]e7244d82008-10-29 18:13:26787void ProfileImpl::ReinitializeSpellChecker() {
788 InitializeSpellChecker(true);
789}
790
initial.commit09911bf2008-07-26 23:55:29791SpellChecker* ProfileImpl::GetSpellChecker() {
792 if (!spellchecker_) {
[email protected]20930852008-10-15 19:30:41793 // This is where spellchecker gets initialized. Note that this is being
794 // initialized in the ui_thread. However, this is not a problem as long as
795 // it is *used* in the io thread.
[email protected]34cc84f2009-02-13 10:04:35796 // TODO(sidchat): One day, change everything so that spellchecker gets
[email protected]20930852008-10-15 19:30:41797 // initialized in the IO thread itself.
[email protected]e7244d82008-10-29 18:13:26798 InitializeSpellChecker(false);
initial.commit09911bf2008-07-26 23:55:29799 }
[email protected]20930852008-10-15 19:30:41800
initial.commit09911bf2008-07-26 23:55:29801 return spellchecker_;
802}
803
804void ProfileImpl::MarkAsCleanShutdown() {
805 if (prefs_.get()) {
806 // The session cleanly exited, set kSessionExitedCleanly appropriately.
807 prefs_->SetBoolean(prefs::kSessionExitedCleanly, true);
808
809 // NOTE: If you change what thread this writes on, be sure and update
810 // ChromeFrame::EndSession().
811 prefs_->SavePersistentPrefs(g_browser_process->file_thread());
812 }
813}
814
[email protected]e7244d82008-10-29 18:13:26815void ProfileImpl::Observe(NotificationType type,
816 const NotificationSource& source,
817 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56818 if (NotificationType::PREF_CHANGED == type) {
[email protected]e7244d82008-10-29 18:13:26819 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
820 PrefService* prefs = Source<PrefService>(source).ptr();
821 DCHECK(pref_name_in && prefs);
822 if (*pref_name_in == prefs::kSpellCheckDictionary ||
823 *pref_name_in == prefs::kEnableSpellCheck) {
824 InitializeSpellChecker(true);
825 }
826 }
827}
828
initial.commit09911bf2008-07-26 23:55:29829void ProfileImpl::StopCreateSessionServiceTimer() {
[email protected]2d316662008-09-03 18:18:14830 create_session_service_timer_.Stop();
initial.commit09911bf2008-07-26 23:55:29831}
[email protected]3a453fa2008-08-15 18:46:34832
833#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07834ProfilePersonalization* ProfileImpl::GetProfilePersonalization() {
[email protected]828e46f2009-02-27 02:43:51835 if (!personalization_.get())
836 personalization_.reset(
837 Personalization::CreateProfilePersonalization(this));
[email protected]57d3d0a2008-09-24 00:50:07838 return personalization_.get();
[email protected]3a453fa2008-08-15 18:46:34839}
license.botbf09a502008-08-24 00:55:55840#endif