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

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: Addressed comments, integrated Interpreter, added unit tests. 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 {
20 const char kMementoFileName[] = "Reset Prompt Memento";
21 }
22
23 // AutomaticProfileResetter::PreferenceHostedPromptMemento -------------------
24
25 PreferenceHostedPromptMemento::PreferenceHostedPromptMemento(Profile* profile)
26 : profile_(profile) {}
27 PreferenceHostedPromptMemento::~PreferenceHostedPromptMemento() {}
28
29 std::string PreferenceHostedPromptMemento::ReadValue() const {
30 PrefService* prefs = profile_->GetPrefs();
31 DCHECK(prefs);
32 return prefs->GetString(prefs::kProfileResetPromptMemento);
33 }
34
35 void PreferenceHostedPromptMemento::StoreValue(const std::string& value) {
36 PrefService* prefs = profile_->GetPrefs();
37 DCHECK(prefs);
38 prefs->SetString(prefs::kProfileResetPromptMemento, value);
39 }
40
41 // AutomaticProfileResetter::LocalStateHostedPromptMemento -------------------
42
43 LocalStateHostedPromptMemento::LocalStateHostedPromptMemento(Profile* profile)
44 : profile_(profile) {}
45 LocalStateHostedPromptMemento::~LocalStateHostedPromptMemento() {}
46
47 std::string LocalStateHostedPromptMemento::ReadValue() const {
48 PrefService* local_state = g_browser_process->local_state();
49 DCHECK(local_state);
50
51 const DictionaryValue* prompt_shown_dict =
52 local_state->GetDictionary(prefs::kProfileResetPromptMemento);
53 std::string profile_key = GetProfileKey();
54 if (!prompt_shown_dict || profile_key.empty()) {
55 NOTREACHED();
56 return "";
57 }
58 std::string value;
59 return prompt_shown_dict->GetStringWithoutPathExpansion(profile_key, &value)
60 ? value
61 : "";
62 }
63
64 void LocalStateHostedPromptMemento::StoreValue(const std::string& value) {
65 PrefService* local_state = g_browser_process->local_state();
66 DCHECK(local_state);
67
68 DictionaryPrefUpdate prompt_shown_dict_update(
69 local_state, prefs::kProfileResetPromptMemento);
70 std::string profile_key = GetProfileKey();
71 if (profile_key.empty()) {
72 NOTREACHED();
73 return;
74 }
75 prompt_shown_dict_update.Get()->SetStringWithoutPathExpansion(profile_key,
76 value);
77 }
78
79 std::string LocalStateHostedPromptMemento::GetProfileKey() const {
80 return profile_->GetPath().BaseName().MaybeAsASCII();
81 }
82
83 // AutomaticProfileResetter::FileHostedPromptMemento -------------------------
84
85 FileHostedPromptMemento::FileHostedPromptMemento(Profile* profile)
86 : profile_(profile) {}
87 FileHostedPromptMemento::~FileHostedPromptMemento() {}
88
89 void FileHostedPromptMemento::ReadValue(
90 const ReadValueCallback& callback) const {
91 base::FilePath path = GetMementoFilePath();
92 content::BrowserThread::PostTaskAndReplyWithResult(
93 content::BrowserThread::FILE,
94 FROM_HERE,
95 base::Bind(&ReadValueOnFileThread, path),
96 callback);
97 }
98
99 void FileHostedPromptMemento::StoreValue(const std::string& value) {
100 base::FilePath path = GetMementoFilePath();
101 content::BrowserThread::PostTask(
102 content::BrowserThread::FILE,
103 FROM_HERE,
104 base::Bind(&StoreValueOnFileThread, path, value));
105 }
106
107 std::string FileHostedPromptMemento::ReadValueOnFileThread(
108 const base::FilePath& memento_file_path) {
109 std::string value;
110 base::ReadFileToString(memento_file_path, &value);
111 return value;
112 }
113
114 void FileHostedPromptMemento::StoreValueOnFileThread(
115 const base::FilePath& memento_file_path,
116 const std::string& value) {
117 int retval =
118 file_util::WriteFile(memento_file_path, value.c_str(), value.size());
119 DCHECK_EQ(retval, (int)value.size());
120 }
121
122 base::FilePath FileHostedPromptMemento::GetMementoFilePath() const {
123 return profile_->GetPath().Append(kMementoFileName);
124 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698