| 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 | 26 |
| 27 // Triggers showing the one-time profile settings reset prompt. | 27 // This service is responsible for evaluating whether the criteria for showing |
| 28 virtual void ShowPrompt() = 0; | 28 // the one-time profile reset prompt are satisfied, and for potentially |
| 29 | 29 // triggering the prompt. To ensure that the prompt only appears at most once |
| 30 // Reports the given metrics through UMA. | 30 // for any given profile, a "memento" that the prompt has appeared is written to |
| 31 virtual void ReportStatistics(uint32 satisfied_criteria_mask, | 31 // the profile on disk; see automatic_profile_resetter_mementos.h for details. |
| 32 uint32 combined_status_mask) = 0; | 32 // The service is created automatically with the Profile and is activated right |
| 33 }; | 33 // away by its factory. To avoid delaying start-up, however, it will only start |
| 34 | 34 // working after a short delay. |
| 35 // This service becomes busy shortly after start-up, and is responsible for | 35 // All methods in this class shall be called on the UI thread, except when noted |
| 36 // evaluating if the criteria for showing the one-time profile reset prompt | 36 // otherwise. |
| 37 // are satisfied, and will potentially trigger the prompt, but only a single | |
| 38 // 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. | |
| 40 class AutomaticProfileResetter : public BrowserContextKeyedService { | 37 class AutomaticProfileResetter : public BrowserContextKeyedService { |
| 41 public: | 38 public: |
| 42 explicit AutomaticProfileResetter(Profile* profile); | 39 explicit AutomaticProfileResetter(Profile* profile); |
| 43 virtual ~AutomaticProfileResetter(); | 40 virtual ~AutomaticProfileResetter(); |
| 44 | 41 |
| 45 // Initializes the service, and sets up the asynchronous evaluation flow. | 42 // Fires up the service by unleashing the asynchronous evaluation flow, unless |
| 46 // Called by AutomaticProfileResetterFactory. | 43 // the service has been already disabled in Initialize() or there is no |
| 47 void Initialize(); | 44 // |program_| to run (in which case the service also gets disabled). |
| 45 // Called by the AutomaticProfileResetterFactory. |
| 46 void Activate(); |
| 48 | 47 |
| 49 // Should be called after Initialize(). | 48 // Should be called before Activate(). |
| 50 void SetHashSeedForTesting(const base::StringPiece& hash_seed); | 49 void SetHashSeedForTesting(const base::StringPiece& hash_seed); |
| 51 | 50 |
| 52 // Should be called after Initialize(). | 51 // Should be called before Activate(). |
| 53 void SetProgramForTesting(const base::StringPiece& program); | 52 void SetProgramForTesting(const base::StringPiece& program); |
| 54 | 53 |
| 55 // Should be called after Initialize(). Takes ownership. | 54 // Should be called before Activate(). |
| 56 void SetDelegateForTesting(AutomaticProfileResetterDelegate* delegate); | 55 void SetDelegateForTesting( |
| 56 scoped_ptr<AutomaticProfileResetterDelegate> delegate); |
| 57 |
| 58 // Should be called before Activate(). Sets the task runner to be used to post |
| 59 // task |PrepareEvaluationFlow| in a delayed manner. |
| 60 void SetTaskRunnerForWaitingForTesting( |
| 61 const scoped_refptr<base::TaskRunner>& task_runner); |
| 57 | 62 |
| 58 private: | 63 private: |
| 59 struct EvaluationResults; | 64 struct EvaluationResults; |
| 60 | 65 |
| 61 enum State { | 66 enum State { |
| 62 STATE_UNINITIALIZED, | 67 STATE_UNINITIALIZED, |
| 68 STATE_INITIALIZED, |
| 63 STATE_DISABLED, | 69 STATE_DISABLED, |
| 70 STATE_WAITING_ON_DEPENDENCIES, |
| 64 STATE_READY, | 71 STATE_READY, |
| 65 STATE_WORKING, | 72 STATE_WORKING, |
| 66 STATE_DONE | 73 STATE_DONE |
| 67 }; | 74 }; |
| 68 | 75 |
| 69 // Returns whether or not a dry-run shall be performed. | 76 // Initializes the service if it is enabled in the field trial, otherwise, |
| 70 bool ShouldPerformDryRun() const; | 77 // skips the initialization steps and also permanently disables the service. |
| 78 void Initialize(); |
| 71 | 79 |
| 72 // Returns whether or not a live-run shall be performed. | 80 // Prepares the asynchronous evaluation flow by requesting services that it |
| 73 bool ShouldPerformLiveRun() const; | 81 // depends on to make themselves ready. |
| 82 void PrepareEvaluationFlow(); |
| 83 |
| 84 // Called back by |resetter_delegate_| when the template URL service is ready. |
| 85 void OnTemplateURLServiceIsLoaded(); |
| 86 |
| 87 // Called back by |resetter_delegate_| when the loaded modules have been |
| 88 // enumerated. |
| 89 void OnLoadedModulesAreEnumerated(); |
| 90 |
| 91 // Invoked by the above two methods. Kicks off the actual evaluation flow. |
| 92 void OnDependencyIsReady(); |
| 74 | 93 |
| 75 // Begins the asynchronous evaluation flow, which will assess whether the | 94 // Begins the asynchronous evaluation flow, which will assess whether the |
| 76 // criteria for showing the reset prompt are met, whether we have already | 95 // 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. | 96 // shown the prompt, and, in the end, will potentially trigger the prompt. |
| 78 void BeginEvaluationFlow(); | 97 void BeginEvaluationFlow(); |
| 79 | 98 |
| 99 // Prepares the input of the evaluator program. This will contain all the |
| 100 // state information required to assess whether or not the conditions for |
| 101 // showing the reset prompt are met. |
| 102 scoped_ptr<base::DictionaryValue> BuildEvaluatorProgramInput( |
| 103 const std::string& memento_value_in_file); |
| 104 |
| 80 // Called back by |memento_in_file_| once it has finished reading the value of | 105 // 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 | 106 // the file-based memento. Continues the evaluation flow with collecting state |
| 82 // information and assembling it as the input for the evaluator program. | 107 // information and assembling it as the input for the evaluator program. |
| 83 void ContinueWithEvaluationFlow(const std::string& memento_value_in_file); | 108 void ContinueWithEvaluationFlow(const std::string& memento_value_in_file); |
| 84 | 109 |
| 85 // Prepare the input of the evaluator program. This will contain all the state | 110 // Performs the bulk of the work. Invokes the JTL interpreter to run the |
| 86 // information required to assess whether or not the conditions for showing | 111 // |program| that will evaluate whether the conditions are met for showing the |
| 87 // the reset prompt are met. | 112 // reset prompt. The program will make this decision based on the state |
| 88 scoped_ptr<base::DictionaryValue> BuildEvaluatorProgramInput( | 113 // information contained in |input| in the form of key-value pairs. The |
| 89 const std::string& memento_value_in_file); | 114 // program will only see hashed keys and values that are produced using |
| 90 | 115 // |hash_seed| as a key. |
| 91 // Performs the bulk of the work. Invokes the interpreter to run the |program| | |
| 92 // that will evaluate whether the conditions are met for showing the reset | |
| 93 // prompt. The program will make this decision based on the state information | |
| 94 // contained in |input| in the form of key-value pairs. The program will only | |
| 95 // see hashed keys and values that are produced using |hash_seed| as a key. | |
| 96 static scoped_ptr<EvaluationResults> EvaluateConditionsOnWorkerPoolThread( | 116 static scoped_ptr<EvaluationResults> EvaluateConditionsOnWorkerPoolThread( |
| 97 const base::StringPiece& hash_seed, | 117 const base::StringPiece& hash_seed, |
| 98 const base::StringPiece& program, | 118 const base::StringPiece& program, |
| 99 scoped_ptr<base::DictionaryValue> program_input); | 119 scoped_ptr<base::DictionaryValue> program_input); |
| 100 | 120 |
| 101 // Called back when EvaluateConditionsOnWorkerPoolThread completes executing | 121 // Called back when EvaluateConditionsOnWorkerPoolThread completes executing |
| 102 // the program with |results|. Finishes the evaluation flow, and, based on the | 122 // the program with |results|. Finishes the evaluation flow, and, based on the |
| 103 // result, will potentially show the reset prompt. | 123 // result, will potentially show the reset prompt. |
| 104 void FinishEvaluationFlow(scoped_ptr<EvaluationResults> results); | 124 void FinishEvaluationFlow(scoped_ptr<EvaluationResults> results); |
| 105 | 125 |
| 106 // BrowserContextKeyedService overrides: | 126 // Reports the given metrics through UMA. Virtual, so it can be mocked out in |
| 127 // tests to verify that the correct value are being reported. |
| 128 virtual void ReportStatistics(uint32 satisfied_criteria_mask, |
| 129 uint32 combined_status_mask); |
| 130 |
| 131 // BrowserContextKeyedService: |
| 107 virtual void Shutdown() OVERRIDE; | 132 virtual void Shutdown() OVERRIDE; |
| 108 | 133 |
| 109 Profile* profile_; | 134 Profile* profile_; |
| 110 | 135 |
| 111 State state_; | 136 State state_; |
| 137 bool enumeration_of_loaded_modules_ready_; |
| 138 bool template_url_service_ready_; |
| 112 | 139 |
| 113 base::StringPiece hash_seed_; | 140 base::StringPiece hash_seed_; |
| 114 base::StringPiece program_; | 141 base::StringPiece program_; |
| 115 | 142 |
| 116 PreferenceHostedPromptMemento memento_in_prefs_; | 143 PreferenceHostedPromptMemento memento_in_prefs_; |
| 117 LocalStateHostedPromptMemento memento_in_local_state_; | 144 LocalStateHostedPromptMemento memento_in_local_state_; |
| 118 FileHostedPromptMemento memento_in_file_; | 145 FileHostedPromptMemento memento_in_file_; |
| 119 | 146 |
| 120 scoped_ptr<AutomaticProfileResetterDelegate> delegate_; | 147 scoped_ptr<AutomaticProfileResetterDelegate> delegate_; |
| 148 scoped_refptr<base::TaskRunner> task_runner_for_waiting_; |
| 121 | 149 |
| 122 base::WeakPtrFactory<AutomaticProfileResetter> weak_ptr_factory_; | 150 base::WeakPtrFactory<AutomaticProfileResetter> weak_ptr_factory_; |
| 123 | 151 |
| 124 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetter); | 152 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetter); |
| 125 }; | 153 }; |
| 126 | 154 |
| 127 #endif // CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_H_ | 155 #endif // CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_H_ |
| OLD | NEW |