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.
5 #ifndef CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_H_
6 #define CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/strings/string_piece.h"
15 #include "base/task_runner.h"
16 #include "chrome/browser/profile_resetter/automatic_profile_resetter_mementos.h"
17 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
19 class AutomaticProfileResetterDelegate;
23 class DictionaryValue;
27 // This service is responsible for evaluating whether the criteria for showing
28 // the one-time profile reset prompt are satisfied, and for potentially
29 // triggering the prompt. To ensure that the prompt only appears at most once
30 // for any given profile, a "memento" that the prompt has appeared is written to
31 // the profile on disk; see automatic_profile_resetter_mementos.h for details.
32 // The service is created automatically with the Profile and is activated right
33 // away by its factory. To avoid delaying start-up, however, it will only start
34 // working after a short delay.
35 // All methods in this class shall be called on the UI thread, except when noted
37 class AutomaticProfileResetter : public BrowserContextKeyedService {
39 explicit AutomaticProfileResetter(Profile* profile);
40 virtual ~AutomaticProfileResetter();
42 // Fires up the service by unleashing the asynchronous evaluation flow, unless
43 // the service has been already disabled in Initialize() or there is no
44 // |program_| to run (in which case the service also gets disabled).
45 // Called by the AutomaticProfileResetterFactory.
48 // Should be called before Activate().
49 void SetHashSeedForTesting(const base::StringPiece& hash_seed);
51 // Should be called before Activate().
52 void SetProgramForTesting(const base::StringPiece& program);
54 // Should be called before Activate().
55 void SetDelegateForTesting(
56 scoped_ptr<AutomaticProfileResetterDelegate> delegate);
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);
64 struct EvaluationResults;
70 STATE_WAITING_ON_DEPENDENCIES,
76 // Initializes the service if it is enabled in the field trial, otherwise,
77 // skips the initialization steps and also permanently disables the service.
80 // Prepares the asynchronous evaluation flow by requesting services that it
81 // depends on to make themselves ready.
82 void PrepareEvaluationFlow();
84 // Called back by |resetter_delegate_| when the template URL service is ready.
85 void OnTemplateURLServiceIsLoaded();
87 // Called back by |resetter_delegate_| when the loaded modules have been
89 void OnLoadedModulesAreEnumerated();
91 // Invoked by the above two methods. Kicks off the actual evaluation flow.
92 void OnDependencyIsReady();
94 // Begins the asynchronous evaluation flow, which will assess whether the
95 // criteria for showing the reset prompt are met, whether we have already
96 // shown the prompt, and, in the end, will potentially trigger the prompt.
97 void BeginEvaluationFlow();
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);
105 // Called back by |memento_in_file_| once it has finished reading the value of
106 // the file-based memento. Continues the evaluation flow with collecting state
107 // information and assembling it as the input for the evaluator program.
108 void ContinueWithEvaluationFlow(const std::string& memento_value_in_file);
110 // Performs the bulk of the work. Invokes the JTL interpreter to run the
111 // |program| that will evaluate whether the conditions are met for showing the
112 // reset prompt. The program will make this decision based on the state
113 // information contained in |input| in the form of key-value pairs. The
114 // program will only see hashed keys and values that are produced using
115 // |hash_seed| as a key.
116 static scoped_ptr<EvaluationResults> EvaluateConditionsOnWorkerPoolThread(
117 const base::StringPiece& hash_seed,
118 const base::StringPiece& program,
119 scoped_ptr<base::DictionaryValue> program_input);
121 // Called back when EvaluateConditionsOnWorkerPoolThread completes executing
122 // the program with |results|. Finishes the evaluation flow, and, based on the
123 // result, will potentially show the reset prompt.
124 void FinishEvaluationFlow(scoped_ptr<EvaluationResults> results);
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);
131 // BrowserContextKeyedService:
132 virtual void Shutdown() OVERRIDE;
137 bool enumeration_of_loaded_modules_ready_;
138 bool template_url_service_ready_;
140 base::StringPiece hash_seed_;
141 base::StringPiece program_;
143 PreferenceHostedPromptMemento memento_in_prefs_;
144 LocalStateHostedPromptMemento memento_in_local_state_;
145 FileHostedPromptMemento memento_in_file_;
147 scoped_ptr<AutomaticProfileResetterDelegate> delegate_;
148 scoped_refptr<base::TaskRunner> task_runner_for_waiting_;
150 base::WeakPtrFactory<AutomaticProfileResetter> weak_ptr_factory_;
152 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetter);
155 #endif // CHROME_BROWSER_PROFILE_RESETTER_AUTOMATIC_PROFILE_RESETTER_H_