1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_PROXIMITY_AUTH_WEBUI_PROXIMITY_AUTH_WEBUI_HANDLER_H_
#define COMPONENTS_PROXIMITY_AUTH_WEBUI_PROXIMITY_AUTH_WEBUI_HANDLER_H_
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "components/cryptauth/connection_observer.h"
#include "components/cryptauth/cryptauth_client.h"
#include "components/cryptauth/cryptauth_device_manager.h"
#include "components/cryptauth/cryptauth_enrollment_manager.h"
#include "components/cryptauth/cryptauth_gcm_manager.h"
#include "components/proximity_auth/authenticator.h"
#include "components/proximity_auth/logging/log_buffer.h"
#include "components/proximity_auth/messenger_observer.h"
#include "components/proximity_auth/proximity_auth_client.h"
#include "components/proximity_auth/remote_device_life_cycle.h"
#include "content/public/browser/web_ui_message_handler.h"
namespace base {
class ListValue;
}
namespace cryptauth {
class ExternalDeviceInfo;
}
namespace proximity_auth {
class ReachablePhoneFlow;
class RemoteDeviceLifeCycle;
class RemoteDeviceLoader;
struct RemoteStatusUpdate;
// Handles messages from the chrome://proximity-auth page.
class ProximityAuthWebUIHandler
: public content::WebUIMessageHandler,
public LogBuffer::Observer,
public cryptauth::CryptAuthEnrollmentManager::Observer,
public cryptauth::CryptAuthDeviceManager::Observer,
public RemoteDeviceLifeCycle::Observer,
public MessengerObserver {
public:
// |client_| is not owned and must outlive this instance.
explicit ProximityAuthWebUIHandler(
ProximityAuthClient* proximity_auth_client);
~ProximityAuthWebUIHandler() override;
// content::WebUIMessageHandler:
void RegisterMessages() override;
private:
// LogBuffer::Observer:
void OnLogMessageAdded(const LogBuffer::LogMessage& log_message) override;
void OnLogBufferCleared() override;
// CryptAuthEnrollmentManager::Observer:
void OnEnrollmentStarted() override;
void OnEnrollmentFinished(bool success) override;
// CryptAuthDeviceManager::Observer:
void OnSyncStarted() override;
void OnSyncFinished(cryptauth::CryptAuthDeviceManager::SyncResult sync_result,
cryptauth::CryptAuthDeviceManager::DeviceChangeResult
device_change_result) override;
// Message handler callbacks.
void OnWebContentsInitialized(const base::ListValue* args);
void GetLogMessages(const base::ListValue* args);
void ClearLogBuffer(const base::ListValue* args);
void ToggleUnlockKey(const base::ListValue* args);
void FindEligibleUnlockDevices(const base::ListValue* args);
void FindReachableDevices(const base::ListValue* args);
void GetLocalState(const base::ListValue* args);
void ForceEnrollment(const base::ListValue* args);
void ForceDeviceSync(const base::ListValue* args);
void ToggleConnection(const base::ListValue* args);
// Initializes CryptAuth managers, used for development purposes.
void InitGCMManager();
void InitEnrollmentManager();
void InitDeviceManager();
// Called when a CryptAuth request fails.
void OnCryptAuthClientError(const std::string& error_message);
// Called when the toggleUnlock request succeeds.
void OnEasyUnlockToggled(const cryptauth::ToggleEasyUnlockResponse& response);
// Called when the findEligibleUnlockDevices request succeeds.
void OnFoundEligibleUnlockDevices(
const cryptauth::FindEligibleUnlockDevicesResponse& response);
// Callback when |reachable_phone_flow_| completes.
void OnReachablePhonesFound(
const std::vector<cryptauth::ExternalDeviceInfo>& reachable_phones);
// Called when the RemoteDevice is loaded so we can create a connection.
void OnRemoteDevicesLoaded(
const std::vector<cryptauth::RemoteDevice>& remote_devices);
// Converts an ExternalDeviceInfo proto to a JSON dictionary used in
// JavaScript.
std::unique_ptr<base::DictionaryValue> ExternalDeviceInfoToDictionary(
const cryptauth::ExternalDeviceInfo& device_info);
// Converts an IneligibleDevice proto to a JSON dictionary used in JavaScript.
std::unique_ptr<base::DictionaryValue> IneligibleDeviceToDictionary(
const cryptauth::IneligibleDevice& ineligible_device);
// Cleans up the connection to the selected remote device.
void CleanUpRemoteDeviceLifeCycle();
// RemoteDeviceLifeCycle::Observer:
void OnLifeCycleStateChanged(RemoteDeviceLifeCycle::State old_state,
RemoteDeviceLifeCycle::State new_state) override;
// MessengerObserver:
void OnRemoteStatusUpdate(const RemoteStatusUpdate& status_update) override;
// Returns the current enrollment state that can be used as a JSON object.
std::unique_ptr<base::DictionaryValue> GetEnrollmentStateDictionary();
// Returns the current device sync state that can be used as a JSON object.
std::unique_ptr<base::DictionaryValue> GetDeviceSyncStateDictionary();
// Returns the current unlock keys that can be used as a JSON object.
std::unique_ptr<base::ListValue> GetUnlockKeysList();
// The delegate used to fetch dependencies. Must outlive this instance.
ProximityAuthClient* proximity_auth_client_;
// Creates CryptAuth client instances to make API calls.
std::unique_ptr<cryptauth::CryptAuthClientFactory> cryptauth_client_factory_;
// We only support one concurrent API call.
std::unique_ptr<cryptauth::CryptAuthClient> cryptauth_client_;
// The flow for getting a list of reachable phones.
std::unique_ptr<ReachablePhoneFlow> reachable_phone_flow_;
// True if we get a message from the loaded WebContents to know that it is
// initialized, and we can inject JavaScript.
bool web_contents_initialized_;
// Member variables for connecting to and authenticating the remote device.
// TODO(tengs): Support multiple simultaenous connections.
std::unique_ptr<RemoteDeviceLoader> remote_device_loader_;
cryptauth::RemoteDevice selected_remote_device_;
std::unique_ptr<RemoteDeviceLifeCycle> life_cycle_;
std::unique_ptr<RemoteStatusUpdate> last_remote_status_update_;
base::WeakPtrFactory<ProximityAuthWebUIHandler> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(ProximityAuthWebUIHandler);
};
} // namespace proximity_auth
#endif // COMPONENTS_PROXIMITY_AUTH_WEBUI_PROXIMITY_AUTH_WEBUI_HANDLER_H_
|