OLD | NEW |
(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/bind_helpers.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/values.h" |
| 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/common/chrome_constants.h" |
| 17 #include "chrome/common/pref_names.h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 |
| 20 using base::DictionaryValue; |
| 21 |
| 22 // AutomaticProfileResetter::PreferenceHostedPromptMemento ------------------- |
| 23 |
| 24 PreferenceHostedPromptMemento::PreferenceHostedPromptMemento(Profile* profile) |
| 25 : profile_(profile) {} |
| 26 PreferenceHostedPromptMemento::~PreferenceHostedPromptMemento() {} |
| 27 |
| 28 std::string PreferenceHostedPromptMemento::ReadValue() const { |
| 29 PrefService* prefs = profile_->GetPrefs(); |
| 30 DCHECK(prefs); |
| 31 return prefs->GetString(prefs::kProfileResetPromptMemento); |
| 32 } |
| 33 |
| 34 void PreferenceHostedPromptMemento::StoreValue(const std::string& value) { |
| 35 PrefService* prefs = profile_->GetPrefs(); |
| 36 DCHECK(prefs); |
| 37 prefs->SetString(prefs::kProfileResetPromptMemento, value); |
| 38 } |
| 39 |
| 40 // AutomaticProfileResetter::LocalStateHostedPromptMemento ------------------- |
| 41 |
| 42 LocalStateHostedPromptMemento::LocalStateHostedPromptMemento(Profile* profile) |
| 43 : profile_(profile) {} |
| 44 LocalStateHostedPromptMemento::~LocalStateHostedPromptMemento() {} |
| 45 |
| 46 std::string LocalStateHostedPromptMemento::ReadValue() const { |
| 47 PrefService* local_state = g_browser_process->local_state(); |
| 48 DCHECK(local_state); |
| 49 |
| 50 const DictionaryValue* prompt_shown_dict = |
| 51 local_state->GetDictionary(prefs::kProfileResetPromptMemento); |
| 52 std::string profile_key = GetProfileKey(); |
| 53 if (!prompt_shown_dict || profile_key.empty()) { |
| 54 NOTREACHED(); |
| 55 return ""; |
| 56 } |
| 57 std::string value; |
| 58 return prompt_shown_dict->GetStringWithoutPathExpansion(profile_key, &value) |
| 59 ? value |
| 60 : ""; |
| 61 } |
| 62 |
| 63 void LocalStateHostedPromptMemento::StoreValue(const std::string& value) { |
| 64 PrefService* local_state = g_browser_process->local_state(); |
| 65 DCHECK(local_state); |
| 66 |
| 67 DictionaryPrefUpdate prompt_shown_dict_update( |
| 68 local_state, prefs::kProfileResetPromptMemento); |
| 69 std::string profile_key = GetProfileKey(); |
| 70 if (profile_key.empty()) { |
| 71 NOTREACHED(); |
| 72 return; |
| 73 } |
| 74 prompt_shown_dict_update.Get()->SetStringWithoutPathExpansion(profile_key, |
| 75 value); |
| 76 } |
| 77 |
| 78 std::string LocalStateHostedPromptMemento::GetProfileKey() const { |
| 79 return profile_->GetPath().BaseName().MaybeAsASCII(); |
| 80 } |
| 81 |
| 82 // AutomaticProfileResetter::FileHostedPromptMemento ------------------------- |
| 83 |
| 84 FileHostedPromptMemento::FileHostedPromptMemento(Profile* profile) |
| 85 : profile_(profile) {} |
| 86 FileHostedPromptMemento::~FileHostedPromptMemento() {} |
| 87 |
| 88 void FileHostedPromptMemento::ReadValue( |
| 89 const ReadValueCallback& callback) const { |
| 90 base::FilePath path = GetMementoFilePath(); |
| 91 content::BrowserThread::PostTaskAndReplyWithResult( |
| 92 content::BrowserThread::FILE, |
| 93 FROM_HERE, |
| 94 base::Bind(&ReadValueOnFileThread, path), |
| 95 callback); |
| 96 } |
| 97 |
| 98 void FileHostedPromptMemento::StoreValue(const std::string& value) { |
| 99 base::FilePath path = GetMementoFilePath(); |
| 100 content::BrowserThread::PostTask( |
| 101 content::BrowserThread::FILE, |
| 102 FROM_HERE, |
| 103 base::Bind(&StoreValueOnFileThread, path, value)); |
| 104 } |
| 105 |
| 106 std::string FileHostedPromptMemento::ReadValueOnFileThread( |
| 107 const base::FilePath& memento_file_path) { |
| 108 std::string value; |
| 109 base::ReadFileToString(memento_file_path, &value); |
| 110 return value; |
| 111 } |
| 112 |
| 113 void FileHostedPromptMemento::StoreValueOnFileThread( |
| 114 const base::FilePath& memento_file_path, |
| 115 const std::string& value) { |
| 116 int retval = |
| 117 file_util::WriteFile(memento_file_path, value.c_str(), value.size()); |
| 118 DCHECK_EQ(retval, (int)value.size()); |
| 119 } |
| 120 |
| 121 base::FilePath FileHostedPromptMemento::GetMementoFilePath() const { |
| 122 return profile_->GetPath().Append(chrome::kResetPromptMementoFilename); |
| 123 } |
OLD | NEW |