profile_impl.h revision f2477e01787aa58f445919b809d89e252beef54f
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/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 LocaleChangeGuard;
31class Preferences;
32}
33#endif
34
35namespace base {
36class SequencedTaskRunner;
37}
38
39namespace extensions {
40class ExtensionSystem;
41}
42
43namespace policy {
44class CloudPolicyManager;
45class ProfilePolicyConnector;
46class SchemaRegistryService;
47}
48
49namespace user_prefs {
50class refRegistrySyncable;
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 RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
62
63  // content::BrowserContext implementation:
64  virtual base::FilePath GetPath() const 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 void RequestMIDISysExPermission(
78      int render_process_id,
79      int render_view_id,
80      int bridge_id,
81      const GURL& requesting_frame,
82      const MIDISysExPermissionCallback& callback) OVERRIDE;
83  virtual void CancelMIDISysExPermissionRequest(
84      int render_process_id,
85      int render_view_id,
86      int bridge_id,
87      const GURL& requesting_frame) OVERRIDE;
88  virtual content::ResourceContext* GetResourceContext() OVERRIDE;
89  virtual content::GeolocationPermissionContext*
90      GetGeolocationPermissionContext() OVERRIDE;
91  virtual quota::SpecialStoragePolicy* GetSpecialStoragePolicy() OVERRIDE;
92
93  // Profile implementation:
94  virtual scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() OVERRIDE;
95  // Note that this implementation returns the Google-services username, if any,
96  // not the Chrome user's display name.
97  virtual std::string GetProfileName() OVERRIDE;
98  virtual bool IsOffTheRecord() const OVERRIDE;
99  virtual Profile* GetOffTheRecordProfile() OVERRIDE;
100  virtual void DestroyOffTheRecordProfile() OVERRIDE;
101  virtual bool HasOffTheRecordProfile() OVERRIDE;
102  virtual Profile* GetOriginalProfile() OVERRIDE;
103  virtual bool IsManaged() OVERRIDE;
104  virtual history::TopSites* GetTopSites() OVERRIDE;
105  virtual history::TopSites* GetTopSitesWithoutCreating() OVERRIDE;
106  virtual ExtensionService* GetExtensionService() OVERRIDE;
107  virtual ExtensionSpecialStoragePolicy*
108      GetExtensionSpecialStoragePolicy() OVERRIDE;
109  virtual PrefService* GetPrefs() OVERRIDE;
110  virtual PrefService* GetOffTheRecordPrefs() OVERRIDE;
111  virtual net::URLRequestContextGetter*
112      GetRequestContextForExtensions() OVERRIDE;
113  virtual net::SSLConfigService* GetSSLConfigService() OVERRIDE;
114  virtual HostContentSettingsMap* GetHostContentSettingsMap() OVERRIDE;
115  virtual bool IsSameProfile(Profile* profile) OVERRIDE;
116  virtual base::Time GetStartTime() const OVERRIDE;
117  virtual net::URLRequestContextGetter* CreateRequestContext(
118      content::ProtocolHandlerMap* protocol_handlers) OVERRIDE;
119  virtual net::URLRequestContextGetter* CreateRequestContextForStoragePartition(
120      const base::FilePath& partition_path,
121      bool in_memory,
122      content::ProtocolHandlerMap* protocol_handlers) OVERRIDE;
123  virtual base::FilePath last_selected_directory() OVERRIDE;
124  virtual void set_last_selected_directory(const base::FilePath& path) OVERRIDE;
125  virtual chrome_browser_net::Predictor* GetNetworkPredictor() OVERRIDE;
126  virtual void ClearNetworkingHistorySince(
127      base::Time time,
128      const base::Closure& completion) OVERRIDE;
129  virtual GURL GetHomePage() OVERRIDE;
130  virtual bool WasCreatedByVersionOrLater(const std::string& version) OVERRIDE;
131  virtual void SetExitType(ExitType exit_type) OVERRIDE;
132  virtual ExitType GetLastSessionExitType() OVERRIDE;
133
134#if defined(OS_CHROMEOS)
135  virtual void ChangeAppLocale(const std::string& locale,
136                               AppLocaleChangedVia) OVERRIDE;
137  virtual void OnLogin() OVERRIDE;
138  virtual void InitChromeOSPreferences() OVERRIDE;
139#endif  // defined(OS_CHROMEOS)
140
141  virtual PrefProxyConfigTracker* GetProxyConfigTracker() OVERRIDE;
142
143 private:
144  friend class Profile;
145  friend class BetterSessionRestoreCrashTest;
146  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest,
147                           ProfilesLaunchedAfterCrash);
148  FRIEND_TEST_ALL_PREFIXES(ProfileBrowserTest, ProfileReadmeCreated);
149  FRIEND_TEST_ALL_PREFIXES(ProfileBrowserTest,
150                           ProfileDeletedBeforeReadmeCreated);
151
152  // Delay, in milliseconds, before README file is created for a new profile.
153  // This is non-const for testing purposes.
154  static int create_readme_delay_ms;
155
156  ProfileImpl(const base::FilePath& path,
157              Delegate* delegate,
158              CreateMode create_mode,
159              base::SequencedTaskRunner* sequenced_task_runner);
160
161  // Does final initialization. Should be called after prefs were loaded.
162  void DoFinalInit();
163
164  void InitHostZoomMap();
165
166  void OnDefaultZoomLevelChanged();
167  void OnZoomLevelChanged(
168      const content::HostZoomMap::ZoomLevelChange& change);
169
170  // Does final prefs initialization and calls Init().
171  void OnPrefsLoaded(bool success);
172
173  base::FilePath GetPrefFilePath();
174
175#if defined(ENABLE_SESSION_SERVICE)
176  void StopCreateSessionServiceTimer();
177
178  void EnsureSessionServiceCreated();
179#endif
180
181
182  void EnsureRequestContextCreated() {
183    GetRequestContext();
184  }
185
186  void UpdateProfileUserNameCache();
187
188  // Updates the ProfileInfoCache with data from this profile.
189  void UpdateProfileNameCache();
190  void UpdateProfileAvatarCache();
191  void UpdateProfileIsEphemeralCache();
192
193  void GetCacheParameters(bool is_media_context,
194                          base::FilePath* cache_path,
195                          int* max_size);
196
197  PrefProxyConfigTracker* CreateProxyConfigTracker();
198
199  scoped_ptr<content::HostZoomMap::Subscription> zoom_subscription_;
200  PrefChangeRegistrar pref_change_registrar_;
201
202  base::FilePath path_;
203  base::FilePath base_cache_path_;
204
205  // !!! BIG HONKING WARNING !!!
206  //  The order of the members below is important. Do not change it unless
207  //  you know what you're doing. Also, if adding a new member here make sure
208  //  that the declaration occurs AFTER things it depends on as destruction
209  //  happens in reverse order of declaration.
210
211  // TODO(mnissler, joaodasilva): The |profile_policy_connector_| provides the
212  // PolicyService that the |prefs_| depend on, and must outlive |prefs_|.
213  // This can be removed once |prefs_| becomes a BrowserContextKeyedService too.
214  // |profile_policy_connector_| in turn depends on |cloud_policy_manager_|,
215  // which depends on |schema_registry_service_|.
216#if defined(ENABLE_CONFIGURATION_POLICY)
217  scoped_ptr<policy::SchemaRegistryService> schema_registry_service_;
218  scoped_ptr<policy::CloudPolicyManager> cloud_policy_manager_;
219#endif
220  scoped_ptr<policy::ProfilePolicyConnector> profile_policy_connector_;
221
222  // Keep |prefs_| on top for destruction order because |extension_prefs_|,
223  // |net_pref_observer_|, |io_data_| and others store pointers to |prefs_| and
224  // shall be destructed first.
225  scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry_;
226  scoped_ptr<PrefServiceSyncable> prefs_;
227  scoped_ptr<PrefServiceSyncable> otr_prefs_;
228  ProfileImplIOData::Handle io_data_;
229  scoped_refptr<ExtensionSpecialStoragePolicy>
230      extension_special_storage_policy_;
231  scoped_ptr<NetPrefObserver> net_pref_observer_;
232  scoped_ptr<SSLConfigServiceManager> ssl_config_service_manager_;
233  scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
234  scoped_refptr<history::ShortcutsBackend> shortcuts_backend_;
235
236  // Exit type the last time the profile was opened. This is set only once from
237  // prefs.
238  ExitType last_session_exit_type_;
239
240#if defined(ENABLE_SESSION_SERVICE)
241  base::OneShotTimer<ProfileImpl> create_session_service_timer_;
242#endif
243
244  scoped_ptr<Profile> off_the_record_profile_;
245
246  // See GetStartTime for details.
247  base::Time start_time_;
248
249  scoped_refptr<history::TopSites> top_sites_;  // For history and thumbnails.
250
251#if defined(OS_CHROMEOS)
252  scoped_ptr<chromeos::Preferences> chromeos_preferences_;
253
254  scoped_ptr<chromeos::LocaleChangeGuard> locale_change_guard_;
255
256  bool is_login_profile_;
257#endif
258
259  scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_;
260
261  // STOP!!!! DO NOT ADD ANY MORE ITEMS HERE!!!!
262  //
263  // Instead, make your Service/Manager/whatever object you're hanging off the
264  // Profile use our new BrowserContextKeyedServiceFactory system instead.
265  // You can find the design document here:
266  //
267  //   https://sites.google.com/a/chromium.org/dev/developers/design-documents/profile-architecture
268  //
269  // and you can read the raw headers here:
270  //
271  //   components/browser_context_keyed_service/browser_context_dependency_manager.{h,cc}
272  //   components/browser_context_keyed_service/browser_context_keyed_service.h
273  //   components/browser_context_keyed_service/browser_context_keyed_service_factory.{h,cc}
274
275  Profile::Delegate* delegate_;
276
277  chrome_browser_net::Predictor* predictor_;
278
279  DISALLOW_COPY_AND_ASSIGN(ProfileImpl);
280};
281
282#endif  // CHROME_BROWSER_PROFILES_PROFILE_IMPL_H_
283