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