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/cryptauth/cryptauth_client.h"
11 #include "components/proximity_auth/cryptauth/cryptauth_device_manager.h"
12 #include "components/proximity_auth/cryptauth/cryptauth_enrollment_manager.h"
13 #include "components/proximity_auth/logging/log_buffer.h"
14 #include "components/proximity_auth/webui/proximity_auth_ui_delegate.h"
15 #include "content/public/browser/web_ui_message_handler.h"
21 namespace proximity_auth
{
23 // Handles messages from the chrome://proximity-auth page.
24 class ProximityAuthWebUIHandler
: public content::WebUIMessageHandler
,
25 public LogBuffer::Observer
,
26 public CryptAuthEnrollmentManager::Observer
,
27 public CryptAuthDeviceManager::Observer
{
29 // |delegate| is not owned and must outlive this instance.
30 explicit ProximityAuthWebUIHandler(ProximityAuthUIDelegate
* delegate
);
31 ~ProximityAuthWebUIHandler() override
;
33 // content::WebUIMessageHandler:
34 void RegisterMessages() override
;
37 // LogBuffer::Observer:
38 void OnLogMessageAdded(const LogBuffer::LogMessage
& log_message
) override
;
39 void OnLogBufferCleared() override
;
41 // CryptAuthEnrollmentManager::Observer:
42 void OnEnrollmentStarted() override
;
43 void OnEnrollmentFinished(bool success
) override
;
45 // CryptAuthDeviceManager::Observer:
46 void OnSyncStarted() override
;
48 CryptAuthDeviceManager::SyncResult sync_result
,
49 CryptAuthDeviceManager::DeviceChangeResult device_change_result
) override
;
51 // Message handler callbacks.
52 void GetLogMessages(const base::ListValue
* args
);
53 void ClearLogBuffer(const base::ListValue
* args
);
54 void FindEligibleUnlockDevices(const base::ListValue
* args
);
55 void GetSyncStates(const base::ListValue
* args
);
56 void ForceEnrollment(const base::ListValue
* args
);
57 void ForceDeviceSync(const base::ListValue
* args
);
59 // Initializes CryptAuth managers, used for development purposes.
60 void InitEnrollmentManager();
61 void InitDeviceManager();
63 // Called when a CryptAuth request fails.
64 void OnCryptAuthClientError(const std::string
& error_message
);
66 // Called when the findEligibleUnlockDevices request succeeds.
67 void OnFoundEligibleUnlockDevices(
68 const cryptauth::FindEligibleUnlockDevicesResponse
& response
);
70 // Returns the current enrollment state that can be used as a JSON object.
71 scoped_ptr
<base::DictionaryValue
> GetEnrollmentStateDictionary();
73 // Returns the current device sync state that can be used as a JSON object.
74 scoped_ptr
<base::DictionaryValue
> GetDeviceSyncStateDictionary();
76 // The delegate used to fetch dependencies. Must outlive this instance.
77 ProximityAuthUIDelegate
* delegate_
;
79 // Creates CryptAuth client instances to make API calls.
80 scoped_ptr
<CryptAuthClientFactory
> cryptauth_client_factory_
;
82 // We only support one concurrent API call.
83 scoped_ptr
<CryptAuthClient
> cryptauth_client_
;
85 // TODO(tengs): These members are temporarily used for development.
86 scoped_ptr
<PrefService
> pref_service
;
87 scoped_ptr
<CryptAuthEnrollmentManager
> enrollment_manager_
;
88 scoped_ptr
<CryptAuthDeviceManager
> device_manager_
;
90 base::WeakPtrFactory
<ProximityAuthWebUIHandler
> weak_ptr_factory_
;
92 DISALLOW_COPY_AND_ASSIGN(ProximityAuthWebUIHandler
);
95 } // namespace proximity_auth
97 #endif // COMPONENTS_PROXIMITY_AUTH_WEBUI_PROXIMITY_AUTH_WEBUI_HANDLER_H_