1 // Copyright 2017 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_SAFE_BROWSING_SETTINGS_RESET_PROMPT_SETTINGS_RESET_PROMPT_MODEL_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_SETTINGS_RESET_PROMPT_SETTINGS_RESET_PROMPT_MODEL_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <string>
12 #include <unordered_map>
13 #include <unordered_set>
14 #include <vector>
15 
16 #include "base/callback_forward.h"
17 #include "base/macros.h"
18 #include "base/time/time.h"
19 #include "chrome/browser/profile_resetter/profile_resetter.h"
20 #include "chrome/browser/profile_resetter/resettable_settings_snapshot.h"
21 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prompt_config.h"
22 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prompt_prefs_manager.h"
23 #include "extensions/common/extension_id.h"
24 #include "url/gurl.h"
25 
26 class BrandcodedDefaultSettings;
27 class Profile;
28 
29 namespace safe_browsing {
30 
31 // Encapsulates information about settings needed for the settings reset prompt
32 // and implements the reset logic.
33 //
34 // When more than one setting has a match in the list of domains in the config
35 // object, the model chooses one of the settings for reset based on the
36 // following priority list:
37 // 1. Default search engine
38 // 2. Startup URLs
39 // 3. Homepage
40 class SettingsResetPromptModel {
41  public:
42   enum ResetState {
43     RESET_REQUIRED = 1,
44     NO_RESET_REQUIRED_DUE_TO_DOMAIN_NOT_MATCHED = 2,
45     NO_RESET_REQUIRED_DUE_TO_ALREADY_PROMPTED_FOR_SETTING = 3,
46     NO_RESET_REQUIRED_DUE_TO_RECENTLY_PROMPTED = 4,
47     NO_RESET_REQUIRED_DUE_TO_OTHER_SETTING_REQUIRING_RESET = 5,
48     NO_RESET_REQUIRED_DUE_TO_POLICY = 6,
49     NO_RESET_REQUIRED_DUE_TO_EXTENSION_OVERRIDE = 7,
50     RESET_STATE_MAX = 8
51   };
52 
53   SettingsResetPromptModel(
54       Profile* profile,
55       std::unique_ptr<SettingsResetPromptConfig> prompt_config,
56       std::unique_ptr<ProfileResetter> profile_resetter);
57   virtual ~SettingsResetPromptModel();
58 
59   Profile* profile() const;
60   SettingsResetPromptConfig* config() const;
61 
62   // Returns true if reset is enabled for any settings type.
63   virtual bool ShouldPromptForReset() const;
64   // Resets the settings whose reset states are set to |RESET_REQUIRED| as
65   // returned by the methods below. Should be called only on the UI
66   // thread. |done_callback| will called from the UI thread when the reset
67   // operation has been completed.
68   //
69   // NOTE: Can only be called once during the lifetime of this object.
70   virtual void PerformReset(
71       std::unique_ptr<BrandcodedDefaultSettings> default_settings,
72       base::OnceClosure done_callback);
73   // To be called when the reset prompt dialog has been shown so that
74   // preferences can be updated.
75   virtual void DialogShown();
76 
77   virtual GURL homepage() const;
78   virtual ResetState homepage_reset_state() const;
79 
80   virtual GURL default_search() const;
81   virtual ResetState default_search_reset_state() const;
82 
83   // Returns list of all current startup URLs. Returns empty list if session
84   // startup is set to show the NTP or restore last session.
85   virtual const std::vector<GURL>& startup_urls() const;
86   // Returns the list of all startup URLs that have a match in the prompt
87   // config. This is a subset of the URLs returned by |startup_urls()|.
88   virtual const std::vector<GURL>& startup_urls_to_reset() const;
89   virtual ResetState startup_urls_reset_state() const;
90 
91   void ReportUmaMetrics() const;
92 
93  private:
94   // Functions to be called by the constructor to initialize the model
95   // object. These functions should be called in the order in which they are
96   // declared here so that the correct setting is chosen for the prompt when
97   // more than one setting requires reset (see also the class description).
98   void InitDefaultSearchData();
99   void InitStartupUrlsData();
100   void InitHomepageData();
101   void BlockResetForSettingOverridenByExtension();
102 
103   // Helper function for the Init* functions above to determine the reset state
104   // of settings that have a match in the config.
105   ResetState GetResetStateForSetting(
106       const base::Time& last_triggered_for_setting) const;
107 
108   // Return true if any setting's reset state is set to |RESET_REQUIRED|.
109   bool SomeSettingRequiresReset() const;
110 
111   bool SomeSettingIsManaged() const;
112 
113   Profile* const profile_;
114 
115   SettingsResetPromptPrefsManager prefs_manager_;
116   std::unique_ptr<SettingsResetPromptConfig> prompt_config_;
117   std::unique_ptr<ResettableSettingsSnapshot> settings_snapshot_;
118   std::unique_ptr<ProfileResetter> profile_resetter_;
119 
120   // A single timestamp to be used by all initialization functions to determine
121   // if enough time has passed between the last time the prompt was shown and
122   // "now" for a new prompt to be shown.
123   base::Time now_;
124   // The time since the last prompt was shown for any setting.
125   base::TimeDelta time_since_last_prompt_;
126 
127   // Bits to keep track of which settings types have been initialized.
128   uint32_t settings_types_initialized_ = 0;
129 
130   GURL homepage_url_;
131   int homepage_reset_domain_id_ = -1;
132   ResetState homepage_reset_state_ =
133       NO_RESET_REQUIRED_DUE_TO_DOMAIN_NOT_MATCHED;
134 
135   GURL default_search_url_;
136   int default_search_reset_domain_id_ = -1;
137   ResetState default_search_reset_state_ =
138       NO_RESET_REQUIRED_DUE_TO_DOMAIN_NOT_MATCHED;
139 
140   std::vector<GURL> startup_urls_;
141   std::vector<GURL> startup_urls_to_reset_;
142   // Reset domain IDs for URLs in |startup_urls_to_reset_|;
143   std::unordered_set<int> domain_ids_for_startup_urls_to_reset_;
144   ResetState startup_urls_reset_state_ =
145       NO_RESET_REQUIRED_DUE_TO_DOMAIN_NOT_MATCHED;
146 
147   DISALLOW_COPY_AND_ASSIGN(SettingsResetPromptModel);
148 };
149 
150 }  // namespace safe_browsing
151 
152 #endif  // CHROME_BROWSER_SAFE_BROWSING_SETTINGS_RESET_PROMPT_SETTINGS_RESET_PROMPT_MODEL_H_
153