profile_impl.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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 PrefService;
25class PrefServiceSyncable;
26class SSLConfigServiceManager;
27
28#if defined(OS_CHROMEOS)
29namespace chromeos {
30class EnterpriseExtensionObserver;
31class LocaleChangeGuard;
32class Preferences;
33}
34#endif
35
36namespace base {
37class SequencedTaskRunner;
38}
39
40namespace content {
41class SpeechRecognitionPreferences;
42}
43
44namespace extensions {
45class ExtensionSystem;
46}
47
48namespace policy {
49class CloudPolicyManager;
50class ProfilePolicyConnector;
51}
52
53namespace user_prefs {
54class refRegistrySyncable;
55}
56
57// The default profile implementation.
58class ProfileImpl : public Profile {
59 public:
60  // Value written to prefs when the exit type is EXIT_NORMAL. Public for tests.
61  static const char* const kPrefExitTypeNormal;
62
63  virtual ~ProfileImpl();
64
65  static void RegisterUserPrefs(user_prefs::PrefRegistrySyncable* registry);
66
67  // content::BrowserContext implementation:
68  virtual base::FilePath GetPath() OVERRIDE;
69  virtual content::DownloadManagerDelegate*
70      GetDownloadManagerDelegate() OVERRIDE;
71  virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
72  virtual net::URLRequestContextGetter* GetRequestContextForRenderProcess(
73      int renderer_child_id) OVERRIDE;
74  virtual net::URLRequestContextGetter* GetMediaRequestContext() OVERRIDE;
75  virtual net::URLRequestContextGetter* GetMediaRequestContextForRenderProcess(
76      int renderer_child_id) OVERRIDE;
77  virtual net::URLRequestContextGetter*
78      GetMediaRequestContextForStoragePartition(
79          const base::FilePath& partition_path,
80          bool in_memory) OVERRIDE;
81  virtual content::ResourceContext* GetResourceContext() OVERRIDE;
82  virtual content::GeolocationPermissionContext*
83      GetGeolocationPermissionContext() OVERRIDE;
84  virtual content::SpeechRecognitionPreferences*
85      GetSpeechRecognitionPreferences() OVERRIDE;
86  virtual quota::SpecialStoragePolicy* GetSpecialStoragePolicy() OVERRIDE;
87
88  // Profile implementation:
89  virtual scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() OVERRIDE;
90  virtual std::string GetProfileName() OVERRIDE;
91  virtual bool IsOffTheRecord() const OVERRIDE;
92  virtual Profile* GetOffTheRecordProfile() OVERRIDE;
93  virtual void DestroyOffTheRecordProfile() OVERRIDE;
94  virtual bool HasOffTheRecordProfile() OVERRIDE;
95  virtual Profile* GetOriginalProfile() OVERRIDE;
96  virtual history::TopSites* GetTopSites() OVERRIDE;
97  virtual history::TopSites* GetTopSitesWithoutCreating() OVERRIDE;
98  virtual ExtensionService* GetExtensionService() OVERRIDE;
99  virtual ExtensionSpecialStoragePolicy*
100      GetExtensionSpecialStoragePolicy() OVERRIDE;
101  virtual PrefService* GetPrefs() OVERRIDE;
102  virtual PrefService* GetOffTheRecordPrefs() OVERRIDE;
103  virtual net::URLRequestContextGetter*
104      GetRequestContextForExtensions() OVERRIDE;
105  virtual net::SSLConfigService* GetSSLConfigService() OVERRIDE;
106  virtual HostContentSettingsMap* GetHostContentSettingsMap() 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();
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  // TODO(mnissler, joaodasilva): The |profile_policy_connector_| provides the
205  // PolicyService that the |prefs_| depend on, and must outlive |prefs_|.
206  // This can be removed once |prefs_| becomes a BrowserContextKeyedService too.
207  // |profile_policy_connector_| in turn depends on |cloud_policy_manager_|.
208#if defined(ENABLE_CONFIGURATION_POLICY)
209  scoped_ptr<policy::CloudPolicyManager> cloud_policy_manager_;
210#endif
211  scoped_ptr<policy::ProfilePolicyConnector> profile_policy_connector_;
212
213  // Keep |prefs_| on top for destruction order because |extension_prefs_|,
214  // |net_pref_observer_|, |io_data_| and others store pointers to |prefs_| and
215  // shall be destructed first.
216  scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry_;
217  scoped_ptr<PrefServiceSyncable> prefs_;
218  scoped_ptr<PrefServiceSyncable> otr_prefs_;
219  ProfileImplIOData::Handle io_data_;
220  scoped_refptr<ExtensionSpecialStoragePolicy>
221      extension_special_storage_policy_;
222  scoped_ptr<NetPrefObserver> net_pref_observer_;
223  scoped_ptr<SSLConfigServiceManager> ssl_config_service_manager_;
224  scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
225  scoped_refptr<history::ShortcutsBackend> shortcuts_backend_;
226
227  // Exit type the last time the profile was opened. This is set only once from
228  // prefs.
229  ExitType last_session_exit_type_;
230
231#if defined(ENABLE_SESSION_SERVICE)
232  base::OneShotTimer<ProfileImpl> create_session_service_timer_;
233#endif
234
235  scoped_ptr<Profile> off_the_record_profile_;
236
237  // See GetStartTime for details.
238  base::Time start_time_;
239
240  scoped_refptr<history::TopSites> top_sites_;  // For history and thumbnails.
241
242#if defined(OS_CHROMEOS)
243  scoped_ptr<chromeos::Preferences> chromeos_preferences_;
244
245  scoped_ptr<chromeos::EnterpriseExtensionObserver>
246      chromeos_enterprise_extension_observer_;
247
248  scoped_ptr<chromeos::LocaleChangeGuard> locale_change_guard_;
249#endif
250
251  scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_;
252
253  // STOP!!!! DO NOT ADD ANY MORE ITEMS HERE!!!!
254  //
255  // Instead, make your Service/Manager/whatever object you're hanging off the
256  // Profile use our new BrowserContextKeyedServiceFactory system instead.
257  // You can find the design document here:
258  //
259  //   https://sites.google.com/a/chromium.org/dev/developers/design-documents/profile-architecture
260  //
261  // and you can read the raw headers here:
262  //
263  //   components/browser_context_keyed_service/browser_context_dependency_manager.{h,cc}
264  //   components/browser_context_keyed_service/browser_context_keyed_service.h
265  //   components/browser_context_keyed_service/browser_context_keyed_service_factory.{h,cc}
266
267  Profile::Delegate* delegate_;
268
269  chrome_browser_net::Predictor* predictor_;
270
271  DISALLOW_COPY_AND_ASSIGN(ProfileImpl);
272};
273
274#endif  // CHROME_BROWSER_PROFILES_PROFILE_IMPL_H_
275