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