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