1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/extensions/echo_private_api.h"
#include <string>
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/location.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/chromeos/ui/echo_dialog_view.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/extensions/api/echo_private.h"
#include "chrome/common/pref_names.h"
#include "chromeos/system/statistics_provider.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/common/extension.h"
namespace echo_api = extensions::api::echo_private;
using content::BrowserThread;
namespace {
// URL of "More info" link shown in echo dialog in GetUserConsent function.
const char kMoreInfoLink[] =
"chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html?"
"answer=2677280";
} // namespace
namespace chromeos {
namespace echo_offer {
void RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterDictionaryPref(prefs::kEchoCheckedOffers);
}
} // namespace echo_offer
} // namespace chromeos
EchoPrivateGetRegistrationCodeFunction::
EchoPrivateGetRegistrationCodeFunction() {}
EchoPrivateGetRegistrationCodeFunction::
~EchoPrivateGetRegistrationCodeFunction() {}
void EchoPrivateGetRegistrationCodeFunction::GetRegistrationCode(
const std::string& type) {
// Possible ECHO code type and corresponding key name in StatisticsProvider.
const std::string kCouponType = "COUPON_CODE";
const std::string kGroupType = "GROUP_CODE";
chromeos::system::StatisticsProvider* provider =
chromeos::system::StatisticsProvider::GetInstance();
std::string result;
if (type == kCouponType) {
provider->GetMachineStatistic(chromeos::system::kOffersCouponCodeKey,
&result);
} else if (type == kGroupType) {
provider->GetMachineStatistic(chromeos::system::kOffersGroupCodeKey,
&result);
}
results_ = echo_api::GetRegistrationCode::Results::Create(result);
SendResponse(true);
}
bool EchoPrivateGetRegistrationCodeFunction::RunSync() {
scoped_ptr<echo_api::GetRegistrationCode::Params> params =
echo_api::GetRegistrationCode::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params);
GetRegistrationCode(params->type);
return true;
}
EchoPrivateSetOfferInfoFunction::EchoPrivateSetOfferInfoFunction() {}
EchoPrivateSetOfferInfoFunction::~EchoPrivateSetOfferInfoFunction() {}
bool EchoPrivateSetOfferInfoFunction::RunSync() {
scoped_ptr<echo_api::SetOfferInfo::Params> params =
echo_api::SetOfferInfo::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params);
const std::string& service_id = params->id;
base::DictionaryValue* dict = params->offer_info.
additional_properties.DeepCopyWithoutEmptyChildren();
PrefService* local_state = g_browser_process->local_state();
DictionaryPrefUpdate offer_update(local_state, prefs::kEchoCheckedOffers);
offer_update->SetWithoutPathExpansion("echo." + service_id, dict);
return true;
}
EchoPrivateGetOfferInfoFunction::EchoPrivateGetOfferInfoFunction() {}
EchoPrivateGetOfferInfoFunction::~EchoPrivateGetOfferInfoFunction() {}
bool EchoPrivateGetOfferInfoFunction::RunSync() {
scoped_ptr<echo_api::GetOfferInfo::Params> params =
echo_api::GetOfferInfo::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params);
const std::string& service_id = params->id;
PrefService* local_state = g_browser_process->local_state();
const base::DictionaryValue* offer_infos = local_state->
GetDictionary(prefs::kEchoCheckedOffers);
const base::DictionaryValue* offer_info = NULL;
if (!offer_infos->GetDictionaryWithoutPathExpansion(
"echo." + service_id, &offer_info)) {
error_ = "Not found";
return false;
}
echo_api::GetOfferInfo::Results::Result result;
result.additional_properties.MergeDictionary(offer_info);
results_ = echo_api::GetOfferInfo::Results::Create(result);
return true;
}
EchoPrivateGetOobeTimestampFunction::EchoPrivateGetOobeTimestampFunction() {
}
EchoPrivateGetOobeTimestampFunction::~EchoPrivateGetOobeTimestampFunction() {
}
bool EchoPrivateGetOobeTimestampFunction::RunAsync() {
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::FILE, FROM_HERE,
base::Bind(
&EchoPrivateGetOobeTimestampFunction::GetOobeTimestampOnFileThread,
this),
base::Bind(
&EchoPrivateGetOobeTimestampFunction::SendResponse, this));
return true;
}
// Get the OOBE timestamp from file /home/chronos/.oobe_completed.
// The timestamp is used to determine when the user first activates the device.
// If we can get the timestamp info, return it as yyyy-mm-dd, otherwise, return
// an empty string.
bool EchoPrivateGetOobeTimestampFunction::GetOobeTimestampOnFileThread() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
const char kOobeTimestampFile[] = "/home/chronos/.oobe_completed";
std::string timestamp = "";
base::File::Info fileInfo;
if (base::GetFileInfo(base::FilePath(kOobeTimestampFile), &fileInfo)) {
base::Time::Exploded ctime;
fileInfo.creation_time.UTCExplode(&ctime);
timestamp += base::StringPrintf("%u-%u-%u",
ctime.year,
ctime.month,
ctime.day_of_month);
}
results_ = echo_api::GetOobeTimestamp::Results::Create(timestamp);
return true;
}
EchoPrivateGetUserConsentFunction::EchoPrivateGetUserConsentFunction()
: redeem_offers_allowed_(false) {
}
// static
scoped_refptr<EchoPrivateGetUserConsentFunction>
EchoPrivateGetUserConsentFunction::CreateForTest(
const DialogShownTestCallback& dialog_shown_callback) {
scoped_refptr<EchoPrivateGetUserConsentFunction> function(
new EchoPrivateGetUserConsentFunction());
function->dialog_shown_callback_ = dialog_shown_callback;
return function;
}
EchoPrivateGetUserConsentFunction::~EchoPrivateGetUserConsentFunction() {}
bool EchoPrivateGetUserConsentFunction::RunAsync() {
CheckRedeemOffersAllowed();
return true;
}
void EchoPrivateGetUserConsentFunction::OnAccept() {
Finalize(true);
}
void EchoPrivateGetUserConsentFunction::OnCancel() {
Finalize(false);
}
void EchoPrivateGetUserConsentFunction::OnMoreInfoLinkClicked() {
chrome::NavigateParams params(
GetProfile(), GURL(kMoreInfoLink), ui::PAGE_TRANSITION_LINK);
// Open the link in a new window. The echo dialog is modal, so the current
// window is useless until the dialog is closed.
params.disposition = NEW_WINDOW;
chrome::Navigate(¶ms);
}
void EchoPrivateGetUserConsentFunction::CheckRedeemOffersAllowed() {
chromeos::CrosSettingsProvider::TrustedStatus status =
chromeos::CrosSettings::Get()->PrepareTrustedValues(base::Bind(
&EchoPrivateGetUserConsentFunction::CheckRedeemOffersAllowed,
this));
if (status == chromeos::CrosSettingsProvider::TEMPORARILY_UNTRUSTED)
return;
bool allow = true;
chromeos::CrosSettings::Get()->GetBoolean(
chromeos::kAllowRedeemChromeOsRegistrationOffers, &allow);
OnRedeemOffersAllowedChecked(allow);
}
void EchoPrivateGetUserConsentFunction::OnRedeemOffersAllowedChecked(
bool is_allowed) {
redeem_offers_allowed_ = is_allowed;
scoped_ptr<echo_api::GetUserConsent::Params> params =
echo_api::GetUserConsent::Params::Create(*args_);
// Verify that the passed origin URL is valid.
GURL service_origin = GURL(params->consent_requester.origin);
if (!service_origin.is_valid()) {
error_ = "Invalid origin.";
SendResponse(false);
return;
}
// Add ref to ensure the function stays around until the dialog listener is
// called. The reference is release in |Finalize|.
AddRef();
// Create and show the dialog.
chromeos::EchoDialogView* dialog = new chromeos::EchoDialogView(this);
if (redeem_offers_allowed_) {
dialog->InitForEnabledEcho(
base::UTF8ToUTF16(params->consent_requester.service_name),
base::UTF8ToUTF16(params->consent_requester.origin));
} else {
dialog->InitForDisabledEcho();
}
dialog->Show(GetCurrentBrowser()->window()->GetNativeWindow());
// If there is a dialog_shown_callback_, invoke it with the created dialog.
if (!dialog_shown_callback_.is_null())
dialog_shown_callback_.Run(dialog);
}
void EchoPrivateGetUserConsentFunction::Finalize(bool consent) {
// Consent should not be true if offers redeeming is disabled.
CHECK(redeem_offers_allowed_ || !consent);
results_ = echo_api::GetUserConsent::Results::Create(consent);
SendResponse(true);
// Release the reference added in |OnRedeemOffersAllowedChecked|, before
// showing the dialog.
Release();
}
|