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