1 // Copyright 2013 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 // Declares a delegate that interacts with the rest of the browser on behalf of 6 // the AutomaticProfileResetter. 7 // 8 // The reason for this separation is to facilitate unit testing. Factoring out 9 // the implementation for each interaction step (encapsulated by one method of 10 // the delegate) allows it to be tested independently in itself. It also becomes 11 // easier to verify that the state machine inside AutomaticProfileResetter works 12 // correctly: by mocking out the interaction methods in the delegate, we can, in 13 // effect, mock out the entire rest of the browser, allowing us to easily 14 // simulate scenarios that are interesting for testing the state machine. 15 // 16 // The delegate is normally instantiated by AutomaticProfileResetter internally, 17 // while a mock implementation can be injected during unit tests. 18 19 #ifndef CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_DELEGATE_H_ 20 #define CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_DELEGATE_H_ 21 22 #include "base/basictypes.h" 23 #include "base/callback_forward.h" 24 #include "base/memory/scoped_ptr.h" 25 #include "base/memory/weak_ptr.h" 26 #include "chrome/browser/profile_resetter/profile_resetter.h" 27 #include "chrome/browser/search_engines/template_url_service_observer.h" 28 #include "content/public/browser/notification_observer.h" 29 #include "content/public/browser/notification_registrar.h" 30 #include "extensions/common/one_shot_event.h" 31 32 class BrandcodeConfigFetcher; 33 class GlobalErrorService; 34 class Profile; 35 class ResettableSettingsSnapshot; 36 class TemplateURLService; 37 38 namespace base { 39 class DictionaryValue; 40 class ListValue; 41 } 42 43 // Defines the interface for the delegate that will interact with the rest of 44 // the browser on behalf of the AutomaticProfileResetter. 45 class AutomaticProfileResetterDelegate { 46 public: ~AutomaticProfileResetterDelegate()47 virtual ~AutomaticProfileResetterDelegate() {} 48 49 // Requests the module enumerator to start scanning for loaded modules now, if 50 // it has not done so already. 51 virtual void EnumerateLoadedModulesIfNeeded() = 0; 52 53 // Requests |ready_callback| to be posted on the UI thread once the module 54 // enumerator has finished scanning for loaded modules. 55 virtual void RequestCallbackWhenLoadedModulesAreEnumerated( 56 const base::Closure& ready_callback) const = 0; 57 58 // Requests the template URL service to load its database (asynchronously). 59 virtual void LoadTemplateURLServiceIfNeeded() = 0; 60 61 // Requests |ready_callback| to be posted on the UI thread once the template 62 // URL service has finished loading its database. 63 virtual void RequestCallbackWhenTemplateURLServiceIsLoaded( 64 const base::Closure& ready_callback) const = 0; 65 66 // Starts downloading the configuration file that specifies the default 67 // settings for the current brandcode; or establishes that we are running a 68 // vanilla (non-branded) build. 69 virtual void FetchBrandcodedDefaultSettingsIfNeeded() = 0; 70 71 // Requests |ready_callback| to be posted on the UI thread once the brandcoded 72 // default settings have been downloaded, or once it has been established that 73 // we are running a vanilla (non-branded) build. 74 virtual void RequestCallbackWhenBrandcodedDefaultsAreFetched( 75 const base::Closure& ready_callback) const = 0; 76 77 // Returns a list of loaded module name digests. 78 virtual scoped_ptr<base::ListValue> GetLoadedModuleNameDigests() const = 0; 79 80 // Returns attributes of the search engine currently set as the default (or 81 // an empty dictionary if there is none). 82 // The dictionary is in the same format as persisted to preferences by 83 // DefaultSearchManager::SetUserSelectedDefaultSearchEngine. 84 virtual scoped_ptr<base::DictionaryValue> 85 GetDefaultSearchProviderDetails() const = 0; 86 87 // Returns whether or not the default search provider is set by policy. 88 virtual bool IsDefaultSearchProviderManaged() const = 0; 89 90 // Returns a list of dictionaries, each containing attributes for each of the 91 // pre-populated search engines, in the format described above. 92 virtual scoped_ptr<base::ListValue> 93 GetPrepopulatedSearchProvidersDetails() const = 0; 94 95 // Triggers showing the one-time profile settings reset prompt, which consists 96 // of two parts: (1.) the profile reset (pop-up) bubble, and (2.) a menu item 97 // in the wrench menu. Showing the bubble might be delayed if there is another 98 // bubble currently shown, in which case the bubble will be shown the first 99 // time a new browser window is opened. 100 // Returns true unless the reset prompt is not supported on the current 101 // platform, in which case it returns false and does nothing. 102 virtual bool TriggerPrompt() = 0; 103 104 // Triggers the ProfileResetter to reset all supported settings and optionally 105 // |send_feedback|. Will post |completion| on the UI thread once completed. 106 // Brandcoded default settings will be fetched if they are not yet available, 107 // the reset will be performed once the download is complete. 108 // NOTE: Should only be called at most once during the lifetime of the object. 109 virtual void TriggerProfileSettingsReset(bool send_feedback, 110 const base::Closure& completion) = 0; 111 112 // Dismisses the one-time profile settings reset prompt, if it is currently 113 // triggered. Will synchronously destroy the corresponding GlobalError. 114 virtual void DismissPrompt() = 0; 115 }; 116 117 // Implementation for AutomaticProfileResetterDelegate. 118 class AutomaticProfileResetterDelegateImpl 119 : public AutomaticProfileResetterDelegate, 120 public base::SupportsWeakPtr<AutomaticProfileResetterDelegateImpl>, 121 public TemplateURLServiceObserver, 122 public content::NotificationObserver { 123 public: 124 explicit AutomaticProfileResetterDelegateImpl( 125 Profile* profile, 126 ProfileResetter::ResettableFlags resettable_aspects); 127 virtual ~AutomaticProfileResetterDelegateImpl(); 128 129 // Returns the brandcoded default settings; empty defaults if this is not a 130 // branded build; or NULL if FetchBrandcodedDefaultSettingsIfNeeded() has not 131 // yet been called or not yet finished. Exposed only for unit tests. brandcoded_defaults()132 const BrandcodedDefaultSettings* brandcoded_defaults() const { 133 return brandcoded_defaults_.get(); 134 } 135 136 // AutomaticProfileResetterDelegate: 137 virtual void EnumerateLoadedModulesIfNeeded() OVERRIDE; 138 virtual void RequestCallbackWhenLoadedModulesAreEnumerated( 139 const base::Closure& ready_callback) const OVERRIDE; 140 virtual void LoadTemplateURLServiceIfNeeded() OVERRIDE; 141 virtual void RequestCallbackWhenTemplateURLServiceIsLoaded( 142 const base::Closure& ready_callback) const OVERRIDE; 143 virtual void FetchBrandcodedDefaultSettingsIfNeeded() OVERRIDE; 144 virtual void RequestCallbackWhenBrandcodedDefaultsAreFetched( 145 const base::Closure& ready_callback) const OVERRIDE; 146 virtual scoped_ptr<base::ListValue> 147 GetLoadedModuleNameDigests() const OVERRIDE; 148 virtual scoped_ptr<base::DictionaryValue> 149 GetDefaultSearchProviderDetails() const OVERRIDE; 150 virtual bool IsDefaultSearchProviderManaged() const OVERRIDE; 151 virtual scoped_ptr<base::ListValue> 152 GetPrepopulatedSearchProvidersDetails() const OVERRIDE; 153 virtual bool TriggerPrompt() OVERRIDE; 154 virtual void TriggerProfileSettingsReset( 155 bool send_feedback, 156 const base::Closure& completion) OVERRIDE; 157 virtual void DismissPrompt() OVERRIDE; 158 159 // TemplateURLServiceObserver: 160 virtual void OnTemplateURLServiceChanged() OVERRIDE; 161 162 // content::NotificationObserver: 163 virtual void Observe(int type, 164 const content::NotificationSource& source, 165 const content::NotificationDetails& details) OVERRIDE; 166 167 private: 168 // Sends a feedback |report|, where |report| is supposed to be result of 169 // ::SerializeSettingsReport(). Virtual, so it can be mocked out for tests. 170 virtual void SendFeedback(const std::string& report) const; 171 172 // Triggers the ProfileResetter to reset |resettable_aspects_| and optionally 173 // |send_feedback|. Will invoke |completion| on the UI thread once completed 174 // The |brandcoded_defaults_| must already be initialized when this is called. 175 void RunProfileSettingsReset(bool send_feedback, 176 const base::Closure& completion); 177 178 // Called by |brandcoded_config_fetcher_| when it finishes downloading the 179 // brandcoded default settings (or the download times out). 180 void OnBrandcodedDefaultsFetched(); 181 182 // Called back by the ProfileResetter once resetting the profile settings has 183 // been completed. If |old_settings_snapshot| is non-NULL, will compare it to 184 // the new settings and send the differences to Google for analysis. Finally, 185 // will post |user_callback|. 186 void OnProfileSettingsResetCompleted( 187 const base::Closure& user_callback, 188 scoped_ptr<ResettableSettingsSnapshot> old_settings_snapshot); 189 190 // The profile that this delegate operates on. 191 Profile* profile_; 192 193 // Shortcuts to |profile_| keyed services, to reduce boilerplate. These may be 194 // NULL in unit tests that do not initialize the respective service(s). 195 GlobalErrorService* global_error_service_; 196 TemplateURLService* template_url_service_; 197 198 // Helper to asynchronously download the default settings for the current 199 // distribution channel (identified by brand code). Instantiated on-demand. 200 scoped_ptr<BrandcodeConfigFetcher> brandcoded_config_fetcher_; 201 202 // Once |brandcoded_defaults_fetched_event_| has fired, this will contain the 203 // brandcoded default settings, or empty settings for a non-branded build. 204 scoped_ptr<BrandcodedDefaultSettings> brandcoded_defaults_; 205 206 // Overwritten to avoid resetting aspects that will not work in unit tests. 207 const ProfileResetter::ResettableFlags resettable_aspects_; 208 209 // The profile resetter to perform the actual reset. Instantiated on-demand. 210 scoped_ptr<ProfileResetter> profile_resetter_; 211 212 // Manages registrations/unregistrations for notifications. 213 content::NotificationRegistrar registrar_; 214 215 // The list of modules found. Even when |modules_have_been_enumerated_event_| 216 // is signaled, this may still be NULL. 217 scoped_ptr<base::ListValue> module_list_; 218 219 // This event is signaled once module enumeration has been attempted. If 220 // during construction, EnumerateModulesModel can supply a non-empty list of 221 // modules, module enumeration has clearly already happened, so the event will 222 // be signaled immediately; otherwise, when EnumerateLoadedModulesIfNeeded() 223 // is called, it will ask the model to scan the modules, and then signal the 224 // event once this process is completed. 225 extensions::OneShotEvent modules_have_been_enumerated_event_; 226 227 // This event is signaled once the TemplateURLService has loaded. If the 228 // TemplateURLService was already loaded prior to the creation of this class, 229 // the event will be signaled during construction. 230 extensions::OneShotEvent template_url_service_ready_event_; 231 232 // This event is signaled once brandcoded default settings have been fetched, 233 // or once it has been established that this is not a branded build. 234 extensions::OneShotEvent brandcoded_defaults_fetched_event_; 235 236 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateImpl); 237 }; 238 239 #endif // CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_DELEGATE_H_ 240