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