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/cryptauth/cryptauth_gcm_manager.h"
14 #include "components/proximity_auth/logging/log_buffer.h"
15 #include "components/proximity_auth/webui/proximity_auth_ui_delegate.h"
16 #include "content/public/browser/web_ui_message_handler.h"
22 namespace proximity_auth
{
24 // Handles messages from the chrome://proximity-auth page.
25 class ProximityAuthWebUIHandler
: public content::WebUIMessageHandler
,
26 public LogBuffer::Observer
,
27 public CryptAuthEnrollmentManager::Observer
,
28 public CryptAuthDeviceManager::Observer
{
30 // |delegate| is not owned and must outlive this instance.
31 explicit ProximityAuthWebUIHandler(ProximityAuthUIDelegate
* delegate
);
32 ~ProximityAuthWebUIHandler() override
;
34 // content::WebUIMessageHandler:
35 void RegisterMessages() override
;
38 // LogBuffer::Observer:
39 void OnLogMessageAdded(const LogBuffer::LogMessage
& log_message
) override
;
40 void OnLogBufferCleared() override
;
42 // CryptAuthEnrollmentManager::Observer:
43 void OnEnrollmentStarted() override
;
44 void OnEnrollmentFinished(bool success
) override
;
46 // CryptAuthDeviceManager::Observer:
47 void OnSyncStarted() override
;
49 CryptAuthDeviceManager::SyncResult sync_result
,
50 CryptAuthDeviceManager::DeviceChangeResult device_change_result
) override
;
52 // Message handler callbacks.
53 void GetLogMessages(const base::ListValue
* args
);
54 void ClearLogBuffer(const base::ListValue
* args
);
55 void FindEligibleUnlockDevices(const base::ListValue
* args
);
56 void GetSyncStates(const base::ListValue
* args
);
57 void ForceEnrollment(const base::ListValue
* args
);
58 void ForceDeviceSync(const base::ListValue
* args
);
60 // Initializes CryptAuth managers, used for development purposes.
61 void InitGCMManager();
62 void InitEnrollmentManager();
63 void InitDeviceManager();
65 // Called when a CryptAuth request fails.
66 void OnCryptAuthClientError(const std::string
& error_message
);
68 // Called when the findEligibleUnlockDevices request succeeds.
69 void OnFoundEligibleUnlockDevices(
70 const cryptauth::FindEligibleUnlockDevicesResponse
& response
);
72 // Returns the current enrollment state that can be used as a JSON object.
73 scoped_ptr
<base::DictionaryValue
> GetEnrollmentStateDictionary();
75 // Returns the current device sync state that can be used as a JSON object.
76 scoped_ptr
<base::DictionaryValue
> GetDeviceSyncStateDictionary();
78 // The delegate used to fetch dependencies. Must outlive this instance.
79 ProximityAuthUIDelegate
* delegate_
;
81 // Creates CryptAuth client instances to make API calls.
82 scoped_ptr
<CryptAuthClientFactory
> cryptauth_client_factory_
;
84 // We only support one concurrent API call.
85 scoped_ptr
<CryptAuthClient
> cryptauth_client_
;
87 // TODO(tengs): These members are temporarily used for development.
88 scoped_ptr
<PrefService
> pref_service
;
89 scoped_ptr
<CryptAuthGCMManager
> gcm_manager_
;
90 scoped_ptr
<CryptAuthEnrollmentManager
> enrollment_manager_
;
91 scoped_ptr
<CryptAuthDeviceManager
> device_manager_
;
93 base::WeakPtrFactory
<ProximityAuthWebUIHandler
> weak_ptr_factory_
;
95 DISALLOW_COPY_AND_ASSIGN(ProximityAuthWebUIHandler
);
98 } // namespace proximity_auth
100 #endif // COMPONENTS_PROXIMITY_AUTH_WEBUI_PROXIMITY_AUTH_WEBUI_HANDLER_H_