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/webui/proximity_auth_ui_delegate.h"
19 #include "content/public/browser/web_ui_message_handler.h"
26 class ExternalDeviceInfo
;
29 namespace proximity_auth
{
32 class BluetoothConnection
;
35 class ReachablePhoneFlow
;
36 struct RemoteStatusUpdate
;
39 // Handles messages from the chrome://proximity-auth page.
40 class ProximityAuthWebUIHandler
: public content::WebUIMessageHandler
,
41 public LogBuffer::Observer
,
42 public CryptAuthEnrollmentManager::Observer
,
43 public CryptAuthDeviceManager::Observer
,
44 public ConnectionObserver
,
45 public ClientObserver
{
47 // |delegate| is not owned and must outlive this instance.
48 explicit ProximityAuthWebUIHandler(ProximityAuthUIDelegate
* delegate
);
49 ~ProximityAuthWebUIHandler() override
;
51 // content::WebUIMessageHandler:
52 void RegisterMessages() override
;
55 // LogBuffer::Observer:
56 void OnLogMessageAdded(const LogBuffer::LogMessage
& log_message
) override
;
57 void OnLogBufferCleared() override
;
59 // CryptAuthEnrollmentManager::Observer:
60 void OnEnrollmentStarted() override
;
61 void OnEnrollmentFinished(bool success
) override
;
63 // CryptAuthDeviceManager::Observer:
64 void OnSyncStarted() override
;
66 CryptAuthDeviceManager::SyncResult sync_result
,
67 CryptAuthDeviceManager::DeviceChangeResult device_change_result
) override
;
69 // Message handler callbacks.
70 void GetLogMessages(const base::ListValue
* args
);
71 void ClearLogBuffer(const base::ListValue
* args
);
72 void FindEligibleUnlockDevices(const base::ListValue
* args
);
73 void FindReachableDevices(const base::ListValue
* args
);
74 void GetLocalState(const base::ListValue
* args
);
75 void ForceEnrollment(const base::ListValue
* args
);
76 void ForceDeviceSync(const base::ListValue
* args
);
77 void ToggleConnection(const base::ListValue
* args
);
79 // Initializes CryptAuth managers, used for development purposes.
80 void InitGCMManager();
81 void InitEnrollmentManager();
82 void InitDeviceManager();
84 // Called when a CryptAuth request fails.
85 void OnCryptAuthClientError(const std::string
& error_message
);
87 // Called when the findEligibleUnlockDevices request succeeds.
88 void OnFoundEligibleUnlockDevices(
89 const cryptauth::FindEligibleUnlockDevicesResponse
& response
);
91 // Callback when |reachable_phone_flow_| completes.
92 void OnReachablePhonesFound(
93 const std::vector
<cryptauth::ExternalDeviceInfo
>& reachable_phones
);
95 // Called when the key agreement of PSK of the remote device completes.
96 void OnPSKDerived(const cryptauth::ExternalDeviceInfo
& unlock_key
,
97 const std::string
& persistent_symmetric_key
);
99 // Callbacks for bluetooth_util::SeekDeviceByAddress().
100 void OnSeekedDeviceByAddress();
101 void OnSeekedDeviceByAddressError(const std::string
& error_message
);
103 // Callback when |authenticator_| completes authentication.
104 void OnAuthenticationResult(Authenticator::Result result
,
105 scoped_ptr
<SecureContext
> secure_context
);
107 // Creates the client which parses status updates.
108 void CreateStatusUpdateClient();
110 // Returns the active connection, whether it's owned the |this| instance or
112 Connection
* GetConnection();
114 // Converts an ExternalDeviceInfo proto to a JSON dictionary used in
116 scoped_ptr
<base::DictionaryValue
> ExternalDeviceInfoToDictionary(
117 const cryptauth::ExternalDeviceInfo
& device_info
);
119 // Converts an IneligibleDevice proto to a JSON dictionary used in JavaScript.
120 scoped_ptr
<base::DictionaryValue
> IneligibleDeviceToDictionary(
121 const cryptauth::IneligibleDevice
& ineligible_device
);
123 // ConnectionObserver:
124 void OnConnectionStatusChanged(Connection
* connection
,
125 Connection::Status old_status
,
126 Connection::Status new_status
) override
;
127 void OnMessageReceived(const Connection
& connection
,
128 const WireMessage
& message
) override
;
131 void OnRemoteStatusUpdate(const RemoteStatusUpdate
& status_update
) override
;
133 // Returns the current enrollment state that can be used as a JSON object.
134 scoped_ptr
<base::DictionaryValue
> GetEnrollmentStateDictionary();
136 // Returns the current device sync state that can be used as a JSON object.
137 scoped_ptr
<base::DictionaryValue
> GetDeviceSyncStateDictionary();
139 // Returns the current unlock keys that can be used as a JSON object.
140 scoped_ptr
<base::ListValue
> GetUnlockKeysList();
142 // The delegate used to fetch dependencies. Must outlive this instance.
143 ProximityAuthUIDelegate
* delegate_
;
145 // Creates CryptAuth client instances to make API calls.
146 scoped_ptr
<CryptAuthClientFactory
> cryptauth_client_factory_
;
148 // We only support one concurrent API call.
149 scoped_ptr
<CryptAuthClient
> cryptauth_client_
;
151 // The flow for getting a list of reachable phones.
152 scoped_ptr
<ReachablePhoneFlow
> reachable_phone_flow_
;
154 // True if the WebContents backing the WebUI has been initialized.
155 bool web_contents_initialized_
;
157 // Member variables related to CryptAuth debugging.
158 // TODO(tengs): These members are temporarily used for development.
159 scoped_ptr
<PrefService
> pref_service
;
160 scoped_ptr
<CryptAuthGCMManager
> gcm_manager_
;
161 scoped_ptr
<CryptAuthEnrollmentManager
> enrollment_manager_
;
162 scoped_ptr
<CryptAuthDeviceManager
> device_manager_
;
163 std::string user_public_key_
;
164 std::string user_private_key_
;
166 // Member variables for connecting to and authenticating the remote device.
167 // TODO(tengs): Support multiple simultaenous connections.
168 scoped_ptr
<SecureMessageDelegate
> secure_message_delegate_
;
169 scoped_ptr
<BluetoothConnection
> bluetooth_connection_
;
170 scoped_ptr
<Authenticator
> authenticator_
;
171 scoped_ptr
<SecureContext
> secure_context_
;
172 scoped_ptr
<ClientImpl
> client_
;
173 scoped_ptr
<RemoteStatusUpdate
> last_remote_status_update_
;
175 base::WeakPtrFactory
<ProximityAuthWebUIHandler
> weak_ptr_factory_
;
177 DISALLOW_COPY_AND_ASSIGN(ProximityAuthWebUIHandler
);
180 } // namespace proximity_auth
182 #endif // COMPONENTS_PROXIMITY_AUTH_WEBUI_PROXIMITY_AUTH_WEBUI_HANDLER_H_