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 #include "chrome/browser/profile_resetter/automatic_profile_resetter_mementos.h"
8 #include "base/bind_helpers.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/logging.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/prefs/scoped_user_pref_update.h"
14 #include "base/values.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/chrome_constants.h"
18 #include "chrome/common/pref_names.h"
19 #include "content/public/browser/browser_thread.h"
21 using base::DictionaryValue
;
24 // AutomaticProfileResetter::PreferenceHostedPromptMemento -------------------
26 PreferenceHostedPromptMemento::PreferenceHostedPromptMemento(Profile
* profile
)
27 : profile_(profile
) {}
29 PreferenceHostedPromptMemento::~PreferenceHostedPromptMemento() {}
31 std::string
PreferenceHostedPromptMemento::ReadValue() const {
32 PrefService
* prefs
= profile_
->GetPrefs();
34 return prefs
->GetString(prefs::kProfileResetPromptMementoInProfilePrefs
);
37 void PreferenceHostedPromptMemento::StoreValue(const std::string
& value
) {
38 PrefService
* prefs
= profile_
->GetPrefs();
40 prefs
->SetString(prefs::kProfileResetPromptMementoInProfilePrefs
, value
);
44 // AutomaticProfileResetter::LocalStateHostedPromptMemento -------------------
46 LocalStateHostedPromptMemento::LocalStateHostedPromptMemento(Profile
* profile
)
47 : profile_(profile
) {}
49 LocalStateHostedPromptMemento::~LocalStateHostedPromptMemento() {}
51 std::string
LocalStateHostedPromptMemento::ReadValue() const {
52 PrefService
* local_state
= g_browser_process
->local_state();
55 const base::DictionaryValue
* prompt_shown_dict
= local_state
->GetDictionary(
56 prefs::kProfileResetPromptMementosInLocalState
);
57 std::string profile_key
= GetProfileKey();
58 if (!prompt_shown_dict
|| profile_key
.empty()) {
63 return prompt_shown_dict
->GetStringWithoutPathExpansion(profile_key
, &value
) ?
64 value
: std::string();
67 void LocalStateHostedPromptMemento::StoreValue(const std::string
& value
) {
68 PrefService
* local_state
= g_browser_process
->local_state();
71 DictionaryPrefUpdate
prompt_shown_dict_update(
72 local_state
, prefs::kProfileResetPromptMementosInLocalState
);
73 std::string profile_key
= GetProfileKey();
74 if (profile_key
.empty()) {
78 prompt_shown_dict_update
.Get()->SetStringWithoutPathExpansion(profile_key
,
82 std::string
LocalStateHostedPromptMemento::GetProfileKey() const {
83 return profile_
->GetPath().BaseName().MaybeAsASCII();
87 // AutomaticProfileResetter::FileHostedPromptMemento -------------------------
89 FileHostedPromptMemento::FileHostedPromptMemento(Profile
* profile
)
90 : profile_(profile
) {}
92 FileHostedPromptMemento::~FileHostedPromptMemento() {}
94 void FileHostedPromptMemento::ReadValue(
95 const ReadValueCallback
& callback
) const {
96 base::FilePath path
= GetMementoFilePath();
97 content::BrowserThread::PostTaskAndReplyWithResult(
98 content::BrowserThread::FILE,
100 base::Bind(&ReadValueOnFileThread
, path
),
104 void FileHostedPromptMemento::StoreValue(const std::string
& value
) {
105 base::FilePath path
= GetMementoFilePath();
106 content::BrowserThread::PostTask(
107 content::BrowserThread::FILE,
109 base::Bind(&StoreValueOnFileThread
, path
, value
));
112 std::string
FileHostedPromptMemento::ReadValueOnFileThread(
113 const base::FilePath
& memento_file_path
) {
115 base::ReadFileToString(memento_file_path
, &value
);
119 void FileHostedPromptMemento::StoreValueOnFileThread(
120 const base::FilePath
& memento_file_path
,
121 const std::string
& value
) {
123 base::WriteFile(memento_file_path
, value
.c_str(), value
.size());
124 DCHECK_EQ(retval
, (int)value
.size());
127 base::FilePath
FileHostedPromptMemento::GetMementoFilePath() const {
128 return profile_
->GetPath().Append(chrome::kResetPromptMementoFilename
);