Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: chrome/browser/profile_resetter/automatic_profile_resetter_mementos.cc

Issue 24533002: Added the AutomaticProfileResetter service. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #include "chrome/browser/profile_resetter/automatic_profile_resetter_mementos.h"
6
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/values.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/prefs/scoped_user_pref_update.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/pref_names.h"
15 #include "content/public/browser/browser_thread.h"
16
17 using base::DictionaryValue;
18
19 namespace profile_resetter {
20
21 namespace {
22 const char kMementoFileName[] = "Reset Prompt Memento";
23 }
24
25 // AutomaticProfileResetter::PreferenceHostedPromptMemento -------------------
26
27 PreferenceHostedPromptMemento::PreferenceHostedPromptMemento(Profile* profile)
28 : profile_(profile) {}
29 PreferenceHostedPromptMemento::~PreferenceHostedPromptMemento() {}
30
31 std::string PreferenceHostedPromptMemento::ReadValue() const {
32 PrefService* prefs = profile_->GetPrefs();
33 DCHECK(prefs);
34 return prefs->GetString(prefs::kProfileResetPromptMemento);
35 }
36
37 void PreferenceHostedPromptMemento::StoreValue(const std::string& value) {
38 PrefService* prefs = profile_->GetPrefs();
39 DCHECK(prefs);
40 prefs->SetString(prefs::kProfileResetPromptMemento, value);
41 }
42
43 // AutomaticProfileResetter::LocalStateHostedPromptMemento -------------------
44
45 LocalStateHostedPromptMemento::LocalStateHostedPromptMemento(Profile* profile)
46 : profile_(profile) {}
47 LocalStateHostedPromptMemento::~LocalStateHostedPromptMemento() {}
48
49 std::string LocalStateHostedPromptMemento::ReadValue() const {
50 PrefService* local_state = g_browser_process->local_state();
51 DCHECK(local_state);
52
53 const DictionaryValue* prompt_shown_dict =
54 local_state->GetDictionary(prefs::kProfileResetPromptMemento);
55 std::string profile_key = GetProfileKey();
56 if (!prompt_shown_dict || profile_key.empty()) {
57 NOTREACHED();
58 return "";
59 }
60 std::string value;
61 return prompt_shown_dict->GetStringWithoutPathExpansion(profile_key, &value)
62 ? value
63 : "";
64 }
65
66 void LocalStateHostedPromptMemento::StoreValue(const std::string& value) {
67 PrefService* local_state = g_browser_process->local_state();
68 DCHECK(local_state);
69
70 DictionaryPrefUpdate prompt_shown_dict_update(
71 local_state, prefs::kProfileResetPromptMemento);
72 std::string profile_key = GetProfileKey();
73 if (profile_key.empty()) {
74 NOTREACHED();
75 return;
76 }
77 prompt_shown_dict_update.Get()->SetStringWithoutPathExpansion(profile_key,
78 value);
79 }
80
81 std::string LocalStateHostedPromptMemento::GetProfileKey() const {
82 return profile_->GetPath().BaseName().MaybeAsASCII();
83 }
84
85 // AutomaticProfileResetter::FileHostedPromptMemento -------------------------
86
87 FileHostedPromptMemento::FileHostedPromptMemento(Profile* profile)
88 : profile_(profile) {}
89 FileHostedPromptMemento::~FileHostedPromptMemento() {}
90
91 void FileHostedPromptMemento::ReadValue(
92 const ReadValueCallback& callback) const {
93 base::FilePath path = GetMementoFilePath();
94 content::BrowserThread::PostTaskAndReplyWithResult(
95 content::BrowserThread::FILE,
96 FROM_HERE,
97 base::Bind(&ReadValueOnFileThread, path),
98 callback);
99 }
100
101 void FileHostedPromptMemento::StoreValue(const std::string& value) {
102 base::FilePath path = GetMementoFilePath();
103 content::BrowserThread::PostTask(
104 content::BrowserThread::FILE,
105 FROM_HERE,
106 base::Bind(&StoreValueOnFileThread, path, value));
107 }
108
109 std::string FileHostedPromptMemento::ReadValueOnFileThread(
110 const base::FilePath& memento_file_path) {
111 std::string value;
112 base::ReadFileToString(memento_file_path, &value);
113 return value;
114 }
115
116 void FileHostedPromptMemento::StoreValueOnFileThread(
117 const base::FilePath& memento_file_path,
118 const std::string& value) {
119 int retval =
120 file_util::WriteFile(memento_file_path, value.c_str(), value.size());
121 DCHECK_EQ(retval, (int)value.size());
122 }
123
124 base::FilePath FileHostedPromptMemento::GetMementoFilePath() const {
125 return profile_->GetPath().Append(kMementoFileName);
126 }
127
128 } // namespace profile_resetter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698