license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/browser/profile.h" |
| 6 | |
| 7 | #include "base/command_line.h" |
[email protected] | 04fba9a9 | 2008-10-28 17:25:25 | [diff] [blame] | 8 | #include "base/file_path.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include "base/file_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "base/path_service.h" |
| 11 | #include "base/scoped_ptr.h" |
| 12 | #include "base/string_util.h" |
[email protected] | a9afddb | 2009-02-12 17:49:42 | [diff] [blame] | 13 | #include "chrome/browser/bookmarks/bookmark_model.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "chrome/browser/browser_list.h" |
| 15 | #include "chrome/browser/browser_process.h" |
[email protected] | b7f0588 | 2009-02-22 01:21:56 | [diff] [blame] | 16 | #include "chrome/browser/download/download_manager.h" |
[email protected] | bb28e06 | 2009-02-27 17:19:18 | [diff] [blame] | 17 | #include "chrome/browser/extensions/extension_error_reporter.h" |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 18 | #include "chrome/browser/extensions/extension_process_manager.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 19 | #include "chrome/browser/extensions/extensions_service.h" |
[email protected] | 0938d3c | 2009-01-09 20:37:35 | [diff] [blame] | 20 | #include "chrome/browser/extensions/user_script_master.h" |
[email protected] | a9afddb | 2009-02-12 17:49:42 | [diff] [blame] | 21 | #include "chrome/browser/history/history.h" |
[email protected] | 6ab9b20 | 2008-12-23 22:34:50 | [diff] [blame] | 22 | #include "chrome/browser/net/chrome_url_request_context.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | #include "chrome/browser/profile_manager.h" |
[email protected] | 8c8657d6 | 2009-01-16 18:31:26 | [diff] [blame] | 24 | #include "chrome/browser/renderer_host/render_process_host.h" |
[email protected] | 1132436e | 2009-04-08 20:06:33 | [diff] [blame] | 25 | #include "chrome/browser/search_engines/template_url_fetcher.h" |
[email protected] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 26 | #include "chrome/browser/search_engines/template_url_model.h" |
[email protected] | 85e921fb8 | 2009-02-11 23:19:44 | [diff] [blame] | 27 | #include "chrome/browser/sessions/session_service.h" |
[email protected] | bd580a25 | 2009-02-12 01:16:30 | [diff] [blame] | 28 | #include "chrome/browser/sessions/tab_restore_service.h" |
[email protected] | f0a64429 | 2009-02-25 23:32:47 | [diff] [blame] | 29 | #include "chrome/browser/spellchecker.h" |
[email protected] | 1132436e | 2009-04-08 20:06:33 | [diff] [blame] | 30 | #include "chrome/browser/ssl/ssl_host_state.h" |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 31 | #include "chrome/browser/browser_theme_provider.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | #include "chrome/browser/visitedlink_master.h" |
| 33 | #include "chrome/browser/webdata/web_data_service.h" |
| 34 | #include "chrome/common/chrome_constants.h" |
| 35 | #include "chrome/common/chrome_paths.h" |
| 36 | #include "chrome/common/chrome_switches.h" |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 37 | #include "chrome/common/net/cookie_monster_sqlite.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | #include "chrome/common/notification_service.h" |
| 39 | #include "chrome/common/pref_names.h" |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 40 | #include "chrome/common/render_messages.h" |
[email protected] | f90f5c51 | 2009-02-18 19:10:58 | [diff] [blame] | 41 | #include "grit/locale_settings.h" |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 42 | #include "net/base/force_tls_state.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 44 | using base::Time; |
| 45 | using base::TimeDelta; |
| 46 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | // Delay, in milliseconds, before we explicitly create the SessionService. |
| 48 | static const int kCreateSessionServiceDelayMS = 500; |
| 49 | |
| 50 | // A pointer to the request context for the default profile. See comments on |
| 51 | // Profile::GetDefaultRequestContext. |
| 52 | URLRequestContext* Profile::default_request_context_; |
| 53 | |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 54 | static void CleanupRequestContext(ChromeURLRequestContext* context) { |
| 55 | if (context) { |
| 56 | context->CleanupOnUIThread(); |
| 57 | |
| 58 | // Clean up request context on IO thread. |
| 59 | g_browser_process->io_thread()->message_loop()->ReleaseSoon(FROM_HERE, |
| 60 | context); |
| 61 | } |
| 62 | } |
| 63 | |
[email protected] | 34cc84f | 2009-02-13 10:04:35 | [diff] [blame] | 64 | // static |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | void Profile::RegisterUserPrefs(PrefService* prefs) { |
[email protected] | 430d3f7 | 2008-10-27 17:56:55 | [diff] [blame] | 66 | prefs->RegisterBooleanPref(prefs::kSearchSuggestEnabled, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | prefs->RegisterBooleanPref(prefs::kSessionExitedCleanly, true); |
| 68 | prefs->RegisterBooleanPref(prefs::kSafeBrowsingEnabled, true); |
[email protected] | 74c8b42 | 2009-03-11 00:34:12 | [diff] [blame] | 69 | // TODO(estade): IDS_SPELLCHECK_DICTIONARY should be an ASCII string. |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 70 | prefs->RegisterLocalizedStringPref(prefs::kSpellCheckDictionary, |
| 71 | IDS_SPELLCHECK_DICTIONARY); |
| 72 | prefs->RegisterBooleanPref(prefs::kEnableSpellCheck, true); |
[email protected] | f93fe78 | 2009-02-19 01:26:13 | [diff] [blame] | 73 | prefs->RegisterBooleanPref(prefs::kEnableUserScripts, false); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 74 | prefs->RegisterStringPref(prefs::kCurrentThemeID, L""); |
| 75 | prefs->RegisterDictionaryPref(prefs::kCurrentThemeImages); |
| 76 | prefs->RegisterDictionaryPref(prefs::kCurrentThemeColors); |
| 77 | prefs->RegisterDictionaryPref(prefs::kCurrentThemeTints); |
[email protected] | f93fe78 | 2009-02-19 01:26:13 | [diff] [blame] | 78 | prefs->RegisterBooleanPref(prefs::kEnableExtensions, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | 34cc84f | 2009-02-13 10:04:35 | [diff] [blame] | 81 | // static |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 82 | Profile* Profile::CreateProfile(const FilePath& path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | return new ProfileImpl(path); |
| 84 | } |
| 85 | |
[email protected] | 34cc84f | 2009-02-13 10:04:35 | [diff] [blame] | 86 | // static |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 87 | URLRequestContext* Profile::GetDefaultRequestContext() { |
| 88 | return default_request_context_; |
| 89 | } |
| 90 | |
| 91 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | //////////////////////////////////////////////////////////////////////////////// |
| 93 | // |
| 94 | // OffTheRecordProfileImpl is a profile subclass that wraps an existing profile |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 95 | // to make it suitable for the off the record mode. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | // |
| 97 | //////////////////////////////////////////////////////////////////////////////// |
| 98 | class OffTheRecordProfileImpl : public Profile, |
| 99 | public NotificationObserver { |
| 100 | public: |
| 101 | explicit OffTheRecordProfileImpl(Profile* real_profile) |
| 102 | : profile_(real_profile), |
[email protected] | 363347b | 2009-03-13 20:06:57 | [diff] [blame] | 103 | media_request_context_(NULL), |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 104 | extensions_request_context_(NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | start_time_(Time::Now()) { |
[email protected] | 6ab9b20 | 2008-12-23 22:34:50 | [diff] [blame] | 106 | request_context_ = ChromeURLRequestContext::CreateOffTheRecord(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | request_context_->AddRef(); |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 108 | |
| 109 | extension_process_manager_.reset(new ExtensionProcessManager(this)); |
| 110 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | // Register for browser close notifications so we can detect when the last |
| 112 | // off-the-record window is closed, in which case we can clean our states |
| 113 | // (cookies, downloads...). |
[email protected] | 88cf329 | 2009-05-22 01:48:43 | [diff] [blame^] | 114 | registrar_.Add(this, NotificationType::BROWSER_CLOSED, |
| 115 | NotificationService::AllSources()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | virtual ~OffTheRecordProfileImpl() { |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 119 | CleanupRequestContext(request_context_); |
| 120 | CleanupRequestContext(media_request_context_); |
| 121 | CleanupRequestContext(extensions_request_context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | } |
| 123 | |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 124 | virtual FilePath GetPath() { return profile_->GetPath(); } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | |
| 126 | virtual bool IsOffTheRecord() { |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | virtual Profile* GetOffTheRecordProfile() { |
| 131 | return this; |
| 132 | } |
| 133 | |
[email protected] | 860f5549 | 2009-03-27 19:50:59 | [diff] [blame] | 134 | virtual void DestroyOffTheRecordProfile() { |
| 135 | // Suicide is bad! |
| 136 | NOTREACHED(); |
| 137 | } |
| 138 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | virtual Profile* GetOriginalProfile() { |
| 140 | return profile_; |
| 141 | } |
| 142 | |
| 143 | virtual VisitedLinkMaster* GetVisitedLinkMaster() { |
[email protected] | 7fb6c86 | 2009-03-13 02:51:49 | [diff] [blame] | 144 | // We don't provide access to the VisitedLinkMaster when we're OffTheRecord |
| 145 | // because we don't want to leak the sites that the user has visited before. |
| 146 | return NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 149 | virtual ExtensionsService* GetExtensionsService() { |
| 150 | return profile_->GetExtensionsService(); |
| 151 | } |
| 152 | |
[email protected] | 0938d3c | 2009-01-09 20:37:35 | [diff] [blame] | 153 | virtual UserScriptMaster* GetUserScriptMaster() { |
| 154 | return profile_->GetUserScriptMaster(); |
[email protected] | 04fba9a9 | 2008-10-28 17:25:25 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 157 | virtual ExtensionProcessManager* GetExtensionProcessManager() { |
| 158 | return extension_process_manager_.get(); |
| 159 | } |
| 160 | |
[email protected] | 34cc84f | 2009-02-13 10:04:35 | [diff] [blame] | 161 | virtual SSLHostState* GetSSLHostState() { |
| 162 | if (!ssl_host_state_.get()) |
| 163 | ssl_host_state_.reset(new SSLHostState()); |
| 164 | |
| 165 | DCHECK(ssl_host_state_->CalledOnValidThread()); |
| 166 | return ssl_host_state_.get(); |
| 167 | } |
| 168 | |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 169 | virtual net::ForceTLSState* GetForceTLSState() { |
| 170 | if (!force_tls_state_.get()) |
| 171 | force_tls_state_.reset(new net::ForceTLSState()); |
| 172 | |
| 173 | return force_tls_state_.get(); |
| 174 | } |
| 175 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | virtual HistoryService* GetHistoryService(ServiceAccessType sat) { |
| 177 | if (sat == EXPLICIT_ACCESS) { |
| 178 | return profile_->GetHistoryService(sat); |
| 179 | } else { |
| 180 | NOTREACHED() << "This profile is OffTheRecord"; |
| 181 | return NULL; |
| 182 | } |
| 183 | } |
| 184 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | virtual WebDataService* GetWebDataService(ServiceAccessType sat) { |
| 186 | if (sat == EXPLICIT_ACCESS) { |
| 187 | return profile_->GetWebDataService(sat); |
| 188 | } else { |
| 189 | NOTREACHED() << "This profile is OffTheRecord"; |
| 190 | return NULL; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | virtual PrefService* GetPrefs() { |
| 195 | return profile_->GetPrefs(); |
| 196 | } |
| 197 | |
| 198 | virtual TemplateURLModel* GetTemplateURLModel() { |
| 199 | return profile_->GetTemplateURLModel(); |
| 200 | } |
| 201 | |
| 202 | virtual TemplateURLFetcher* GetTemplateURLFetcher() { |
| 203 | return profile_->GetTemplateURLFetcher(); |
| 204 | } |
| 205 | |
| 206 | virtual DownloadManager* GetDownloadManager() { |
| 207 | if (!download_manager_.get()) { |
| 208 | scoped_refptr<DownloadManager> dlm(new DownloadManager); |
| 209 | dlm->Init(this); |
| 210 | download_manager_.swap(dlm); |
| 211 | } |
| 212 | return download_manager_.get(); |
| 213 | } |
| 214 | |
| 215 | virtual bool HasCreatedDownloadManager() const { |
| 216 | return (download_manager_.get() != NULL); |
| 217 | } |
| 218 | |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 219 | virtual void InitThemes() { |
| 220 | GetOriginalProfile()->InitThemes(); |
| 221 | } |
| 222 | |
| 223 | virtual void SetTheme(Extension* extension) { |
| 224 | GetOriginalProfile()->SetTheme(extension); |
| 225 | } |
| 226 | |
| 227 | virtual void ClearTheme() { |
| 228 | GetOriginalProfile()->ClearTheme(); |
| 229 | } |
| 230 | |
| 231 | virtual ThemeProvider* GetThemeProvider() { |
| 232 | return GetOriginalProfile()->GetThemeProvider(); |
| 233 | } |
| 234 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 235 | virtual URLRequestContext* GetRequestContext() { |
| 236 | return request_context_; |
| 237 | } |
| 238 | |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 239 | virtual URLRequestContext* GetRequestContextForMedia() { |
| 240 | if (!media_request_context_) { |
| 241 | FilePath cache_path = GetPath(); |
[email protected] | e3edeba | 2009-03-23 18:57:14 | [diff] [blame] | 242 | |
| 243 | // Override the cache location if specified by the user. |
| 244 | const std::wstring user_cache_dir( |
| 245 | CommandLine::ForCurrentProcess()->GetSwitchValue( |
| 246 | switches::kDiskCacheDir)); |
| 247 | if (!user_cache_dir.empty()) { |
| 248 | cache_path = FilePath::FromWStringHack(user_cache_dir); |
| 249 | } |
| 250 | |
[email protected] | 4e7b5dbb | 2009-03-25 00:09:23 | [diff] [blame] | 251 | cache_path = cache_path.Append(chrome::kOffTheRecordMediaCacheDirname); |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 252 | media_request_context_ = |
| 253 | ChromeURLRequestContext::CreateOffTheRecordForMedia( |
| 254 | this, cache_path); |
| 255 | media_request_context_->AddRef(); |
| 256 | |
| 257 | DCHECK(media_request_context_->cookie_store()); |
| 258 | } |
| 259 | return media_request_context_; |
| 260 | } |
| 261 | |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 262 | URLRequestContext* GetRequestContextForExtensions() { |
| 263 | if (!extensions_request_context_) { |
| 264 | extensions_request_context_ = |
| 265 | ChromeURLRequestContext::CreateOffTheRecordForExtensions(this); |
| 266 | extensions_request_context_->AddRef(); |
| 267 | |
| 268 | DCHECK(extensions_request_context_->cookie_store()); |
| 269 | } |
| 270 | |
| 271 | return extensions_request_context_; |
| 272 | } |
| 273 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 274 | virtual SessionService* GetSessionService() { |
| 275 | // Don't save any sessions when off the record. |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | virtual void ShutdownSessionService() { |
| 280 | // We don't allow a session service, nothing to do. |
| 281 | } |
| 282 | |
| 283 | virtual bool HasSessionService() const { |
| 284 | // We never have a session service. |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | virtual std::wstring GetName() { |
| 289 | return profile_->GetName(); |
| 290 | } |
| 291 | |
| 292 | virtual void SetName(const std::wstring& name) { |
| 293 | profile_->SetName(name); |
| 294 | } |
| 295 | |
| 296 | virtual std::wstring GetID() { |
| 297 | return profile_->GetID(); |
| 298 | } |
| 299 | |
| 300 | virtual void SetID(const std::wstring& id) { |
| 301 | profile_->SetID(id); |
| 302 | } |
| 303 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 304 | virtual bool DidLastSessionExitCleanly() { |
| 305 | return profile_->DidLastSessionExitCleanly(); |
| 306 | } |
| 307 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 308 | virtual BookmarkModel* GetBookmarkModel() { |
| 309 | return profile_->GetBookmarkModel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 310 | } |
| 311 | |
[email protected] | 3a453fa | 2008-08-15 18:46:34 | [diff] [blame] | 312 | #ifdef CHROME_PERSONALIZATION |
[email protected] | 57d3d0a | 2008-09-24 00:50:07 | [diff] [blame] | 313 | virtual ProfilePersonalization* GetProfilePersonalization() { |
[email protected] | 3a453fa | 2008-08-15 18:46:34 | [diff] [blame] | 314 | return profile_->GetProfilePersonalization(); |
| 315 | } |
| 316 | #endif |
| 317 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 318 | virtual bool IsSameProfile(Profile* profile) { |
| 319 | if (profile == static_cast<Profile*>(this)) |
| 320 | return true; |
| 321 | return profile == profile_; |
| 322 | } |
| 323 | |
| 324 | virtual Time GetStartTime() const { |
| 325 | return start_time_; |
| 326 | } |
| 327 | |
| 328 | virtual TabRestoreService* GetTabRestoreService() { |
| 329 | return NULL; |
| 330 | } |
| 331 | |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 332 | virtual void ResetTabRestoreService() { |
[email protected] | 2093085 | 2008-10-15 19:30:41 | [diff] [blame] | 333 | } |
| 334 | |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 335 | virtual void ReinitializeSpellChecker() { |
| 336 | profile_->ReinitializeSpellChecker(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | virtual SpellChecker* GetSpellChecker() { |
| 340 | return profile_->GetSpellChecker(); |
| 341 | } |
| 342 | |
| 343 | virtual void MarkAsCleanShutdown() { |
| 344 | } |
| 345 | |
[email protected] | bdbc87c | 2009-01-25 05:08:54 | [diff] [blame] | 346 | virtual void InitExtensions() { |
| 347 | NOTREACHED(); |
| 348 | } |
| 349 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 350 | virtual void ExitedOffTheRecordMode() { |
| 351 | // Drop our download manager so we forget about all the downloads made |
| 352 | // in off-the-record mode. |
| 353 | download_manager_ = NULL; |
| 354 | } |
| 355 | |
| 356 | virtual void Observe(NotificationType type, |
| 357 | const NotificationSource& source, |
| 358 | const NotificationDetails& details) { |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 359 | DCHECK_EQ(NotificationType::BROWSER_CLOSED, type.value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 360 | // We are only interested in OTR browser closing. |
| 361 | if (Source<Browser>(source)->profile() != this) |
| 362 | return; |
| 363 | |
| 364 | // Let's check if we still have an Off The Record window opened. |
| 365 | // Note that we check against 1 as this notification is sent before the |
| 366 | // browser window is actually removed from the list. |
| 367 | if (BrowserList::GetBrowserCount(this) <= 1) |
| 368 | ExitedOffTheRecordMode(); |
| 369 | } |
| 370 | |
| 371 | private: |
[email protected] | 88cf329 | 2009-05-22 01:48:43 | [diff] [blame^] | 372 | NotificationRegistrar registrar_; |
| 373 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 374 | // The real underlying profile. |
| 375 | Profile* profile_; |
| 376 | |
[email protected] | 6ab9b20 | 2008-12-23 22:34:50 | [diff] [blame] | 377 | // The context to use for requests made from this OTR session. |
| 378 | ChromeURLRequestContext* request_context_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 379 | |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 380 | // The context for requests for media resources. |
| 381 | ChromeURLRequestContext* media_request_context_; |
| 382 | |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 383 | ChromeURLRequestContext* extensions_request_context_; |
| 384 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 385 | // The download manager that only stores downloaded items in memory. |
| 386 | scoped_refptr<DownloadManager> download_manager_; |
| 387 | |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 388 | // The download manager that only stores downloaded items in memory. |
| 389 | scoped_refptr<BrowserThemeProvider> theme_provider_; |
| 390 | |
[email protected] | 34cc84f | 2009-02-13 10:04:35 | [diff] [blame] | 391 | // We don't want SSLHostState from the OTR profile to leak back to the main |
| 392 | // profile because then the main profile would learn some of the host names |
| 393 | // the user visited while OTR. |
| 394 | scoped_ptr<SSLHostState> ssl_host_state_; |
| 395 | |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 396 | // The ForceTLSState that only stores enabled sites in memory. |
| 397 | scoped_ptr<net::ForceTLSState> force_tls_state_; |
| 398 | |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 399 | // Extensions run in a different context in incognito mode. |
| 400 | scoped_ptr<ExtensionProcessManager> extension_process_manager_; |
| 401 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 402 | // Time we were started. |
| 403 | Time start_time_; |
| 404 | |
| 405 | DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl); |
| 406 | }; |
| 407 | |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 408 | ProfileImpl::ProfileImpl(const FilePath& path) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 409 | : path_(path), |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 410 | request_context_(NULL), |
[email protected] | 363347b | 2009-03-13 20:06:57 | [diff] [blame] | 411 | media_request_context_(NULL), |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 412 | extensions_request_context_(NULL), |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 413 | history_service_created_(false), |
| 414 | created_web_data_service_(false), |
| 415 | created_download_manager_(false), |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 416 | created_theme_provider_(false), |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 417 | start_time_(Time::Now()), |
| 418 | spellchecker_(NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 419 | shutdown_session_service_(false) { |
| 420 | DCHECK(!path.empty()) << "Using an empty path will attempt to write " << |
| 421 | "profile files to the root directory!"; |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 422 | create_session_service_timer_.Start( |
| 423 | TimeDelta::FromMilliseconds(kCreateSessionServiceDelayMS), this, |
| 424 | &ProfileImpl::EnsureSessionServiceCreated); |
[email protected] | bdbc87c | 2009-01-25 05:08:54 | [diff] [blame] | 425 | |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 426 | extension_process_manager_.reset(new ExtensionProcessManager(this)); |
| 427 | |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 428 | PrefService* prefs = GetPrefs(); |
| 429 | prefs->AddPrefObserver(prefs::kSpellCheckDictionary, this); |
| 430 | prefs->AddPrefObserver(prefs::kEnableSpellCheck, this); |
[email protected] | 380ab46 | 2009-04-24 01:23:58 | [diff] [blame] | 431 | |
| 432 | #ifdef CHROME_PERSONALIZATION |
| 433 | if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableP13n)) |
| 434 | personalization_.reset(Personalization::CreateProfilePersonalization(this)); |
| 435 | #endif |
| 436 | |
[email protected] | 1351b6a | 2009-04-09 20:13:22 | [diff] [blame] | 437 | #if defined(OS_LINUX) |
| 438 | // TODO(port): Remove ifdef when the Linux splash page is not needed. |
| 439 | prefs->transient()->SetString(prefs::kHomePage, "about:linux-splash"); |
| 440 | prefs->transient()->SetBoolean(prefs::kHomePageIsNewTabPage, false); |
| 441 | #endif |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 442 | |
| 443 | // Listen for theme installation. |
[email protected] | 88cf329 | 2009-05-22 01:48:43 | [diff] [blame^] | 444 | registrar_.Add(this, NotificationType::THEME_INSTALLED, |
| 445 | NotificationService::AllSources()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 446 | } |
| 447 | |
[email protected] | bdbc87c | 2009-01-25 05:08:54 | [diff] [blame] | 448 | void ProfileImpl::InitExtensions() { |
[email protected] | 8c756ac | 2009-01-30 23:36:41 | [diff] [blame] | 449 | if (user_script_master_ || extensions_service_) |
| 450 | return; // Already initialized. |
| 451 | |
[email protected] | bdbc87c | 2009-01-25 05:08:54 | [diff] [blame] | 452 | const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
[email protected] | f93fe78 | 2009-02-19 01:26:13 | [diff] [blame] | 453 | PrefService* prefs = GetPrefs(); |
[email protected] | bdbc87c | 2009-01-25 05:08:54 | [diff] [blame] | 454 | bool user_scripts_enabled = |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 455 | command_line->HasSwitch(switches::kEnableUserScripts) || |
[email protected] | f93fe78 | 2009-02-19 01:26:13 | [diff] [blame] | 456 | prefs->GetBoolean(prefs::kEnableUserScripts); |
[email protected] | a9afddb | 2009-02-12 17:49:42 | [diff] [blame] | 457 | bool extensions_enabled = |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 458 | command_line->HasSwitch(switches::kEnableExtensions) || |
[email protected] | f93fe78 | 2009-02-19 01:26:13 | [diff] [blame] | 459 | prefs->GetBoolean(prefs::kEnableExtensions); |
[email protected] | bdbc87c | 2009-01-25 05:08:54 | [diff] [blame] | 460 | |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 461 | FilePath script_dir; |
[email protected] | bdbc87c | 2009-01-25 05:08:54 | [diff] [blame] | 462 | if (user_scripts_enabled) { |
[email protected] | 0cd957b | 2009-03-06 20:13:23 | [diff] [blame] | 463 | if (command_line->HasSwitch(switches::kUserScriptsDir)) { |
| 464 | std::wstring path_string = |
| 465 | command_line->GetSwitchValue(switches::kUserScriptsDir); |
| 466 | script_dir = FilePath::FromWStringHack(path_string); |
| 467 | } else { |
| 468 | script_dir = GetPath(); |
| 469 | script_dir = script_dir.Append(chrome::kUserScriptsDirname); |
| 470 | } |
[email protected] | bdbc87c | 2009-01-25 05:08:54 | [diff] [blame] | 471 | } |
| 472 | |
[email protected] | bb28e06 | 2009-02-27 17:19:18 | [diff] [blame] | 473 | ExtensionErrorReporter::Init(true); // allow noisy errors. |
[email protected] | bdbc87c | 2009-01-25 05:08:54 | [diff] [blame] | 474 | user_script_master_ = new UserScriptMaster( |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 475 | g_browser_process->file_thread()->message_loop(), script_dir); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 476 | extensions_service_ = new ExtensionsService( |
| 477 | this, MessageLoop::current(), |
| 478 | g_browser_process->file_thread()->message_loop(), |
| 479 | std::string()); // Use default registry path |
[email protected] | bdbc87c | 2009-01-25 05:08:54 | [diff] [blame] | 480 | |
| 481 | // If we have extensions, the extension service will kick off the first scan |
| 482 | // after extensions are loaded. Otherwise, we need to do that now. |
| 483 | if (extensions_enabled) |
| 484 | extensions_service_->Init(); |
| 485 | else if (user_scripts_enabled) |
| 486 | user_script_master_->StartScan(); |
| 487 | } |
| 488 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 489 | ProfileImpl::~ProfileImpl() { |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 490 | tab_restore_service_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 491 | |
| 492 | StopCreateSessionServiceTimer(); |
| 493 | // TemplateURLModel schedules a task on the WebDataService from its |
| 494 | // destructor. Delete it first to ensure the task gets scheduled before we |
| 495 | // shut down the database. |
| 496 | template_url_model_.reset(); |
| 497 | |
| 498 | // The download manager queries the history system and should be deleted |
| 499 | // before the history is shutdown so it can properly cancel all requests. |
| 500 | download_manager_ = NULL; |
| 501 | |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 502 | // The theme provider provides bitmaps to whoever wants them. |
| 503 | theme_provider_ = NULL; |
| 504 | |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 505 | // Remove pref observers. |
| 506 | PrefService* prefs = GetPrefs(); |
| 507 | prefs->RemovePrefObserver(prefs::kSpellCheckDictionary, this); |
| 508 | prefs->RemovePrefObserver(prefs::kEnableSpellCheck, this); |
| 509 | |
[email protected] | 3a453fa | 2008-08-15 18:46:34 | [diff] [blame] | 510 | #ifdef CHROME_PERSONALIZATION |
[email protected] | 57d3d0a | 2008-09-24 00:50:07 | [diff] [blame] | 511 | personalization_.reset(); |
[email protected] | 3a453fa | 2008-08-15 18:46:34 | [diff] [blame] | 512 | #endif |
| 513 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 514 | // Both HistoryService and WebDataService maintain threads for background |
| 515 | // processing. Its possible each thread still has tasks on it that have |
| 516 | // increased the ref count of the service. In such a situation, when we |
| 517 | // decrement the refcount, it won't be 0, and the threads/databases aren't |
| 518 | // properly shut down. By explicitly calling Cleanup/Shutdown we ensure the |
| 519 | // databases are properly closed. |
| 520 | if (web_data_service_.get()) |
| 521 | web_data_service_->Shutdown(); |
| 522 | |
| 523 | if (history_service_.get()) |
| 524 | history_service_->Cleanup(); |
| 525 | |
| 526 | // The I/O thread may be NULL during testing. |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 527 | base::Thread* io_thread = g_browser_process->io_thread(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 528 | |
| 529 | if (spellchecker_) { |
| 530 | // The spellchecker must be deleted on the I/O thread. During testing, we |
| 531 | // don't have an I/O thread. |
| 532 | if (io_thread) |
| 533 | io_thread->message_loop()->ReleaseSoon(FROM_HERE, spellchecker_); |
| 534 | else |
| 535 | spellchecker_->Release(); |
| 536 | } |
| 537 | |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 538 | if (default_request_context_ == request_context_) |
| 539 | default_request_context_ = NULL; |
[email protected] | 6ab9b20 | 2008-12-23 22:34:50 | [diff] [blame] | 540 | |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 541 | CleanupRequestContext(request_context_); |
| 542 | CleanupRequestContext(media_request_context_); |
| 543 | CleanupRequestContext(extensions_request_context_); |
[email protected] | 1e744f2 | 2009-04-08 01:00:17 | [diff] [blame] | 544 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 545 | // HistoryService may call into the BookmarkModel, as such we need to |
| 546 | // delete HistoryService before the BookmarkModel. The destructor for |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 547 | // HistoryService will join with HistoryService's backend thread so that |
| 548 | // by the time the destructor has finished we're sure it will no longer call |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 549 | // into the BookmarkModel. |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 550 | history_service_ = NULL; |
| 551 | bookmark_bar_model_.reset(); |
| 552 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 553 | MarkAsCleanShutdown(); |
| 554 | } |
| 555 | |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 556 | FilePath ProfileImpl::GetPath() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 557 | return path_; |
| 558 | } |
| 559 | |
| 560 | bool ProfileImpl::IsOffTheRecord() { |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | Profile* ProfileImpl::GetOffTheRecordProfile() { |
| 565 | if (!off_the_record_profile_.get()) { |
| 566 | scoped_ptr<OffTheRecordProfileImpl> p(new OffTheRecordProfileImpl(this)); |
| 567 | off_the_record_profile_.swap(p); |
| 568 | } |
| 569 | return off_the_record_profile_.get(); |
| 570 | } |
| 571 | |
[email protected] | 860f5549 | 2009-03-27 19:50:59 | [diff] [blame] | 572 | void ProfileImpl::DestroyOffTheRecordProfile() { |
| 573 | off_the_record_profile_.reset(); |
| 574 | } |
| 575 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 576 | Profile* ProfileImpl::GetOriginalProfile() { |
| 577 | return this; |
| 578 | } |
| 579 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 580 | static void BroadcastNewHistoryTable(base::SharedMemory* table_memory) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 581 | if (!table_memory) |
| 582 | return; |
| 583 | |
| 584 | // send to all RenderProcessHosts |
| 585 | for (RenderProcessHost::iterator i = RenderProcessHost::begin(); |
| 586 | i != RenderProcessHost::end(); i++) { |
| 587 | if (!i->second->channel()) |
| 588 | continue; |
| 589 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 590 | base::SharedMemoryHandle new_table; |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 591 | base::ProcessHandle process = i->second->process().handle(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 592 | if (!process) { |
| 593 | // process can be null if it's started with the --single-process flag. |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 594 | process = base::Process::Current().handle(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | table_memory->ShareToProcess(process, &new_table); |
| 598 | IPC::Message* msg = new ViewMsg_VisitedLink_NewTable(new_table); |
| 599 | i->second->channel()->Send(msg); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | VisitedLinkMaster* ProfileImpl::GetVisitedLinkMaster() { |
| 604 | if (!visited_link_master_.get()) { |
| 605 | scoped_ptr<VisitedLinkMaster> visited_links( |
| 606 | new VisitedLinkMaster(g_browser_process->file_thread(), |
| 607 | BroadcastNewHistoryTable, this)); |
| 608 | if (!visited_links->Init()) |
| 609 | return NULL; |
| 610 | visited_link_master_.swap(visited_links); |
| 611 | } |
| 612 | |
| 613 | return visited_link_master_.get(); |
| 614 | } |
| 615 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 616 | ExtensionsService* ProfileImpl::GetExtensionsService() { |
| 617 | return extensions_service_.get(); |
| 618 | } |
| 619 | |
[email protected] | 0938d3c | 2009-01-09 20:37:35 | [diff] [blame] | 620 | UserScriptMaster* ProfileImpl::GetUserScriptMaster() { |
[email protected] | 0938d3c | 2009-01-09 20:37:35 | [diff] [blame] | 621 | return user_script_master_.get(); |
[email protected] | 04fba9a9 | 2008-10-28 17:25:25 | [diff] [blame] | 622 | } |
| 623 | |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 624 | ExtensionProcessManager* ProfileImpl::GetExtensionProcessManager() { |
| 625 | return extension_process_manager_.get(); |
| 626 | } |
| 627 | |
[email protected] | 34cc84f | 2009-02-13 10:04:35 | [diff] [blame] | 628 | SSLHostState* ProfileImpl::GetSSLHostState() { |
| 629 | if (!ssl_host_state_.get()) |
| 630 | ssl_host_state_.reset(new SSLHostState()); |
| 631 | |
| 632 | DCHECK(ssl_host_state_->CalledOnValidThread()); |
| 633 | return ssl_host_state_.get(); |
| 634 | } |
| 635 | |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 636 | net::ForceTLSState* ProfileImpl::GetForceTLSState() { |
| 637 | if (!force_tls_state_.get()) |
| 638 | force_tls_state_.reset(new net::ForceTLSState()); |
| 639 | |
| 640 | return force_tls_state_.get(); |
| 641 | } |
| 642 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 643 | PrefService* ProfileImpl::GetPrefs() { |
| 644 | if (!prefs_.get()) { |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 645 | prefs_.reset(new PrefService(GetPrefFilePath(), |
| 646 | g_browser_process->file_thread())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 647 | |
| 648 | // The Profile class and ProfileManager class may read some prefs so |
| 649 | // register known prefs as soon as possible. |
| 650 | Profile::RegisterUserPrefs(prefs_.get()); |
| 651 | ProfileManager::RegisterUserPrefs(prefs_.get()); |
| 652 | |
| 653 | // The last session exited cleanly if there is no pref for |
| 654 | // kSessionExitedCleanly or the value for kSessionExitedCleanly is true. |
| 655 | last_session_exited_cleanly_ = |
| 656 | prefs_->GetBoolean(prefs::kSessionExitedCleanly); |
| 657 | // Mark the session as open. |
| 658 | prefs_->SetBoolean(prefs::kSessionExitedCleanly, false); |
| 659 | // Make sure we save to disk that the session has opened. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 660 | prefs_->ScheduleSavePersistentPrefs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | return prefs_.get(); |
| 664 | } |
| 665 | |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 666 | FilePath ProfileImpl::GetPrefFilePath() { |
| 667 | FilePath pref_file_path = path_; |
| 668 | pref_file_path = pref_file_path.Append(chrome::kPreferencesFilename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 669 | return pref_file_path; |
| 670 | } |
| 671 | |
| 672 | URLRequestContext* ProfileImpl::GetRequestContext() { |
| 673 | if (!request_context_) { |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 674 | FilePath cookie_path = GetPath(); |
| 675 | cookie_path = cookie_path.Append(chrome::kCookieFilename); |
| 676 | FilePath cache_path = GetPath(); |
[email protected] | 2b2830a | 2009-02-07 01:58:42 | [diff] [blame] | 677 | |
| 678 | // Override the cache location if specified by the user. |
| 679 | const std::wstring user_cache_dir( |
| 680 | CommandLine::ForCurrentProcess()->GetSwitchValue( |
| 681 | switches::kDiskCacheDir)); |
| 682 | if (!user_cache_dir.empty()) { |
| 683 | cache_path = FilePath::FromWStringHack(user_cache_dir); |
| 684 | } |
| 685 | |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 686 | cache_path = cache_path.Append(chrome::kCacheDirname); |
[email protected] | 6ab9b20 | 2008-12-23 22:34:50 | [diff] [blame] | 687 | request_context_ = ChromeURLRequestContext::CreateOriginal( |
| 688 | this, cookie_path, cache_path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 689 | request_context_->AddRef(); |
| 690 | |
[email protected] | 6ab9b20 | 2008-12-23 22:34:50 | [diff] [blame] | 691 | // The first request context is always a normal (non-OTR) request context. |
| 692 | // Even when Chromium is started in OTR mode, a normal profile is always |
| 693 | // created first. |
| 694 | if (!default_request_context_) { |
| 695 | default_request_context_ = request_context_; |
| 696 | NotificationService::current()->Notify( |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 697 | NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, |
[email protected] | 6ab9b20 | 2008-12-23 22:34:50 | [diff] [blame] | 698 | NotificationService::AllSources(), NotificationService::NoDetails()); |
| 699 | } |
| 700 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 701 | DCHECK(request_context_->cookie_store()); |
| 702 | } |
| 703 | |
| 704 | return request_context_; |
| 705 | } |
| 706 | |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 707 | URLRequestContext* ProfileImpl::GetRequestContextForMedia() { |
| 708 | if (!media_request_context_) { |
| 709 | FilePath cache_path = GetPath(); |
[email protected] | e3edeba | 2009-03-23 18:57:14 | [diff] [blame] | 710 | |
| 711 | // Override the cache location if specified by the user. |
| 712 | const std::wstring user_cache_dir( |
| 713 | CommandLine::ForCurrentProcess()->GetSwitchValue( |
| 714 | switches::kDiskCacheDir)); |
| 715 | if (!user_cache_dir.empty()) { |
| 716 | cache_path = FilePath::FromWStringHack(user_cache_dir); |
| 717 | } |
| 718 | |
[email protected] | 4e7b5dbb | 2009-03-25 00:09:23 | [diff] [blame] | 719 | cache_path = cache_path.Append(chrome::kMediaCacheDirname); |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 720 | media_request_context_ = ChromeURLRequestContext::CreateOriginalForMedia( |
| 721 | this, cache_path); |
| 722 | media_request_context_->AddRef(); |
| 723 | |
| 724 | DCHECK(media_request_context_->cookie_store()); |
| 725 | } |
| 726 | |
| 727 | return media_request_context_; |
| 728 | } |
| 729 | |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 730 | URLRequestContext* ProfileImpl::GetRequestContextForExtensions() { |
| 731 | if (!extensions_request_context_) { |
| 732 | FilePath cookie_path = GetPath(); |
| 733 | cookie_path = cookie_path.Append(chrome::kExtensionsCookieFilename); |
| 734 | |
| 735 | extensions_request_context_ = |
| 736 | ChromeURLRequestContext::CreateOriginalForExtensions(this, cookie_path); |
| 737 | extensions_request_context_->AddRef(); |
| 738 | |
| 739 | DCHECK(extensions_request_context_->cookie_store()); |
| 740 | } |
| 741 | |
| 742 | return extensions_request_context_; |
| 743 | } |
| 744 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 745 | HistoryService* ProfileImpl::GetHistoryService(ServiceAccessType sat) { |
| 746 | if (!history_service_created_) { |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 747 | history_service_created_ = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 748 | scoped_refptr<HistoryService> history(new HistoryService(this)); |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 749 | if (!history->Init(GetPath(), GetBookmarkModel())) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 750 | return NULL; |
| 751 | history_service_.swap(history); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 752 | |
| 753 | // Send out the notification that the history service was created. |
| 754 | NotificationService::current()-> |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 755 | Notify(NotificationType::HISTORY_CREATED, Source<Profile>(this), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 756 | Details<HistoryService>(history_service_.get())); |
| 757 | } |
| 758 | return history_service_.get(); |
| 759 | } |
| 760 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 761 | TemplateURLModel* ProfileImpl::GetTemplateURLModel() { |
| 762 | if (!template_url_model_.get()) |
| 763 | template_url_model_.reset(new TemplateURLModel(this)); |
| 764 | return template_url_model_.get(); |
| 765 | } |
| 766 | |
| 767 | TemplateURLFetcher* ProfileImpl::GetTemplateURLFetcher() { |
| 768 | if (!template_url_fetcher_.get()) |
| 769 | template_url_fetcher_.reset(new TemplateURLFetcher(this)); |
| 770 | return template_url_fetcher_.get(); |
| 771 | } |
| 772 | |
| 773 | WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) { |
| 774 | if (!created_web_data_service_) |
| 775 | CreateWebDataService(); |
| 776 | return web_data_service_.get(); |
| 777 | } |
| 778 | |
| 779 | void ProfileImpl::CreateWebDataService() { |
| 780 | DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL); |
| 781 | created_web_data_service_ = true; |
| 782 | scoped_refptr<WebDataService> wds(new WebDataService()); |
| 783 | if (!wds->Init(GetPath())) |
| 784 | return; |
| 785 | web_data_service_.swap(wds); |
| 786 | } |
| 787 | |
| 788 | DownloadManager* ProfileImpl::GetDownloadManager() { |
| 789 | if (!created_download_manager_) { |
| 790 | scoped_refptr<DownloadManager> dlm(new DownloadManager); |
| 791 | dlm->Init(this); |
| 792 | created_download_manager_ = true; |
| 793 | download_manager_.swap(dlm); |
| 794 | } |
| 795 | return download_manager_.get(); |
| 796 | } |
| 797 | |
| 798 | bool ProfileImpl::HasCreatedDownloadManager() const { |
| 799 | return created_download_manager_; |
| 800 | } |
| 801 | |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 802 | void ProfileImpl::InitThemes() { |
| 803 | if (!created_theme_provider_) { |
| 804 | scoped_refptr<BrowserThemeProvider> themes(new BrowserThemeProvider); |
| 805 | themes->Init(this); |
| 806 | created_theme_provider_ = true; |
| 807 | theme_provider_.swap(themes); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | void ProfileImpl::SetTheme(Extension* extension) { |
| 812 | InitThemes(); |
| 813 | theme_provider_.get()->SetTheme(extension); |
| 814 | } |
| 815 | |
| 816 | void ProfileImpl::ClearTheme() { |
| 817 | InitThemes(); |
| 818 | theme_provider_.get()->UseDefaultTheme(); |
| 819 | } |
| 820 | |
| 821 | ThemeProvider* ProfileImpl::GetThemeProvider() { |
| 822 | InitThemes(); |
| 823 | return theme_provider_.get(); |
| 824 | } |
| 825 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 826 | SessionService* ProfileImpl::GetSessionService() { |
| 827 | if (!session_service_.get() && !shutdown_session_service_) { |
| 828 | session_service_ = new SessionService(this); |
| 829 | session_service_->ResetFromCurrentBrowsers(); |
| 830 | } |
| 831 | return session_service_.get(); |
| 832 | } |
| 833 | |
| 834 | void ProfileImpl::ShutdownSessionService() { |
| 835 | if (shutdown_session_service_) |
| 836 | return; |
| 837 | |
| 838 | // We're about to exit, force creation of the session service if it hasn't |
| 839 | // been created yet. We do this to ensure session state matches the point in |
| 840 | // time the user exited. |
| 841 | GetSessionService(); |
| 842 | shutdown_session_service_ = true; |
| 843 | session_service_ = NULL; |
| 844 | } |
| 845 | |
| 846 | bool ProfileImpl::HasSessionService() const { |
| 847 | return (session_service_.get() != NULL); |
| 848 | } |
| 849 | |
| 850 | std::wstring ProfileImpl::GetName() { |
| 851 | return GetPrefs()->GetString(prefs::kProfileName); |
| 852 | } |
| 853 | void ProfileImpl::SetName(const std::wstring& name) { |
| 854 | GetPrefs()->SetString(prefs::kProfileName, name); |
| 855 | } |
| 856 | |
| 857 | std::wstring ProfileImpl::GetID() { |
| 858 | return GetPrefs()->GetString(prefs::kProfileID); |
| 859 | } |
| 860 | void ProfileImpl::SetID(const std::wstring& id) { |
| 861 | GetPrefs()->SetString(prefs::kProfileID, id); |
| 862 | } |
| 863 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 864 | bool ProfileImpl::DidLastSessionExitCleanly() { |
| 865 | // last_session_exited_cleanly_ is set when the preferences are loaded. Force |
| 866 | // it to be set by asking for the prefs. |
| 867 | GetPrefs(); |
| 868 | return last_session_exited_cleanly_; |
| 869 | } |
| 870 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 871 | BookmarkModel* ProfileImpl::GetBookmarkModel() { |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 872 | if (!bookmark_bar_model_.get()) { |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 873 | bookmark_bar_model_.reset(new BookmarkModel(this)); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 874 | bookmark_bar_model_->Load(); |
| 875 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 876 | return bookmark_bar_model_.get(); |
| 877 | } |
| 878 | |
| 879 | bool ProfileImpl::IsSameProfile(Profile* profile) { |
| 880 | if (profile == static_cast<Profile*>(this)) |
| 881 | return true; |
| 882 | OffTheRecordProfileImpl* otr_profile = off_the_record_profile_.get(); |
| 883 | return otr_profile && profile == static_cast<Profile*>(otr_profile); |
| 884 | } |
| 885 | |
| 886 | Time ProfileImpl::GetStartTime() const { |
| 887 | return start_time_; |
| 888 | } |
| 889 | |
| 890 | TabRestoreService* ProfileImpl::GetTabRestoreService() { |
| 891 | if (!tab_restore_service_.get()) |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 892 | tab_restore_service_ = new TabRestoreService(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 893 | return tab_restore_service_.get(); |
| 894 | } |
| 895 | |
| 896 | void ProfileImpl::ResetTabRestoreService() { |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 897 | tab_restore_service_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 898 | } |
| 899 | |
[email protected] | a9afddb | 2009-02-12 17:49:42 | [diff] [blame] | 900 | // To be run in the IO thread to notify all resource message filters that the |
[email protected] | 2093085 | 2008-10-15 19:30:41 | [diff] [blame] | 901 | // spellchecker has changed. |
| 902 | class NotifySpellcheckerChangeTask : public Task { |
| 903 | public: |
| 904 | NotifySpellcheckerChangeTask( |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 905 | Profile* profile, |
| 906 | const SpellcheckerReinitializedDetails& spellchecker) |
[email protected] | 2093085 | 2008-10-15 19:30:41 | [diff] [blame] | 907 | : profile_(profile), |
| 908 | spellchecker_(spellchecker) { |
| 909 | } |
| 910 | |
| 911 | private: |
| 912 | void Run(void) { |
| 913 | NotificationService::current()->Notify( |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 914 | NotificationType::SPELLCHECKER_REINITIALIZED, |
[email protected] | 2093085 | 2008-10-15 19:30:41 | [diff] [blame] | 915 | Source<Profile>(profile_), |
| 916 | Details<SpellcheckerReinitializedDetails>(&spellchecker_)); |
| 917 | } |
| 918 | |
| 919 | Profile* profile_; |
| 920 | SpellcheckerReinitializedDetails spellchecker_; |
| 921 | }; |
| 922 | |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 923 | void ProfileImpl::InitializeSpellChecker(bool need_to_broadcast) { |
[email protected] | 2093085 | 2008-10-15 19:30:41 | [diff] [blame] | 924 | // The I/O thread may be NULL during testing. |
| 925 | base::Thread* io_thread = g_browser_process->io_thread(); |
| 926 | if (spellchecker_) { |
| 927 | // The spellchecker must be deleted on the I/O thread. |
| 928 | // A dummy variable to aid in logical clarity. |
| 929 | SpellChecker* last_spellchecker = spellchecker_; |
| 930 | |
| 931 | if (io_thread) |
| 932 | io_thread->message_loop()->ReleaseSoon(FROM_HERE, last_spellchecker); |
| 933 | else // during testing, we don't have an I/O thread |
| 934 | last_spellchecker->Release(); |
[email protected] | 2093085 | 2008-10-15 19:30:41 | [diff] [blame] | 935 | } |
| 936 | |
[email protected] | 2093085 | 2008-10-15 19:30:41 | [diff] [blame] | 937 | // Retrieve the (perhaps updated recently) dictionary name from preferences. |
| 938 | PrefService* prefs = GetPrefs(); |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 939 | bool enable_spellcheck = prefs->GetBoolean(prefs::kEnableSpellCheck); |
[email protected] | 2093085 | 2008-10-15 19:30:41 | [diff] [blame] | 940 | |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 941 | if (enable_spellcheck) { |
[email protected] | bd17b70 | 2009-02-25 20:44:08 | [diff] [blame] | 942 | FilePath dict_dir; |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 943 | PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir); |
[email protected] | a9afddb | 2009-02-12 17:49:42 | [diff] [blame] | 944 | // Note that, as the object pointed to by previously by spellchecker_ |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 945 | // is being deleted in the io thread, the spellchecker_ can be made to point |
| 946 | // to a new object (RE-initialized) in parallel in this UI thread. |
[email protected] | 4b4d1adc | 2008-12-10 22:28:58 | [diff] [blame] | 947 | spellchecker_ = new SpellChecker(dict_dir, |
[email protected] | 74c8b42 | 2009-03-11 00:34:12 | [diff] [blame] | 948 | WideToASCII(prefs->GetString(prefs::kSpellCheckDictionary)), |
| 949 | GetRequestContext(), |
[email protected] | bd17b70 | 2009-02-25 20:44:08 | [diff] [blame] | 950 | FilePath()); |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 951 | spellchecker_->AddRef(); // Manual refcounting. |
| 952 | } else { |
| 953 | spellchecker_ = NULL; |
| 954 | } |
[email protected] | 2093085 | 2008-10-15 19:30:41 | [diff] [blame] | 955 | |
| 956 | if (need_to_broadcast && io_thread) { // Notify resource message filters. |
| 957 | SpellcheckerReinitializedDetails scoped_spellchecker; |
| 958 | scoped_spellchecker.spellchecker = spellchecker_; |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 959 | if (io_thread) { |
| 960 | io_thread->message_loop()->PostTask( |
[email protected] | a9afddb | 2009-02-12 17:49:42 | [diff] [blame] | 961 | FROM_HERE, |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 962 | new NotifySpellcheckerChangeTask(this, scoped_spellchecker)); |
| 963 | } |
[email protected] | 2093085 | 2008-10-15 19:30:41 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 967 | void ProfileImpl::ReinitializeSpellChecker() { |
| 968 | InitializeSpellChecker(true); |
| 969 | } |
| 970 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 971 | SpellChecker* ProfileImpl::GetSpellChecker() { |
| 972 | if (!spellchecker_) { |
[email protected] | 2093085 | 2008-10-15 19:30:41 | [diff] [blame] | 973 | // This is where spellchecker gets initialized. Note that this is being |
| 974 | // initialized in the ui_thread. However, this is not a problem as long as |
| 975 | // it is *used* in the io thread. |
[email protected] | 34cc84f | 2009-02-13 10:04:35 | [diff] [blame] | 976 | // TODO(sidchat): One day, change everything so that spellchecker gets |
[email protected] | 2093085 | 2008-10-15 19:30:41 | [diff] [blame] | 977 | // initialized in the IO thread itself. |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 978 | InitializeSpellChecker(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 979 | } |
[email protected] | 2093085 | 2008-10-15 19:30:41 | [diff] [blame] | 980 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 981 | return spellchecker_; |
| 982 | } |
| 983 | |
| 984 | void ProfileImpl::MarkAsCleanShutdown() { |
| 985 | if (prefs_.get()) { |
| 986 | // The session cleanly exited, set kSessionExitedCleanly appropriately. |
| 987 | prefs_->SetBoolean(prefs::kSessionExitedCleanly, true); |
| 988 | |
| 989 | // NOTE: If you change what thread this writes on, be sure and update |
| 990 | // ChromeFrame::EndSession(). |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 991 | prefs_->SavePersistentPrefs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 995 | void ProfileImpl::Observe(NotificationType type, |
| 996 | const NotificationSource& source, |
| 997 | const NotificationDetails& details) { |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 998 | if (NotificationType::PREF_CHANGED == type) { |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 999 | std::wstring* pref_name_in = Details<std::wstring>(details).ptr(); |
| 1000 | PrefService* prefs = Source<PrefService>(source).ptr(); |
| 1001 | DCHECK(pref_name_in && prefs); |
| 1002 | if (*pref_name_in == prefs::kSpellCheckDictionary || |
| 1003 | *pref_name_in == prefs::kEnableSpellCheck) { |
| 1004 | InitializeSpellChecker(true); |
| 1005 | } |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 1006 | } else if (NotificationType::THEME_INSTALLED == type) { |
| 1007 | Extension* extension = Details<Extension>(details).ptr(); |
| 1008 | SetTheme(extension); |
[email protected] | e7244d8 | 2008-10-29 18:13:26 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1012 | void ProfileImpl::StopCreateSessionServiceTimer() { |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 1013 | create_session_service_timer_.Stop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1014 | } |
[email protected] | 3a453fa | 2008-08-15 18:46:34 | [diff] [blame] | 1015 | |
| 1016 | #ifdef CHROME_PERSONALIZATION |
[email protected] | 57d3d0a | 2008-09-24 00:50:07 | [diff] [blame] | 1017 | ProfilePersonalization* ProfileImpl::GetProfilePersonalization() { |
[email protected] | 57d3d0a | 2008-09-24 00:50:07 | [diff] [blame] | 1018 | return personalization_.get(); |
[email protected] | 3a453fa | 2008-08-15 18:46:34 | [diff] [blame] | 1019 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1020 | #endif |