blob: 0602de78568274350393fc9719488ad4664a0385 [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]6014d672008-12-05 00:38:2516#include "chrome/browser/extensions/extensions_service.h"
[email protected]0938d3c2009-01-09 20:37:3517#include "chrome/browser/extensions/user_script_master.h"
[email protected]a9afddb2009-02-12 17:49:4218#include "chrome/browser/history/history.h"
[email protected]6ab9b202008-12-23 22:34:5019#include "chrome/browser/net/chrome_url_request_context.h"
initial.commit09911bf2008-07-26 23:55:2920#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2621#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]f63ae312009-02-04 17:58:4622#include "chrome/browser/search_engines/template_url_model.h"
[email protected]34cc84f2009-02-13 10:04:3523#include "chrome/browser/ssl/ssl_host_state.h"
[email protected]85e921fb82009-02-11 23:19:4424#include "chrome/browser/sessions/session_service.h"
[email protected]bd580a252009-02-12 01:16:3025#include "chrome/browser/sessions/tab_restore_service.h"
initial.commit09911bf2008-07-26 23:55:2926#include "chrome/browser/visitedlink_master.h"
27#include "chrome/browser/webdata/web_data_service.h"
28#include "chrome/common/chrome_constants.h"
29#include "chrome/common/chrome_paths.h"
30#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2931#include "chrome/common/notification_service.h"
32#include "chrome/common/pref_names.h"
33#include "chrome/common/pref_service.h"
[email protected]f7011fcb2009-01-28 21:54:3234#include "chrome/common/render_messages.h"
initial.commit09911bf2008-07-26 23:55:2935#include "chrome/common/resource_bundle.h"
[email protected]f90f5c512009-02-18 19:10:5836#include "grit/locale_settings.h"
initial.commit09911bf2008-07-26 23:55:2937#include "net/url_request/url_request_context.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/download/download_manager.h"
[email protected]f7011fcb2009-01-28 21:54:3248#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]f7011fcb2009-01-28 21:54:3249#include "chrome/browser/spellchecker.h"
50#include "chrome/browser/tab_contents/navigation_controller.h"
51#endif
52
[email protected]e1acf6f2008-10-27 20:43:3353using base::Time;
54using base::TimeDelta;
55
initial.commit09911bf2008-07-26 23:55:2956// Delay, in milliseconds, before we explicitly create the SessionService.
57static const int kCreateSessionServiceDelayMS = 500;
58
59// A pointer to the request context for the default profile. See comments on
60// Profile::GetDefaultRequestContext.
61URLRequestContext* Profile::default_request_context_;
62
[email protected]34cc84f2009-02-13 10:04:3563// static
initial.commit09911bf2008-07-26 23:55:2964void Profile::RegisterUserPrefs(PrefService* prefs) {
[email protected]430d3f72008-10-27 17:56:5565 prefs->RegisterBooleanPref(prefs::kSearchSuggestEnabled, true);
initial.commit09911bf2008-07-26 23:55:2966 prefs->RegisterBooleanPref(prefs::kSessionExitedCleanly, true);
67 prefs->RegisterBooleanPref(prefs::kSafeBrowsingEnabled, true);
[email protected]f7011fcb2009-01-28 21:54:3268#if defined(OS_MACOSX)
69 // MASSIVE HACK!!! We don't have localization working yet. Undo this once we
70 // do. TODO(port): take this out
71 prefs->RegisterStringPref(prefs::kSpellCheckDictionary,
72 L"IDS_SPELLCHECK_DICTIONARY");
73#elif defined(OS_WIN) || defined(OS_LINUX)
[email protected]e7244d82008-10-29 18:13:2674 prefs->RegisterLocalizedStringPref(prefs::kSpellCheckDictionary,
75 IDS_SPELLCHECK_DICTIONARY);
[email protected]f7011fcb2009-01-28 21:54:3276#endif
[email protected]e7244d82008-10-29 18:13:2677 prefs->RegisterBooleanPref(prefs::kEnableSpellCheck, true);
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
211 virtual SessionService* GetSessionService() {
212 // Don't save any sessions when off the record.
213 return NULL;
214 }
215
216 virtual void ShutdownSessionService() {
217 // We don't allow a session service, nothing to do.
218 }
219
220 virtual bool HasSessionService() const {
221 // We never have a session service.
222 return false;
223 }
224
225 virtual std::wstring GetName() {
226 return profile_->GetName();
227 }
228
229 virtual void SetName(const std::wstring& name) {
230 profile_->SetName(name);
231 }
232
233 virtual std::wstring GetID() {
234 return profile_->GetID();
235 }
236
237 virtual void SetID(const std::wstring& id) {
238 profile_->SetID(id);
239 }
240
initial.commit09911bf2008-07-26 23:55:29241 virtual bool DidLastSessionExitCleanly() {
242 return profile_->DidLastSessionExitCleanly();
243 }
244
[email protected]d8e41ed2008-09-11 15:22:32245 virtual BookmarkModel* GetBookmarkModel() {
246 return profile_->GetBookmarkModel();
initial.commit09911bf2008-07-26 23:55:29247 }
248
[email protected]3a453fa2008-08-15 18:46:34249#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07250 virtual ProfilePersonalization* GetProfilePersonalization() {
[email protected]3a453fa2008-08-15 18:46:34251 return profile_->GetProfilePersonalization();
252 }
253#endif
254
initial.commit09911bf2008-07-26 23:55:29255 virtual bool IsSameProfile(Profile* profile) {
256 if (profile == static_cast<Profile*>(this))
257 return true;
258 return profile == profile_;
259 }
260
261 virtual Time GetStartTime() const {
262 return start_time_;
263 }
264
265 virtual TabRestoreService* GetTabRestoreService() {
266 return NULL;
267 }
268
[email protected]e7244d82008-10-29 18:13:26269 virtual void ResetTabRestoreService() {
[email protected]20930852008-10-15 19:30:41270 }
271
[email protected]e7244d82008-10-29 18:13:26272 virtual void ReinitializeSpellChecker() {
273 profile_->ReinitializeSpellChecker();
initial.commit09911bf2008-07-26 23:55:29274 }
275
276 virtual SpellChecker* GetSpellChecker() {
277 return profile_->GetSpellChecker();
278 }
279
280 virtual void MarkAsCleanShutdown() {
281 }
282
[email protected]bdbc87c2009-01-25 05:08:54283 virtual void InitExtensions() {
284 NOTREACHED();
285 }
286
initial.commit09911bf2008-07-26 23:55:29287 virtual void ExitedOffTheRecordMode() {
288 // Drop our download manager so we forget about all the downloads made
289 // in off-the-record mode.
290 download_manager_ = NULL;
291 }
292
293 virtual void Observe(NotificationType type,
294 const NotificationSource& source,
295 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56296 DCHECK_EQ(NotificationType::BROWSER_CLOSED, type.value);
initial.commit09911bf2008-07-26 23:55:29297 // We are only interested in OTR browser closing.
298 if (Source<Browser>(source)->profile() != this)
299 return;
300
301 // Let's check if we still have an Off The Record window opened.
302 // Note that we check against 1 as this notification is sent before the
303 // browser window is actually removed from the list.
304 if (BrowserList::GetBrowserCount(this) <= 1)
305 ExitedOffTheRecordMode();
306 }
307
308 private:
309 // The real underlying profile.
310 Profile* profile_;
311
[email protected]6ab9b202008-12-23 22:34:50312 // The context to use for requests made from this OTR session.
313 ChromeURLRequestContext* request_context_;
initial.commit09911bf2008-07-26 23:55:29314
315 // The download manager that only stores downloaded items in memory.
316 scoped_refptr<DownloadManager> download_manager_;
317
[email protected]34cc84f2009-02-13 10:04:35318 // We don't want SSLHostState from the OTR profile to leak back to the main
319 // profile because then the main profile would learn some of the host names
320 // the user visited while OTR.
321 scoped_ptr<SSLHostState> ssl_host_state_;
322
initial.commit09911bf2008-07-26 23:55:29323 // Time we were started.
324 Time start_time_;
325
326 DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl);
327};
328
[email protected]f7011fcb2009-01-28 21:54:32329ProfileImpl::ProfileImpl(const FilePath& path)
initial.commit09911bf2008-07-26 23:55:29330 : path_(path),
331 off_the_record_(false),
[email protected]3a453fa2008-08-15 18:46:34332#ifdef CHROME_PERSONALIZATION
333 personalization_(NULL),
334#endif
[email protected]f7011fcb2009-01-28 21:54:32335 request_context_(NULL),
336 history_service_created_(false),
337 created_web_data_service_(false),
338 created_download_manager_(false),
339 start_time_(Time::Now()),
340 spellchecker_(NULL),
initial.commit09911bf2008-07-26 23:55:29341 shutdown_session_service_(false) {
342 DCHECK(!path.empty()) << "Using an empty path will attempt to write " <<
343 "profile files to the root directory!";
[email protected]2d316662008-09-03 18:18:14344 create_session_service_timer_.Start(
345 TimeDelta::FromMilliseconds(kCreateSessionServiceDelayMS), this,
346 &ProfileImpl::EnsureSessionServiceCreated);
[email protected]bdbc87c2009-01-25 05:08:54347
[email protected]e7244d82008-10-29 18:13:26348 PrefService* prefs = GetPrefs();
349 prefs->AddPrefObserver(prefs::kSpellCheckDictionary, this);
350 prefs->AddPrefObserver(prefs::kEnableSpellCheck, this);
initial.commit09911bf2008-07-26 23:55:29351}
352
[email protected]bdbc87c2009-01-25 05:08:54353void ProfileImpl::InitExtensions() {
[email protected]8c756ac2009-01-30 23:36:41354 if (user_script_master_ || extensions_service_)
355 return; // Already initialized.
356
[email protected]bdbc87c2009-01-25 05:08:54357 const CommandLine* command_line = CommandLine::ForCurrentProcess();
358 bool user_scripts_enabled =
359 command_line->HasSwitch(switches::kEnableUserScripts);
[email protected]a9afddb2009-02-12 17:49:42360 bool extensions_enabled =
[email protected]bdbc87c2009-01-25 05:08:54361 command_line->HasSwitch(switches::kEnableExtensions);
362
[email protected]f7011fcb2009-01-28 21:54:32363 FilePath script_dir;
[email protected]bdbc87c2009-01-25 05:08:54364 if (user_scripts_enabled) {
365 script_dir = GetPath();
[email protected]f7011fcb2009-01-28 21:54:32366 script_dir = script_dir.Append(chrome::kUserScriptsDirname);
[email protected]bdbc87c2009-01-25 05:08:54367 }
368
369 user_script_master_ = new UserScriptMaster(
[email protected]f7011fcb2009-01-28 21:54:32370 g_browser_process->file_thread()->message_loop(), script_dir);
[email protected]bdbc87c2009-01-25 05:08:54371 extensions_service_ = new ExtensionsService(
372 FilePath(GetPath()), user_script_master_.get());
373
374 // If we have extensions, the extension service will kick off the first scan
375 // after extensions are loaded. Otherwise, we need to do that now.
376 if (extensions_enabled)
377 extensions_service_->Init();
378 else if (user_scripts_enabled)
379 user_script_master_->StartScan();
380}
381
initial.commit09911bf2008-07-26 23:55:29382ProfileImpl::~ProfileImpl() {
[email protected]169627b2008-12-06 19:30:19383 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29384
385 StopCreateSessionServiceTimer();
386 // TemplateURLModel schedules a task on the WebDataService from its
387 // destructor. Delete it first to ensure the task gets scheduled before we
388 // shut down the database.
389 template_url_model_.reset();
390
391 // The download manager queries the history system and should be deleted
392 // before the history is shutdown so it can properly cancel all requests.
393 download_manager_ = NULL;
394
[email protected]e7244d82008-10-29 18:13:26395 // Remove pref observers.
396 PrefService* prefs = GetPrefs();
397 prefs->RemovePrefObserver(prefs::kSpellCheckDictionary, this);
398 prefs->RemovePrefObserver(prefs::kEnableSpellCheck, this);
399
[email protected]3a453fa2008-08-15 18:46:34400#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07401 personalization_.reset();
[email protected]3a453fa2008-08-15 18:46:34402#endif
403
initial.commit09911bf2008-07-26 23:55:29404 // Both HistoryService and WebDataService maintain threads for background
405 // processing. Its possible each thread still has tasks on it that have
406 // increased the ref count of the service. In such a situation, when we
407 // decrement the refcount, it won't be 0, and the threads/databases aren't
408 // properly shut down. By explicitly calling Cleanup/Shutdown we ensure the
409 // databases are properly closed.
410 if (web_data_service_.get())
411 web_data_service_->Shutdown();
412
413 if (history_service_.get())
414 history_service_->Cleanup();
415
416 // The I/O thread may be NULL during testing.
[email protected]ab820df2008-08-26 05:55:10417 base::Thread* io_thread = g_browser_process->io_thread();
initial.commit09911bf2008-07-26 23:55:29418
419 if (spellchecker_) {
420 // The spellchecker must be deleted on the I/O thread. During testing, we
421 // don't have an I/O thread.
422 if (io_thread)
423 io_thread->message_loop()->ReleaseSoon(FROM_HERE, spellchecker_);
424 else
425 spellchecker_->Release();
426 }
427
428 if (request_context_) {
[email protected]6ab9b202008-12-23 22:34:50429 request_context_->CleanupOnUIThread();
430
431 if (default_request_context_ == request_context_)
432 default_request_context_ = NULL;
433
initial.commit09911bf2008-07-26 23:55:29434 // Clean up request context on IO thread.
[email protected]6ab9b202008-12-23 22:34:50435 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
436 NewRunnableMethod(request_context_,
437 &base::RefCountedThreadSafe<URLRequestContext>::Release));
initial.commit09911bf2008-07-26 23:55:29438 request_context_ = NULL;
439 }
440
[email protected]d8e41ed2008-09-11 15:22:32441 // HistoryService may call into the BookmarkModel, as such we need to
442 // delete HistoryService before the BookmarkModel. The destructor for
[email protected]90ef13132008-08-27 03:27:46443 // HistoryService will join with HistoryService's backend thread so that
444 // by the time the destructor has finished we're sure it will no longer call
[email protected]d8e41ed2008-09-11 15:22:32445 // into the BookmarkModel.
[email protected]90ef13132008-08-27 03:27:46446 history_service_ = NULL;
447 bookmark_bar_model_.reset();
448
initial.commit09911bf2008-07-26 23:55:29449 MarkAsCleanShutdown();
450}
451
[email protected]f7011fcb2009-01-28 21:54:32452FilePath ProfileImpl::GetPath() {
initial.commit09911bf2008-07-26 23:55:29453 return path_;
454}
455
456bool ProfileImpl::IsOffTheRecord() {
457 return false;
458}
459
460Profile* ProfileImpl::GetOffTheRecordProfile() {
461 if (!off_the_record_profile_.get()) {
462 scoped_ptr<OffTheRecordProfileImpl> p(new OffTheRecordProfileImpl(this));
463 off_the_record_profile_.swap(p);
464 }
465 return off_the_record_profile_.get();
466}
467
468Profile* ProfileImpl::GetOriginalProfile() {
469 return this;
470}
471
[email protected]176aa482008-11-14 03:25:15472static void BroadcastNewHistoryTable(base::SharedMemory* table_memory) {
initial.commit09911bf2008-07-26 23:55:29473 if (!table_memory)
474 return;
475
476 // send to all RenderProcessHosts
477 for (RenderProcessHost::iterator i = RenderProcessHost::begin();
478 i != RenderProcessHost::end(); i++) {
479 if (!i->second->channel())
480 continue;
481
[email protected]176aa482008-11-14 03:25:15482 base::SharedMemoryHandle new_table;
[email protected]f7011fcb2009-01-28 21:54:32483 base::ProcessHandle process = i->second->process().handle();
initial.commit09911bf2008-07-26 23:55:29484 if (!process) {
485 // process can be null if it's started with the --single-process flag.
[email protected]f7011fcb2009-01-28 21:54:32486 process = base::Process::Current().handle();
initial.commit09911bf2008-07-26 23:55:29487 }
488
489 table_memory->ShareToProcess(process, &new_table);
490 IPC::Message* msg = new ViewMsg_VisitedLink_NewTable(new_table);
491 i->second->channel()->Send(msg);
492 }
493}
494
495VisitedLinkMaster* ProfileImpl::GetVisitedLinkMaster() {
496 if (!visited_link_master_.get()) {
497 scoped_ptr<VisitedLinkMaster> visited_links(
498 new VisitedLinkMaster(g_browser_process->file_thread(),
499 BroadcastNewHistoryTable, this));
500 if (!visited_links->Init())
501 return NULL;
502 visited_link_master_.swap(visited_links);
503 }
504
505 return visited_link_master_.get();
506}
507
[email protected]6014d672008-12-05 00:38:25508ExtensionsService* ProfileImpl::GetExtensionsService() {
509 return extensions_service_.get();
510}
511
[email protected]0938d3c2009-01-09 20:37:35512UserScriptMaster* ProfileImpl::GetUserScriptMaster() {
[email protected]0938d3c2009-01-09 20:37:35513 return user_script_master_.get();
[email protected]04fba9a92008-10-28 17:25:25514}
515
[email protected]34cc84f2009-02-13 10:04:35516SSLHostState* ProfileImpl::GetSSLHostState() {
517 if (!ssl_host_state_.get())
518 ssl_host_state_.reset(new SSLHostState());
519
520 DCHECK(ssl_host_state_->CalledOnValidThread());
521 return ssl_host_state_.get();
522}
523
initial.commit09911bf2008-07-26 23:55:29524PrefService* ProfileImpl::GetPrefs() {
525 if (!prefs_.get()) {
[email protected]f7011fcb2009-01-28 21:54:32526 prefs_.reset(new PrefService(GetPrefFilePath().ToWStringHack()));
initial.commit09911bf2008-07-26 23:55:29527
528 // The Profile class and ProfileManager class may read some prefs so
529 // register known prefs as soon as possible.
530 Profile::RegisterUserPrefs(prefs_.get());
531 ProfileManager::RegisterUserPrefs(prefs_.get());
532
533 // The last session exited cleanly if there is no pref for
534 // kSessionExitedCleanly or the value for kSessionExitedCleanly is true.
535 last_session_exited_cleanly_ =
536 prefs_->GetBoolean(prefs::kSessionExitedCleanly);
537 // Mark the session as open.
538 prefs_->SetBoolean(prefs::kSessionExitedCleanly, false);
539 // Make sure we save to disk that the session has opened.
540 prefs_->ScheduleSavePersistentPrefs(g_browser_process->file_thread());
541 }
542
543 return prefs_.get();
544}
545
[email protected]f7011fcb2009-01-28 21:54:32546FilePath ProfileImpl::GetPrefFilePath() {
547 FilePath pref_file_path = path_;
548 pref_file_path = pref_file_path.Append(chrome::kPreferencesFilename);
initial.commit09911bf2008-07-26 23:55:29549 return pref_file_path;
550}
551
552URLRequestContext* ProfileImpl::GetRequestContext() {
553 if (!request_context_) {
[email protected]f7011fcb2009-01-28 21:54:32554 FilePath cookie_path = GetPath();
555 cookie_path = cookie_path.Append(chrome::kCookieFilename);
556 FilePath cache_path = GetPath();
[email protected]2b2830a2009-02-07 01:58:42557
558 // Override the cache location if specified by the user.
559 const std::wstring user_cache_dir(
560 CommandLine::ForCurrentProcess()->GetSwitchValue(
561 switches::kDiskCacheDir));
562 if (!user_cache_dir.empty()) {
563 cache_path = FilePath::FromWStringHack(user_cache_dir);
564 }
565
[email protected]f7011fcb2009-01-28 21:54:32566 cache_path = cache_path.Append(chrome::kCacheDirname);
[email protected]6ab9b202008-12-23 22:34:50567 request_context_ = ChromeURLRequestContext::CreateOriginal(
568 this, cookie_path, cache_path);
initial.commit09911bf2008-07-26 23:55:29569 request_context_->AddRef();
570
[email protected]6ab9b202008-12-23 22:34:50571 // The first request context is always a normal (non-OTR) request context.
572 // Even when Chromium is started in OTR mode, a normal profile is always
573 // created first.
574 if (!default_request_context_) {
575 default_request_context_ = request_context_;
576 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56577 NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE,
[email protected]6ab9b202008-12-23 22:34:50578 NotificationService::AllSources(), NotificationService::NoDetails());
579 }
580
initial.commit09911bf2008-07-26 23:55:29581 DCHECK(request_context_->cookie_store());
582 }
583
584 return request_context_;
585}
586
587HistoryService* ProfileImpl::GetHistoryService(ServiceAccessType sat) {
588 if (!history_service_created_) {
[email protected]90ef13132008-08-27 03:27:46589 history_service_created_ = true;
initial.commit09911bf2008-07-26 23:55:29590 scoped_refptr<HistoryService> history(new HistoryService(this));
[email protected]d8e41ed2008-09-11 15:22:32591 if (!history->Init(GetPath(), GetBookmarkModel()))
initial.commit09911bf2008-07-26 23:55:29592 return NULL;
593 history_service_.swap(history);
initial.commit09911bf2008-07-26 23:55:29594
595 // Send out the notification that the history service was created.
596 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56597 Notify(NotificationType::HISTORY_CREATED, Source<Profile>(this),
initial.commit09911bf2008-07-26 23:55:29598 Details<HistoryService>(history_service_.get()));
599 }
600 return history_service_.get();
601}
602
initial.commit09911bf2008-07-26 23:55:29603TemplateURLModel* ProfileImpl::GetTemplateURLModel() {
604 if (!template_url_model_.get())
605 template_url_model_.reset(new TemplateURLModel(this));
606 return template_url_model_.get();
607}
608
609TemplateURLFetcher* ProfileImpl::GetTemplateURLFetcher() {
610 if (!template_url_fetcher_.get())
611 template_url_fetcher_.reset(new TemplateURLFetcher(this));
612 return template_url_fetcher_.get();
613}
614
615WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) {
616 if (!created_web_data_service_)
617 CreateWebDataService();
618 return web_data_service_.get();
619}
620
621void ProfileImpl::CreateWebDataService() {
622 DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL);
623 created_web_data_service_ = true;
624 scoped_refptr<WebDataService> wds(new WebDataService());
625 if (!wds->Init(GetPath()))
626 return;
627 web_data_service_.swap(wds);
628}
629
630DownloadManager* ProfileImpl::GetDownloadManager() {
631 if (!created_download_manager_) {
632 scoped_refptr<DownloadManager> dlm(new DownloadManager);
633 dlm->Init(this);
634 created_download_manager_ = true;
635 download_manager_.swap(dlm);
636 }
637 return download_manager_.get();
638}
639
640bool ProfileImpl::HasCreatedDownloadManager() const {
641 return created_download_manager_;
642}
643
644SessionService* ProfileImpl::GetSessionService() {
645 if (!session_service_.get() && !shutdown_session_service_) {
646 session_service_ = new SessionService(this);
647 session_service_->ResetFromCurrentBrowsers();
648 }
649 return session_service_.get();
650}
651
652void ProfileImpl::ShutdownSessionService() {
653 if (shutdown_session_service_)
654 return;
655
656 // We're about to exit, force creation of the session service if it hasn't
657 // been created yet. We do this to ensure session state matches the point in
658 // time the user exited.
659 GetSessionService();
660 shutdown_session_service_ = true;
661 session_service_ = NULL;
662}
663
664bool ProfileImpl::HasSessionService() const {
665 return (session_service_.get() != NULL);
666}
667
668std::wstring ProfileImpl::GetName() {
669 return GetPrefs()->GetString(prefs::kProfileName);
670}
671void ProfileImpl::SetName(const std::wstring& name) {
672 GetPrefs()->SetString(prefs::kProfileName, name);
673}
674
675std::wstring ProfileImpl::GetID() {
676 return GetPrefs()->GetString(prefs::kProfileID);
677}
678void ProfileImpl::SetID(const std::wstring& id) {
679 GetPrefs()->SetString(prefs::kProfileID, id);
680}
681
initial.commit09911bf2008-07-26 23:55:29682bool ProfileImpl::DidLastSessionExitCleanly() {
683 // last_session_exited_cleanly_ is set when the preferences are loaded. Force
684 // it to be set by asking for the prefs.
685 GetPrefs();
686 return last_session_exited_cleanly_;
687}
688
[email protected]d8e41ed2008-09-11 15:22:32689BookmarkModel* ProfileImpl::GetBookmarkModel() {
[email protected]90ef13132008-08-27 03:27:46690 if (!bookmark_bar_model_.get()) {
[email protected]d8e41ed2008-09-11 15:22:32691 bookmark_bar_model_.reset(new BookmarkModel(this));
[email protected]90ef13132008-08-27 03:27:46692 bookmark_bar_model_->Load();
693 }
initial.commit09911bf2008-07-26 23:55:29694 return bookmark_bar_model_.get();
695}
696
697bool ProfileImpl::IsSameProfile(Profile* profile) {
698 if (profile == static_cast<Profile*>(this))
699 return true;
700 OffTheRecordProfileImpl* otr_profile = off_the_record_profile_.get();
701 return otr_profile && profile == static_cast<Profile*>(otr_profile);
702}
703
704Time ProfileImpl::GetStartTime() const {
705 return start_time_;
706}
707
708TabRestoreService* ProfileImpl::GetTabRestoreService() {
709 if (!tab_restore_service_.get())
[email protected]169627b2008-12-06 19:30:19710 tab_restore_service_ = new TabRestoreService(this);
initial.commit09911bf2008-07-26 23:55:29711 return tab_restore_service_.get();
712}
713
714void ProfileImpl::ResetTabRestoreService() {
[email protected]169627b2008-12-06 19:30:19715 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29716}
717
[email protected]a9afddb2009-02-12 17:49:42718// To be run in the IO thread to notify all resource message filters that the
[email protected]20930852008-10-15 19:30:41719// spellchecker has changed.
720class NotifySpellcheckerChangeTask : public Task {
721 public:
722 NotifySpellcheckerChangeTask(
[email protected]e7244d82008-10-29 18:13:26723 Profile* profile,
724 const SpellcheckerReinitializedDetails& spellchecker)
[email protected]20930852008-10-15 19:30:41725 : profile_(profile),
726 spellchecker_(spellchecker) {
727 }
728
729 private:
730 void Run(void) {
731 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56732 NotificationType::SPELLCHECKER_REINITIALIZED,
[email protected]20930852008-10-15 19:30:41733 Source<Profile>(profile_),
734 Details<SpellcheckerReinitializedDetails>(&spellchecker_));
735 }
736
737 Profile* profile_;
738 SpellcheckerReinitializedDetails spellchecker_;
739};
740
[email protected]e7244d82008-10-29 18:13:26741void ProfileImpl::InitializeSpellChecker(bool need_to_broadcast) {
[email protected]20930852008-10-15 19:30:41742 // The I/O thread may be NULL during testing.
743 base::Thread* io_thread = g_browser_process->io_thread();
744 if (spellchecker_) {
745 // The spellchecker must be deleted on the I/O thread.
746 // A dummy variable to aid in logical clarity.
747 SpellChecker* last_spellchecker = spellchecker_;
748
749 if (io_thread)
750 io_thread->message_loop()->ReleaseSoon(FROM_HERE, last_spellchecker);
751 else // during testing, we don't have an I/O thread
752 last_spellchecker->Release();
[email protected]20930852008-10-15 19:30:41753 }
754
[email protected]20930852008-10-15 19:30:41755 // Retrieve the (perhaps updated recently) dictionary name from preferences.
756 PrefService* prefs = GetPrefs();
[email protected]e7244d82008-10-29 18:13:26757 bool enable_spellcheck = prefs->GetBoolean(prefs::kEnableSpellCheck);
[email protected]20930852008-10-15 19:30:41758
[email protected]e7244d82008-10-29 18:13:26759 if (enable_spellcheck) {
760 std::wstring dict_dir;
761 PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
[email protected]a9afddb2009-02-12 17:49:42762 // Note that, as the object pointed to by previously by spellchecker_
[email protected]e7244d82008-10-29 18:13:26763 // is being deleted in the io thread, the spellchecker_ can be made to point
764 // to a new object (RE-initialized) in parallel in this UI thread.
[email protected]4b4d1adc2008-12-10 22:28:58765 spellchecker_ = new SpellChecker(dict_dir,
766 prefs->GetString(prefs::kSpellCheckDictionary), GetRequestContext(),
767 std::wstring());
[email protected]e7244d82008-10-29 18:13:26768 spellchecker_->AddRef(); // Manual refcounting.
769 } else {
770 spellchecker_ = NULL;
771 }
[email protected]20930852008-10-15 19:30:41772
773 if (need_to_broadcast && io_thread) { // Notify resource message filters.
774 SpellcheckerReinitializedDetails scoped_spellchecker;
775 scoped_spellchecker.spellchecker = spellchecker_;
[email protected]e7244d82008-10-29 18:13:26776 if (io_thread) {
777 io_thread->message_loop()->PostTask(
[email protected]a9afddb2009-02-12 17:49:42778 FROM_HERE,
[email protected]e7244d82008-10-29 18:13:26779 new NotifySpellcheckerChangeTask(this, scoped_spellchecker));
780 }
[email protected]20930852008-10-15 19:30:41781 }
782}
783
[email protected]e7244d82008-10-29 18:13:26784void ProfileImpl::ReinitializeSpellChecker() {
785 InitializeSpellChecker(true);
786}
787
initial.commit09911bf2008-07-26 23:55:29788SpellChecker* ProfileImpl::GetSpellChecker() {
789 if (!spellchecker_) {
[email protected]20930852008-10-15 19:30:41790 // This is where spellchecker gets initialized. Note that this is being
791 // initialized in the ui_thread. However, this is not a problem as long as
792 // it is *used* in the io thread.
[email protected]34cc84f2009-02-13 10:04:35793 // TODO(sidchat): One day, change everything so that spellchecker gets
[email protected]20930852008-10-15 19:30:41794 // initialized in the IO thread itself.
[email protected]e7244d82008-10-29 18:13:26795 InitializeSpellChecker(false);
initial.commit09911bf2008-07-26 23:55:29796 }
[email protected]20930852008-10-15 19:30:41797
initial.commit09911bf2008-07-26 23:55:29798 return spellchecker_;
799}
800
801void ProfileImpl::MarkAsCleanShutdown() {
802 if (prefs_.get()) {
803 // The session cleanly exited, set kSessionExitedCleanly appropriately.
804 prefs_->SetBoolean(prefs::kSessionExitedCleanly, true);
805
806 // NOTE: If you change what thread this writes on, be sure and update
807 // ChromeFrame::EndSession().
808 prefs_->SavePersistentPrefs(g_browser_process->file_thread());
809 }
810}
811
[email protected]e7244d82008-10-29 18:13:26812void ProfileImpl::Observe(NotificationType type,
813 const NotificationSource& source,
814 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56815 if (NotificationType::PREF_CHANGED == type) {
[email protected]e7244d82008-10-29 18:13:26816 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
817 PrefService* prefs = Source<PrefService>(source).ptr();
818 DCHECK(pref_name_in && prefs);
819 if (*pref_name_in == prefs::kSpellCheckDictionary ||
820 *pref_name_in == prefs::kEnableSpellCheck) {
821 InitializeSpellChecker(true);
822 }
823 }
824}
825
initial.commit09911bf2008-07-26 23:55:29826void ProfileImpl::StopCreateSessionServiceTimer() {
[email protected]2d316662008-09-03 18:18:14827 create_session_service_timer_.Stop();
initial.commit09911bf2008-07-26 23:55:29828}
[email protected]3a453fa2008-08-15 18:46:34829
830#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07831ProfilePersonalization* ProfileImpl::GetProfilePersonalization() {
832 if (!personalization_.get())
833 personalization_.reset(
834 Personalization::CreateProfilePersonalization(this));
835 return personalization_.get();
[email protected]3a453fa2008-08-15 18:46:34836}
license.botbf09a502008-08-24 00:55:55837#endif