blob: 9289c2a6ac5975ac0754ba8ccaa7e7384d52cc35 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/profile.h"
6
7#include "base/command_line.h"
[email protected]04fba9a92008-10-28 17:25:258#include "base/file_path.h"
initial.commit09911bf2008-07-26 23:55:299#include "base/file_util.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/path_service.h"
11#include "base/scoped_ptr.h"
12#include "base/string_util.h"
[email protected]a9afddb2009-02-12 17:49:4213#include "chrome/browser/bookmarks/bookmark_model.h"
initial.commit09911bf2008-07-26 23:55:2914#include "chrome/browser/browser_list.h"
15#include "chrome/browser/browser_process.h"
[email protected]b7f05882009-02-22 01:21:5616#include "chrome/browser/download/download_manager.h"
[email protected]bb28e062009-02-27 17:19:1817#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]6014d672008-12-05 00:38:2518#include "chrome/browser/extensions/extensions_service.h"
[email protected]0938d3c2009-01-09 20:37:3519#include "chrome/browser/extensions/user_script_master.h"
[email protected]a9afddb2009-02-12 17:49:4220#include "chrome/browser/history/history.h"
[email protected]6ab9b202008-12-23 22:34:5021#include "chrome/browser/net/chrome_url_request_context.h"
initial.commit09911bf2008-07-26 23:55:2922#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2623#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]f63ae312009-02-04 17:58:4624#include "chrome/browser/search_engines/template_url_model.h"
[email protected]34cc84f2009-02-13 10:04:3525#include "chrome/browser/ssl/ssl_host_state.h"
[email protected]85e921fb82009-02-11 23:19:4426#include "chrome/browser/sessions/session_service.h"
[email protected]bd580a252009-02-12 01:16:3027#include "chrome/browser/sessions/tab_restore_service.h"
[email protected]f0a644292009-02-25 23:32:4728#include "chrome/browser/spellchecker.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/browser/visitedlink_master.h"
30#include "chrome/browser/webdata/web_data_service.h"
31#include "chrome/common/chrome_constants.h"
32#include "chrome/common/chrome_paths.h"
33#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2934#include "chrome/common/notification_service.h"
35#include "chrome/common/pref_names.h"
[email protected]f7011fcb2009-01-28 21:54:3236#include "chrome/common/render_messages.h"
[email protected]f90f5c512009-02-18 19:10:5837#include "grit/locale_settings.h"
initial.commit09911bf2008-07-26 23:55:2938
[email protected]f7011fcb2009-01-28 21:54:3239#if defined(OS_POSIX)
40// TODO(port): get rid of this include. It's used just to provide declarations
41// and stub definitions for classes we encouter during the porting effort.
42#include "chrome/common/temp_scaffolding_stubs.h"
43#endif
44
45// TODO(port): Get rid of this section and finish porting.
46#if defined(OS_WIN)
[email protected]f7011fcb2009-01-28 21:54:3247#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]f7011fcb2009-01-28 21:54:3248#endif
49
[email protected]e1acf6f2008-10-27 20:43:3350using base::Time;
51using base::TimeDelta;
52
initial.commit09911bf2008-07-26 23:55:2953// Delay, in milliseconds, before we explicitly create the SessionService.
54static const int kCreateSessionServiceDelayMS = 500;
55
56// A pointer to the request context for the default profile. See comments on
57// Profile::GetDefaultRequestContext.
58URLRequestContext* Profile::default_request_context_;
59
[email protected]34cc84f2009-02-13 10:04:3560// static
initial.commit09911bf2008-07-26 23:55:2961void Profile::RegisterUserPrefs(PrefService* prefs) {
[email protected]430d3f72008-10-27 17:56:5562 prefs->RegisterBooleanPref(prefs::kSearchSuggestEnabled, true);
initial.commit09911bf2008-07-26 23:55:2963 prefs->RegisterBooleanPref(prefs::kSessionExitedCleanly, true);
64 prefs->RegisterBooleanPref(prefs::kSafeBrowsingEnabled, true);
[email protected]f7011fcb2009-01-28 21:54:3265#if defined(OS_MACOSX)
66 // MASSIVE HACK!!! We don't have localization working yet. Undo this once we
67 // do. TODO(port): take this out
68 prefs->RegisterStringPref(prefs::kSpellCheckDictionary,
69 L"IDS_SPELLCHECK_DICTIONARY");
70#elif defined(OS_WIN) || defined(OS_LINUX)
[email protected]e7244d82008-10-29 18:13:2671 prefs->RegisterLocalizedStringPref(prefs::kSpellCheckDictionary,
72 IDS_SPELLCHECK_DICTIONARY);
[email protected]f7011fcb2009-01-28 21:54:3273#endif
[email protected]e7244d82008-10-29 18:13:2674 prefs->RegisterBooleanPref(prefs::kEnableSpellCheck, true);
[email protected]f93fe782009-02-19 01:26:1375 prefs->RegisterBooleanPref(prefs::kEnableUserScripts, false);
76 prefs->RegisterBooleanPref(prefs::kEnableExtensions, false);
initial.commit09911bf2008-07-26 23:55:2977}
78
[email protected]34cc84f2009-02-13 10:04:3579// static
[email protected]f7011fcb2009-01-28 21:54:3280Profile* Profile::CreateProfile(const FilePath& path) {
initial.commit09911bf2008-07-26 23:55:2981 return new ProfileImpl(path);
82}
83
[email protected]34cc84f2009-02-13 10:04:3584// static
initial.commit09911bf2008-07-26 23:55:2985URLRequestContext* Profile::GetDefaultRequestContext() {
86 return default_request_context_;
87}
88
89
initial.commit09911bf2008-07-26 23:55:2990////////////////////////////////////////////////////////////////////////////////
91//
92// OffTheRecordProfileImpl is a profile subclass that wraps an existing profile
93// to make it suitable for the off the record mode.
94//
95////////////////////////////////////////////////////////////////////////////////
96class OffTheRecordProfileImpl : public Profile,
97 public NotificationObserver {
98 public:
99 explicit OffTheRecordProfileImpl(Profile* real_profile)
100 : profile_(real_profile),
101 start_time_(Time::Now()) {
[email protected]6ab9b202008-12-23 22:34:50102 request_context_ = ChromeURLRequestContext::CreateOffTheRecord(this);
initial.commit09911bf2008-07-26 23:55:29103 request_context_->AddRef();
104 // Register for browser close notifications so we can detect when the last
105 // off-the-record window is closed, in which case we can clean our states
106 // (cookies, downloads...).
107 NotificationService::current()->AddObserver(
[email protected]bfd04a62009-02-01 18:16:56108 this,
109 NotificationType::BROWSER_CLOSED,
110 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29111 }
112
113 virtual ~OffTheRecordProfileImpl() {
114 if (request_context_) {
[email protected]6ab9b202008-12-23 22:34:50115 request_context_->CleanupOnUIThread();
116
initial.commit09911bf2008-07-26 23:55:29117 // Clean up request context on IO thread.
118 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]6ab9b202008-12-23 22:34:50119 NewRunnableMethod(request_context_,
120 &base::RefCountedThreadSafe<URLRequestContext>::Release));
initial.commit09911bf2008-07-26 23:55:29121 request_context_ = NULL;
122 }
123 NotificationService::current()->RemoveObserver(
[email protected]bfd04a62009-02-01 18:16:56124 this,
125 NotificationType::BROWSER_CLOSED,
126 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29127 }
128
[email protected]f7011fcb2009-01-28 21:54:32129 virtual FilePath GetPath() { return profile_->GetPath(); }
initial.commit09911bf2008-07-26 23:55:29130
131 virtual bool IsOffTheRecord() {
132 return true;
133 }
134
135 virtual Profile* GetOffTheRecordProfile() {
136 return this;
137 }
138
139 virtual Profile* GetOriginalProfile() {
140 return profile_;
141 }
142
143 virtual VisitedLinkMaster* GetVisitedLinkMaster() {
144 return profile_->GetVisitedLinkMaster();
145 }
146
[email protected]6014d672008-12-05 00:38:25147 virtual ExtensionsService* GetExtensionsService() {
148 return profile_->GetExtensionsService();
149 }
150
[email protected]0938d3c2009-01-09 20:37:35151 virtual UserScriptMaster* GetUserScriptMaster() {
152 return profile_->GetUserScriptMaster();
[email protected]04fba9a92008-10-28 17:25:25153 }
154
[email protected]34cc84f2009-02-13 10:04:35155 virtual SSLHostState* GetSSLHostState() {
156 if (!ssl_host_state_.get())
157 ssl_host_state_.reset(new SSLHostState());
158
159 DCHECK(ssl_host_state_->CalledOnValidThread());
160 return ssl_host_state_.get();
161 }
162
initial.commit09911bf2008-07-26 23:55:29163 virtual HistoryService* GetHistoryService(ServiceAccessType sat) {
164 if (sat == EXPLICIT_ACCESS) {
165 return profile_->GetHistoryService(sat);
166 } else {
167 NOTREACHED() << "This profile is OffTheRecord";
168 return NULL;
169 }
170 }
171
initial.commit09911bf2008-07-26 23:55:29172 virtual WebDataService* GetWebDataService(ServiceAccessType sat) {
173 if (sat == EXPLICIT_ACCESS) {
174 return profile_->GetWebDataService(sat);
175 } else {
176 NOTREACHED() << "This profile is OffTheRecord";
177 return NULL;
178 }
179 }
180
181 virtual PrefService* GetPrefs() {
182 return profile_->GetPrefs();
183 }
184
185 virtual TemplateURLModel* GetTemplateURLModel() {
186 return profile_->GetTemplateURLModel();
187 }
188
189 virtual TemplateURLFetcher* GetTemplateURLFetcher() {
190 return profile_->GetTemplateURLFetcher();
191 }
192
193 virtual DownloadManager* GetDownloadManager() {
194 if (!download_manager_.get()) {
195 scoped_refptr<DownloadManager> dlm(new DownloadManager);
196 dlm->Init(this);
197 download_manager_.swap(dlm);
198 }
199 return download_manager_.get();
200 }
201
202 virtual bool HasCreatedDownloadManager() const {
203 return (download_manager_.get() != NULL);
204 }
205
206 virtual URLRequestContext* GetRequestContext() {
207 return request_context_;
208 }
209
[email protected]e7f29642009-03-02 22:53:18210 virtual URLRequestContext* GetRequestContextForMedia() {
211 if (!media_request_context_) {
212 FilePath cache_path = GetPath();
213 cache_path.Append(chrome::kOffTheRecordMediaCacheDirname);
214 media_request_context_ =
215 ChromeURLRequestContext::CreateOffTheRecordForMedia(
216 this, cache_path);
217 media_request_context_->AddRef();
218
219 DCHECK(media_request_context_->cookie_store());
220 }
221 return media_request_context_;
222 }
223
initial.commit09911bf2008-07-26 23:55:29224 virtual SessionService* GetSessionService() {
225 // Don't save any sessions when off the record.
226 return NULL;
227 }
228
229 virtual void ShutdownSessionService() {
230 // We don't allow a session service, nothing to do.
231 }
232
233 virtual bool HasSessionService() const {
234 // We never have a session service.
235 return false;
236 }
237
238 virtual std::wstring GetName() {
239 return profile_->GetName();
240 }
241
242 virtual void SetName(const std::wstring& name) {
243 profile_->SetName(name);
244 }
245
246 virtual std::wstring GetID() {
247 return profile_->GetID();
248 }
249
250 virtual void SetID(const std::wstring& id) {
251 profile_->SetID(id);
252 }
253
initial.commit09911bf2008-07-26 23:55:29254 virtual bool DidLastSessionExitCleanly() {
255 return profile_->DidLastSessionExitCleanly();
256 }
257
[email protected]d8e41ed2008-09-11 15:22:32258 virtual BookmarkModel* GetBookmarkModel() {
259 return profile_->GetBookmarkModel();
initial.commit09911bf2008-07-26 23:55:29260 }
261
[email protected]3a453fa2008-08-15 18:46:34262#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07263 virtual ProfilePersonalization* GetProfilePersonalization() {
[email protected]3a453fa2008-08-15 18:46:34264 return profile_->GetProfilePersonalization();
265 }
266#endif
267
initial.commit09911bf2008-07-26 23:55:29268 virtual bool IsSameProfile(Profile* profile) {
269 if (profile == static_cast<Profile*>(this))
270 return true;
271 return profile == profile_;
272 }
273
274 virtual Time GetStartTime() const {
275 return start_time_;
276 }
277
278 virtual TabRestoreService* GetTabRestoreService() {
279 return NULL;
280 }
281
[email protected]e7244d82008-10-29 18:13:26282 virtual void ResetTabRestoreService() {
[email protected]20930852008-10-15 19:30:41283 }
284
[email protected]e7244d82008-10-29 18:13:26285 virtual void ReinitializeSpellChecker() {
286 profile_->ReinitializeSpellChecker();
initial.commit09911bf2008-07-26 23:55:29287 }
288
289 virtual SpellChecker* GetSpellChecker() {
290 return profile_->GetSpellChecker();
291 }
292
293 virtual void MarkAsCleanShutdown() {
294 }
295
[email protected]bdbc87c2009-01-25 05:08:54296 virtual void InitExtensions() {
297 NOTREACHED();
298 }
299
initial.commit09911bf2008-07-26 23:55:29300 virtual void ExitedOffTheRecordMode() {
301 // Drop our download manager so we forget about all the downloads made
302 // in off-the-record mode.
303 download_manager_ = NULL;
304 }
305
306 virtual void Observe(NotificationType type,
307 const NotificationSource& source,
308 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56309 DCHECK_EQ(NotificationType::BROWSER_CLOSED, type.value);
initial.commit09911bf2008-07-26 23:55:29310 // We are only interested in OTR browser closing.
311 if (Source<Browser>(source)->profile() != this)
312 return;
313
314 // Let's check if we still have an Off The Record window opened.
315 // Note that we check against 1 as this notification is sent before the
316 // browser window is actually removed from the list.
317 if (BrowserList::GetBrowserCount(this) <= 1)
318 ExitedOffTheRecordMode();
319 }
320
321 private:
322 // The real underlying profile.
323 Profile* profile_;
324
[email protected]6ab9b202008-12-23 22:34:50325 // The context to use for requests made from this OTR session.
326 ChromeURLRequestContext* request_context_;
initial.commit09911bf2008-07-26 23:55:29327
[email protected]e7f29642009-03-02 22:53:18328 // The context for requests for media resources.
329 ChromeURLRequestContext* media_request_context_;
330
initial.commit09911bf2008-07-26 23:55:29331 // The download manager that only stores downloaded items in memory.
332 scoped_refptr<DownloadManager> download_manager_;
333
[email protected]34cc84f2009-02-13 10:04:35334 // We don't want SSLHostState from the OTR profile to leak back to the main
335 // profile because then the main profile would learn some of the host names
336 // the user visited while OTR.
337 scoped_ptr<SSLHostState> ssl_host_state_;
338
initial.commit09911bf2008-07-26 23:55:29339 // Time we were started.
340 Time start_time_;
341
342 DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl);
343};
344
[email protected]f7011fcb2009-01-28 21:54:32345ProfileImpl::ProfileImpl(const FilePath& path)
initial.commit09911bf2008-07-26 23:55:29346 : path_(path),
347 off_the_record_(false),
[email protected]3a453fa2008-08-15 18:46:34348#ifdef CHROME_PERSONALIZATION
349 personalization_(NULL),
350#endif
[email protected]f7011fcb2009-01-28 21:54:32351 request_context_(NULL),
352 history_service_created_(false),
353 created_web_data_service_(false),
354 created_download_manager_(false),
355 start_time_(Time::Now()),
356 spellchecker_(NULL),
initial.commit09911bf2008-07-26 23:55:29357 shutdown_session_service_(false) {
358 DCHECK(!path.empty()) << "Using an empty path will attempt to write " <<
359 "profile files to the root directory!";
[email protected]2d316662008-09-03 18:18:14360 create_session_service_timer_.Start(
361 TimeDelta::FromMilliseconds(kCreateSessionServiceDelayMS), this,
362 &ProfileImpl::EnsureSessionServiceCreated);
[email protected]bdbc87c2009-01-25 05:08:54363
[email protected]e7244d82008-10-29 18:13:26364 PrefService* prefs = GetPrefs();
365 prefs->AddPrefObserver(prefs::kSpellCheckDictionary, this);
366 prefs->AddPrefObserver(prefs::kEnableSpellCheck, this);
initial.commit09911bf2008-07-26 23:55:29367}
368
[email protected]bdbc87c2009-01-25 05:08:54369void ProfileImpl::InitExtensions() {
[email protected]8c756ac2009-01-30 23:36:41370 if (user_script_master_ || extensions_service_)
371 return; // Already initialized.
372
[email protected]bdbc87c2009-01-25 05:08:54373 const CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]f93fe782009-02-19 01:26:13374 PrefService* prefs = GetPrefs();
[email protected]bdbc87c2009-01-25 05:08:54375 bool user_scripts_enabled =
[email protected]f0a51fb52009-03-05 12:46:38376 command_line->HasSwitch(switches::kEnableUserScripts) ||
[email protected]f93fe782009-02-19 01:26:13377 prefs->GetBoolean(prefs::kEnableUserScripts);
[email protected]a9afddb2009-02-12 17:49:42378 bool extensions_enabled =
[email protected]f0a51fb52009-03-05 12:46:38379 command_line->HasSwitch(switches::kEnableExtensions) ||
[email protected]f93fe782009-02-19 01:26:13380 prefs->GetBoolean(prefs::kEnableExtensions);
[email protected]bdbc87c2009-01-25 05:08:54381
[email protected]f7011fcb2009-01-28 21:54:32382 FilePath script_dir;
[email protected]bdbc87c2009-01-25 05:08:54383 if (user_scripts_enabled) {
384 script_dir = GetPath();
[email protected]f7011fcb2009-01-28 21:54:32385 script_dir = script_dir.Append(chrome::kUserScriptsDirname);
[email protected]bdbc87c2009-01-25 05:08:54386 }
387
[email protected]bb28e062009-02-27 17:19:18388 ExtensionErrorReporter::Init(true); // allow noisy errors.
[email protected]bdbc87c2009-01-25 05:08:54389 user_script_master_ = new UserScriptMaster(
[email protected]f7011fcb2009-01-28 21:54:32390 g_browser_process->file_thread()->message_loop(), script_dir);
[email protected]81e63782009-02-27 19:35:09391 extensions_service_ = new ExtensionsService(this, user_script_master_.get());
[email protected]bdbc87c2009-01-25 05:08:54392
393 // If we have extensions, the extension service will kick off the first scan
394 // after extensions are loaded. Otherwise, we need to do that now.
395 if (extensions_enabled)
396 extensions_service_->Init();
397 else if (user_scripts_enabled)
398 user_script_master_->StartScan();
399}
400
initial.commit09911bf2008-07-26 23:55:29401ProfileImpl::~ProfileImpl() {
[email protected]169627b2008-12-06 19:30:19402 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29403
404 StopCreateSessionServiceTimer();
405 // TemplateURLModel schedules a task on the WebDataService from its
406 // destructor. Delete it first to ensure the task gets scheduled before we
407 // shut down the database.
408 template_url_model_.reset();
409
410 // The download manager queries the history system and should be deleted
411 // before the history is shutdown so it can properly cancel all requests.
412 download_manager_ = NULL;
413
[email protected]e7244d82008-10-29 18:13:26414 // Remove pref observers.
415 PrefService* prefs = GetPrefs();
416 prefs->RemovePrefObserver(prefs::kSpellCheckDictionary, this);
417 prefs->RemovePrefObserver(prefs::kEnableSpellCheck, this);
418
[email protected]3a453fa2008-08-15 18:46:34419#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07420 personalization_.reset();
[email protected]3a453fa2008-08-15 18:46:34421#endif
422
initial.commit09911bf2008-07-26 23:55:29423 // Both HistoryService and WebDataService maintain threads for background
424 // processing. Its possible each thread still has tasks on it that have
425 // increased the ref count of the service. In such a situation, when we
426 // decrement the refcount, it won't be 0, and the threads/databases aren't
427 // properly shut down. By explicitly calling Cleanup/Shutdown we ensure the
428 // databases are properly closed.
429 if (web_data_service_.get())
430 web_data_service_->Shutdown();
431
432 if (history_service_.get())
433 history_service_->Cleanup();
434
435 // The I/O thread may be NULL during testing.
[email protected]ab820df2008-08-26 05:55:10436 base::Thread* io_thread = g_browser_process->io_thread();
initial.commit09911bf2008-07-26 23:55:29437
438 if (spellchecker_) {
439 // The spellchecker must be deleted on the I/O thread. During testing, we
440 // don't have an I/O thread.
441 if (io_thread)
442 io_thread->message_loop()->ReleaseSoon(FROM_HERE, spellchecker_);
443 else
444 spellchecker_->Release();
445 }
446
447 if (request_context_) {
[email protected]6ab9b202008-12-23 22:34:50448 request_context_->CleanupOnUIThread();
449
450 if (default_request_context_ == request_context_)
451 default_request_context_ = NULL;
452
initial.commit09911bf2008-07-26 23:55:29453 // Clean up request context on IO thread.
[email protected]6ab9b202008-12-23 22:34:50454 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
455 NewRunnableMethod(request_context_,
456 &base::RefCountedThreadSafe<URLRequestContext>::Release));
initial.commit09911bf2008-07-26 23:55:29457 request_context_ = NULL;
458 }
459
[email protected]d8e41ed2008-09-11 15:22:32460 // HistoryService may call into the BookmarkModel, as such we need to
461 // delete HistoryService before the BookmarkModel. The destructor for
[email protected]90ef13132008-08-27 03:27:46462 // HistoryService will join with HistoryService's backend thread so that
463 // by the time the destructor has finished we're sure it will no longer call
[email protected]d8e41ed2008-09-11 15:22:32464 // into the BookmarkModel.
[email protected]90ef13132008-08-27 03:27:46465 history_service_ = NULL;
466 bookmark_bar_model_.reset();
467
initial.commit09911bf2008-07-26 23:55:29468 MarkAsCleanShutdown();
469}
470
[email protected]f7011fcb2009-01-28 21:54:32471FilePath ProfileImpl::GetPath() {
initial.commit09911bf2008-07-26 23:55:29472 return path_;
473}
474
475bool ProfileImpl::IsOffTheRecord() {
476 return false;
477}
478
479Profile* ProfileImpl::GetOffTheRecordProfile() {
480 if (!off_the_record_profile_.get()) {
481 scoped_ptr<OffTheRecordProfileImpl> p(new OffTheRecordProfileImpl(this));
482 off_the_record_profile_.swap(p);
483 }
484 return off_the_record_profile_.get();
485}
486
487Profile* ProfileImpl::GetOriginalProfile() {
488 return this;
489}
490
[email protected]176aa482008-11-14 03:25:15491static void BroadcastNewHistoryTable(base::SharedMemory* table_memory) {
initial.commit09911bf2008-07-26 23:55:29492 if (!table_memory)
493 return;
494
495 // send to all RenderProcessHosts
496 for (RenderProcessHost::iterator i = RenderProcessHost::begin();
497 i != RenderProcessHost::end(); i++) {
498 if (!i->second->channel())
499 continue;
500
[email protected]176aa482008-11-14 03:25:15501 base::SharedMemoryHandle new_table;
[email protected]f7011fcb2009-01-28 21:54:32502 base::ProcessHandle process = i->second->process().handle();
initial.commit09911bf2008-07-26 23:55:29503 if (!process) {
504 // process can be null if it's started with the --single-process flag.
[email protected]f7011fcb2009-01-28 21:54:32505 process = base::Process::Current().handle();
initial.commit09911bf2008-07-26 23:55:29506 }
507
508 table_memory->ShareToProcess(process, &new_table);
509 IPC::Message* msg = new ViewMsg_VisitedLink_NewTable(new_table);
510 i->second->channel()->Send(msg);
511 }
512}
513
514VisitedLinkMaster* ProfileImpl::GetVisitedLinkMaster() {
515 if (!visited_link_master_.get()) {
516 scoped_ptr<VisitedLinkMaster> visited_links(
517 new VisitedLinkMaster(g_browser_process->file_thread(),
518 BroadcastNewHistoryTable, this));
519 if (!visited_links->Init())
520 return NULL;
521 visited_link_master_.swap(visited_links);
522 }
523
524 return visited_link_master_.get();
525}
526
[email protected]6014d672008-12-05 00:38:25527ExtensionsService* ProfileImpl::GetExtensionsService() {
528 return extensions_service_.get();
529}
530
[email protected]0938d3c2009-01-09 20:37:35531UserScriptMaster* ProfileImpl::GetUserScriptMaster() {
[email protected]0938d3c2009-01-09 20:37:35532 return user_script_master_.get();
[email protected]04fba9a92008-10-28 17:25:25533}
534
[email protected]34cc84f2009-02-13 10:04:35535SSLHostState* ProfileImpl::GetSSLHostState() {
536 if (!ssl_host_state_.get())
537 ssl_host_state_.reset(new SSLHostState());
538
539 DCHECK(ssl_host_state_->CalledOnValidThread());
540 return ssl_host_state_.get();
541}
542
initial.commit09911bf2008-07-26 23:55:29543PrefService* ProfileImpl::GetPrefs() {
544 if (!prefs_.get()) {
[email protected]b9636002009-03-04 00:05:25545 prefs_.reset(new PrefService(GetPrefFilePath()));
initial.commit09911bf2008-07-26 23:55:29546
547 // The Profile class and ProfileManager class may read some prefs so
548 // register known prefs as soon as possible.
549 Profile::RegisterUserPrefs(prefs_.get());
550 ProfileManager::RegisterUserPrefs(prefs_.get());
551
552 // The last session exited cleanly if there is no pref for
553 // kSessionExitedCleanly or the value for kSessionExitedCleanly is true.
554 last_session_exited_cleanly_ =
555 prefs_->GetBoolean(prefs::kSessionExitedCleanly);
556 // Mark the session as open.
557 prefs_->SetBoolean(prefs::kSessionExitedCleanly, false);
558 // Make sure we save to disk that the session has opened.
559 prefs_->ScheduleSavePersistentPrefs(g_browser_process->file_thread());
560 }
561
562 return prefs_.get();
563}
564
[email protected]f7011fcb2009-01-28 21:54:32565FilePath ProfileImpl::GetPrefFilePath() {
566 FilePath pref_file_path = path_;
567 pref_file_path = pref_file_path.Append(chrome::kPreferencesFilename);
initial.commit09911bf2008-07-26 23:55:29568 return pref_file_path;
569}
570
571URLRequestContext* ProfileImpl::GetRequestContext() {
572 if (!request_context_) {
[email protected]f7011fcb2009-01-28 21:54:32573 FilePath cookie_path = GetPath();
574 cookie_path = cookie_path.Append(chrome::kCookieFilename);
575 FilePath cache_path = GetPath();
[email protected]2b2830a2009-02-07 01:58:42576
577 // Override the cache location if specified by the user.
578 const std::wstring user_cache_dir(
579 CommandLine::ForCurrentProcess()->GetSwitchValue(
580 switches::kDiskCacheDir));
581 if (!user_cache_dir.empty()) {
582 cache_path = FilePath::FromWStringHack(user_cache_dir);
583 }
584
[email protected]f7011fcb2009-01-28 21:54:32585 cache_path = cache_path.Append(chrome::kCacheDirname);
[email protected]6ab9b202008-12-23 22:34:50586 request_context_ = ChromeURLRequestContext::CreateOriginal(
587 this, cookie_path, cache_path);
initial.commit09911bf2008-07-26 23:55:29588 request_context_->AddRef();
589
[email protected]6ab9b202008-12-23 22:34:50590 // The first request context is always a normal (non-OTR) request context.
591 // Even when Chromium is started in OTR mode, a normal profile is always
592 // created first.
593 if (!default_request_context_) {
594 default_request_context_ = request_context_;
595 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56596 NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE,
[email protected]6ab9b202008-12-23 22:34:50597 NotificationService::AllSources(), NotificationService::NoDetails());
598 }
599
initial.commit09911bf2008-07-26 23:55:29600 DCHECK(request_context_->cookie_store());
601 }
602
603 return request_context_;
604}
605
[email protected]e7f29642009-03-02 22:53:18606URLRequestContext* ProfileImpl::GetRequestContextForMedia() {
607 if (!media_request_context_) {
608 FilePath cache_path = GetPath();
609 cache_path.Append(chrome::kMediaCacheDirname);
610 media_request_context_ = ChromeURLRequestContext::CreateOriginalForMedia(
611 this, cache_path);
612 media_request_context_->AddRef();
613
614 DCHECK(media_request_context_->cookie_store());
615 }
616
617 return media_request_context_;
618}
619
initial.commit09911bf2008-07-26 23:55:29620HistoryService* ProfileImpl::GetHistoryService(ServiceAccessType sat) {
621 if (!history_service_created_) {
[email protected]90ef13132008-08-27 03:27:46622 history_service_created_ = true;
initial.commit09911bf2008-07-26 23:55:29623 scoped_refptr<HistoryService> history(new HistoryService(this));
[email protected]d8e41ed2008-09-11 15:22:32624 if (!history->Init(GetPath(), GetBookmarkModel()))
initial.commit09911bf2008-07-26 23:55:29625 return NULL;
626 history_service_.swap(history);
initial.commit09911bf2008-07-26 23:55:29627
628 // Send out the notification that the history service was created.
629 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56630 Notify(NotificationType::HISTORY_CREATED, Source<Profile>(this),
initial.commit09911bf2008-07-26 23:55:29631 Details<HistoryService>(history_service_.get()));
632 }
633 return history_service_.get();
634}
635
initial.commit09911bf2008-07-26 23:55:29636TemplateURLModel* ProfileImpl::GetTemplateURLModel() {
637 if (!template_url_model_.get())
638 template_url_model_.reset(new TemplateURLModel(this));
639 return template_url_model_.get();
640}
641
642TemplateURLFetcher* ProfileImpl::GetTemplateURLFetcher() {
643 if (!template_url_fetcher_.get())
644 template_url_fetcher_.reset(new TemplateURLFetcher(this));
645 return template_url_fetcher_.get();
646}
647
648WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) {
649 if (!created_web_data_service_)
650 CreateWebDataService();
651 return web_data_service_.get();
652}
653
654void ProfileImpl::CreateWebDataService() {
655 DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL);
656 created_web_data_service_ = true;
657 scoped_refptr<WebDataService> wds(new WebDataService());
658 if (!wds->Init(GetPath()))
659 return;
660 web_data_service_.swap(wds);
661}
662
663DownloadManager* ProfileImpl::GetDownloadManager() {
664 if (!created_download_manager_) {
665 scoped_refptr<DownloadManager> dlm(new DownloadManager);
666 dlm->Init(this);
667 created_download_manager_ = true;
668 download_manager_.swap(dlm);
669 }
670 return download_manager_.get();
671}
672
673bool ProfileImpl::HasCreatedDownloadManager() const {
674 return created_download_manager_;
675}
676
677SessionService* ProfileImpl::GetSessionService() {
678 if (!session_service_.get() && !shutdown_session_service_) {
679 session_service_ = new SessionService(this);
680 session_service_->ResetFromCurrentBrowsers();
681 }
682 return session_service_.get();
683}
684
685void ProfileImpl::ShutdownSessionService() {
686 if (shutdown_session_service_)
687 return;
688
689 // We're about to exit, force creation of the session service if it hasn't
690 // been created yet. We do this to ensure session state matches the point in
691 // time the user exited.
692 GetSessionService();
693 shutdown_session_service_ = true;
694 session_service_ = NULL;
695}
696
697bool ProfileImpl::HasSessionService() const {
698 return (session_service_.get() != NULL);
699}
700
701std::wstring ProfileImpl::GetName() {
702 return GetPrefs()->GetString(prefs::kProfileName);
703}
704void ProfileImpl::SetName(const std::wstring& name) {
705 GetPrefs()->SetString(prefs::kProfileName, name);
706}
707
708std::wstring ProfileImpl::GetID() {
709 return GetPrefs()->GetString(prefs::kProfileID);
710}
711void ProfileImpl::SetID(const std::wstring& id) {
712 GetPrefs()->SetString(prefs::kProfileID, id);
713}
714
initial.commit09911bf2008-07-26 23:55:29715bool ProfileImpl::DidLastSessionExitCleanly() {
716 // last_session_exited_cleanly_ is set when the preferences are loaded. Force
717 // it to be set by asking for the prefs.
718 GetPrefs();
719 return last_session_exited_cleanly_;
720}
721
[email protected]d8e41ed2008-09-11 15:22:32722BookmarkModel* ProfileImpl::GetBookmarkModel() {
[email protected]90ef13132008-08-27 03:27:46723 if (!bookmark_bar_model_.get()) {
[email protected]d8e41ed2008-09-11 15:22:32724 bookmark_bar_model_.reset(new BookmarkModel(this));
[email protected]90ef13132008-08-27 03:27:46725 bookmark_bar_model_->Load();
726 }
initial.commit09911bf2008-07-26 23:55:29727 return bookmark_bar_model_.get();
728}
729
730bool ProfileImpl::IsSameProfile(Profile* profile) {
731 if (profile == static_cast<Profile*>(this))
732 return true;
733 OffTheRecordProfileImpl* otr_profile = off_the_record_profile_.get();
734 return otr_profile && profile == static_cast<Profile*>(otr_profile);
735}
736
737Time ProfileImpl::GetStartTime() const {
738 return start_time_;
739}
740
741TabRestoreService* ProfileImpl::GetTabRestoreService() {
742 if (!tab_restore_service_.get())
[email protected]169627b2008-12-06 19:30:19743 tab_restore_service_ = new TabRestoreService(this);
initial.commit09911bf2008-07-26 23:55:29744 return tab_restore_service_.get();
745}
746
747void ProfileImpl::ResetTabRestoreService() {
[email protected]169627b2008-12-06 19:30:19748 tab_restore_service_ = NULL;
initial.commit09911bf2008-07-26 23:55:29749}
750
[email protected]a9afddb2009-02-12 17:49:42751// To be run in the IO thread to notify all resource message filters that the
[email protected]20930852008-10-15 19:30:41752// spellchecker has changed.
753class NotifySpellcheckerChangeTask : public Task {
754 public:
755 NotifySpellcheckerChangeTask(
[email protected]e7244d82008-10-29 18:13:26756 Profile* profile,
757 const SpellcheckerReinitializedDetails& spellchecker)
[email protected]20930852008-10-15 19:30:41758 : profile_(profile),
759 spellchecker_(spellchecker) {
760 }
761
762 private:
763 void Run(void) {
764 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56765 NotificationType::SPELLCHECKER_REINITIALIZED,
[email protected]20930852008-10-15 19:30:41766 Source<Profile>(profile_),
767 Details<SpellcheckerReinitializedDetails>(&spellchecker_));
768 }
769
770 Profile* profile_;
771 SpellcheckerReinitializedDetails spellchecker_;
772};
773
[email protected]e7244d82008-10-29 18:13:26774void ProfileImpl::InitializeSpellChecker(bool need_to_broadcast) {
[email protected]20930852008-10-15 19:30:41775 // The I/O thread may be NULL during testing.
776 base::Thread* io_thread = g_browser_process->io_thread();
777 if (spellchecker_) {
778 // The spellchecker must be deleted on the I/O thread.
779 // A dummy variable to aid in logical clarity.
780 SpellChecker* last_spellchecker = spellchecker_;
781
782 if (io_thread)
783 io_thread->message_loop()->ReleaseSoon(FROM_HERE, last_spellchecker);
784 else // during testing, we don't have an I/O thread
785 last_spellchecker->Release();
[email protected]20930852008-10-15 19:30:41786 }
787
[email protected]20930852008-10-15 19:30:41788 // Retrieve the (perhaps updated recently) dictionary name from preferences.
789 PrefService* prefs = GetPrefs();
[email protected]e7244d82008-10-29 18:13:26790 bool enable_spellcheck = prefs->GetBoolean(prefs::kEnableSpellCheck);
[email protected]20930852008-10-15 19:30:41791
[email protected]e7244d82008-10-29 18:13:26792 if (enable_spellcheck) {
[email protected]bd17b702009-02-25 20:44:08793 FilePath dict_dir;
[email protected]e7244d82008-10-29 18:13:26794 PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
[email protected]a9afddb2009-02-12 17:49:42795 // Note that, as the object pointed to by previously by spellchecker_
[email protected]e7244d82008-10-29 18:13:26796 // is being deleted in the io thread, the spellchecker_ can be made to point
797 // to a new object (RE-initialized) in parallel in this UI thread.
[email protected]4b4d1adc2008-12-10 22:28:58798 spellchecker_ = new SpellChecker(dict_dir,
799 prefs->GetString(prefs::kSpellCheckDictionary), GetRequestContext(),
[email protected]bd17b702009-02-25 20:44:08800 FilePath());
[email protected]e7244d82008-10-29 18:13:26801 spellchecker_->AddRef(); // Manual refcounting.
802 } else {
803 spellchecker_ = NULL;
804 }
[email protected]20930852008-10-15 19:30:41805
806 if (need_to_broadcast && io_thread) { // Notify resource message filters.
807 SpellcheckerReinitializedDetails scoped_spellchecker;
808 scoped_spellchecker.spellchecker = spellchecker_;
[email protected]e7244d82008-10-29 18:13:26809 if (io_thread) {
810 io_thread->message_loop()->PostTask(
[email protected]a9afddb2009-02-12 17:49:42811 FROM_HERE,
[email protected]e7244d82008-10-29 18:13:26812 new NotifySpellcheckerChangeTask(this, scoped_spellchecker));
813 }
[email protected]20930852008-10-15 19:30:41814 }
815}
816
[email protected]e7244d82008-10-29 18:13:26817void ProfileImpl::ReinitializeSpellChecker() {
818 InitializeSpellChecker(true);
819}
820
initial.commit09911bf2008-07-26 23:55:29821SpellChecker* ProfileImpl::GetSpellChecker() {
822 if (!spellchecker_) {
[email protected]20930852008-10-15 19:30:41823 // This is where spellchecker gets initialized. Note that this is being
824 // initialized in the ui_thread. However, this is not a problem as long as
825 // it is *used* in the io thread.
[email protected]34cc84f2009-02-13 10:04:35826 // TODO(sidchat): One day, change everything so that spellchecker gets
[email protected]20930852008-10-15 19:30:41827 // initialized in the IO thread itself.
[email protected]e7244d82008-10-29 18:13:26828 InitializeSpellChecker(false);
initial.commit09911bf2008-07-26 23:55:29829 }
[email protected]20930852008-10-15 19:30:41830
initial.commit09911bf2008-07-26 23:55:29831 return spellchecker_;
832}
833
834void ProfileImpl::MarkAsCleanShutdown() {
835 if (prefs_.get()) {
836 // The session cleanly exited, set kSessionExitedCleanly appropriately.
837 prefs_->SetBoolean(prefs::kSessionExitedCleanly, true);
838
839 // NOTE: If you change what thread this writes on, be sure and update
840 // ChromeFrame::EndSession().
841 prefs_->SavePersistentPrefs(g_browser_process->file_thread());
842 }
843}
844
[email protected]e7244d82008-10-29 18:13:26845void ProfileImpl::Observe(NotificationType type,
846 const NotificationSource& source,
847 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56848 if (NotificationType::PREF_CHANGED == type) {
[email protected]e7244d82008-10-29 18:13:26849 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
850 PrefService* prefs = Source<PrefService>(source).ptr();
851 DCHECK(pref_name_in && prefs);
852 if (*pref_name_in == prefs::kSpellCheckDictionary ||
853 *pref_name_in == prefs::kEnableSpellCheck) {
854 InitializeSpellChecker(true);
855 }
856 }
857}
858
initial.commit09911bf2008-07-26 23:55:29859void ProfileImpl::StopCreateSessionServiceTimer() {
[email protected]2d316662008-09-03 18:18:14860 create_session_service_timer_.Stop();
initial.commit09911bf2008-07-26 23:55:29861}
[email protected]3a453fa2008-08-15 18:46:34862
863#ifdef CHROME_PERSONALIZATION
[email protected]57d3d0a2008-09-24 00:50:07864ProfilePersonalization* ProfileImpl::GetProfilePersonalization() {
[email protected]bb95ef9e2009-03-05 23:20:29865 DCHECK(!Personalization::IsP13NDisabled());
[email protected]828e46f2009-02-27 02:43:51866 if (!personalization_.get())
867 personalization_.reset(
868 Personalization::CreateProfilePersonalization(this));
[email protected]57d3d0a2008-09-24 00:50:07869 return personalization_.get();
[email protected]3a453fa2008-08-15 18:46:34870}
license.botbf09a502008-08-24 00:55:55871#endif