1 // Copyright 2015 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 COMPONENTS_PROXIMITY_AUTH_WEBUI_PROXIMITY_AUTH_WEBUI_HANDLER_H_
6 #define COMPONENTS_PROXIMITY_AUTH_WEBUI_PROXIMITY_AUTH_WEBUI_HANDLER_H_
8 #include "base/memory/weak_ptr.h"
9 #include "base/values.h"
10 #include "components/proximity_auth/authenticator.h"
11 #include "components/proximity_auth/client_observer.h"
12 #include "components/proximity_auth/connection_observer.h"
13 #include "components/proximity_auth/cryptauth/cryptauth_client.h"
14 #include "components/proximity_auth/cryptauth/cryptauth_device_manager.h"
15 #include "components/proximity_auth/cryptauth/cryptauth_enrollment_manager.h"
16 #include "components/proximity_auth/cryptauth/cryptauth_gcm_manager.h"
17 #include "components/proximity_auth/logging/log_buffer.h"
18 #include "components/proximity_auth/proximity_auth_client.h"
19 #include "content/public/browser/web_ui_message_handler.h"
26 class ExternalDeviceInfo
;
29 namespace proximity_auth
{
32 class BluetoothConnection
;
33 class BluetoothThrottler
;
34 class BluetoothLowEnergyDeviceWhitelist
;
36 class ConnectionFinder
;
38 class ReachablePhoneFlow
;
39 struct RemoteStatusUpdate
;
42 // Handles messages from the chrome://proximity-auth page.
43 class ProximityAuthWebUIHandler
: public content::WebUIMessageHandler
,
44 public LogBuffer::Observer
,
45 public CryptAuthEnrollmentManager::Observer
,
46 public CryptAuthDeviceManager::Observer
,
47 public ConnectionObserver
,
48 public ClientObserver
{
50 // |client_| is not owned and must outlive this instance.
51 explicit ProximityAuthWebUIHandler(
52 ProximityAuthClient
* proximity_auth_client
);
53 ~ProximityAuthWebUIHandler() override
;
55 // content::WebUIMessageHandler:
56 void RegisterMessages() override
;
59 // LogBuffer::Observer:
60 void OnLogMessageAdded(const LogBuffer::LogMessage
& log_message
) override
;
61 void OnLogBufferCleared() override
;
63 // CryptAuthEnrollmentManager::Observer:
64 void OnEnrollmentStarted() override
;
65 void OnEnrollmentFinished(bool success
) override
;
67 // CryptAuthDeviceManager::Observer:
68 void OnSyncStarted() override
;
70 CryptAuthDeviceManager::SyncResult sync_result
,
71 CryptAuthDeviceManager::DeviceChangeResult device_change_result
) override
;
73 // Message handler callbacks.
74 void OnWebContentsInitialized(const base::ListValue
* args
);
75 void GetLogMessages(const base::ListValue
* args
);
76 void ClearLogBuffer(const base::ListValue
* args
);
77 void ToggleUnlockKey(const base::ListValue
* args
);
78 void FindEligibleUnlockDevices(const base::ListValue
* args
);
79 void FindReachableDevices(const base::ListValue
* args
);
80 void GetLocalState(const base::ListValue
* args
);
81 void ForceEnrollment(const base::ListValue
* args
);
82 void ForceDeviceSync(const base::ListValue
* args
);
83 void ToggleConnection(const base::ListValue
* args
);
85 // Initializes CryptAuth managers, used for development purposes.
86 void InitGCMManager();
87 void InitEnrollmentManager();
88 void InitDeviceManager();
90 // Called when a CryptAuth request fails.
91 void OnCryptAuthClientError(const std::string
& error_message
);
93 // Called when the toggleUnlock request succeeds.
94 void OnEasyUnlockToggled(const cryptauth::ToggleEasyUnlockResponse
& response
);
96 // Called when the findEligibleUnlockDevices request succeeds.
97 void OnFoundEligibleUnlockDevices(
98 const cryptauth::FindEligibleUnlockDevicesResponse
& response
);
100 // Callback when |reachable_phone_flow_| completes.
101 void OnReachablePhonesFound(
102 const std::vector
<cryptauth::ExternalDeviceInfo
>& reachable_phones
);
104 // Called when the key agreement of PSK of the remote device completes.
105 void OnPSKDerived(const cryptauth::ExternalDeviceInfo
& unlock_key
,
106 const std::string
& persistent_symmetric_key
);
108 // Tries to create a classic Bluetooth connection to the unlock key.
109 void FindBluetoothClassicConnection(const RemoteDevice
& remote_device
);
111 // Tries to create a Bluetooth Low Energy connection to the unlock key.
112 void FindBluetoothLowEnergyConnection(const RemoteDevice
& remote_device
);
114 // Called when |connection_finder_| finds a connection.
115 void OnConnectionFound(scoped_ptr
<Connection
> connection
);
117 // Callback when |authenticator_| completes authentication.
118 void OnAuthenticationResult(Authenticator::Result result
,
119 scoped_ptr
<SecureContext
> secure_context
);
121 // Creates the client which parses status updates.
122 void CreateStatusUpdateClient();
124 // Returns the active connection, whether it's owned the |this| instance or
126 Connection
* GetConnection();
128 // Converts an ExternalDeviceInfo proto to a JSON dictionary used in
130 scoped_ptr
<base::DictionaryValue
> ExternalDeviceInfoToDictionary(
131 const cryptauth::ExternalDeviceInfo
& device_info
);
133 // Converts an IneligibleDevice proto to a JSON dictionary used in JavaScript.
134 scoped_ptr
<base::DictionaryValue
> IneligibleDeviceToDictionary(
135 const cryptauth::IneligibleDevice
& ineligible_device
);
137 // ConnectionObserver:
138 void OnConnectionStatusChanged(Connection
* connection
,
139 Connection::Status old_status
,
140 Connection::Status new_status
) override
;
141 void OnMessageReceived(const Connection
& connection
,
142 const WireMessage
& message
) override
;
145 void OnRemoteStatusUpdate(const RemoteStatusUpdate
& status_update
) override
;
147 // Returns the current enrollment state that can be used as a JSON object.
148 scoped_ptr
<base::DictionaryValue
> GetEnrollmentStateDictionary();
150 // Returns the current device sync state that can be used as a JSON object.
151 scoped_ptr
<base::DictionaryValue
> GetDeviceSyncStateDictionary();
153 // Returns the current unlock keys that can be used as a JSON object.
154 scoped_ptr
<base::ListValue
> GetUnlockKeysList();
156 // The delegate used to fetch dependencies. Must outlive this instance.
157 ProximityAuthClient
* proximity_auth_client_
;
159 // Creates CryptAuth client instances to make API calls.
160 scoped_ptr
<CryptAuthClientFactory
> cryptauth_client_factory_
;
162 // We only support one concurrent API call.
163 scoped_ptr
<CryptAuthClient
> cryptauth_client_
;
165 // The flow for getting a list of reachable phones.
166 scoped_ptr
<ReachablePhoneFlow
> reachable_phone_flow_
;
168 // True if we get a message from the loaded WebContents to know that it is
169 // initialized, and we can inject JavaScript.
170 bool web_contents_initialized_
;
172 // Member variables for connecting to and authenticating the remote device.
173 // TODO(tengs): Support multiple simultaenous connections.
174 scoped_ptr
<SecureMessageDelegate
> secure_message_delegate_
;
175 scoped_ptr
<BluetoothLowEnergyDeviceWhitelist
> ble_device_whitelist_
;
176 RemoteDevice selected_remote_device_
;
177 scoped_ptr
<BluetoothThrottler
> bluetooth_throttler_
;
178 scoped_ptr
<ConnectionFinder
> connection_finder_
;
179 scoped_ptr
<Connection
> connection_
;
180 scoped_ptr
<Authenticator
> authenticator_
;
181 scoped_ptr
<SecureContext
> secure_context_
;
182 scoped_ptr
<ClientImpl
> client_
;
183 scoped_ptr
<RemoteStatusUpdate
> last_remote_status_update_
;
185 base::WeakPtrFactory
<ProximityAuthWebUIHandler
> weak_ptr_factory_
;
187 DISALLOW_COPY_AND_ASSIGN(ProximityAuthWebUIHandler
);
190 } // namespace proximity_auth
192 #endif // COMPONENTS_PROXIMITY_AUTH_WEBUI_PROXIMITY_AUTH_WEBUI_HANDLER_H_