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