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