OLD | NEW |
---|---|
(Empty) | |
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_H_ | |
6 #define CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/memory/weak_ptr.h" | |
11 #include "base/strings/string_piece.h" | |
12 #include "base/values.h" | |
13 #include "chrome/browser/profile_resetter/automatic_profile_resetter_mementos.h" | |
14 #include "components/browser_context_keyed_service/browser_context_keyed_service .h" | |
15 | |
16 class Profile; | |
17 | |
18 namespace profile_resetter { | |
battre
2013/09/25 09:43:26
Using namespaces is discouraged according to the m
engedy
2013/09/27 23:27:34
Done.
| |
19 | |
20 // Defines the interface for the delegate that will actually show the prompt | |
21 // and/or report statistics on behalf of the AutomaticProfileResetter. | |
22 // The primary reason for this separation is to facilitate unit testing. | |
23 class AutomaticProfileResetterDelegate { | |
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 | |
35 // This service becomes busy shortly after start-up, and is responsible for | |
36 // evaluating if the criteria for showing the one-time profile reset prompt | |
37 // are satisfied, and potentially triggering the prompt, but only a single time | |
38 // 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. | |
40 class AutomaticProfileResetter : public BrowserContextKeyedService { | |
41 public: | |
42 explicit AutomaticProfileResetter(Profile* profile); | |
43 virtual ~AutomaticProfileResetter(); | |
44 | |
45 // Initializes the service, and sets up the asynchronous evaluation flow. | |
46 // Called by AutomaticProfileResetterFactory. | |
47 void Initialize(); | |
48 | |
49 // Should be called after Initialize(). | |
50 void SetHashSeedForTesting(const std::string& hash_seed); | |
51 | |
52 // Should be called after Initialize(). | |
53 void SetProgramForTesting(const std::string& program); | |
54 | |
55 // Should be called after Initialize(). Takes ownership. | |
56 void SetDelegateForTesting(AutomaticProfileResetterDelegate* delegate); | |
57 | |
58 private: | |
59 struct EvaluationResults; | |
60 | |
61 enum State { | |
62 STATE_UNINITIALIZED, | |
63 STATE_DISABLED, | |
64 STATE_READY, | |
65 STATE_WORKING, | |
66 STATE_DONE | |
67 }; | |
68 | |
69 // Returns whether or not a dry-run shall be performed. | |
70 bool IsDryRun() const; | |
71 | |
72 // Returns whether or not a live-run shall be performed. | |
73 bool IsLiveRun() const; | |
74 | |
75 // Begins the asynchronous evaluation flow, which will assess whether the | |
76 // 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. | |
78 void BeginEvaluationFlow(); | |
79 | |
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 that will contain all the state | |
86 // information required to assess whether the conditions for showing the reset | |
87 // prompt are met. | |
88 scoped_ptr<base::DictionaryValue> BuildEvaluatorProgramInput( | |
89 const std::string& memento_value_in_file); | |
90 | |
91 // Performs the bulk of the work. Invokes the interpreter to run the |program| | |
92 // that will evaluate whether the conditions for showing the reset prompt are | |
93 // satisfied. The program will make this decision based on state information | |
94 // contained in |input| in the form of key-value pairs that which will be | |
95 // hashed using |hash_key| as a key. | |
96 static scoped_ptr<EvaluationResults> EvaluateConditionsOnWorkerPoolThread( | |
97 const std::string& hash_key, | |
98 const std::string& program, | |
99 scoped_ptr<base::DictionaryValue> program_input); | |
100 | |
101 // Called back when EvaluateConditionsOnWorkerPoolThread completes executing | |
102 // the program with |results|. Finishes the evaluation flow, and, based on the | |
103 // result, will potentially show the reset prompt. | |
104 void FinishEvaluationFlow(scoped_ptr<EvaluationResults> results); | |
105 | |
106 // BrowserContextKeyedService overrides: | |
107 virtual void Shutdown() OVERRIDE; | |
108 | |
109 Profile* profile_; | |
110 | |
111 State state_; | |
112 | |
113 base::StringPiece hash_seed_; | |
114 base::StringPiece program_; | |
115 | |
116 PreferenceHostedPromptMemento memento_in_prefs_; | |
117 LocalStateHostedPromptMemento memento_in_local_state_; | |
118 FileHostedPromptMemento memento_in_file_; | |
119 | |
120 scoped_ptr<AutomaticProfileResetterDelegate> delegate_; | |
121 | |
122 base::WeakPtrFactory<AutomaticProfileResetter> weak_ptr_factory_; | |
123 | |
124 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetter); | |
125 }; | |
126 | |
127 } // namespace profile_resetter | |
128 | |
129 #endif // CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_H_ | |
OLD | NEW |