easy_unlock_private_api.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1// Copyright 2014 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/extensions/api/easy_unlock_private/easy_unlock_private_api.h"
6
7#include "base/bind.h"
8#include "base/lazy_instance.h"
9#include "base/strings/utf_string_conversions.h"
10#include "base/values.h"
11#include "chrome/browser/extensions/api/easy_unlock_private/easy_unlock_private_bluetooth_util.h"
12#include "chrome/browser/extensions/api/easy_unlock_private/easy_unlock_private_crypto_delegate.h"
13#include "chrome/common/extensions/api/easy_unlock_private.h"
14#include "extensions/browser/browser_context_keyed_api_factory.h"
15#include "grit/generated_resources.h"
16#include "ui/base/l10n/l10n_util.h"
17
18#if defined(OS_CHROMEOS)
19#include "chrome/browser/chromeos/chromeos_utils.h"
20#endif
21
22namespace extensions {
23namespace api {
24
25namespace {
26
27static base::LazyInstance<BrowserContextKeyedAPIFactory<EasyUnlockPrivateAPI> >
28    g_factory = LAZY_INSTANCE_INITIALIZER;
29
30// Utility method for getting the API's crypto delegate.
31EasyUnlockPrivateCryptoDelegate* GetCryptoDelegate(
32    content::BrowserContext* context) {
33  return BrowserContextKeyedAPIFactory<EasyUnlockPrivateAPI>::Get(context)
34             ->crypto_delegate();
35}
36
37}  // namespace
38
39// static
40BrowserContextKeyedAPIFactory<EasyUnlockPrivateAPI>*
41    EasyUnlockPrivateAPI::GetFactoryInstance() {
42  return g_factory.Pointer();
43}
44
45EasyUnlockPrivateAPI::EasyUnlockPrivateAPI(content::BrowserContext* context)
46    : crypto_delegate_(EasyUnlockPrivateCryptoDelegate::Create()) {
47}
48
49EasyUnlockPrivateAPI::~EasyUnlockPrivateAPI() {}
50
51EasyUnlockPrivateGetStringsFunction::EasyUnlockPrivateGetStringsFunction() {
52}
53EasyUnlockPrivateGetStringsFunction::~EasyUnlockPrivateGetStringsFunction() {
54}
55
56bool EasyUnlockPrivateGetStringsFunction::RunSync() {
57  scoped_ptr<base::DictionaryValue> strings(new base::DictionaryValue);
58
59#if defined(OS_CHROMEOS)
60  const base::string16 device_type = chromeos::GetChromeDeviceType();
61#else
62  // TODO(isherman): Set an appropriate device name for non-ChromeOS devices.
63  const base::string16 device_type = base::ASCIIToUTF16("Chromeschnozzle");
64#endif  // defined(OS_CHROMEOS)
65
66  // Setup notification strings.
67  strings->SetString(
68      "setupNotificationTitle",
69      l10n_util::GetStringFUTF16(IDS_EASY_UNLOCK_SETUP_NOTIFICATION_TITLE,
70                                 device_type));
71  strings->SetString(
72      "setupNotificationMessage",
73      l10n_util::GetStringFUTF16(IDS_EASY_UNLOCK_SETUP_NOTIFICATION_MESSAGE,
74                                 device_type));
75  strings->SetString(
76      "setupNotificationButtonTitle",
77      l10n_util::GetStringUTF16(
78          IDS_EASY_UNLOCK_SETUP_NOTIFICATION_BUTTON_TITLE));
79
80  // Success notification strings.
81  strings->SetString(
82      "successNotificationTitle",
83      l10n_util::GetStringUTF16(IDS_EASY_UNLOCK_SUCCESS_NOTIFICATION_TITLE));
84  strings->SetString(
85      "successNotificationMessage",
86      l10n_util::GetStringFUTF16(IDS_EASY_UNLOCK_SUCCESS_NOTIFICATION_MESSAGE,
87                                 device_type));
88
89  // Setup dialog strings.
90  // Step 1: Intro.
91  strings->SetString(
92      "setupIntroHeaderTitle",
93      l10n_util::GetStringFUTF16(
94          IDS_EASY_UNLOCK_SETUP_INTRO_HEADER_TITLE, device_type));
95  strings->SetString(
96      "setupIntroHeaderText",
97      l10n_util::GetStringFUTF16(
98          IDS_EASY_UNLOCK_SETUP_INTRO_HEADER_TEXT, device_type));
99  strings->SetString(
100      "setupIntroHeaderFootnote",
101      l10n_util::GetStringUTF16(
102          IDS_EASY_UNLOCK_SETUP_INTRO_HEADER_FOOTNOTE));
103  strings->SetString(
104      "setupIntroFindPhoneButtonLabel",
105      l10n_util::GetStringUTF16(
106          IDS_EASY_UNLOCK_SETUP_INTRO_FIND_PHONE_BUTTON_LABEL));
107  strings->SetString(
108      "setupIntroFindingPhoneButtonLabel",
109      l10n_util::GetStringUTF16(
110          IDS_EASY_UNLOCK_SETUP_INTRO_FINDING_PHONE_BUTTON_LABEL));
111  strings->SetString(
112      "setupIntroHowIsThisSecureLinkText",
113      l10n_util::GetStringUTF16(
114          IDS_EASY_UNLOCK_SETUP_INTRO_HOW_IS_THIS_SECURE_LINK_TEXT));
115  // Step 2: Found a viable phone.
116  strings->SetString(
117      "setupFoundPhoneHeaderTitle",
118      l10n_util::GetStringFUTF16(
119          IDS_EASY_UNLOCK_SETUP_FOUND_PHONE_HEADER_TITLE, device_type));
120  strings->SetString(
121      "setupFoundPhoneHeaderText",
122      l10n_util::GetStringFUTF16(
123          IDS_EASY_UNLOCK_SETUP_FOUND_PHONE_HEADER_TEXT, device_type));
124  strings->SetString(
125      "setupFoundPhoneUseThisPhoneButtonLabel",
126      l10n_util::GetStringUTF16(
127          IDS_EASY_UNLOCK_SETUP_FOUND_PHONE_USE_THIS_PHONE_BUTTON_LABEL));
128  // Step 3: Setup completed successfully.
129  strings->SetString(
130      "setupCompleteHeaderTitle",
131      l10n_util::GetStringUTF16(
132          IDS_EASY_UNLOCK_SETUP_COMPLETE_HEADER_TITLE));
133  strings->SetString(
134      "setupCompleteHeaderText",
135      l10n_util::GetStringUTF16(
136          IDS_EASY_UNLOCK_SETUP_COMPLETE_HEADER_TEXT));
137  strings->SetString(
138      "setupCompleteTryItOutButtonLabel",
139      l10n_util::GetStringUTF16(
140          IDS_EASY_UNLOCK_SETUP_COMPLETE_TRY_IT_OUT_BUTTON_LABEL));
141  strings->SetString(
142      "setupCompleteSettingsLinkText",
143      l10n_util::GetStringUTF16(
144          IDS_EASY_UNLOCK_SETUP_COMPLETE_SETTINGS_LINK_TEXT));
145
146  // Error strings.
147  strings->SetString(
148      "setupErrorBluetoothUnavailable",
149      l10n_util::GetStringFUTF16(
150          IDS_EASY_UNLOCK_SETUP_ERROR_BLUETOOTH_UNAVAILBLE, device_type));
151  strings->SetString(
152      "setupErrorOffline",
153      l10n_util::GetStringFUTF16(
154          IDS_EASY_UNLOCK_SETUP_ERROR_OFFLINE, device_type));
155  strings->SetString(
156      "setupErrorFindingPhone",
157      l10n_util::GetStringUTF16(IDS_EASY_UNLOCK_SETUP_ERROR_FINDING_PHONE));
158  strings->SetString(
159      "setupErrorBluetoothConnectionFailed",
160      l10n_util::GetStringFUTF16(
161          IDS_EASY_UNLOCK_SETUP_ERROR_BLUETOOTH_CONNECTION_FAILED,
162          device_type));
163  strings->SetString(
164      "setupErrorConnectingToPhone",
165      l10n_util::GetStringFUTF16(
166          IDS_EASY_UNLOCK_SETUP_ERROR_CONNECTING_TO_PHONE, device_type));
167
168  // TODO(isherman): Remove these strings once the app has been updated.
169  strings->SetString(
170      "notificationTitle",
171      l10n_util::GetStringFUTF16(IDS_EASY_UNLOCK_SETUP_NOTIFICATION_TITLE,
172                                 device_type));
173  strings->SetString(
174      "notificationMessage",
175      l10n_util::GetStringFUTF16(IDS_EASY_UNLOCK_SETUP_NOTIFICATION_MESSAGE,
176                                 device_type));
177  strings->SetString(
178      "notificationButtonTitle",
179      l10n_util::GetStringUTF16(
180          IDS_EASY_UNLOCK_SETUP_NOTIFICATION_BUTTON_TITLE));
181
182  SetResult(strings.release());
183  return true;
184}
185
186EasyUnlockPrivatePerformECDHKeyAgreementFunction::
187EasyUnlockPrivatePerformECDHKeyAgreementFunction() {}
188
189EasyUnlockPrivatePerformECDHKeyAgreementFunction::
190~EasyUnlockPrivatePerformECDHKeyAgreementFunction() {}
191
192bool EasyUnlockPrivatePerformECDHKeyAgreementFunction::RunAsync() {
193  scoped_ptr<easy_unlock_private::PerformECDHKeyAgreement::Params> params =
194      easy_unlock_private::PerformECDHKeyAgreement::Params::Create(*args_);
195  EXTENSION_FUNCTION_VALIDATE(params);
196
197  GetCryptoDelegate(browser_context())->PerformECDHKeyAgreement(
198      params->private_key,
199      params->public_key,
200      base::Bind(&EasyUnlockPrivatePerformECDHKeyAgreementFunction::OnData,
201                 this));
202  return true;
203}
204
205void EasyUnlockPrivatePerformECDHKeyAgreementFunction::OnData(
206    const std::string& secret_key) {
207  // TODO(tbarzic): Improve error handling.
208  if (!secret_key.empty()) {
209    results_ = easy_unlock_private::PerformECDHKeyAgreement::Results::Create(
210        secret_key);
211  }
212  SendResponse(true);
213}
214
215EasyUnlockPrivateGenerateEcP256KeyPairFunction::
216EasyUnlockPrivateGenerateEcP256KeyPairFunction() {}
217
218EasyUnlockPrivateGenerateEcP256KeyPairFunction::
219~EasyUnlockPrivateGenerateEcP256KeyPairFunction() {}
220
221bool EasyUnlockPrivateGenerateEcP256KeyPairFunction::RunAsync() {
222  GetCryptoDelegate(browser_context())->GenerateEcP256KeyPair(
223      base::Bind(&EasyUnlockPrivateGenerateEcP256KeyPairFunction::OnData,
224                 this));
225  return true;
226}
227
228void EasyUnlockPrivateGenerateEcP256KeyPairFunction::OnData(
229    const std::string& private_key,
230    const std::string& public_key) {
231  // TODO(tbarzic): Improve error handling.
232  if (!public_key.empty() && !private_key.empty()) {
233    results_ = easy_unlock_private::GenerateEcP256KeyPair::Results::Create(
234        public_key, private_key);
235  }
236  SendResponse(true);
237}
238
239EasyUnlockPrivateCreateSecureMessageFunction::
240EasyUnlockPrivateCreateSecureMessageFunction() {}
241
242EasyUnlockPrivateCreateSecureMessageFunction::
243~EasyUnlockPrivateCreateSecureMessageFunction() {}
244
245bool EasyUnlockPrivateCreateSecureMessageFunction::RunAsync() {
246  scoped_ptr<easy_unlock_private::CreateSecureMessage::Params> params =
247      easy_unlock_private::CreateSecureMessage::Params::Create(*args_);
248  EXTENSION_FUNCTION_VALIDATE(params);
249
250  GetCryptoDelegate(browser_context())->CreateSecureMessage(
251      params->payload,
252      params->key,
253      params->options.associated_data ?
254          *params->options.associated_data : std::string(),
255      params->options.public_metadata ?
256          *params->options.public_metadata : std::string(),
257      params->options.verification_key_id ?
258          *params->options.verification_key_id : std::string(),
259      params->options.encrypt_type,
260      params->options.sign_type,
261      base::Bind(&EasyUnlockPrivateCreateSecureMessageFunction::OnData,
262                 this));
263  return true;
264}
265
266void EasyUnlockPrivateCreateSecureMessageFunction::OnData(
267    const std::string& message) {
268  // TODO(tbarzic): Improve error handling.
269  if (!message.empty()) {
270    results_ = easy_unlock_private::CreateSecureMessage::Results::Create(
271        message);
272  }
273  SendResponse(true);
274}
275
276EasyUnlockPrivateUnwrapSecureMessageFunction::
277EasyUnlockPrivateUnwrapSecureMessageFunction() {}
278
279EasyUnlockPrivateUnwrapSecureMessageFunction::
280~EasyUnlockPrivateUnwrapSecureMessageFunction() {}
281
282bool EasyUnlockPrivateUnwrapSecureMessageFunction::RunAsync() {
283  scoped_ptr<easy_unlock_private::UnwrapSecureMessage::Params> params =
284      easy_unlock_private::UnwrapSecureMessage::Params::Create(*args_);
285  EXTENSION_FUNCTION_VALIDATE(params);
286
287  GetCryptoDelegate(browser_context())->UnwrapSecureMessage(
288      params->secure_message,
289      params->key,
290      params->options.associated_data ?
291          *params->options.associated_data : std::string(),
292      params->options.encrypt_type,
293      params->options.sign_type,
294      base::Bind(&EasyUnlockPrivateUnwrapSecureMessageFunction::OnData,
295                 this));
296  return true;
297}
298
299void EasyUnlockPrivateUnwrapSecureMessageFunction::OnData(
300    const std::string& data) {
301  // TODO(tbarzic): Improve error handling.
302  if (!data.empty())
303    results_ = easy_unlock_private::UnwrapSecureMessage::Results::Create(data);
304  SendResponse(true);
305}
306
307EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction::
308    EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction() {}
309
310EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction::
311    ~EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction() {}
312
313bool EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction::RunAsync() {
314  scoped_ptr<easy_unlock_private::SeekBluetoothDeviceByAddress::Params> params(
315      easy_unlock_private::SeekBluetoothDeviceByAddress::Params::Create(
316          *args_));
317  EXTENSION_FUNCTION_VALIDATE(params.get());
318
319  easy_unlock::SeekBluetoothDeviceByAddress(
320      params->device_address,
321      base::Bind(
322          &EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction::
323              OnSeekCompleted,
324          this));
325  return true;
326}
327
328void EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction::OnSeekCompleted(
329    const easy_unlock::SeekDeviceResult& seek_result) {
330  if (seek_result.success) {
331    SendResponse(true);
332  } else {
333    SetError(seek_result.error_message);
334    SendResponse(false);
335  }
336}
337
338}  // namespace api
339}  // namespace extensions
340