profile_impl.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 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.
4
5// This class gathers state related to a single user profile.
6
7#ifndef CHROME_BROWSER_PROFILES_PROFILE_IMPL_H_
8#define CHROME_BROWSER_PROFILES_PROFILE_IMPL_H_
9
10#include <string>
11
12#include "base/files/file_path.h"
13#include "base/gtest_prod_util.h"
14#include "base/memory/ref_counted.h"
15#include "base/memory/scoped_ptr.h"
16#include "base/prefs/pref_change_registrar.h"
17#include "base/timer.h"
18#include "chrome/browser/profiles/profile.h"
19#include "chrome/browser/profiles/profile_impl_io_data.h"
20#include "content/public/browser/content_browser_client.h"
21#include "content/public/browser/host_zoom_map.h"
22
23class NetPrefObserver;
24class PrefRegistrySyncable;
25class PrefService;
26class PrefServiceSyncable;
27class SSLConfigServiceManager;
28
29#if defined(OS_CHROMEOS)
30namespace chromeos {
31class EnterpriseExtensionObserver;
32class LocaleChangeGuard;
33class Preferences;
34}
35#endif
36
37namespace base {
38class SequencedTaskRunner;
39}
40
41namespace content {
42class SpeechRecognitionPreferences;
43}
44
45namespace extensions {
46class ExtensionSystem;
47}
48
49namespace policy {
50class UserCloudPolicyManager;
51}
52
53// The default profile implementation.
54class ProfileImpl : public Profile {
55 public:
56  // Value written to prefs when the exit type is EXIT_NORMAL. Public for tests.
57  static const char* const kPrefExitTypeNormal;
58
59  virtual ~ProfileImpl();
60
61  static void RegisterUserPrefs(PrefRegistrySyncable* registry);
62
63  // content::BrowserContext implementation:
64  virtual base::FilePath GetPath() OVERRIDE;
65  virtual content::DownloadManagerDelegate*
66      GetDownloadManagerDelegate() OVERRIDE;
67  virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
68  virtual net::URLRequestContextGetter* GetRequestContextForRenderProcess(
69      int renderer_child_id) OVERRIDE;
70  virtual net::URLRequestContextGetter* GetMediaRequestContext() OVERRIDE;
71  virtual net::URLRequestContextGetter* GetMediaRequestContextForRenderProcess(
72      int renderer_child_id) OVERRIDE;
73  virtual net::URLRequestContextGetter*
74      GetMediaRequestContextForStoragePartition(
75          const base::FilePath& partition_path,
76          bool in_memory) OVERRIDE;
77  virtual content::ResourceContext* GetResourceContext() OVERRIDE;
78  virtual content::GeolocationPermissionContext*
79      GetGeolocationPermissionContext() OVERRIDE;
80  virtual content::SpeechRecognitionPreferences*
81      GetSpeechRecognitionPreferences() OVERRIDE;
82  virtual quota::SpecialStoragePolicy* GetSpecialStoragePolicy() OVERRIDE;
83
84  // Profile implementation:
85  virtual scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() OVERRIDE;
86  virtual std::string GetProfileName() OVERRIDE;
87  virtual bool IsOffTheRecord() const OVERRIDE;
88  virtual Profile* GetOffTheRecordProfile() OVERRIDE;
89  virtual void DestroyOffTheRecordProfile() OVERRIDE;
90  virtual bool HasOffTheRecordProfile() OVERRIDE;
91  virtual Profile* GetOriginalProfile() OVERRIDE;
92  virtual history::TopSites* GetTopSites() OVERRIDE;
93  virtual history::TopSites* GetTopSitesWithoutCreating() OVERRIDE;
94  virtual ExtensionService* GetExtensionService() OVERRIDE;
95  virtual ExtensionSpecialStoragePolicy*
96      GetExtensionSpecialStoragePolicy() OVERRIDE;
97  virtual policy::ManagedModePolicyProvider*
98      GetManagedModePolicyProvider() OVERRIDE;
99  virtual policy::PolicyService* GetPolicyService() OVERRIDE;
100  virtual PrefService* GetPrefs() OVERRIDE;
101  virtual PrefService* GetOffTheRecordPrefs() OVERRIDE;
102  virtual net::URLRequestContextGetter*
103      GetRequestContextForExtensions() OVERRIDE;
104  virtual net::SSLConfigService* GetSSLConfigService() OVERRIDE;
105  virtual HostContentSettingsMap* GetHostContentSettingsMap() OVERRIDE;
106  virtual ProtocolHandlerRegistry* GetProtocolHandlerRegistry() OVERRIDE;
107  virtual bool IsSameProfile(Profile* profile) OVERRIDE;
108  virtual base::Time GetStartTime() const OVERRIDE;
109  virtual net::URLRequestContextGetter* CreateRequestContext(
110      content::ProtocolHandlerMap* protocol_handlers) OVERRIDE;
111  virtual net::URLRequestContextGetter* CreateRequestContextForStoragePartition(
112      const base::FilePath& partition_path,
113      bool in_memory,
114      content::ProtocolHandlerMap* protocol_handlers) OVERRIDE;
115  virtual base::FilePath last_selected_directory() OVERRIDE;
116  virtual void set_last_selected_directory(const base::FilePath& path) OVERRIDE;
117  virtual chrome_browser_net::Predictor* GetNetworkPredictor() OVERRIDE;
118  virtual void ClearNetworkingHistorySince(
119      base::Time time,
120      const base::Closure& completion) OVERRIDE;
121  virtual GURL GetHomePage() OVERRIDE;
122  virtual bool WasCreatedByVersionOrLater(const std::string& version) OVERRIDE;
123  virtual void SetExitType(ExitType exit_type) OVERRIDE;
124  virtual ExitType GetLastSessionExitType() OVERRIDE;
125
126#if defined(OS_CHROMEOS)
127  virtual void ChangeAppLocale(const std::string& locale,
128                               AppLocaleChangedVia) OVERRIDE;
129  virtual void OnLogin() OVERRIDE;
130  virtual void SetupChromeOSEnterpriseExtensionObserver() OVERRIDE;
131  virtual void InitChromeOSPreferences() OVERRIDE;
132#endif  // defined(OS_CHROMEOS)
133
134  virtual PrefProxyConfigTracker* GetProxyConfigTracker() OVERRIDE;
135
136 private:
137  friend class Profile;
138  friend class BetterSessionRestoreCrashTest;
139  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest,
140                           ProfilesLaunchedAfterCrash);
141  FRIEND_TEST_ALL_PREFIXES(ProfileBrowserTest, ProfileReadmeCreated);
142  FRIEND_TEST_ALL_PREFIXES(ProfileBrowserTest,
143                           ProfileDeletedBeforeReadmeCreated);
144
145  // Delay, in milliseconds, before README file is created for a new profile.
146  // This is non-const for testing purposes.
147  static int create_readme_delay_ms;
148
149  ProfileImpl(const base::FilePath& path,
150              Delegate* delegate,
151              CreateMode create_mode,
152              base::SequencedTaskRunner* sequenced_task_runner);
153
154  // Does final initialization. Should be called after prefs were loaded.
155  void DoFinalInit(bool is_new_profile);
156
157  void InitHostZoomMap();
158
159  void OnDefaultZoomLevelChanged();
160  void OnZoomLevelChanged(
161      const content::HostZoomMap::ZoomLevelChange& change);
162
163  void OnInitializationCompleted(PrefService* pref_service,
164                                 bool succeeded);
165
166  // Does final prefs initialization and calls Init().
167  void OnPrefsLoaded(bool success);
168
169  base::FilePath GetPrefFilePath();
170
171#if defined(ENABLE_SESSION_SERVICE)
172  void StopCreateSessionServiceTimer();
173
174  void EnsureSessionServiceCreated();
175#endif
176
177
178  void EnsureRequestContextCreated() {
179    GetRequestContext();
180  }
181
182  void UpdateProfileUserNameCache();
183
184  // Updates the ProfileInfoCache with data from this profile.
185  void UpdateProfileNameCache();
186  void UpdateProfileAvatarCache();
187
188  void GetCacheParameters(bool is_media_context,
189                          base::FilePath* cache_path,
190                          int* max_size);
191
192  content::HostZoomMap::ZoomLevelChangedCallback zoom_callback_;
193  PrefChangeRegistrar pref_change_registrar_;
194
195  base::FilePath path_;
196  base::FilePath base_cache_path_;
197
198  // !!! BIG HONKING WARNING !!!
199  //  The order of the members below is important. Do not change it unless
200  //  you know what you're doing. Also, if adding a new member here make sure
201  //  that the declaration occurs AFTER things it depends on as destruction
202  //  happens in reverse order of declaration.
203
204#if defined(ENABLE_CONFIGURATION_POLICY)
205  // |prefs_| depends on |policy_service_|, which depends on
206  // |user_cloud_policy_manager_| and |managed_mode_policy_provider_|.
207  // TODO(bauerb, mnissler): Once |prefs_| is a ProfileKeyedService, these
208  // should become proper ProfileKeyedServices as well.
209#if !defined(OS_CHROMEOS)
210  scoped_ptr<policy::UserCloudPolicyManager> cloud_policy_manager_;
211#endif  // !defined(OS_CHROMEOS)
212#if defined(ENABLE_MANAGED_USERS)
213  scoped_ptr<policy::ManagedModePolicyProvider> managed_mode_policy_provider_;
214#endif  // defined(ENABLE_MANAGED_USERS)
215#endif  // defined(ENABLE_CONFIGURATION_POLICY)
216  scoped_ptr<policy::PolicyService> policy_service_;
217
218  // Keep |prefs_| on top for destruction order because |extension_prefs_|,
219  // |net_pref_observer_|, |io_data_| and others store pointers to |prefs_| and
220  // shall be destructed first.
221  scoped_refptr<PrefRegistrySyncable> pref_registry_;
222  scoped_ptr<PrefServiceSyncable> prefs_;
223  scoped_ptr<PrefServiceSyncable> otr_prefs_;
224  ProfileImplIOData::Handle io_data_;
225  scoped_refptr<ExtensionSpecialStoragePolicy>
226      extension_special_storage_policy_;
227  scoped_ptr<NetPrefObserver> net_pref_observer_;
228  scoped_ptr<SSLConfigServiceManager> ssl_config_service_manager_;
229  scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
230  scoped_refptr<history::ShortcutsBackend> shortcuts_backend_;
231
232  // Exit type the last time the profile was opened. This is set only once from
233  // prefs.
234  ExitType last_session_exit_type_;
235
236#if defined(ENABLE_SESSION_SERVICE)
237  base::OneShotTimer<ProfileImpl> create_session_service_timer_;
238#endif
239
240  scoped_ptr<Profile> off_the_record_profile_;
241
242  // See GetStartTime for details.
243  base::Time start_time_;
244
245  scoped_refptr<history::TopSites> top_sites_;  // For history and thumbnails.
246
247#if defined(OS_CHROMEOS)
248  scoped_ptr<chromeos::Preferences> chromeos_preferences_;
249
250  scoped_ptr<chromeos::EnterpriseExtensionObserver>
251      chromeos_enterprise_extension_observer_;
252
253  scoped_ptr<chromeos::LocaleChangeGuard> locale_change_guard_;
254#endif
255
256  scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_;
257
258  // STOP!!!! DO NOT ADD ANY MORE ITEMS HERE!!!!
259  //
260  // Instead, make your Service/Manager/whatever object you're hanging off the
261  // Profile use our new ProfileKeyedServiceFactory system instead. You can
262  // find the design document here:
263  //
264  //   https://sites.google.com/a/chromium.org/dev/developers/design-documents/profile-architecture
265  //
266  // and you can read the raw headers here:
267  //
268  //   chrome/browser/profile/profile_keyed_service.h
269  //   chrome/browser/profile/profile_keyed_service_factory.{h,cc}
270  //   chrome/browser/profile/profile_keyed_dependency_manager.{h,cc}
271
272  Profile::Delegate* delegate_;
273
274  chrome_browser_net::Predictor* predictor_;
275
276  DISALLOW_COPY_AND_ASSIGN(ProfileImpl);
277};
278
279#endif  // CHROME_BROWSER_PROFILES_PROFILE_IMPL_H_
280