OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_H_ | 5 #ifndef CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_H_ |
6 #define CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_H_ | 6 #define CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
13 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
14 #include "base/values.h" | 15 #include "base/task_runner.h" |
15 #include "chrome/browser/profile_resetter/automatic_profile_resetter_mementos.h" | 16 #include "chrome/browser/profile_resetter/automatic_profile_resetter_mementos.h" |
16 #include "components/browser_context_keyed_service/browser_context_keyed_service .h" | 17 #include "components/browser_context_keyed_service/browser_context_keyed_service .h" |
17 | 18 |
19 class AutomaticProfileResetterDelegate; | |
18 class Profile; | 20 class Profile; |
19 | 21 |
20 // Defines the interface for the delegate that will actually show the prompt | 22 namespace base { |
21 // and/or report statistics on behalf of the AutomaticProfileResetter. | 23 class DictionaryValue; |
22 // The primary reason for this separation is to facilitate unit testing. | 24 class ListValue; |
23 class AutomaticProfileResetterDelegate { | 25 } |
24 public: | |
25 virtual ~AutomaticProfileResetterDelegate() {} | |
26 | |
27 // Triggers showing the one-time profile settings reset prompt. | |
28 virtual void ShowPrompt() = 0; | |
29 | |
30 // Reports the given metrics through UMA. | |
31 virtual void ReportStatistics(uint32 satisfied_criteria_mask, | |
32 uint32 combined_status_mask) = 0; | |
33 }; | |
34 | 26 |
35 // This service becomes busy shortly after start-up, and is responsible for | 27 // This service becomes busy shortly after start-up, and is responsible for |
Peter Kasting
2013/10/17 19:06:14
Nit: "becomes busy" is confusing, do you mean "is
engedy
2013/10/18 11:08:12
Done.
| |
36 // evaluating if the criteria for showing the one-time profile reset prompt | 28 // evaluating if the criteria for showing the one-time profile reset prompt |
Peter Kasting
2013/10/17 19:06:14
Nit: if -> whether
engedy
2013/10/18 11:08:12
Done.
| |
37 // are satisfied, and will potentially trigger the prompt, but only a single | 29 // are satisfied, and will potentially trigger the prompt, but only a single |
Peter Kasting
2013/10/17 19:06:14
Nit: End sentence after "are satisfied, then begin
engedy
2013/10/18 11:08:12
I have rephrased to something very similar to this
| |
38 // time during the lifetime of a profile on disk (this is achieved by storing | 30 // time during the lifetime of a profile on disk (this is achieved by storing |
39 // "mementos"). All methods in this class shall be called on the UI thread. | 31 // "memento"-s, see "automatic_profile_resetter_mementos.h" for details). |
32 // All methods in this class shall be called on the UI thread. | |
40 class AutomaticProfileResetter : public BrowserContextKeyedService { | 33 class AutomaticProfileResetter : public BrowserContextKeyedService { |
41 public: | 34 public: |
35 // Number of bits, and maximum value (exclusive) for the mask whose bits | |
36 // indicate which of reset criteria were satisfied. | |
37 static const size_t kSatisfiedCriteriaMaskNumberOfBits; | |
38 static const uint32 kSatisfiedCriteriaMaskMaximumValue; | |
39 | |
40 // Number of bits, and maximum value (exclusive) for the mask whose bits | |
41 // indicate if any of reset criteria were satisfied, and which of the mementos | |
42 // were already present. | |
43 static const size_t kCombinedStatusMaskNumberOfBits; | |
44 static const uint32 kCombinedStatusMaskMaximumValue; | |
45 | |
42 explicit AutomaticProfileResetter(Profile* profile); | 46 explicit AutomaticProfileResetter(Profile* profile); |
43 virtual ~AutomaticProfileResetter(); | 47 virtual ~AutomaticProfileResetter(); |
44 | 48 |
45 // Initializes the service, and sets up the asynchronous evaluation flow. | 49 // Fires up the service by unleashing the asynchronous evaluation flow, unless |
46 // Called by AutomaticProfileResetterFactory. | 50 // the service has been already disabled in Initialize() or there is no |
47 void Initialize(); | 51 // |program_| to run (in which case the service also gets disabled). |
52 // Called by the AutomaticProfileResetterFactory. | |
53 void Activate(); | |
48 | 54 |
49 // Should be called after Initialize(). | 55 // Should be called before Activate(). |
50 void SetHashSeedForTesting(const base::StringPiece& hash_seed); | 56 void SetHashSeedForTesting(const base::StringPiece& hash_seed); |
51 | 57 |
52 // Should be called after Initialize(). | 58 // Should be called before Activate(). |
53 void SetProgramForTesting(const base::StringPiece& program); | 59 void SetProgramForTesting(const base::StringPiece& program); |
54 | 60 |
55 // Should be called after Initialize(). Takes ownership. | 61 // Should be called before Activate(). |
56 void SetDelegateForTesting(AutomaticProfileResetterDelegate* delegate); | 62 void SetDelegateForTesting( |
63 scoped_ptr<AutomaticProfileResetterDelegate> delegate); | |
64 | |
65 // Should be called before Activate(). Sets the task runner to be used to post | |
66 // task |PrepareEvaluationFlow| in a delayed manner. | |
67 void SetTaskRunnerForWaitingForTesting( | |
68 const scoped_refptr<base::TaskRunner>& task_runner); | |
57 | 69 |
58 private: | 70 private: |
59 struct EvaluationResults; | 71 struct EvaluationResults; |
60 | 72 |
61 enum State { | 73 enum State { |
62 STATE_UNINITIALIZED, | 74 STATE_UNINITIALIZED, |
75 STATE_INITIALIZED, | |
63 STATE_DISABLED, | 76 STATE_DISABLED, |
77 STATE_WAITING_ON_DEPENDENCIES, | |
64 STATE_READY, | 78 STATE_READY, |
65 STATE_WORKING, | 79 STATE_WORKING, |
66 STATE_DONE | 80 STATE_DONE |
67 }; | 81 }; |
68 | 82 |
69 // Returns whether or not a dry-run shall be performed. | 83 // Initializes the service if it is enabled in the field trial, otherwise, |
70 bool ShouldPerformDryRun() const; | 84 // skips the initialization steps and also permanently disables the service. |
85 void Initialize(); | |
71 | 86 |
72 // Returns whether or not a live-run shall be performed. | 87 // Prepares the asynchronous evaluation flow by requesting services that it |
73 bool ShouldPerformLiveRun() const; | 88 // depends on to make themselves ready. |
89 void PrepareEvaluationFlow(); | |
90 | |
91 // Called back by |resetter_delegate_| when the template URL service is ready. | |
92 void OnTemplateURLServiceIsLoaded(); | |
93 | |
94 // Called back by |resetter_delegate_| when the loaded modules have been | |
95 // enumerated. | |
96 void OnLoadedModulesAreEnumerated(); | |
97 | |
98 // Invoked by the above two methods. Kicks off the actual evaluation flow. | |
99 void OnDependencyIsReady(); | |
74 | 100 |
75 // Begins the asynchronous evaluation flow, which will assess whether the | 101 // Begins the asynchronous evaluation flow, which will assess whether the |
76 // criteria for showing the reset prompt are met, whether we have already | 102 // criteria for showing the reset prompt are met, whether we have already |
77 // shown the prompt, and, in the end, will potentially trigger the prompt. | 103 // shown the prompt, and, in the end, will potentially trigger the prompt. |
78 void BeginEvaluationFlow(); | 104 void BeginEvaluationFlow(); |
79 | 105 |
80 // Called back by |memento_in_file_| once it has finished reading the value of | |
81 // the file-based memento. Continues the evaluation flow with collecting state | |
82 // information and assembling it as the input for the evaluator program. | |
83 void ContinueWithEvaluationFlow(const std::string& memento_value_in_file); | |
84 | |
85 // Prepare the input of the evaluator program. This will contain all the state | 106 // Prepare the input of the evaluator program. This will contain all the state |
Peter Kasting
2013/10/17 19:06:14
Nit: Prepare -> Prepares
engedy
2013/10/18 11:08:12
Done.
| |
86 // information required to assess whether or not the conditions for showing | 107 // information required to assess whether or not the conditions for showing |
87 // the reset prompt are met. | 108 // the reset prompt are met. |
88 scoped_ptr<base::DictionaryValue> BuildEvaluatorProgramInput( | 109 scoped_ptr<base::DictionaryValue> BuildEvaluatorProgramInput( |
89 const std::string& memento_value_in_file); | 110 const std::string& memento_value_in_file); |
90 | 111 |
91 // Performs the bulk of the work. Invokes the interpreter to run the |program| | 112 // Called back by |memento_in_file_| once it has finished reading the value of |
92 // that will evaluate whether the conditions are met for showing the reset | 113 // the file-based memento. Continues the evaluation flow with collecting state |
93 // prompt. The program will make this decision based on the state information | 114 // information and assembling it as the input for the evaluator program. |
94 // contained in |input| in the form of key-value pairs. The program will only | 115 void ContinueWithEvaluationFlow(const std::string& memento_value_in_file); |
95 // see hashed keys and values that are produced using |hash_seed| as a key. | 116 |
117 // Performs the bulk of the work. Invokes the JTL interpreter to run the | |
118 // |program| that will evaluate whether the conditions are met for showing the | |
119 // reset prompt. The program will make this decision based on the state | |
120 // information contained in |input| in the form of key-value pairs. The | |
121 // program will only see hashed keys and values that are produced using | |
122 // |hash_seed| as a key. | |
96 static scoped_ptr<EvaluationResults> EvaluateConditionsOnWorkerPoolThread( | 123 static scoped_ptr<EvaluationResults> EvaluateConditionsOnWorkerPoolThread( |
97 const base::StringPiece& hash_seed, | 124 const base::StringPiece& hash_seed, |
98 const base::StringPiece& program, | 125 const base::StringPiece& program, |
99 scoped_ptr<base::DictionaryValue> program_input); | 126 scoped_ptr<base::DictionaryValue> program_input); |
100 | 127 |
101 // Called back when EvaluateConditionsOnWorkerPoolThread completes executing | 128 // Called back when EvaluateConditionsOnWorkerPoolThread completes executing |
102 // the program with |results|. Finishes the evaluation flow, and, based on the | 129 // the program with |results|. Finishes the evaluation flow, and, based on the |
103 // result, will potentially show the reset prompt. | 130 // result, will potentially show the reset prompt. |
104 void FinishEvaluationFlow(scoped_ptr<EvaluationResults> results); | 131 void FinishEvaluationFlow(scoped_ptr<EvaluationResults> results); |
105 | 132 |
106 // BrowserContextKeyedService overrides: | 133 // Reports the given metrics through UMA. Virtual, so it can be mocked out in |
134 // tests to verify that the correct value are being reported. | |
135 virtual void ReportStatistics(uint32 satisfied_criteria_mask, | |
136 uint32 combined_status_mask); | |
137 | |
138 // BrowserContextKeyedService: | |
107 virtual void Shutdown() OVERRIDE; | 139 virtual void Shutdown() OVERRIDE; |
108 | 140 |
109 Profile* profile_; | 141 Profile* profile_; |
110 | 142 |
111 State state_; | 143 State state_; |
144 bool enumeration_of_loaded_modules_ready_; | |
145 bool template_url_service_ready_; | |
112 | 146 |
113 base::StringPiece hash_seed_; | 147 base::StringPiece hash_seed_; |
114 base::StringPiece program_; | 148 base::StringPiece program_; |
115 | 149 |
116 PreferenceHostedPromptMemento memento_in_prefs_; | 150 PreferenceHostedPromptMemento memento_in_prefs_; |
117 LocalStateHostedPromptMemento memento_in_local_state_; | 151 LocalStateHostedPromptMemento memento_in_local_state_; |
118 FileHostedPromptMemento memento_in_file_; | 152 FileHostedPromptMemento memento_in_file_; |
119 | 153 |
120 scoped_ptr<AutomaticProfileResetterDelegate> delegate_; | 154 scoped_ptr<AutomaticProfileResetterDelegate> delegate_; |
155 scoped_refptr<base::TaskRunner> task_runner_for_waiting_; | |
121 | 156 |
122 base::WeakPtrFactory<AutomaticProfileResetter> weak_ptr_factory_; | 157 base::WeakPtrFactory<AutomaticProfileResetter> weak_ptr_factory_; |
123 | 158 |
124 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetter); | 159 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetter); |
125 }; | 160 }; |
126 | 161 |
127 #endif // CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_H_ | 162 #endif // CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_H_ |
OLD | NEW |