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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_EASY_UNLOCK_PRIVATE_EASY_UNLOCK_PRIVATE_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_EASY_UNLOCK_PRIVATE_EASY_UNLOCK_PRIVATE_API_H_
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "device/bluetooth/bluetooth_device.h"
13 #include "extensions/browser/api/bluetooth/bluetooth_extension_function.h"
14 #include "extensions/browser/api/bluetooth_socket/bluetooth_socket_api.h"
15 #include "extensions/browser/browser_context_keyed_api_factory.h"
16 #include "extensions/browser/extension_function.h"
18 // Implementations for chrome.easyUnlockPrivate API functions.
29 class ExternalDeviceInfo
;
32 namespace proximity_auth
{
34 class BluetoothLowEnergyConnectionFinder
;
35 class BluetoothThrottler
;
36 class SecureMessageDelegate
;
39 namespace extensions
{
41 class EasyUnlockPrivateConnectionManager
;
42 class EasyUnlockPrivateCryptoDelegate
;
44 class EasyUnlockPrivateAPI
: public BrowserContextKeyedAPI
{
46 static BrowserContextKeyedAPIFactory
<EasyUnlockPrivateAPI
>*
49 static const bool kServiceRedirectedInIncognito
= true;
51 explicit EasyUnlockPrivateAPI(content::BrowserContext
* context
);
52 ~EasyUnlockPrivateAPI() override
;
54 EasyUnlockPrivateCryptoDelegate
* GetCryptoDelegate();
56 EasyUnlockPrivateConnectionManager
* get_connection_manager() {
57 return connection_manager_
.get();
61 friend class BrowserContextKeyedAPIFactory
<EasyUnlockPrivateAPI
>;
63 // BrowserContextKeyedAPI implementation.
64 static const char* service_name() { return "EasyUnlockPrivate"; }
66 scoped_ptr
<EasyUnlockPrivateCryptoDelegate
> crypto_delegate_
;
68 scoped_ptr
<EasyUnlockPrivateConnectionManager
> connection_manager_
;
70 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateAPI
);
73 // TODO(tbarzic): Replace SyncExtensionFunction/AsyncExtensionFunction overrides
74 // with UIThreadExtensionFunction throughout the file.
75 class EasyUnlockPrivateGetStringsFunction
: public SyncExtensionFunction
{
77 EasyUnlockPrivateGetStringsFunction();
80 ~EasyUnlockPrivateGetStringsFunction() override
;
82 // SyncExtensionFunction:
83 bool RunSync() override
;
86 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getStrings",
87 EASYUNLOCKPRIVATE_GETSTRINGS
)
89 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGetStringsFunction
);
92 class EasyUnlockPrivatePerformECDHKeyAgreementFunction
93 : public AsyncExtensionFunction
{
95 EasyUnlockPrivatePerformECDHKeyAgreementFunction();
98 ~EasyUnlockPrivatePerformECDHKeyAgreementFunction() override
;
100 bool RunAsync() override
;
103 void OnData(const std::string
& secret_key
);
105 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.performECDHKeyAgreement",
106 EASYUNLOCKPRIVATE_PERFORMECDHKEYAGREEMENT
)
108 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivatePerformECDHKeyAgreementFunction
);
111 class EasyUnlockPrivateGenerateEcP256KeyPairFunction
112 : public AsyncExtensionFunction
{
114 EasyUnlockPrivateGenerateEcP256KeyPairFunction();
117 ~EasyUnlockPrivateGenerateEcP256KeyPairFunction() override
;
119 bool RunAsync() override
;
122 void OnData(const std::string
& public_key
,
123 const std::string
& private_key
);
125 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.generateEcP256KeyPair",
126 EASYUNLOCKPRIVATE_GENERATEECP256KEYPAIR
)
128 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGenerateEcP256KeyPairFunction
);
131 class EasyUnlockPrivateCreateSecureMessageFunction
132 : public AsyncExtensionFunction
{
134 EasyUnlockPrivateCreateSecureMessageFunction();
137 ~EasyUnlockPrivateCreateSecureMessageFunction() override
;
139 bool RunAsync() override
;
142 void OnData(const std::string
& message
);
144 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.createSecureMessage",
145 EASYUNLOCKPRIVATE_CREATESECUREMESSAGE
)
147 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateCreateSecureMessageFunction
);
150 class EasyUnlockPrivateUnwrapSecureMessageFunction
151 : public AsyncExtensionFunction
{
153 EasyUnlockPrivateUnwrapSecureMessageFunction();
156 ~EasyUnlockPrivateUnwrapSecureMessageFunction() override
;
158 bool RunAsync() override
;
161 void OnData(const std::string
& data
);
163 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.unwrapSecureMessage",
164 EASYUNLOCKPRIVATE_UNWRAPSECUREMESSAGE
)
166 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateUnwrapSecureMessageFunction
);
169 class EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction
170 : public AsyncExtensionFunction
{
172 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.seekBluetoothDeviceByAddress",
173 EASYUNLOCKPRIVATE_SEEKBLUETOOTHDEVICEBYADDRESS
)
174 EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction();
177 ~EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction() override
;
179 // AsyncExtensionFunction:
180 bool RunAsync() override
;
182 // Callbacks that are called when the seek operation succeeds or fails.
183 void OnSeekSuccess();
184 void OnSeekFailure(const std::string
& error_message
);
186 DISALLOW_COPY_AND_ASSIGN(
187 EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction
);
190 class EasyUnlockPrivateConnectToBluetoothServiceInsecurelyFunction
191 : public api::BluetoothSocketAbstractConnectFunction
{
193 DECLARE_EXTENSION_FUNCTION(
194 "easyUnlockPrivate.connectToBluetoothServiceInsecurely",
195 EASYUNLOCKPRIVATE_CONNECTTOBLUETOOTHSERVICEINSECURELY
)
196 EasyUnlockPrivateConnectToBluetoothServiceInsecurelyFunction();
199 ~EasyUnlockPrivateConnectToBluetoothServiceInsecurelyFunction() override
;
201 // BluetoothSocketAbstractConnectFunction:
202 void ConnectToService(device::BluetoothDevice
* device
,
203 const device::BluetoothUUID
& uuid
) override
;
205 DISALLOW_COPY_AND_ASSIGN(
206 EasyUnlockPrivateConnectToBluetoothServiceInsecurelyFunction
);
209 class EasyUnlockPrivateUpdateScreenlockStateFunction
210 : public SyncExtensionFunction
{
212 EasyUnlockPrivateUpdateScreenlockStateFunction();
215 ~EasyUnlockPrivateUpdateScreenlockStateFunction() override
;
217 bool RunSync() override
;
220 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.updateScreenlockState",
221 EASYUNLOCKPRIVATE_UPDATESCREENLOCKSTATE
)
223 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateUpdateScreenlockStateFunction
);
226 class EasyUnlockPrivateSetPermitAccessFunction
: public SyncExtensionFunction
{
228 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setPermitAccess",
229 EASYUNLOCKPRIVATE_SETPERMITACCESS
)
230 EasyUnlockPrivateSetPermitAccessFunction();
233 ~EasyUnlockPrivateSetPermitAccessFunction() override
;
235 // SyncExtensionFunction:
236 bool RunSync() override
;
238 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateSetPermitAccessFunction
);
241 class EasyUnlockPrivateGetPermitAccessFunction
: public SyncExtensionFunction
{
243 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getPermitAccess",
244 EASYUNLOCKPRIVATE_GETPERMITACCESS
)
245 EasyUnlockPrivateGetPermitAccessFunction();
248 ~EasyUnlockPrivateGetPermitAccessFunction() override
;
250 // Writes the user's public and private key in base64 form to the
251 // |user_public_key| and |user_private_key| fields. Exposed for testing.
252 virtual void GetKeyPairForExperiment(std::string
* user_public_key
,
253 std::string
* user_private_key
);
256 // SyncExtensionFunction:
257 bool RunSync() override
;
259 // Instead of returning the value set by easyUnlockPrivate.setPermitAccess,
260 // return the permit access used by the native CryptAuthEnrollmentManager.
261 void ReturnPermitAccessForExperiment();
263 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGetPermitAccessFunction
);
266 class EasyUnlockPrivateClearPermitAccessFunction
267 : public SyncExtensionFunction
{
269 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.clearPermitAccess",
270 EASYUNLOCKPRIVATE_CLEARPERMITACCESS
)
271 EasyUnlockPrivateClearPermitAccessFunction();
274 ~EasyUnlockPrivateClearPermitAccessFunction() override
;
276 // SyncExtensionFunction:
277 bool RunSync() override
;
279 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateClearPermitAccessFunction
);
282 class EasyUnlockPrivateSetRemoteDevicesFunction
: public SyncExtensionFunction
{
284 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setRemoteDevices",
285 EASYUNLOCKPRIVATE_SETREMOTEDEVICES
)
286 EasyUnlockPrivateSetRemoteDevicesFunction();
289 ~EasyUnlockPrivateSetRemoteDevicesFunction() override
;
291 // SyncExtensionFunction:
292 bool RunSync() override
;
294 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateSetRemoteDevicesFunction
);
297 class EasyUnlockPrivateGetRemoteDevicesFunction
298 : public AsyncExtensionFunction
{
300 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getRemoteDevices",
301 EASYUNLOCKPRIVATE_GETREMOTEDEVICES
)
302 EasyUnlockPrivateGetRemoteDevicesFunction();
305 ~EasyUnlockPrivateGetRemoteDevicesFunction() override
;
307 // Returns the user's private key used for the native experiment.
308 // Exposed for testing.
309 virtual std::string
GetUserPrivateKey();
311 // Returns the user's unlock keys used for the native experiment.
312 // Exposed for testing.
313 virtual std::vector
<cryptauth::ExternalDeviceInfo
> GetUnlockKeys();
316 // AsyncExtensionFunction:
317 bool RunAsync() override
;
319 // Returns devices managed by the native Chrome component if the
320 // kEnableBluetoothLowEnergyDiscovery flag is set.
321 void ReturnDevicesForExperiment();
323 // Callback when the PSK of a device is derived.
324 void OnPSKDerivedForDevice(const cryptauth::ExternalDeviceInfo
& device
,
325 const std::string
& persistent_symmetric_key
);
327 // The permit id of the user. Used for the native experiment.
328 std::string permit_id_
;
330 // The expected number of devices to return. Used for the native experiment.
331 size_t expected_devices_count_
;
333 // Working list of the devices to return. Used for the native experiment.
334 scoped_ptr
<base::ListValue
> remote_devices_
;
336 // Used to derive devices' PSK. Used for the native experiment.
337 scoped_ptr
<proximity_auth::SecureMessageDelegate
> secure_message_delegate_
;
339 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGetRemoteDevicesFunction
);
342 class EasyUnlockPrivateGetSignInChallengeFunction
:
343 public AsyncExtensionFunction
{
345 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getSignInChallenge",
346 EASYUNLOCKPRIVATE_GETSIGNINCHALLENGE
)
347 EasyUnlockPrivateGetSignInChallengeFunction();
350 ~EasyUnlockPrivateGetSignInChallengeFunction() override
;
352 // AsyncExtensionFunction:
353 bool RunAsync() override
;
355 // Called when the challenge and the signed nonce have been generated.
356 void OnDone(const std::string
& challenge
, const std::string
& signed_nonce
);
358 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGetSignInChallengeFunction
);
361 class EasyUnlockPrivateTrySignInSecretFunction
:
362 public SyncExtensionFunction
{
364 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.trySignInSecret",
365 EASYUNLOCKPRIVATE_TRYSIGNINSECRET
)
366 EasyUnlockPrivateTrySignInSecretFunction();
369 ~EasyUnlockPrivateTrySignInSecretFunction() override
;
371 // SyncExtensionFunction:
372 bool RunSync() override
;
374 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateTrySignInSecretFunction
);
377 class EasyUnlockPrivateGetUserInfoFunction
: public SyncExtensionFunction
{
379 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getUserInfo",
380 EASYUNLOCKPRIVATE_GETUSERINFO
)
381 EasyUnlockPrivateGetUserInfoFunction();
384 ~EasyUnlockPrivateGetUserInfoFunction() override
;
386 // SyncExtensionFunction:
387 bool RunSync() override
;
389 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGetUserInfoFunction
);
392 class EasyUnlockPrivateGetConnectionInfoFunction
393 : public api::BluetoothExtensionFunction
{
395 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getConnectionInfo",
396 EASYUNLOCKPRIVATE_GETCONNECTIONINFO
)
397 EasyUnlockPrivateGetConnectionInfoFunction();
400 ~EasyUnlockPrivateGetConnectionInfoFunction() override
;
402 // BluetoothExtensionFunction:
403 bool DoWork(scoped_refptr
<device::BluetoothAdapter
> adapter
) override
;
405 void OnConnectionInfo(
406 const device::BluetoothDevice::ConnectionInfo
& connection_info
);
408 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGetConnectionInfoFunction
);
411 class EasyUnlockPrivateShowErrorBubbleFunction
: public SyncExtensionFunction
{
413 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.showErrorBubble",
414 EASYUNLOCKPRIVATE_SHOWERRORBUBBLE
)
415 EasyUnlockPrivateShowErrorBubbleFunction();
418 ~EasyUnlockPrivateShowErrorBubbleFunction() override
;
420 // SyncExtensionFunction:
421 bool RunSync() override
;
423 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateShowErrorBubbleFunction
);
426 class EasyUnlockPrivateHideErrorBubbleFunction
: public SyncExtensionFunction
{
428 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.hideErrorBubble",
429 EASYUNLOCKPRIVATE_HIDEERRORBUBBLE
)
430 EasyUnlockPrivateHideErrorBubbleFunction();
433 ~EasyUnlockPrivateHideErrorBubbleFunction() override
;
435 // SyncExtensionFunction:
436 bool RunSync() override
;
438 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateHideErrorBubbleFunction
);
441 class EasyUnlockPrivateSetAutoPairingResultFunction
442 : public SyncExtensionFunction
{
444 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setAutoPairingResult",
445 EASYUNLOCKPRIVATE_SETAUTOPAIRINGRESULT
)
446 EasyUnlockPrivateSetAutoPairingResultFunction();
449 ~EasyUnlockPrivateSetAutoPairingResultFunction() override
;
451 // SyncExtensionFunction:
452 bool RunSync() override
;
454 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateSetAutoPairingResultFunction
);
457 class EasyUnlockPrivateFindSetupConnectionFunction
458 : public AsyncExtensionFunction
{
460 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.findSetupConnection",
461 EASYUNLOCKPRIVATE_FINDSETUPCONNECTION
)
462 EasyUnlockPrivateFindSetupConnectionFunction();
465 ~EasyUnlockPrivateFindSetupConnectionFunction() override
;
467 // AsyncExtensionFunction:
468 bool RunAsync() override
;
470 // Called when the connection with the remote device advertising the setup
471 // service was found.
472 void OnConnectionFound(scoped_ptr
<proximity_auth::Connection
> connection
);
474 // Callback when waiting for |connection_finder_| to return.
475 void OnConnectionFinderTimedOut();
477 // The BLE connection finder instance.
478 scoped_ptr
<proximity_auth::BluetoothLowEnergyConnectionFinder
>
481 // The connection throttler passed to the BLE connection finder.
482 scoped_ptr
<proximity_auth::BluetoothThrottler
> bluetooth_throttler_
;
484 // Used for timing out when waiting for the connection finder to return.
485 scoped_ptr
<base::Timer
> timer_
;
487 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateFindSetupConnectionFunction
);
490 class EasyUnlockPrivateSetupConnectionStatusFunction
491 : public SyncExtensionFunction
{
493 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setupConnectionStatus",
494 EASYUNLOCKPRIVATE_SETUPCONNECTIONSTATUS
)
495 EasyUnlockPrivateSetupConnectionStatusFunction();
498 ~EasyUnlockPrivateSetupConnectionStatusFunction() override
;
500 // SyncExtensionFunction:
501 bool RunSync() override
;
503 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateSetupConnectionStatusFunction
);
506 class EasyUnlockPrivateSetupConnectionDisconnectFunction
507 : public SyncExtensionFunction
{
509 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setupConnectionDisconnect",
510 EASYUNLOCKPRIVATE_SETUPCONNECTIONDISCONNECT
)
511 EasyUnlockPrivateSetupConnectionDisconnectFunction();
514 ~EasyUnlockPrivateSetupConnectionDisconnectFunction() override
;
516 // SyncExtensionFunction:
517 bool RunSync() override
;
519 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateSetupConnectionDisconnectFunction
);
522 class EasyUnlockPrivateSetupConnectionSendFunction
523 : public SyncExtensionFunction
{
525 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setupConnectionSend",
526 EASYUNLOCKPRIVATE_SETUPCONNECTIONSEND
)
527 EasyUnlockPrivateSetupConnectionSendFunction();
530 ~EasyUnlockPrivateSetupConnectionSendFunction() override
;
532 // SyncExtensionFunction:
533 bool RunSync() override
;
535 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateSetupConnectionSendFunction
);
538 } // namespace extensions
540 #endif // CHROME_BROWSER_EXTENSIONS_API_EASY_UNLOCK_PRIVATE_EASY_UNLOCK_PRIVATE_API_H_