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