BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / echo_private_api.cc
blob7b74050fe8ba40511406ebc98a26f79a5329adb5
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/chromeos/extensions/echo_private_api.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/files/file_util.h"
11 #include "base/location.h"
12 #include "base/prefs/pref_registry_simple.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/prefs/scoped_user_pref_update.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/time/time.h"
18 #include "base/values.h"
19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/chromeos/settings/cros_settings.h"
21 #include "chrome/browser/chromeos/ui/echo_dialog_view.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/common/extensions/api/echo_private.h"
24 #include "chrome/common/pref_names.h"
25 #include "chromeos/system/statistics_provider.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/web_contents.h"
28 #include "extensions/common/extension.h"
30 namespace echo_api = extensions::api::echo_private;
32 using content::BrowserThread;
34 namespace {
36 // URL of "More info" link shown in echo dialog in GetUserConsent function.
37 const char kMoreInfoLink[] =
38 "chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html?"
39 "answer=2677280";
41 } // namespace
43 namespace chromeos {
45 namespace echo_offer {
47 void RegisterPrefs(PrefRegistrySimple* registry) {
48 registry->RegisterDictionaryPref(prefs::kEchoCheckedOffers);
51 } // namespace echo_offer
53 } // namespace chromeos
55 EchoPrivateGetRegistrationCodeFunction::
56 EchoPrivateGetRegistrationCodeFunction() {}
58 EchoPrivateGetRegistrationCodeFunction::
59 ~EchoPrivateGetRegistrationCodeFunction() {}
61 void EchoPrivateGetRegistrationCodeFunction::GetRegistrationCode(
62 const std::string& type) {
63 // Possible ECHO code type and corresponding key name in StatisticsProvider.
64 const std::string kCouponType = "COUPON_CODE";
65 const std::string kGroupType = "GROUP_CODE";
67 chromeos::system::StatisticsProvider* provider =
68 chromeos::system::StatisticsProvider::GetInstance();
69 std::string result;
70 if (type == kCouponType) {
71 provider->GetMachineStatistic(chromeos::system::kOffersCouponCodeKey,
72 &result);
73 } else if (type == kGroupType) {
74 provider->GetMachineStatistic(chromeos::system::kOffersGroupCodeKey,
75 &result);
78 results_ = echo_api::GetRegistrationCode::Results::Create(result);
79 SendResponse(true);
82 bool EchoPrivateGetRegistrationCodeFunction::RunSync() {
83 scoped_ptr<echo_api::GetRegistrationCode::Params> params =
84 echo_api::GetRegistrationCode::Params::Create(*args_);
85 EXTENSION_FUNCTION_VALIDATE(params);
86 GetRegistrationCode(params->type);
87 return true;
90 EchoPrivateSetOfferInfoFunction::EchoPrivateSetOfferInfoFunction() {}
92 EchoPrivateSetOfferInfoFunction::~EchoPrivateSetOfferInfoFunction() {}
94 bool EchoPrivateSetOfferInfoFunction::RunSync() {
95 scoped_ptr<echo_api::SetOfferInfo::Params> params =
96 echo_api::SetOfferInfo::Params::Create(*args_);
97 EXTENSION_FUNCTION_VALIDATE(params);
99 const std::string& service_id = params->id;
100 scoped_ptr<base::DictionaryValue> dict =
101 params->offer_info.additional_properties.DeepCopyWithoutEmptyChildren();
103 PrefService* local_state = g_browser_process->local_state();
104 DictionaryPrefUpdate offer_update(local_state, prefs::kEchoCheckedOffers);
105 offer_update->SetWithoutPathExpansion("echo." + service_id, dict.Pass());
106 return true;
109 EchoPrivateGetOfferInfoFunction::EchoPrivateGetOfferInfoFunction() {}
111 EchoPrivateGetOfferInfoFunction::~EchoPrivateGetOfferInfoFunction() {}
113 bool EchoPrivateGetOfferInfoFunction::RunSync() {
114 scoped_ptr<echo_api::GetOfferInfo::Params> params =
115 echo_api::GetOfferInfo::Params::Create(*args_);
116 EXTENSION_FUNCTION_VALIDATE(params);
118 const std::string& service_id = params->id;
119 PrefService* local_state = g_browser_process->local_state();
120 const base::DictionaryValue* offer_infos = local_state->
121 GetDictionary(prefs::kEchoCheckedOffers);
123 const base::DictionaryValue* offer_info = NULL;
124 if (!offer_infos->GetDictionaryWithoutPathExpansion(
125 "echo." + service_id, &offer_info)) {
126 error_ = "Not found";
127 return false;
130 echo_api::GetOfferInfo::Results::Result result;
131 result.additional_properties.MergeDictionary(offer_info);
132 results_ = echo_api::GetOfferInfo::Results::Create(result);
133 return true;
136 EchoPrivateGetOobeTimestampFunction::EchoPrivateGetOobeTimestampFunction() {
139 EchoPrivateGetOobeTimestampFunction::~EchoPrivateGetOobeTimestampFunction() {
142 bool EchoPrivateGetOobeTimestampFunction::RunAsync() {
143 BrowserThread::PostTaskAndReplyWithResult(
144 BrowserThread::FILE, FROM_HERE,
145 base::Bind(
146 &EchoPrivateGetOobeTimestampFunction::GetOobeTimestampOnFileThread,
147 this),
148 base::Bind(
149 &EchoPrivateGetOobeTimestampFunction::SendResponse, this));
150 return true;
153 // Get the OOBE timestamp from file /home/chronos/.oobe_completed.
154 // The timestamp is used to determine when the user first activates the device.
155 // If we can get the timestamp info, return it as yyyy-mm-dd, otherwise, return
156 // an empty string.
157 bool EchoPrivateGetOobeTimestampFunction::GetOobeTimestampOnFileThread() {
158 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
160 const char kOobeTimestampFile[] = "/home/chronos/.oobe_completed";
161 std::string timestamp = "";
162 base::File::Info fileInfo;
163 if (base::GetFileInfo(base::FilePath(kOobeTimestampFile), &fileInfo)) {
164 base::Time::Exploded ctime;
165 fileInfo.creation_time.UTCExplode(&ctime);
166 timestamp += base::StringPrintf("%u-%u-%u",
167 ctime.year,
168 ctime.month,
169 ctime.day_of_month);
171 results_ = echo_api::GetOobeTimestamp::Results::Create(timestamp);
172 return true;
175 EchoPrivateGetUserConsentFunction::EchoPrivateGetUserConsentFunction()
176 : redeem_offers_allowed_(false) {
179 // static
180 scoped_refptr<EchoPrivateGetUserConsentFunction>
181 EchoPrivateGetUserConsentFunction::CreateForTest(
182 const DialogShownTestCallback& dialog_shown_callback) {
183 scoped_refptr<EchoPrivateGetUserConsentFunction> function(
184 new EchoPrivateGetUserConsentFunction());
185 function->dialog_shown_callback_ = dialog_shown_callback;
186 return function;
189 EchoPrivateGetUserConsentFunction::~EchoPrivateGetUserConsentFunction() {}
191 bool EchoPrivateGetUserConsentFunction::RunAsync() {
192 CheckRedeemOffersAllowed();
193 return true;
196 void EchoPrivateGetUserConsentFunction::OnAccept() {
197 Finalize(true);
200 void EchoPrivateGetUserConsentFunction::OnCancel() {
201 Finalize(false);
204 void EchoPrivateGetUserConsentFunction::OnMoreInfoLinkClicked() {
205 chrome::NavigateParams params(
206 GetProfile(), GURL(kMoreInfoLink), ui::PAGE_TRANSITION_LINK);
207 // Open the link in a new window. The echo dialog is modal, so the current
208 // window is useless until the dialog is closed.
209 params.disposition = NEW_WINDOW;
210 chrome::Navigate(&params);
213 void EchoPrivateGetUserConsentFunction::CheckRedeemOffersAllowed() {
214 chromeos::CrosSettingsProvider::TrustedStatus status =
215 chromeos::CrosSettings::Get()->PrepareTrustedValues(base::Bind(
216 &EchoPrivateGetUserConsentFunction::CheckRedeemOffersAllowed,
217 this));
218 if (status == chromeos::CrosSettingsProvider::TEMPORARILY_UNTRUSTED)
219 return;
221 bool allow = true;
222 chromeos::CrosSettings::Get()->GetBoolean(
223 chromeos::kAllowRedeemChromeOsRegistrationOffers, &allow);
225 OnRedeemOffersAllowedChecked(allow);
228 void EchoPrivateGetUserConsentFunction::OnRedeemOffersAllowedChecked(
229 bool is_allowed) {
230 redeem_offers_allowed_ = is_allowed;
232 scoped_ptr<echo_api::GetUserConsent::Params> params =
233 echo_api::GetUserConsent::Params::Create(*args_);
235 // Verify that the passed origin URL is valid.
236 GURL service_origin = GURL(params->consent_requester.origin);
237 if (!service_origin.is_valid()) {
238 error_ = "Invalid origin.";
239 SendResponse(false);
240 return;
243 content::WebContents* web_contents = GetAssociatedWebContents();
244 if (!web_contents) {
245 error_ = "No web contents.";
246 SendResponse(false);
247 return;
250 // Add ref to ensure the function stays around until the dialog listener is
251 // called. The reference is release in |Finalize|.
252 AddRef();
254 // Create and show the dialog.
255 chromeos::EchoDialogView* dialog = new chromeos::EchoDialogView(this);
256 if (redeem_offers_allowed_) {
257 dialog->InitForEnabledEcho(
258 base::UTF8ToUTF16(params->consent_requester.service_name),
259 base::UTF8ToUTF16(params->consent_requester.origin));
260 } else {
261 dialog->InitForDisabledEcho();
263 dialog->Show(web_contents->GetTopLevelNativeWindow());
265 // If there is a dialog_shown_callback_, invoke it with the created dialog.
266 if (!dialog_shown_callback_.is_null())
267 dialog_shown_callback_.Run(dialog);
270 void EchoPrivateGetUserConsentFunction::Finalize(bool consent) {
271 // Consent should not be true if offers redeeming is disabled.
272 CHECK(redeem_offers_allowed_ || !consent);
273 results_ = echo_api::GetUserConsent::Results::Create(consent);
274 SendResponse(true);
276 // Release the reference added in |OnRedeemOffersAllowedChecked|, before
277 // showing the dialog.
278 Release();