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