chrome.gcdPrivate allows app to choose pairing method.
[chromium-blink-merge.git] / chrome / browser / ui / webui / local_discovery / local_discovery_ui_handler.h
blob69d7289f01a58ab85f80ef384cddb301dd674916
1 // Copyright 2013 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 CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/cancelable_callback.h"
13 #include "base/prefs/pref_member.h"
14 #include "chrome/browser/local_discovery/cloud_device_list.h"
15 #include "chrome/browser/local_discovery/cloud_print_printer_list.h"
16 #include "chrome/browser/local_discovery/privet_device_lister.h"
17 #include "chrome/browser/local_discovery/privet_http.h"
18 #include "chrome/browser/local_discovery/privetv3_setup_flow.h"
19 #include "components/signin/core/browser/signin_manager.h"
20 #include "content/public/browser/web_ui_message_handler.h"
22 #if defined(ENABLE_FULL_PRINTING) && !defined(OS_CHROMEOS)
23 #define CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
24 #endif
26 #if defined(ENABLE_WIFI_BOOTSTRAPPING)
27 #include "chrome/browser/local_discovery/wifi/bootstrapping_device_lister.h"
28 #include "chrome/browser/local_discovery/wifi/wifi_manager.h"
29 #endif
31 // TODO(noamsml): Factor out full registration flow into single class
32 namespace local_discovery {
34 class PrivetConfirmApiCallFlow;
35 class PrivetHTTPAsynchronousFactory;
36 class PrivetHTTPResolution;
37 class PrivetV1HTTPClient;
38 class ServiceDiscoverySharedClient;
40 // UI Handler for chrome://devices/
41 // It listens to local discovery notifications and passes those notifications
42 // into the Javascript to update the page.
43 class LocalDiscoveryUIHandler : public content::WebUIMessageHandler,
44 public PrivetRegisterOperation::Delegate,
45 public PrivetV3SetupFlow::Delegate,
46 public PrivetDeviceLister::Delegate,
47 public CloudDeviceListDelegate,
48 public SigninManagerBase::Observer {
49 public:
50 LocalDiscoveryUIHandler();
51 ~LocalDiscoveryUIHandler() override;
53 static bool GetHasVisible();
55 // WebUIMessageHandler implementation.
56 void RegisterMessages() override;
57 // PrivetRegisterOperation::Delegate implementation.
58 void OnPrivetRegisterClaimToken(PrivetRegisterOperation* operation,
59 const std::string& token,
60 const GURL& url) override;
61 void OnPrivetRegisterError(PrivetRegisterOperation* operation,
62 const std::string& action,
63 PrivetRegisterOperation::FailureReason reason,
64 int printer_http_code,
65 const base::DictionaryValue* json) override;
66 void OnPrivetRegisterDone(PrivetRegisterOperation* operation,
67 const std::string& device_id) override;
69 // PrivetV3SetupFlow::Delegate implementation.
70 scoped_ptr<GCDApiFlow> CreateApiFlow() override;
71 void GetWiFiCredentials(const CredentialsCallback& callback) override;
72 void SwitchToSetupWiFi(const ResultCallback& callback) override;
73 void CreatePrivetV3Client(const std::string& service_name,
74 const PrivetClientCallback& callback) override;
75 void ConfirmSecurityCode(const ResultCallback& callback) override;
76 void RestoreWifi(const ResultCallback& callback) override;
77 void OnSetupDone() override;
78 void OnSetupError() override;
80 // PrivetDeviceLister::Delegate implementation.
81 void DeviceChanged(bool added,
82 const std::string& name,
83 const DeviceDescription& description) override;
84 void DeviceRemoved(const std::string& name) override;
85 void DeviceCacheFlushed() override;
87 // CloudDeviceListDelegate implementation.
88 void OnDeviceListReady(const std::vector<Device>& devices) override;
89 void OnDeviceListUnavailable() override;
91 // SigninManagerBase::Observer implementation.
92 void GoogleSigninSucceeded(const std::string& account_id,
93 const std::string& username,
94 const std::string& password) override;
95 void GoogleSignedOut(const std::string& account_id,
96 const std::string& username) override;
98 private:
99 typedef std::map<std::string, DeviceDescription> DeviceDescriptionMap;
101 // Message handlers:
102 // For when the page is ready to receive device notifications.
103 void HandleStart(const base::ListValue* args);
105 // For when a visibility change occurs.
106 void HandleIsVisible(const base::ListValue* args);
108 // For when a user choice is made.
109 void HandleRegisterDevice(const base::ListValue* args);
111 // For when a code is confirmed.
112 void HandleConfirmCode(const base::ListValue* args);
114 // For when a cancellation is made.
115 void HandleCancelRegistration(const base::ListValue* args);
117 // For requesting the device list.
118 void HandleRequestDeviceList(const base::ListValue* args);
120 // For opening URLs (relative to the Google Cloud Print base URL) in a new
121 // tab.
122 void HandleOpenCloudPrintURL(const base::ListValue* args);
124 // For showing sync login UI.
125 void HandleShowSyncUI(const base::ListValue* args);
127 // For when the IP address of the printer has been resolved for registration.
128 void StartRegisterHTTP(scoped_ptr<PrivetHTTPClient> http_client);
130 // For when the confirm operation on the cloudprint server has finished
131 // executing.
132 void OnConfirmDone(GCDApiFlow::Status status);
134 // Signal to the web interface an error has ocurred while registering.
135 void SendRegisterError();
137 // Singal to the web interface that registration has finished.
138 void SendRegisterDone(const std::string& service_name);
140 // Set the visibility of the page.
141 void SetIsVisible(bool visible);
143 // Get the sync account email.
144 std::string GetSyncAccount();
146 // Reset and cancel the current registration.
147 void ResetCurrentRegistration();
149 // Creates |PrivetV3HTTPClient| privet from |PrivetHTTPClient| and calls
150 // callback.
151 void PrivetClientToV3(const PrivetClientCallback& callback,
152 scoped_ptr<PrivetHTTPClient> client);
154 // Announcement hasn't been sent for a certain time after registration
155 // finished. Consider it failed.
156 // TODO(noamsml): Re-resolve service first.
157 void OnAnnouncementTimeoutReached();
159 void CheckUserLoggedIn();
161 void CheckListingDone();
163 bool IsUserSupervisedOrOffTheRecord();
165 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
166 void StartCloudPrintConnector();
167 void OnCloudPrintPrefsChanged();
168 void ShowCloudPrintSetupDialog(const base::ListValue* args);
169 void HandleDisableCloudPrintConnector(const base::ListValue* args);
170 void SetupCloudPrintConnectorSection();
171 void RemoveCloudPrintConnectorSection();
172 void RefreshCloudPrintStatusFromService();
173 #endif
175 #if defined(ENABLE_WIFI_BOOTSTRAPPING)
176 void StartWifiBootstrapping();
177 void OnBootstrappingDeviceChanged(
178 bool available,
179 const wifi::BootstrappingDeviceDescription& description);
180 #endif
182 // A map of current device descriptions provided by the PrivetDeviceLister.
183 DeviceDescriptionMap device_descriptions_;
185 // The service discovery client used listen for devices on the local network.
186 scoped_refptr<ServiceDiscoverySharedClient> service_discovery_client_;
188 // A factory for creating the privet HTTP Client.
189 scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_;
191 // An object representing the resolution process for the privet_http_factory.
192 scoped_ptr<PrivetHTTPResolution> privet_resolution_;
194 // The current HTTP client (used for the current operation).
195 scoped_ptr<PrivetV1HTTPClient> current_http_client_;
197 // The current register operation. Only one allowed at any time.
198 scoped_ptr<PrivetRegisterOperation> current_register_operation_;
200 // The current Privet v3 setup operation. Only one allowed at any time.
201 scoped_ptr<PrivetV3SetupFlow> current_setup_operation_;
203 // The current confirm call used during the registration flow.
204 scoped_ptr<GCDApiFlow> confirm_api_call_flow_;
206 // The device lister used to list devices on the local network.
207 scoped_ptr<PrivetDeviceLister> privet_lister_;
209 // Whether or not the page is marked as visible.
210 bool is_visible_;
212 // List of printers from cloud print.
213 scoped_ptr<GCDApiFlow> cloud_print_printer_list_;
214 scoped_ptr<GCDApiFlow> cloud_device_list_;
215 std::vector<Device> cloud_devices_;
216 int failed_list_count_;
217 int succeded_list_count_;
218 ResultCallback device_code_callback_;
220 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
221 StringPrefMember cloud_print_connector_email_;
222 BooleanPrefMember cloud_print_connector_enabled_;
223 bool cloud_print_connector_ui_enabled_;
224 #endif
226 #if defined(ENABLE_WIFI_BOOTSTRAPPING)
227 scoped_ptr<wifi::WifiManager> wifi_manager_;
228 scoped_ptr<wifi::BootstrappingDeviceLister> bootstrapping_device_lister_;
229 #endif
231 DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUIHandler);
234 #undef CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
236 } // namespace local_discovery
237 #endif // CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_