| 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 // Defines the interface for the delegate that will actually show the prompt |
| 19 // and/or report statistics on behalf of the AutomaticProfileResetter. |
| 20 // The primary reason for this separation is to facilitate unit testing. |
| 21 class AutomaticProfileResetterDelegate { |
| 22 public: |
| 23 virtual ~AutomaticProfileResetterDelegate() {} |
| 24 |
| 25 // Triggers showing the one-time profile settings reset prompt. |
| 26 virtual void ShowPrompt() = 0; |
| 27 |
| 28 // Reports the given metrics through UMA. |
| 29 virtual void ReportStatistics(uint32 satisfied_criteria_mask, |
| 30 uint32 combined_status_mask) = 0; |
| 31 }; |
| 32 |
| 33 // This service becomes busy shortly after start-up, and is responsible for |
| 34 // evaluating if the criteria for showing the one-time profile reset prompt |
| 35 // are satisfied, and potentially triggering the prompt, but only a single time |
| 36 // during the lifetime of a profile on disk (this is achieved by storing |
| 37 // "mementos"). All methods in this class shall be called on the UI thread. |
| 38 class AutomaticProfileResetter : public BrowserContextKeyedService { |
| 39 public: |
| 40 explicit AutomaticProfileResetter(Profile* profile); |
| 41 virtual ~AutomaticProfileResetter(); |
| 42 |
| 43 // Initializes the service, and sets up the asynchronous evaluation flow. |
| 44 // Called by AutomaticProfileResetterFactory. |
| 45 void Initialize(); |
| 46 |
| 47 // Should be called after Initialize(). |
| 48 void SetHashSeedForTesting(const base::StringPiece& hash_seed); |
| 49 |
| 50 // Should be called after Initialize(). |
| 51 void SetProgramForTesting(const base::StringPiece& program); |
| 52 |
| 53 // Should be called after Initialize(). Takes ownership. |
| 54 void SetDelegateForTesting(AutomaticProfileResetterDelegate* delegate); |
| 55 |
| 56 private: |
| 57 struct EvaluationResults; |
| 58 |
| 59 enum State { |
| 60 STATE_UNINITIALIZED, |
| 61 STATE_DISABLED, |
| 62 STATE_READY, |
| 63 STATE_WORKING, |
| 64 STATE_DONE |
| 65 }; |
| 66 |
| 67 // Returns whether or not a dry-run shall be performed. |
| 68 bool ShouldPerformDryRun() const; |
| 69 |
| 70 // Returns whether or not a live-run shall be performed. |
| 71 bool ShouldPerformLiveRun() const; |
| 72 |
| 73 // Begins the asynchronous evaluation flow, which will assess whether the |
| 74 // criteria for showing the reset prompt are met, whether we have already |
| 75 // shown the prompt, and, in the end, will potentially trigger the prompt. |
| 76 void BeginEvaluationFlow(); |
| 77 |
| 78 // Called back by |memento_in_file_| once it has finished reading the value of |
| 79 // the file-based memento. Continues the evaluation flow with collecting state |
| 80 // information and assembling it as the input for the evaluator program. |
| 81 void ContinueWithEvaluationFlow(const std::string& memento_value_in_file); |
| 82 |
| 83 // Prepare the input of the evaluator program that will contain all the state |
| 84 // information required to assess whether the conditions for showing the reset |
| 85 // prompt are met. |
| 86 scoped_ptr<base::DictionaryValue> BuildEvaluatorProgramInput( |
| 87 const std::string& memento_value_in_file); |
| 88 |
| 89 // Performs the bulk of the work. Invokes the interpreter to run the |program| |
| 90 // that will evaluate whether the conditions for showing the reset prompt are |
| 91 // satisfied. The program will make this decision based on state information |
| 92 // contained in |input| in the form of key-value pairs that which will be |
| 93 // hashed using |hash_key| as a key. |
| 94 static scoped_ptr<EvaluationResults> EvaluateConditionsOnWorkerPoolThread( |
| 95 const base::StringPiece& hash_key, |
| 96 const base::StringPiece& program, |
| 97 scoped_ptr<base::DictionaryValue> program_input); |
| 98 |
| 99 // Called back when EvaluateConditionsOnWorkerPoolThread completes executing |
| 100 // the program with |results|. Finishes the evaluation flow, and, based on the |
| 101 // result, will potentially show the reset prompt. |
| 102 void FinishEvaluationFlow(scoped_ptr<EvaluationResults> results); |
| 103 |
| 104 // BrowserContextKeyedService overrides: |
| 105 virtual void Shutdown() OVERRIDE; |
| 106 |
| 107 Profile* profile_; |
| 108 |
| 109 State state_; |
| 110 |
| 111 base::StringPiece hash_seed_; |
| 112 base::StringPiece program_; |
| 113 |
| 114 PreferenceHostedPromptMemento memento_in_prefs_; |
| 115 LocalStateHostedPromptMemento memento_in_local_state_; |
| 116 FileHostedPromptMemento memento_in_file_; |
| 117 |
| 118 scoped_ptr<AutomaticProfileResetterDelegate> delegate_; |
| 119 |
| 120 base::WeakPtrFactory<AutomaticProfileResetter> weak_ptr_factory_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetter); |
| 123 }; |
| 124 |
| 125 #endif // CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_H_ |
| OLD | NEW |