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"
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
;
36 // URL of "More info" link shown in echo dialog in GetUserConsent function.
37 const char kMoreInfoLink
[] =
38 "chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html?"
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();
70 if (type
== kCouponType
) {
71 provider
->GetMachineStatistic(chromeos::system::kOffersCouponCodeKey
,
73 } else if (type
== kGroupType
) {
74 provider
->GetMachineStatistic(chromeos::system::kOffersGroupCodeKey
,
78 results_
= echo_api::GetRegistrationCode::Results::Create(result
);
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
);
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());
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";
130 echo_api::GetOfferInfo::Results::Result result
;
131 result
.additional_properties
.MergeDictionary(offer_info
);
132 results_
= echo_api::GetOfferInfo::Results::Create(result
);
136 EchoPrivateGetOobeTimestampFunction::EchoPrivateGetOobeTimestampFunction() {
139 EchoPrivateGetOobeTimestampFunction::~EchoPrivateGetOobeTimestampFunction() {
142 bool EchoPrivateGetOobeTimestampFunction::RunAsync() {
143 BrowserThread::PostTaskAndReplyWithResult(
144 BrowserThread::FILE, FROM_HERE
,
146 &EchoPrivateGetOobeTimestampFunction::GetOobeTimestampOnFileThread
,
149 &EchoPrivateGetOobeTimestampFunction::SendResponse
, this));
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
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",
171 results_
= echo_api::GetOobeTimestamp::Results::Create(timestamp
);
175 EchoPrivateGetUserConsentFunction::EchoPrivateGetUserConsentFunction()
176 : redeem_offers_allowed_(false) {
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
;
189 EchoPrivateGetUserConsentFunction::~EchoPrivateGetUserConsentFunction() {}
191 bool EchoPrivateGetUserConsentFunction::RunAsync() {
192 CheckRedeemOffersAllowed();
196 void EchoPrivateGetUserConsentFunction::OnAccept() {
200 void EchoPrivateGetUserConsentFunction::OnCancel() {
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(¶ms
);
213 void EchoPrivateGetUserConsentFunction::CheckRedeemOffersAllowed() {
214 chromeos::CrosSettingsProvider::TrustedStatus status
=
215 chromeos::CrosSettings::Get()->PrepareTrustedValues(base::Bind(
216 &EchoPrivateGetUserConsentFunction::CheckRedeemOffersAllowed
,
218 if (status
== chromeos::CrosSettingsProvider::TEMPORARILY_UNTRUSTED
)
222 chromeos::CrosSettings::Get()->GetBoolean(
223 chromeos::kAllowRedeemChromeOsRegistrationOffers
, &allow
);
225 OnRedeemOffersAllowedChecked(allow
);
228 void EchoPrivateGetUserConsentFunction::OnRedeemOffersAllowedChecked(
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.";
243 content::WebContents
* web_contents
= GetAssociatedWebContents();
245 error_
= "No web contents.";
250 // Add ref to ensure the function stays around until the dialog listener is
251 // called. The reference is release in |Finalize|.
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
));
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
);
276 // Release the reference added in |OnRedeemOffersAllowedChecked|, before
277 // showing the dialog.