Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / easy_unlock_private / easy_unlock_private_api.h
blob31c15ca6a8c1b8ae1310654ac9df56204cc1fb17
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_
8 #include <string>
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.
20 namespace base {
21 class Timer;
24 namespace content {
25 class BrowserContext;
28 namespace cryptauth {
29 class ExternalDeviceInfo;
32 namespace proximity_auth {
33 class Connection;
34 class BluetoothLowEnergyConnectionFinder;
35 class BluetoothThrottler;
36 class SecureMessageDelegate;
39 namespace extensions {
41 class EasyUnlockPrivateConnectionManager;
42 class EasyUnlockPrivateCryptoDelegate;
44 class EasyUnlockPrivateAPI : public BrowserContextKeyedAPI {
45 public:
46 static BrowserContextKeyedAPIFactory<EasyUnlockPrivateAPI>*
47 GetFactoryInstance();
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();
60 private:
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 {
76 public:
77 EasyUnlockPrivateGetStringsFunction();
79 protected:
80 ~EasyUnlockPrivateGetStringsFunction() override;
82 // SyncExtensionFunction:
83 bool RunSync() override;
85 private:
86 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getStrings",
87 EASYUNLOCKPRIVATE_GETSTRINGS)
89 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGetStringsFunction);
92 class EasyUnlockPrivatePerformECDHKeyAgreementFunction
93 : public AsyncExtensionFunction {
94 public:
95 EasyUnlockPrivatePerformECDHKeyAgreementFunction();
97 protected:
98 ~EasyUnlockPrivatePerformECDHKeyAgreementFunction() override;
100 bool RunAsync() override;
102 private:
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 {
113 public:
114 EasyUnlockPrivateGenerateEcP256KeyPairFunction();
116 protected:
117 ~EasyUnlockPrivateGenerateEcP256KeyPairFunction() override;
119 bool RunAsync() override;
121 private:
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 {
133 public:
134 EasyUnlockPrivateCreateSecureMessageFunction();
136 protected:
137 ~EasyUnlockPrivateCreateSecureMessageFunction() override;
139 bool RunAsync() override;
141 private:
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 {
152 public:
153 EasyUnlockPrivateUnwrapSecureMessageFunction();
155 protected:
156 ~EasyUnlockPrivateUnwrapSecureMessageFunction() override;
158 bool RunAsync() override;
160 private:
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 {
171 public:
172 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.seekBluetoothDeviceByAddress",
173 EASYUNLOCKPRIVATE_SEEKBLUETOOTHDEVICEBYADDRESS)
174 EasyUnlockPrivateSeekBluetoothDeviceByAddressFunction();
176 private:
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 {
192 public:
193 DECLARE_EXTENSION_FUNCTION(
194 "easyUnlockPrivate.connectToBluetoothServiceInsecurely",
195 EASYUNLOCKPRIVATE_CONNECTTOBLUETOOTHSERVICEINSECURELY)
196 EasyUnlockPrivateConnectToBluetoothServiceInsecurelyFunction();
198 private:
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 {
211 public:
212 EasyUnlockPrivateUpdateScreenlockStateFunction();
214 protected:
215 ~EasyUnlockPrivateUpdateScreenlockStateFunction() override;
217 bool RunSync() override;
219 private:
220 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.updateScreenlockState",
221 EASYUNLOCKPRIVATE_UPDATESCREENLOCKSTATE)
223 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateUpdateScreenlockStateFunction);
226 class EasyUnlockPrivateSetPermitAccessFunction : public SyncExtensionFunction {
227 public:
228 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setPermitAccess",
229 EASYUNLOCKPRIVATE_SETPERMITACCESS)
230 EasyUnlockPrivateSetPermitAccessFunction();
232 private:
233 ~EasyUnlockPrivateSetPermitAccessFunction() override;
235 // SyncExtensionFunction:
236 bool RunSync() override;
238 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateSetPermitAccessFunction);
241 class EasyUnlockPrivateGetPermitAccessFunction : public SyncExtensionFunction {
242 public:
243 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getPermitAccess",
244 EASYUNLOCKPRIVATE_GETPERMITACCESS)
245 EasyUnlockPrivateGetPermitAccessFunction();
247 protected:
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);
255 private:
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 {
268 public:
269 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.clearPermitAccess",
270 EASYUNLOCKPRIVATE_CLEARPERMITACCESS)
271 EasyUnlockPrivateClearPermitAccessFunction();
273 private:
274 ~EasyUnlockPrivateClearPermitAccessFunction() override;
276 // SyncExtensionFunction:
277 bool RunSync() override;
279 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateClearPermitAccessFunction);
282 class EasyUnlockPrivateSetRemoteDevicesFunction : public SyncExtensionFunction {
283 public:
284 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setRemoteDevices",
285 EASYUNLOCKPRIVATE_SETREMOTEDEVICES)
286 EasyUnlockPrivateSetRemoteDevicesFunction();
288 private:
289 ~EasyUnlockPrivateSetRemoteDevicesFunction() override;
291 // SyncExtensionFunction:
292 bool RunSync() override;
294 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateSetRemoteDevicesFunction);
297 class EasyUnlockPrivateGetRemoteDevicesFunction
298 : public AsyncExtensionFunction {
299 public:
300 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getRemoteDevices",
301 EASYUNLOCKPRIVATE_GETREMOTEDEVICES)
302 EasyUnlockPrivateGetRemoteDevicesFunction();
304 protected:
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();
315 private:
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 {
344 public:
345 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getSignInChallenge",
346 EASYUNLOCKPRIVATE_GETSIGNINCHALLENGE)
347 EasyUnlockPrivateGetSignInChallengeFunction();
349 private:
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 {
363 public:
364 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.trySignInSecret",
365 EASYUNLOCKPRIVATE_TRYSIGNINSECRET)
366 EasyUnlockPrivateTrySignInSecretFunction();
368 private:
369 ~EasyUnlockPrivateTrySignInSecretFunction() override;
371 // SyncExtensionFunction:
372 bool RunSync() override;
374 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateTrySignInSecretFunction);
377 class EasyUnlockPrivateGetUserInfoFunction : public SyncExtensionFunction {
378 public:
379 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getUserInfo",
380 EASYUNLOCKPRIVATE_GETUSERINFO)
381 EasyUnlockPrivateGetUserInfoFunction();
383 private:
384 ~EasyUnlockPrivateGetUserInfoFunction() override;
386 // SyncExtensionFunction:
387 bool RunSync() override;
389 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateGetUserInfoFunction);
392 class EasyUnlockPrivateGetConnectionInfoFunction
393 : public api::BluetoothExtensionFunction {
394 public:
395 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.getConnectionInfo",
396 EASYUNLOCKPRIVATE_GETCONNECTIONINFO)
397 EasyUnlockPrivateGetConnectionInfoFunction();
399 private:
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 {
412 public:
413 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.showErrorBubble",
414 EASYUNLOCKPRIVATE_SHOWERRORBUBBLE)
415 EasyUnlockPrivateShowErrorBubbleFunction();
417 private:
418 ~EasyUnlockPrivateShowErrorBubbleFunction() override;
420 // SyncExtensionFunction:
421 bool RunSync() override;
423 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateShowErrorBubbleFunction);
426 class EasyUnlockPrivateHideErrorBubbleFunction : public SyncExtensionFunction {
427 public:
428 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.hideErrorBubble",
429 EASYUNLOCKPRIVATE_HIDEERRORBUBBLE)
430 EasyUnlockPrivateHideErrorBubbleFunction();
432 private:
433 ~EasyUnlockPrivateHideErrorBubbleFunction() override;
435 // SyncExtensionFunction:
436 bool RunSync() override;
438 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateHideErrorBubbleFunction);
441 class EasyUnlockPrivateSetAutoPairingResultFunction
442 : public SyncExtensionFunction {
443 public:
444 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setAutoPairingResult",
445 EASYUNLOCKPRIVATE_SETAUTOPAIRINGRESULT)
446 EasyUnlockPrivateSetAutoPairingResultFunction();
448 private:
449 ~EasyUnlockPrivateSetAutoPairingResultFunction() override;
451 // SyncExtensionFunction:
452 bool RunSync() override;
454 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateSetAutoPairingResultFunction);
457 class EasyUnlockPrivateFindSetupConnectionFunction
458 : public AsyncExtensionFunction {
459 public:
460 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.findSetupConnection",
461 EASYUNLOCKPRIVATE_FINDSETUPCONNECTION)
462 EasyUnlockPrivateFindSetupConnectionFunction();
464 private:
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>
479 connection_finder_;
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 {
492 public:
493 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setupConnectionStatus",
494 EASYUNLOCKPRIVATE_SETUPCONNECTIONSTATUS)
495 EasyUnlockPrivateSetupConnectionStatusFunction();
497 private:
498 ~EasyUnlockPrivateSetupConnectionStatusFunction() override;
500 // SyncExtensionFunction:
501 bool RunSync() override;
503 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateSetupConnectionStatusFunction);
506 class EasyUnlockPrivateSetupConnectionDisconnectFunction
507 : public SyncExtensionFunction {
508 public:
509 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setupConnectionDisconnect",
510 EASYUNLOCKPRIVATE_SETUPCONNECTIONDISCONNECT)
511 EasyUnlockPrivateSetupConnectionDisconnectFunction();
513 private:
514 ~EasyUnlockPrivateSetupConnectionDisconnectFunction() override;
516 // SyncExtensionFunction:
517 bool RunSync() override;
519 DISALLOW_COPY_AND_ASSIGN(EasyUnlockPrivateSetupConnectionDisconnectFunction);
522 class EasyUnlockPrivateSetupConnectionSendFunction
523 : public SyncExtensionFunction {
524 public:
525 DECLARE_EXTENSION_FUNCTION("easyUnlockPrivate.setupConnectionSend",
526 EASYUNLOCKPRIVATE_SETUPCONNECTIONSEND)
527 EasyUnlockPrivateSetupConnectionSendFunction();
529 private:
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_