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.
25 class ExternalDeviceInfo
;
28 namespace proximity_auth
{
29 class SecureMessageDelegate
;
32 namespace extensions
{
34 class EasyUnlockPrivateCryptoDelegate
;
36 class EasyUnlockPrivateAPI
: public BrowserContextKeyedAPI
{
38 static BrowserContextKeyedAPIFactory
<EasyUnlockPrivateAPI
>*
41 static const bool kServiceRedirectedInIncognito
= true;
43 explicit EasyUnlockPrivateAPI(content::BrowserContext
* context
);
44 ~EasyUnlockPrivateAPI() override
;
46 EasyUnlockPrivateCryptoDelegate
* GetCryptoDelegate();
49 friend class BrowserContextKeyedAPIFactory
<EasyUnlockPrivateAPI
>;
51 // BrowserContextKeyedAPI implementation.
52 static const char* service_name() { return "EasyUnlockPrivate"; }
54 scoped_ptr
<EasyUnlockPrivateCryptoDelegate
> crypto_delegate_
;
56 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateAPI
);
59 // TODO(tbarzic): Replace SyncExtensionFunction/AsyncExtensionFunction overrides
60 // with UIThreadExtensionFunction throughout the file.
61 class EasyUnlockPrivateGetStringsFunction
: public SyncExtensionFunction
{
63 EasyUnlockPrivateGetStringsFunction();
66 ~EasyUnlockPrivateGetStringsFunction() override
;
68 // SyncExtensionFunction:
69 bool RunSync() override
;
72 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getStrings",
73 EASYUNLOCKPRIVATE_GETSTRINGS
)
75 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGetStringsFunction
);
78 class EasyUnlockPrivatePerformECDHKeyAgreementFunction
79 : public AsyncExtensionFunction
{
81 EasyUnlockPrivatePerformECDHKeyAgreementFunction();
84 ~EasyUnlockPrivatePerformECDHKeyAgreementFunction() override
;
86 bool RunAsync() override
;
89 void OnData(const std::string
& secret_key
);
91 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.performECDHKeyAgreement",
92 EASYUNLOCKPRIVATE_PERFORMECDHKEYAGREEMENT
)
94 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivatePerformECDHKeyAgreementFunction
);
97 class EasyUnlockPrivateGenerateEcP256KeyPairFunction
98 : public AsyncExtensionFunction
{
100 EasyUnlockPrivateGenerateEcP256KeyPairFunction();
103 ~EasyUnlockPrivateGenerateEcP256KeyPairFunction() override
;
105 bool RunAsync() override
;
108 void OnData(const std::string
& public_key
,
109 const std::string
& private_key
);
111 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.generateEcP256KeyPair",
112 EASYUNLOCKPRIVATE_GENERATEECP256KEYPAIR
)
114 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGenerateEcP256KeyPairFunction
);
117 class EasyUnlockPrivateCreateSecureMessageFunction
118 : public AsyncExtensionFunction
{
120 EasyUnlockPrivateCreateSecureMessageFunction();
123 ~EasyUnlockPrivateCreateSecureMessageFunction() override
;
125 bool RunAsync() override
;
128 void OnData(const std::string
& message
);
130 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.createSecureMessage",
131 EASYUNLOCKPRIVATE_CREATESECUREMESSAGE
)
133 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateCreateSecureMessageFunction
);
136 class EasyUnlockPrivateUnwrapSecureMessageFunction
137 : public AsyncExtensionFunction
{
139 EasyUnlockPrivateUnwrapSecureMessageFunction();
142 ~EasyUnlockPrivateUnwrapSecureMessageFunction() override
;
144 bool RunAsync() override
;
147 void OnData(const std::string
& data
);
149 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.unwrapSecureMessage",
150 EASYUNLOCKPRIVATE_UNWRAPSECUREMESSAGE
)
152 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateUnwrapSecureMessageFunction
);
155 class EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction
156 : public AsyncExtensionFunction
{
158 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.seekBluetoothDeviceByAddress",
159 EASYUNLOCKPRIVATE_SEEKBLUETOOTHDEVICEBYADDRESS
)
160 EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction();
163 ~EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction() override
;
165 // AsyncExtensionFunction:
166 bool RunAsync() override
;
168 // Callbacks that are called when the seek operation succeeds or fails.
169 void OnSeekSuccess();
170 void OnSeekFailure(const std::string
& error_message
);
172 DISALLOW_COPY_AND_ASSIGN(
173 EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction
);
176 class EasyUnlockPrivateConnectToBluetoothServiceInsecurelyFunction
177 : public api::BluetoothSocketAbstractConnectFunction
{
179 DECLARE_EXTENSION_FUNCTION(
180 "easyUnlockPrivate.connectToBluetoothServiceInsecurely",
181 EASYUNLOCKPRIVATE_CONNECTTOBLUETOOTHSERVICEINSECURELY
)
182 EasyUnlockPrivateConnectToBluetoothServiceInsecurelyFunction();
185 ~EasyUnlockPrivateConnectToBluetoothServiceInsecurelyFunction() override
;
187 // BluetoothSocketAbstractConnectFunction:
188 void ConnectToService(device::BluetoothDevice
* device
,
189 const device::BluetoothUUID
& uuid
) override
;
191 DISALLOW_COPY_AND_ASSIGN(
192 EasyUnlockPrivateConnectToBluetoothServiceInsecurelyFunction
);
195 class EasyUnlockPrivateUpdateScreenlockStateFunction
196 : public SyncExtensionFunction
{
198 EasyUnlockPrivateUpdateScreenlockStateFunction();
201 ~EasyUnlockPrivateUpdateScreenlockStateFunction() override
;
203 bool RunSync() override
;
206 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.updateScreenlockState",
207 EASYUNLOCKPRIVATE_UPDATESCREENLOCKSTATE
)
209 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateUpdateScreenlockStateFunction
);
212 class EasyUnlockPrivateSetPermitAccessFunction
: public SyncExtensionFunction
{
214 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setPermitAccess",
215 EASYUNLOCKPRIVATE_SETPERMITACCESS
)
216 EasyUnlockPrivateSetPermitAccessFunction();
219 ~EasyUnlockPrivateSetPermitAccessFunction() override
;
221 // SyncExtensionFunction:
222 bool RunSync() override
;
224 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateSetPermitAccessFunction
);
227 class EasyUnlockPrivateGetPermitAccessFunction
: public SyncExtensionFunction
{
229 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getPermitAccess",
230 EASYUNLOCKPRIVATE_GETPERMITACCESS
)
231 EasyUnlockPrivateGetPermitAccessFunction();
234 ~EasyUnlockPrivateGetPermitAccessFunction() override
;
236 // Writes the user's public and private key in base64 form to the
237 // |user_public_key| and |user_private_key| fields. Exposed for testing.
238 virtual void GetKeyPairForExperiment(std::string
* user_public_key
,
239 std::string
* user_private_key
);
242 // SyncExtensionFunction:
243 bool RunSync() override
;
245 // Instead of returning the value set by easyUnlockPrivate.setPermitAccess,
246 // return the permit access used by the native CryptAuthEnrollmentManager.
247 void ReturnPermitAccessForExperiment();
249 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGetPermitAccessFunction
);
252 class EasyUnlockPrivateClearPermitAccessFunction
253 : public SyncExtensionFunction
{
255 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.clearPermitAccess",
256 EASYUNLOCKPRIVATE_CLEARPERMITACCESS
)
257 EasyUnlockPrivateClearPermitAccessFunction();
260 ~EasyUnlockPrivateClearPermitAccessFunction() override
;
262 // SyncExtensionFunction:
263 bool RunSync() override
;
265 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateClearPermitAccessFunction
);
268 class EasyUnlockPrivateSetRemoteDevicesFunction
: public SyncExtensionFunction
{
270 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setRemoteDevices",
271 EASYUNLOCKPRIVATE_SETREMOTEDEVICES
)
272 EasyUnlockPrivateSetRemoteDevicesFunction();
275 ~EasyUnlockPrivateSetRemoteDevicesFunction() override
;
277 // SyncExtensionFunction:
278 bool RunSync() override
;
280 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateSetRemoteDevicesFunction
);
283 class EasyUnlockPrivateGetRemoteDevicesFunction
284 : public AsyncExtensionFunction
{
286 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getRemoteDevices",
287 EASYUNLOCKPRIVATE_GETREMOTEDEVICES
)
288 EasyUnlockPrivateGetRemoteDevicesFunction();
291 ~EasyUnlockPrivateGetRemoteDevicesFunction() override
;
293 // Returns the user's private key used for the native experiment.
294 // Exposed for testing.
295 virtual std::string
GetUserPrivateKey();
297 // Returns the user's unlock keys used for the native experiment.
298 // Exposed for testing.
299 virtual std::vector
<cryptauth::ExternalDeviceInfo
> GetUnlockKeys();
302 // AsyncExtensionFunction:
303 bool RunAsync() override
;
305 // Returns devices managed by the native Chrome component if the
306 // kEnableBluetoothLowEnergyDiscovery flag is set.
307 void ReturnDevicesForExperiment();
309 // Callback when the PSK of a device is derived.
310 void OnPSKDerivedForDevice(const cryptauth::ExternalDeviceInfo
& device
,
311 const std::string
& persistent_symmetric_key
);
313 // The permit id of the user. Used for the native experiment.
314 std::string permit_id_
;
316 // The expected number of devices to return. Used for the native experiment.
317 size_t expected_devices_count_
;
319 // Working list of the devices to return. Used for the native experiment.
320 scoped_ptr
<base::ListValue
> remote_devices_
;
322 // Used to derive devices' PSK. Used for the native experiment.
323 scoped_ptr
<proximity_auth::SecureMessageDelegate
> secure_message_delegate_
;
325 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGetRemoteDevicesFunction
);
328 class EasyUnlockPrivateGetSignInChallengeFunction
:
329 public AsyncExtensionFunction
{
331 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getSignInChallenge",
332 EASYUNLOCKPRIVATE_GETSIGNINCHALLENGE
)
333 EasyUnlockPrivateGetSignInChallengeFunction();
336 ~EasyUnlockPrivateGetSignInChallengeFunction() override
;
338 // AsyncExtensionFunction:
339 bool RunAsync() override
;
341 // Called when the challenge and the signed nonce have been generated.
342 void OnDone(const std::string
& challenge
, const std::string
& signed_nonce
);
344 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGetSignInChallengeFunction
);
347 class EasyUnlockPrivateTrySignInSecretFunction
:
348 public SyncExtensionFunction
{
350 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.trySignInSecret",
351 EASYUNLOCKPRIVATE_TRYSIGNINSECRET
)
352 EasyUnlockPrivateTrySignInSecretFunction();
355 ~EasyUnlockPrivateTrySignInSecretFunction() override
;
357 // SyncExtensionFunction:
358 bool RunSync() override
;
360 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateTrySignInSecretFunction
);
363 class EasyUnlockPrivateGetUserInfoFunction
: public SyncExtensionFunction
{
365 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getUserInfo",
366 EASYUNLOCKPRIVATE_GETUSERINFO
)
367 EasyUnlockPrivateGetUserInfoFunction();
370 ~EasyUnlockPrivateGetUserInfoFunction() override
;
372 // SyncExtensionFunction:
373 bool RunSync() override
;
375 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGetUserInfoFunction
);
378 class EasyUnlockPrivateGetConnectionInfoFunction
379 : public api::BluetoothExtensionFunction
{
381 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getConnectionInfo",
382 EASYUNLOCKPRIVATE_GETCONNECTIONINFO
)
383 EasyUnlockPrivateGetConnectionInfoFunction();
386 ~EasyUnlockPrivateGetConnectionInfoFunction() override
;
388 // BluetoothExtensionFunction:
389 bool DoWork(scoped_refptr
<device::BluetoothAdapter
> adapter
) override
;
391 void OnConnectionInfo(
392 const device::BluetoothDevice::ConnectionInfo
& connection_info
);
394 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGetConnectionInfoFunction
);
397 class EasyUnlockPrivateShowErrorBubbleFunction
: public SyncExtensionFunction
{
399 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.showErrorBubble",
400 EASYUNLOCKPRIVATE_SHOWERRORBUBBLE
)
401 EasyUnlockPrivateShowErrorBubbleFunction();
404 ~EasyUnlockPrivateShowErrorBubbleFunction() override
;
406 // SyncExtensionFunction:
407 bool RunSync() override
;
409 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateShowErrorBubbleFunction
);
412 class EasyUnlockPrivateHideErrorBubbleFunction
: public SyncExtensionFunction
{
414 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.hideErrorBubble",
415 EASYUNLOCKPRIVATE_HIDEERRORBUBBLE
)
416 EasyUnlockPrivateHideErrorBubbleFunction();
419 ~EasyUnlockPrivateHideErrorBubbleFunction() override
;
421 // SyncExtensionFunction:
422 bool RunSync() override
;
424 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateHideErrorBubbleFunction
);
427 class EasyUnlockPrivateSetAutoPairingResultFunction
428 : public SyncExtensionFunction
{
430 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setAutoPairingResult",
431 EASYUNLOCKPRIVATE_SETAUTOPAIRINGRESULT
)
432 EasyUnlockPrivateSetAutoPairingResultFunction();
435 ~EasyUnlockPrivateSetAutoPairingResultFunction() override
;
437 // SyncExtensionFunction:
438 bool RunSync() override
;
440 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateSetAutoPairingResultFunction
);
443 } // namespace extensions
445 #endif // CHROME_BROWSER_EXTENSIONS_API_EASY_UNLOCK_PRIVATE_EASY_UNLOCK_PRIVATE_API_H_