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