blob: 86e9d1642215953c514ddea95310ba6b0d1bd23c [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]6014d672008-12-05 00:38:2517#include "chrome/browser/extensions/extensions_service.h"
[email protected]0938d3c2009-01-09 20:37:3518#include "chrome/browser/extensions/user_script_master.h"
[email protected]a9afddb2009-02-12 17:49:4219#include "chrome/browser/history/history.h"
[email protected]6ab9b202008-12-23 22:34:5020#include "chrome/browser/net/chrome_url_request_context.h"
initial.commit09911bf2008-07-26 23:55:2921#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2622#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]f63ae312009-02-04 17:58:4623#include "chrome/browser/search_engines/template_url_model.h"
[email protected]34cc84f2009-02-13 10:04:3524#include "chrome/browser/ssl/ssl_host_state.h"
[email protected]85e921fb82009-02-11 23:19:4425#include "chrome/browser/sessions/session_service.h"
[email protected]bd580a252009-02-12 01:16:3026#include "chrome/browser/sessions/tab_restore_service.h"
[email protected]f0a644292009-02-25 23:32:4727#include "chrome/browser/spellchecker.h"
initial.commit09911bf2008-07-26 23:55:2928#include "chrome/browser/visitedlink_master.h"
29#include "chrome/browser/webdata/web_data_service.h"
30#include "chrome/common/chrome_constants.h"
31#include "chrome/common/chrome_paths.h"
32#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2933#include "chrome/common/notification_service.h"
34#include "chrome/common/pref_names.h"
[email protected]f7011fcb2009-01-28 21:54:3235#include "chrome/common/render_messages.h"
[email protected]f90f5c512009-02-18 19:10:5836#include "grit/locale_settings.h"
initial.commit09911bf2008-07-26 23:55:2937
[email protected]f7011fcb2009-01-28 21:54:3238#if defined(OS_POSIX)
39// TODO(port): get rid of this include. It's used just to provide declarations
40// and stub definitions for classes we encouter during the porting effort.
41#include "chrome/common/temp_scaffolding_stubs.h"
42#endif
43
44// TODO(port): Get rid of this section and finish porting.
45#if defined(OS_WIN)
[email protected]f7011fcb2009-01-28 21:54:3246#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]f7011fcb2009-01-28 21:54:3247#endif
48
[email protected]e1acf6f2008-10-27 20:43:3349using base::Time;
50using base::TimeDelta;
51
initial.commit09911bf2008-07-26 23:55:2952// Delay, in milliseconds, before we explicitly create the SessionService.
53static const int kCreateSessionServiceDelayMS = 500;
54
55// A pointer to the request context for the default profile. See comments on
56// Profile::GetDefaultRequestContext.
57URLRequestContext* Profile::default_request_context_;
58
[email protected]34cc84f2009-02-13 10:04:3559// static
initial.commit09911bf2008-07-26 23:55:2960void Profile::RegisterUserPrefs(PrefService* prefs) {
[email protected]430d3f72008-10-27 17:56:5561 prefs->RegisterBooleanPref(prefs::kSearchSuggestEnabled, true);
initial.commit09911bf2008-07-26 23:55:2962 prefs->RegisterBooleanPref(prefs::kSessionExitedCleanly, true);
63 prefs->RegisterBooleanPref(prefs::kSafeBrowsingEnabled, true);
[email protected]f7011fcb2009-01-28 21:54:3264#if defined(OS_MACOSX)
65 // MASSIVE HACK!!! We don't have localization working yet. Undo this once we
66 // do. TODO(port): take this out
67 prefs->RegisterStringPref(prefs::kSpellCheckDictionary,
68 L"IDS_SPELLCHECK_DICTIONARY");
69#elif defined(OS_WIN) || defined(OS_LINUX)
[email protected]e7244d82008-10-29 18:13:2670 prefs->RegisterLocalizedStringPref(prefs::kSpellCheckDictionary,
71 IDS_SPELLCHECK_DICTIONARY);
[email protected]f7011fcb2009-01-28 21:54:3272#endif
[email protected]e7244d82008-10-29 18:13:2673 prefs->RegisterBooleanPref(prefs::kEnableSpellCheck, true);
[email protected]f93fe782009-02-19 01:26:1374 prefs->RegisterBooleanPref(prefs::kEnableUserScripts, false);
75 prefs->RegisterBooleanPref(prefs::kEnableExtensions, false);
initial.commit09911bf2008-07-26 23:55:2976}
77
[email protected]34cc84f2009-02-13 10:04:3578// static
[email protected]f7011fcb2009-01-28 21:54:3279Profile* Profile::CreateProfile(const FilePath& path) {
initial.commit09911bf2008-07-26 23:55:2980 return new ProfileImpl(path);
81}
82
[email protected]34cc84f2009-02-13 10:04:3583// static
initial.commit09911bf2008-07-26 23:55:2984URLRequestContext* Profile::GetDefaultRequestContext() {
85 return default_request_context_;
86}
87
88
initial.commit09911bf2008-07-26 23:55:2989////////////////////////////////////////////////////////////////////////////////
90//
91// OffTheRecordProfileImpl is a profile subclass that wraps an existing profile
92// to make it suitable for the off the record mode.
93//
94////////////////////////////////////////////////////////////////////////////////
95class OffTheRecordProfileImpl : public Profile,
96 public NotificationObserver {
97 public:
98 explicit OffTheRecordProfileImpl(Profile* real_profile)
99 : profile_(real_profile),
100 start_time_(Time::Now()) {
[email protected]6ab9b202008-12-23 22:34:50101 request_context_ = ChromeURLRequestContext::CreateOffTheRecord(this);
initial.commit09911bf2008-07-26 23:55:29102 request_context_->AddRef();
103 // Register for browser close notifications so we can detect when the last
104 // off-the-record window is closed, in which case we can clean our states
105 // (cookies, downloads...).
106 NotificationService::current()->AddObserver(
[email protected]bfd04a62009-02-01 18:16:56107 this,
108 NotificationType::BROWSER_CLOSED,
109 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29110 }
111
112 virtual ~OffTheRecordProfileImpl() {
113 if (request_context_) {
[email protected]6ab9b202008-12-23 22:34:50114 request_context_->CleanupOnUIThread();
115
initial.commit09911bf2008-07-26 23:55:29116 // Clean up request context on IO thread.
117 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]6ab9b202008-12-23 22:34:50118 NewRunnableMethod(request_context_,
119 &base::RefCountedThreadSafe<URLRequestContext>::Release));
initial.commit09911bf2008-07-26 23:55:29120 request_context_ = NULL;
121 }
122 NotificationService::current()->RemoveObserver(
[email protected]bfd04a62009-02-01 18:16:56123 this,
124 NotificationType::BROWSER_CLOSED,
125 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29126 }
127
[email protected]f7011fcb2009-01-28 21:54:32128 virtual FilePath GetPath() { return profile_->GetPath(); }
initial.commit09911bf2008-07-26 23:55:29129
130 virtual bool IsOffTheRecord() {
131 return true;
132 }
133
134 virtual Profile* GetOffTheRecordProfile() {
135 return this;
136 }
137
138 virtual Profile* GetOriginalProfile() {
139 return profile_;
140 }
141
142 virtual VisitedLinkMaster* GetVisitedLinkMaster() {
143 return profile_->GetVisitedLinkMaster();
144 }
145
[email protected]6014d672008-12-05 00:38:25146 virtual ExtensionsService* GetExtensionsService() {
147 return profile_->GetExtensionsService();
148 }
149
[email protected]0938d3c2009-01-09 20:37:35150 virtual UserScriptMaster* GetUserScriptMaster() {
151 return profile_->GetUserScriptMaster();
[email protected]04fba9a92008-10-28 17:25:25152 }
153
[email protected]34cc84f2009-02-13 10:04:35154 virtual SSLHostState* GetSSLHostState() {
155 if (!ssl_host_state_.get())
156 ssl_host_state_.reset(new SSLHostState());
157
158 DCHECK(ssl_host_state_->CalledOnValidThread());
159 return ssl_host_state_.get();
160 }
161
initial.commit09911bf2008-07-26 23:55:29162 virtual HistoryService* GetHistoryService(ServiceAccessType sat) {
163 if (sat == EXPLICIT_ACCESS) {
164 return profile_->GetHistoryService(sat);
165 } else {
166 NOTREACHED() << "This profile is OffTheRecord";
167 return NULL;
168 }
169 }
170
initial.commit09911bf2008-07-26 23:55:29171 virtual WebDataService* GetWebDataService(ServiceAccessType sat) {
172 if (sat == EXPLICIT_ACCESS) {
173 return profile_->GetWebDataService(sat);
174 } else {
175 NOTREACHED() << "This profile is OffTheRecord";
176 return NULL;
177 }
178 }
179
180 virtual PrefService* GetPrefs() {
181 return profile_->GetPrefs();
182 }
183
184 virtual TemplateURLModel* GetTemplateURLModel() {
185 return profile_->GetTemplateURLModel();
186 }
187
188 virtual TemplateURLFetcher* GetTemplateURLFetcher() {
189 return profile_->GetTemplateURLFetcher();
190 }
191
192 virtual DownloadManager* GetDownloadManager() {
193 if (!download_manager_.get()) {
194 scoped_refptr<DownloadManager> dlm(new DownloadManager);
195 dlm->Init(this);
196 download_manager_.swap(dlm);
197 }
198 return download_manager_.get();
199 }
200
201 virtual bool HasCreatedDownloadManager() const {
202 return (download_manager_.get() != NULL);
203 }
204
205 virtual URLRequestContext* GetRequestContext() {
206 return request_context_;
207 }
208
209 virtual SessionService* GetSessionService() {
210 // Don't save any sessions when off the record.
211 return NULL;
212 }
213
214 virtual void ShutdownSessionService() {
215 // We don't allow a session service, nothing to do.
216 }
217
218 virtual bool HasSessionService() const {
219 // We never have a session service.
220 return false;
221 }
222
223 virtual std::wstring GetName() {
224 return profile_->GetName();
225 }
226
227 virtual void SetName(const std::wstring& name) {
228 profile_->SetName(name);
229 }
230
231 virtual std::wstring GetID() {
232 return profile_->GetID();
233 }
234
235 virtual void SetID(const std::wstring& id) {
236 profile_->SetID(id);
237 }
238
initial.commit09911bf2008-07-26 23:55:29239 virtual bool DidLastSessionExitCleanly() {
240 return profile_->DidLastSessionExitCleanly();
241 }
242
[email protected]d8e41ed2008-09-11 15:22:32243 virtual BookmarkModel* GetBookmarkModel() {
244 return profile_->GetBookmarkModel();
initial.commit09911bf2008-07-26 23:55:29245 }
246
[email protected]3a453fa2008-08-15 18:46:34247#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07248 virtual ProfilePersonalization* GetProfilePersonalization() {
[email protected]3a453fa2008-08-15 18:46:34249 return profile_->GetProfilePersonalization();
250 }
251#endif
252
initial.commit09911bf2008-07-26 23:55:29253 virtual bool IsSameProfile(Profile* profile) {
254 if (profile == static_cast<Profile*>(this))
255 return true;
256 return profile == profile_;
257 }
258
259 virtual Time GetStartTime() const {
260 return start_time_;
261 }
262
263 virtual TabRestoreService* GetTabRestoreService() {
264 return NULL;
265 }
266
[email protected]e7244d82008-10-29 18:13:26267 virtual void ResetTabRestoreService() {
[email protected]20930852008-10-15 19:30:41268 }
269
[email protected]e7244d82008-10-29 18:13:26270 virtual void ReinitializeSpellChecker() {
271 profile_->ReinitializeSpellChecker();
initial.commit09911bf2008-07-26 23:55:29272 }
273
274 virtual SpellChecker* GetSpellChecker() {
275 return profile_->GetSpellChecker();
276 }
277
278 virtual void MarkAsCleanShutdown() {
279 }
280
[email protected]bdbc87c2009-01-25 05:08:54281 virtual void InitExtensions() {
282 NOTREACHED();
283 }
284
initial.commit09911bf2008-07-26 23:55:29285 virtual void ExitedOffTheRecordMode() {
286 // Drop our download manager so we forget about all the downloads made
287 // in off-the-record mode.
288 download_manager_ = NULL;
289 }
290
291 virtual void Observe(NotificationType type,
292 const NotificationSource& source,
293 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56294 DCHECK_EQ(NotificationType::BROWSER_CLOSED, type.value);
initial.commit09911bf2008-07-26 23:55:29295 // We are only interested in OTR browser closing.
296 if (Source<Browser>(source)->profile() != this)
297 return;
298
299 // Let's check if we still have an Off The Record window opened.
300 // Note that we check against 1 as this notification is sent before the
301 // browser window is actually removed from the list.
302 if (BrowserList::GetBrowserCount(this) <= 1)
303 ExitedOffTheRecordMode();
304 }
305
306 private:
307 // The real underlying profile.
308 Profile* profile_;
309
[email protected]6ab9b202008-12-23 22:34:50310 // The context to use for requests made from this OTR session.
311 ChromeURLRequestContext* request_context_;
initial.commit09911bf2008-07-26 23:55:29312
313 // The download manager that only stores downloaded items in memory.
314 scoped_refptr<DownloadManager> download_manager_;
315
[email protected]34cc84f2009-02-13 10:04:35316 // We don't want SSLHostState from the OTR profile to leak back to the main
317 // profile because then the main profile would learn some of the host names
318 // the user visited while OTR.
319 scoped_ptr<SSLHostState> ssl_host_state_;
320
initial.commit09911bf2008-07-26 23:55:29321 // Time we were started.
322 Time start_time_;
323
324 DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl);
325};
326
[email protected]f7011fcb2009-01-28 21:54:32327ProfileImpl::ProfileImpl(const FilePath& path)
initial.commit09911bf2008-07-26 23:55:29328 : path_(path),
329 off_the_record_(false),
[email protected]3a453fa2008-08-15 18:46:34330#ifdef CHROME_PERSONALIZATION
331 personalization_(NULL),
332#endif
[email protected]f7011fcb2009-01-28 21:54:32333 request_context_(NULL),
334 history_service_created_(false),
335 created_web_data_service_(false),
336 created_download_manager_(false),
337 start_time_(Time::Now()),
338 spellchecker_(NULL),
initial.commit09911bf2008-07-26 23:55:29339 shutdown_session_service_(false) {
340 DCHECK(!path.empty()) << "Using an empty path will attempt to write " <<
341 "profile files to the root directory!";
[email protected]2d316662008-09-03 18:18:14342 create_session_service_timer_.Start(
343 TimeDelta::FromMilliseconds(kCreateSessionServiceDelayMS), this,
344 &ProfileImpl::EnsureSessionServiceCreated);
[email protected]bdbc87c2009-01-25 05:08:54345
[email protected]e7244d82008-10-29 18:13:26346 PrefService* prefs = GetPrefs();
347 prefs->AddPrefObserver(prefs::kSpellCheckDictionary, this);
348 prefs->AddPrefObserver(prefs::kEnableSpellCheck, this);
initial.commit09911bf2008-07-26 23:55:29349}
350
[email protected]bdbc87c2009-01-25 05:08:54351void ProfileImpl::InitExtensions() {
[email protected]8c756ac2009-01-30 23:36:41352 if (user_script_master_ || extensions_service_)
353 return; // Already initialized.
354
[email protected]bdbc87c2009-01-25 05:08:54355 const CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]f93fe782009-02-19 01:26:13356 PrefService* prefs = GetPrefs();
[email protected]bdbc87c2009-01-25 05:08:54357 bool user_scripts_enabled =
[email protected]f93fe782009-02-19 01:26:13358 command_line->HasSwitch(switches::kEnableUserScripts) ||
359 prefs->GetBoolean(prefs::kEnableUserScripts);
[email protected]a9afddb2009-02-12 17:49:42360 bool extensions_enabled =
[email protected]f93fe782009-02-19 01:26:13361 command_line->HasSwitch(switches::kEnableExtensions) ||
362 prefs->GetBoolean(prefs::kEnableExtensions);
[email protected]bdbc87c2009-01-25 05:08:54363
[email protected]f7011fcb2009-01-28 21:54:32364 FilePath script_dir;
[email protected]bdbc87c2009-01-25 05:08:54365 if (user_scripts_enabled) {
366 script_dir = GetPath();
[email protected]f7011fcb2009-01-28 21:54:32367 script_dir = script_dir.Append(chrome::kUserScriptsDirname);
[email protected]bdbc87c2009-01-25 05:08:54368 }
369
370 user_script_master_ = new UserScriptMaster(
[email protected]f7011fcb2009-01-28 21:54:32371 g_browser_process->file_thread()->message_loop(), script_dir);
[email protected]bdbc87c2009-01-25 05:08:54372 extensions_service_ = new ExtensionsService(
373 FilePath(GetPath()), user_script_master_.get());
374
375 // If we have extensions, the extension service will kick off the first scan
376 // after extensions are loaded. Otherwise, we need to do that now.
377 if (extensions_enabled)
378 extensions_service_->Init();
379 else if (user_scripts_enabled)
380 user_script_master_->StartScan();
381}
382
initial.commit09911bf2008-07-26 23:55:29383ProfileImpl::~ProfileImpl() {
[email protected]169627b2008-12-06 19:30:19384 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29385
386 StopCreateSessionServiceTimer();
387 // TemplateURLModel schedules a task on the WebDataService from its
388 // destructor. Delete it first to ensure the task gets scheduled before we
389 // shut down the database.
390 template_url_model_.reset();
391
392 // The download manager queries the history system and should be deleted
393 // before the history is shutdown so it can properly cancel all requests.
394 download_manager_ = NULL;
395
[email protected]e7244d82008-10-29 18:13:26396 // Remove pref observers.
397 PrefService* prefs = GetPrefs();
398 prefs->RemovePrefObserver(prefs::kSpellCheckDictionary, this);
399 prefs->RemovePrefObserver(prefs::kEnableSpellCheck, this);
400
[email protected]3a453fa2008-08-15 18:46:34401#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07402 personalization_.reset();
[email protected]3a453fa2008-08-15 18:46:34403#endif
404
initial.commit09911bf2008-07-26 23:55:29405 // Both HistoryService and WebDataService maintain threads for background
406 // processing. Its possible each thread still has tasks on it that have
407 // increased the ref count of the service. In such a situation, when we
408 // decrement the refcount, it won't be 0, and the threads/databases aren't
409 // properly shut down. By explicitly calling Cleanup/Shutdown we ensure the
410 // databases are properly closed.
411 if (web_data_service_.get())
412 web_data_service_->Shutdown();
413
414 if (history_service_.get())
415 history_service_->Cleanup();
416
417 // The I/O thread may be NULL during testing.
[email protected]ab820df2008-08-26 05:55:10418 base::Thread* io_thread = g_browser_process->io_thread();
initial.commit09911bf2008-07-26 23:55:29419
420 if (spellchecker_) {
421 // The spellchecker must be deleted on the I/O thread. During testing, we
422 // don't have an I/O thread.
423 if (io_thread)
424 io_thread->message_loop()->ReleaseSoon(FROM_HERE, spellchecker_);
425 else
426 spellchecker_->Release();
427 }
428
429 if (request_context_) {
[email protected]6ab9b202008-12-23 22:34:50430 request_context_->CleanupOnUIThread();
431
432 if (default_request_context_ == request_context_)
433 default_request_context_ = NULL;
434
initial.commit09911bf2008-07-26 23:55:29435 // Clean up request context on IO thread.
[email protected]6ab9b202008-12-23 22:34:50436 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
437 NewRunnableMethod(request_context_,
438 &base::RefCountedThreadSafe<URLRequestContext>::Release));
initial.commit09911bf2008-07-26 23:55:29439 request_context_ = NULL;
440 }
441
[email protected]d8e41ed2008-09-11 15:22:32442 // HistoryService may call into the BookmarkModel, as such we need to
443 // delete HistoryService before the BookmarkModel. The destructor for
[email protected]90ef13132008-08-27 03:27:46444 // HistoryService will join with HistoryService's backend thread so that
445 // by the time the destructor has finished we're sure it will no longer call
[email protected]d8e41ed2008-09-11 15:22:32446 // into the BookmarkModel.
[email protected]90ef13132008-08-27 03:27:46447 history_service_ = NULL;
448 bookmark_bar_model_.reset();
449
initial.commit09911bf2008-07-26 23:55:29450 MarkAsCleanShutdown();
451}
452
[email protected]f7011fcb2009-01-28 21:54:32453FilePath ProfileImpl::GetPath() {
initial.commit09911bf2008-07-26 23:55:29454 return path_;
455}
456
457bool ProfileImpl::IsOffTheRecord() {
458 return false;
459}
460
461Profile* ProfileImpl::GetOffTheRecordProfile() {
462 if (!off_the_record_profile_.get()) {
463 scoped_ptr<OffTheRecordProfileImpl> p(new OffTheRecordProfileImpl(this));
464 off_the_record_profile_.swap(p);
465 }
466 return off_the_record_profile_.get();
467}
468
469Profile* ProfileImpl::GetOriginalProfile() {
470 return this;
471}
472
[email protected]176aa482008-11-14 03:25:15473static void BroadcastNewHistoryTable(base::SharedMemory* table_memory) {
initial.commit09911bf2008-07-26 23:55:29474 if (!table_memory)
475 return;
476
477 // send to all RenderProcessHosts
478 for (RenderProcessHost::iterator i = RenderProcessHost::begin();
479 i != RenderProcessHost::end(); i++) {
480 if (!i->second->channel())
481 continue;
482
[email protected]176aa482008-11-14 03:25:15483 base::SharedMemoryHandle new_table;
[email protected]f7011fcb2009-01-28 21:54:32484 base::ProcessHandle process = i->second->process().handle();
initial.commit09911bf2008-07-26 23:55:29485 if (!process) {
486 // process can be null if it's started with the --single-process flag.
[email protected]f7011fcb2009-01-28 21:54:32487 process = base::Process::Current().handle();
initial.commit09911bf2008-07-26 23:55:29488 }
489
490 table_memory->ShareToProcess(process, &new_table);
491 IPC::Message* msg = new ViewMsg_VisitedLink_NewTable(new_table);
492 i->second->channel()->Send(msg);
493 }
494}
495
496VisitedLinkMaster* ProfileImpl::GetVisitedLinkMaster() {
497 if (!visited_link_master_.get()) {
498 scoped_ptr<VisitedLinkMaster> visited_links(
499 new VisitedLinkMaster(g_browser_process->file_thread(),
500 BroadcastNewHistoryTable, this));
501 if (!visited_links->Init())
502 return NULL;
503 visited_link_master_.swap(visited_links);
504 }
505
506 return visited_link_master_.get();
507}
508
[email protected]6014d672008-12-05 00:38:25509ExtensionsService* ProfileImpl::GetExtensionsService() {
510 return extensions_service_.get();
511}
512
[email protected]0938d3c2009-01-09 20:37:35513UserScriptMaster* ProfileImpl::GetUserScriptMaster() {
[email protected]0938d3c2009-01-09 20:37:35514 return user_script_master_.get();
[email protected]04fba9a92008-10-28 17:25:25515}
516
[email protected]34cc84f2009-02-13 10:04:35517SSLHostState* ProfileImpl::GetSSLHostState() {
518 if (!ssl_host_state_.get())
519 ssl_host_state_.reset(new SSLHostState());
520
521 DCHECK(ssl_host_state_->CalledOnValidThread());
522 return ssl_host_state_.get();
523}
524
initial.commit09911bf2008-07-26 23:55:29525PrefService* ProfileImpl::GetPrefs() {
526 if (!prefs_.get()) {
[email protected]f7011fcb2009-01-28 21:54:32527 prefs_.reset(new PrefService(GetPrefFilePath().ToWStringHack()));
initial.commit09911bf2008-07-26 23:55:29528
529 // The Profile class and ProfileManager class may read some prefs so
530 // register known prefs as soon as possible.
531 Profile::RegisterUserPrefs(prefs_.get());
532 ProfileManager::RegisterUserPrefs(prefs_.get());
533
534 // The last session exited cleanly if there is no pref for
535 // kSessionExitedCleanly or the value for kSessionExitedCleanly is true.
536 last_session_exited_cleanly_ =
537 prefs_->GetBoolean(prefs::kSessionExitedCleanly);
538 // Mark the session as open.
539 prefs_->SetBoolean(prefs::kSessionExitedCleanly, false);
540 // Make sure we save to disk that the session has opened.
541 prefs_->ScheduleSavePersistentPrefs(g_browser_process->file_thread());
542 }
543
544 return prefs_.get();
545}
546
[email protected]f7011fcb2009-01-28 21:54:32547FilePath ProfileImpl::GetPrefFilePath() {
548 FilePath pref_file_path = path_;
549 pref_file_path = pref_file_path.Append(chrome::kPreferencesFilename);
initial.commit09911bf2008-07-26 23:55:29550 return pref_file_path;
551}
552
553URLRequestContext* ProfileImpl::GetRequestContext() {
554 if (!request_context_) {
[email protected]f7011fcb2009-01-28 21:54:32555 FilePath cookie_path = GetPath();
556 cookie_path = cookie_path.Append(chrome::kCookieFilename);
557 FilePath cache_path = GetPath();
[email protected]2b2830a2009-02-07 01:58:42558
559 // Override the cache location if specified by the user.
560 const std::wstring user_cache_dir(
561 CommandLine::ForCurrentProcess()->GetSwitchValue(
562 switches::kDiskCacheDir));
563 if (!user_cache_dir.empty()) {
564 cache_path = FilePath::FromWStringHack(user_cache_dir);
565 }
566
[email protected]f7011fcb2009-01-28 21:54:32567 cache_path = cache_path.Append(chrome::kCacheDirname);
[email protected]6ab9b202008-12-23 22:34:50568 request_context_ = ChromeURLRequestContext::CreateOriginal(
569 this, cookie_path, cache_path);
initial.commit09911bf2008-07-26 23:55:29570 request_context_->AddRef();
571
[email protected]6ab9b202008-12-23 22:34:50572 // The first request context is always a normal (non-OTR) request context.
573 // Even when Chromium is started in OTR mode, a normal profile is always
574 // created first.
575 if (!default_request_context_) {
576 default_request_context_ = request_context_;
577 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56578 NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE,
[email protected]6ab9b202008-12-23 22:34:50579 NotificationService::AllSources(), NotificationService::NoDetails());
580 }
581
initial.commit09911bf2008-07-26 23:55:29582 DCHECK(request_context_->cookie_store());
583 }
584
585 return request_context_;
586}
587
588HistoryService* ProfileImpl::GetHistoryService(ServiceAccessType sat) {
589 if (!history_service_created_) {
[email protected]90ef13132008-08-27 03:27:46590 history_service_created_ = true;
initial.commit09911bf2008-07-26 23:55:29591 scoped_refptr<HistoryService> history(new HistoryService(this));
[email protected]d8e41ed2008-09-11 15:22:32592 if (!history->Init(GetPath(), GetBookmarkModel()))
initial.commit09911bf2008-07-26 23:55:29593 return NULL;
594 history_service_.swap(history);
initial.commit09911bf2008-07-26 23:55:29595
596 // Send out the notification that the history service was created.
597 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56598 Notify(NotificationType::HISTORY_CREATED, Source<Profile>(this),
initial.commit09911bf2008-07-26 23:55:29599 Details<HistoryService>(history_service_.get()));
600 }
601 return history_service_.get();
602}
603
initial.commit09911bf2008-07-26 23:55:29604TemplateURLModel* ProfileImpl::GetTemplateURLModel() {
605 if (!template_url_model_.get())
606 template_url_model_.reset(new TemplateURLModel(this));
607 return template_url_model_.get();
608}
609
610TemplateURLFetcher* ProfileImpl::GetTemplateURLFetcher() {
611 if (!template_url_fetcher_.get())
612 template_url_fetcher_.reset(new TemplateURLFetcher(this));
613 return template_url_fetcher_.get();
614}
615
616WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) {
617 if (!created_web_data_service_)
618 CreateWebDataService();
619 return web_data_service_.get();
620}
621
622void ProfileImpl::CreateWebDataService() {
623 DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL);
624 created_web_data_service_ = true;
625 scoped_refptr<WebDataService> wds(new WebDataService());
626 if (!wds->Init(GetPath()))
627 return;
628 web_data_service_.swap(wds);
629}
630
631DownloadManager* ProfileImpl::GetDownloadManager() {
632 if (!created_download_manager_) {
633 scoped_refptr<DownloadManager> dlm(new DownloadManager);
634 dlm->Init(this);
635 created_download_manager_ = true;
636 download_manager_.swap(dlm);
637 }
638 return download_manager_.get();
639}
640
641bool ProfileImpl::HasCreatedDownloadManager() const {
642 return created_download_manager_;
643}
644
645SessionService* ProfileImpl::GetSessionService() {
646 if (!session_service_.get() && !shutdown_session_service_) {
647 session_service_ = new SessionService(this);
648 session_service_->ResetFromCurrentBrowsers();
649 }
650 return session_service_.get();
651}
652
653void ProfileImpl::ShutdownSessionService() {
654 if (shutdown_session_service_)
655 return;
656
657 // We're about to exit, force creation of the session service if it hasn't
658 // been created yet. We do this to ensure session state matches the point in
659 // time the user exited.
660 GetSessionService();
661 shutdown_session_service_ = true;
662 session_service_ = NULL;
663}
664
665bool ProfileImpl::HasSessionService() const {
666 return (session_service_.get() != NULL);
667}
668
669std::wstring ProfileImpl::GetName() {
670 return GetPrefs()->GetString(prefs::kProfileName);
671}
672void ProfileImpl::SetName(const std::wstring& name) {
673 GetPrefs()->SetString(prefs::kProfileName, name);
674}
675
676std::wstring ProfileImpl::GetID() {
677 return GetPrefs()->GetString(prefs::kProfileID);
678}
679void ProfileImpl::SetID(const std::wstring& id) {
680 GetPrefs()->SetString(prefs::kProfileID, id);
681}
682
initial.commit09911bf2008-07-26 23:55:29683bool ProfileImpl::DidLastSessionExitCleanly() {
684 // last_session_exited_cleanly_ is set when the preferences are loaded. Force
685 // it to be set by asking for the prefs.
686 GetPrefs();
687 return last_session_exited_cleanly_;
688}
689
[email protected]d8e41ed2008-09-11 15:22:32690BookmarkModel* ProfileImpl::GetBookmarkModel() {
[email protected]90ef13132008-08-27 03:27:46691 if (!bookmark_bar_model_.get()) {
[email protected]d8e41ed2008-09-11 15:22:32692 bookmark_bar_model_.reset(new BookmarkModel(this));
[email protected]90ef13132008-08-27 03:27:46693 bookmark_bar_model_->Load();
694 }
initial.commit09911bf2008-07-26 23:55:29695 return bookmark_bar_model_.get();
696}
697
698bool ProfileImpl::IsSameProfile(Profile* profile) {
699 if (profile == static_cast<Profile*>(this))
700 return true;
701 OffTheRecordProfileImpl* otr_profile = off_the_record_profile_.get();
702 return otr_profile && profile == static_cast<Profile*>(otr_profile);
703}
704
705Time ProfileImpl::GetStartTime() const {
706 return start_time_;
707}
708
709TabRestoreService* ProfileImpl::GetTabRestoreService() {
710 if (!tab_restore_service_.get())
[email protected]169627b2008-12-06 19:30:19711 tab_restore_service_ = new TabRestoreService(this);
initial.commit09911bf2008-07-26 23:55:29712 return tab_restore_service_.get();
713}
714
715void ProfileImpl::ResetTabRestoreService() {
[email protected]169627b2008-12-06 19:30:19716 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29717}
718
[email protected]a9afddb2009-02-12 17:49:42719// To be run in the IO thread to notify all resource message filters that the
[email protected]20930852008-10-15 19:30:41720// spellchecker has changed.
721class NotifySpellcheckerChangeTask : public Task {
722 public:
723 NotifySpellcheckerChangeTask(
[email protected]e7244d82008-10-29 18:13:26724 Profile* profile,
725 const SpellcheckerReinitializedDetails& spellchecker)
[email protected]20930852008-10-15 19:30:41726 : profile_(profile),
727 spellchecker_(spellchecker) {
728 }
729
730 private:
731 void Run(void) {
732 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56733 NotificationType::SPELLCHECKER_REINITIALIZED,
[email protected]20930852008-10-15 19:30:41734 Source<Profile>(profile_),
735 Details<SpellcheckerReinitializedDetails>(&spellchecker_));
736 }
737
738 Profile* profile_;
739 SpellcheckerReinitializedDetails spellchecker_;
740};
741
[email protected]e7244d82008-10-29 18:13:26742void ProfileImpl::InitializeSpellChecker(bool need_to_broadcast) {
[email protected]20930852008-10-15 19:30:41743 // The I/O thread may be NULL during testing.
744 base::Thread* io_thread = g_browser_process->io_thread();
745 if (spellchecker_) {
746 // The spellchecker must be deleted on the I/O thread.
747 // A dummy variable to aid in logical clarity.
748 SpellChecker* last_spellchecker = spellchecker_;
749
750 if (io_thread)
751 io_thread->message_loop()->ReleaseSoon(FROM_HERE, last_spellchecker);
752 else // during testing, we don't have an I/O thread
753 last_spellchecker->Release();
[email protected]20930852008-10-15 19:30:41754 }
755
[email protected]20930852008-10-15 19:30:41756 // Retrieve the (perhaps updated recently) dictionary name from preferences.
757 PrefService* prefs = GetPrefs();
[email protected]e7244d82008-10-29 18:13:26758 bool enable_spellcheck = prefs->GetBoolean(prefs::kEnableSpellCheck);
[email protected]20930852008-10-15 19:30:41759
[email protected]e7244d82008-10-29 18:13:26760 if (enable_spellcheck) {
[email protected]bd17b702009-02-25 20:44:08761 FilePath dict_dir;
[email protected]e7244d82008-10-29 18:13:26762 PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
[email protected]a9afddb2009-02-12 17:49:42763 // Note that, as the object pointed to by previously by spellchecker_
[email protected]e7244d82008-10-29 18:13:26764 // is being deleted in the io thread, the spellchecker_ can be made to point
765 // to a new object (RE-initialized) in parallel in this UI thread.
[email protected]4b4d1adc2008-12-10 22:28:58766 spellchecker_ = new SpellChecker(dict_dir,
767 prefs->GetString(prefs::kSpellCheckDictionary), GetRequestContext(),
[email protected]bd17b702009-02-25 20:44:08768 FilePath());
[email protected]e7244d82008-10-29 18:13:26769 spellchecker_->AddRef(); // Manual refcounting.
770 } else {
771 spellchecker_ = NULL;
772 }
[email protected]20930852008-10-15 19:30:41773
774 if (need_to_broadcast && io_thread) { // Notify resource message filters.
775 SpellcheckerReinitializedDetails scoped_spellchecker;
776 scoped_spellchecker.spellchecker = spellchecker_;
[email protected]e7244d82008-10-29 18:13:26777 if (io_thread) {
778 io_thread->message_loop()->PostTask(
[email protected]a9afddb2009-02-12 17:49:42779 FROM_HERE,
[email protected]e7244d82008-10-29 18:13:26780 new NotifySpellcheckerChangeTask(this, scoped_spellchecker));
781 }
[email protected]20930852008-10-15 19:30:41782 }
783}
784
[email protected]e7244d82008-10-29 18:13:26785void ProfileImpl::ReinitializeSpellChecker() {
786 InitializeSpellChecker(true);
787}
788
initial.commit09911bf2008-07-26 23:55:29789SpellChecker* ProfileImpl::GetSpellChecker() {
790 if (!spellchecker_) {
[email protected]20930852008-10-15 19:30:41791 // This is where spellchecker gets initialized. Note that this is being
792 // initialized in the ui_thread. However, this is not a problem as long as
793 // it is *used* in the io thread.
[email protected]34cc84f2009-02-13 10:04:35794 // TODO(sidchat): One day, change everything so that spellchecker gets
[email protected]20930852008-10-15 19:30:41795 // initialized in the IO thread itself.
[email protected]e7244d82008-10-29 18:13:26796 InitializeSpellChecker(false);
initial.commit09911bf2008-07-26 23:55:29797 }
[email protected]20930852008-10-15 19:30:41798
initial.commit09911bf2008-07-26 23:55:29799 return spellchecker_;
800}
801
802void ProfileImpl::MarkAsCleanShutdown() {
803 if (prefs_.get()) {
804 // The session cleanly exited, set kSessionExitedCleanly appropriately.
805 prefs_->SetBoolean(prefs::kSessionExitedCleanly, true);
806
807 // NOTE: If you change what thread this writes on, be sure and update
808 // ChromeFrame::EndSession().
809 prefs_->SavePersistentPrefs(g_browser_process->file_thread());
810 }
811}
812
[email protected]e7244d82008-10-29 18:13:26813void ProfileImpl::Observe(NotificationType type,
814 const NotificationSource& source,
815 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56816 if (NotificationType::PREF_CHANGED == type) {
[email protected]e7244d82008-10-29 18:13:26817 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
818 PrefService* prefs = Source<PrefService>(source).ptr();
819 DCHECK(pref_name_in && prefs);
820 if (*pref_name_in == prefs::kSpellCheckDictionary ||
821 *pref_name_in == prefs::kEnableSpellCheck) {
822 InitializeSpellChecker(true);
823 }
824 }
825}
826
initial.commit09911bf2008-07-26 23:55:29827void ProfileImpl::StopCreateSessionServiceTimer() {
[email protected]2d316662008-09-03 18:18:14828 create_session_service_timer_.Stop();
initial.commit09911bf2008-07-26 23:55:29829}
[email protected]3a453fa2008-08-15 18:46:34830
831#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07832ProfilePersonalization* ProfileImpl::GetProfilePersonalization() {
[email protected]828e46f2009-02-27 02:43:51833 if (!personalization_.get())
834 personalization_.reset(
835 Personalization::CreateProfilePersonalization(this));
[email protected]57d3d0a2008-09-24 00:50:07836 return personalization_.get();
[email protected]3a453fa2008-08-15 18:46:34837}
license.botbf09a502008-08-24 00:55:55838#endif