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