Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profile_resetter / automatic_profile_resetter_delegate.h
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 "components/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:
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   ~AutomaticProfileResetterDelegateImpl() override;
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.
132   const BrandcodedDefaultSettings* brandcoded_defaults() const {
133     return brandcoded_defaults_.get();
134   }
135
136   // AutomaticProfileResetterDelegate:
137   void EnumerateLoadedModulesIfNeeded() override;
138   void RequestCallbackWhenLoadedModulesAreEnumerated(
139       const base::Closure& ready_callback) const override;
140   void LoadTemplateURLServiceIfNeeded() override;
141   void RequestCallbackWhenTemplateURLServiceIsLoaded(
142       const base::Closure& ready_callback) const override;
143   void FetchBrandcodedDefaultSettingsIfNeeded() override;
144   void RequestCallbackWhenBrandcodedDefaultsAreFetched(
145       const base::Closure& ready_callback) const override;
146   scoped_ptr<base::ListValue> GetLoadedModuleNameDigests() const override;
147   scoped_ptr<base::DictionaryValue> GetDefaultSearchProviderDetails()
148       const override;
149   bool IsDefaultSearchProviderManaged() const override;
150   scoped_ptr<base::ListValue> GetPrepopulatedSearchProvidersDetails()
151       const override;
152   bool TriggerPrompt() override;
153   void TriggerProfileSettingsReset(bool send_feedback,
154                                    const base::Closure& completion) override;
155   void DismissPrompt() override;
156
157   // TemplateURLServiceObserver:
158   void OnTemplateURLServiceChanged() override;
159
160   // content::NotificationObserver:
161   void Observe(int type,
162                const content::NotificationSource& source,
163                const content::NotificationDetails& details) override;
164
165  private:
166   // Sends a feedback |report|, where |report| is supposed to be result of
167   // ::SerializeSettingsReport(). Virtual, so it can be mocked out for tests.
168   virtual void SendFeedback(const std::string& report) const;
169
170   // Triggers the ProfileResetter to reset |resettable_aspects_| and optionally
171   // |send_feedback|. Will invoke |completion| on the UI thread once completed
172   // The |brandcoded_defaults_| must already be initialized when this is called.
173   void RunProfileSettingsReset(bool send_feedback,
174                                const base::Closure& completion);
175
176   // Called by |brandcoded_config_fetcher_| when it finishes downloading the
177   // brandcoded default settings (or the download times out).
178   void OnBrandcodedDefaultsFetched();
179
180   // Called back by the ProfileResetter once resetting the profile settings has
181   // been completed. If |old_settings_snapshot| is non-NULL, will compare it to
182   // the new settings and send the differences to Google for analysis. Finally,
183   // will post |user_callback|.
184   void OnProfileSettingsResetCompleted(
185       const base::Closure& user_callback,
186       scoped_ptr<ResettableSettingsSnapshot> old_settings_snapshot);
187
188   // The profile that this delegate operates on.
189   Profile* profile_;
190
191   // Shortcuts to |profile_| keyed services, to reduce boilerplate. These may be
192   // NULL in unit tests that do not initialize the respective service(s).
193   GlobalErrorService* global_error_service_;
194   TemplateURLService* template_url_service_;
195
196   // Helper to asynchronously download the default settings for the current
197   // distribution channel (identified by brand code). Instantiated on-demand.
198   scoped_ptr<BrandcodeConfigFetcher> brandcoded_config_fetcher_;
199
200   // Once |brandcoded_defaults_fetched_event_| has fired, this will contain the
201   // brandcoded default settings, or empty settings for a non-branded build.
202   scoped_ptr<BrandcodedDefaultSettings> brandcoded_defaults_;
203
204   // Overwritten to avoid resetting aspects that will not work in unit tests.
205   const ProfileResetter::ResettableFlags resettable_aspects_;
206
207   // The profile resetter to perform the actual reset. Instantiated on-demand.
208   scoped_ptr<ProfileResetter> profile_resetter_;
209
210   // Manages registrations/unregistrations for notifications.
211   content::NotificationRegistrar registrar_;
212
213   // The list of modules found. Even when |modules_have_been_enumerated_event_|
214   // is signaled, this may still be NULL.
215   scoped_ptr<base::ListValue> module_list_;
216
217   // This event is signaled once module enumeration has been attempted. If
218   // during construction, EnumerateModulesModel can supply a non-empty list of
219   // modules, module enumeration has clearly already happened, so the event will
220   // be signaled immediately; otherwise, when EnumerateLoadedModulesIfNeeded()
221   // is called, it will ask the model to scan the modules, and then signal the
222   // event once this process is completed.
223   extensions::OneShotEvent modules_have_been_enumerated_event_;
224
225   // This event is signaled once the TemplateURLService has loaded.  If the
226   // TemplateURLService was already loaded prior to the creation of this class,
227   // the event will be signaled during construction.
228   extensions::OneShotEvent template_url_service_ready_event_;
229
230   // This event is signaled once brandcoded default settings have been fetched,
231   // or once it has been established that this is not a branded build.
232   extensions::OneShotEvent brandcoded_defaults_fetched_event_;
233
234   DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateImpl);
235 };
236
237 #endif  // CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_DELEGATE_H_