local_discovery_ui_handler.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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. 4 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_ 7 8#include <map> 9#include <string> 10#include <vector> 11 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" 21 22#if defined(ENABLE_FULL_PRINTING) && !defined(OS_CHROMEOS) 23#define CLOUD_PRINT_CONNECTOR_UI_AVAILABLE 24#endif 25 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 30 31// TODO(noamsml): Factor out full registration flow into single class 32namespace local_discovery { 33 34class PrivetConfirmApiCallFlow; 35class PrivetHTTPAsynchronousFactory; 36class PrivetHTTPResolution; 37class PrivetV1HTTPClient; 38class ServiceDiscoverySharedClient; 39 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. 43class 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 virtual ~LocalDiscoveryUIHandler(); 52 53 static bool GetHasVisible(); 54 55 // WebUIMessageHandler implementation. 56 virtual void RegisterMessages() OVERRIDE; 57 // PrivetRegisterOperation::Delegate implementation. 58 virtual void OnPrivetRegisterClaimToken( 59 PrivetRegisterOperation* operation, 60 const std::string& token, 61 const GURL& url) OVERRIDE; 62 virtual void OnPrivetRegisterError( 63 PrivetRegisterOperation* operation, 64 const std::string& action, 65 PrivetRegisterOperation::FailureReason reason, 66 int printer_http_code, 67 const base::DictionaryValue* json) OVERRIDE; 68 virtual void OnPrivetRegisterDone( 69 PrivetRegisterOperation* operation, 70 const std::string& device_id) OVERRIDE; 71 72 // PrivetV3SetupFlow::Delegate implementation. 73 virtual scoped_ptr<GCDApiFlow> CreateApiFlow() OVERRIDE; 74 virtual void GetWiFiCredentials(const CredentialsCallback& callback) OVERRIDE; 75 virtual void SwitchToSetupWiFi(const ResultCallback& callback) OVERRIDE; 76 virtual void CreatePrivetV3Client( 77 const std::string& service_name, 78 const PrivetClientCallback& callback) OVERRIDE; 79 virtual void ConfirmSecurityCode(const std::string& confirmation_code, 80 const ResultCallback& callback) OVERRIDE; 81 virtual void RestoreWifi(const ResultCallback& callback) OVERRIDE; 82 virtual void OnSetupDone() OVERRIDE; 83 virtual void OnSetupError() OVERRIDE; 84 85 // PrivetDeviceLister::Delegate implementation. 86 virtual void DeviceChanged(bool added, 87 const std::string& name, 88 const DeviceDescription& description) OVERRIDE; 89 virtual void DeviceRemoved(const std::string& name) OVERRIDE; 90 virtual void DeviceCacheFlushed() OVERRIDE; 91 92 // CloudDeviceListDelegate implementation. 93 virtual void OnDeviceListReady(const std::vector<Device>& devices) OVERRIDE; 94 virtual void OnDeviceListUnavailable() OVERRIDE; 95 96 // SigninManagerBase::Observer implementation. 97 virtual void GoogleSigninSucceeded(const std::string& username, 98 const std::string& password) OVERRIDE; 99 virtual void GoogleSignedOut(const std::string& username) OVERRIDE; 100 101 private: 102 typedef std::map<std::string, DeviceDescription> DeviceDescriptionMap; 103 104 // Message handlers: 105 // For when the page is ready to receive device notifications. 106 void HandleStart(const base::ListValue* args); 107 108 // For when a visibility change occurs. 109 void HandleIsVisible(const base::ListValue* args); 110 111 // For when a user choice is made. 112 void HandleRegisterDevice(const base::ListValue* args); 113 114 // For when a code is confirmed. 115 void HandleConfirmCode(const base::ListValue* args); 116 117 // For when a cancellation is made. 118 void HandleCancelRegistration(const base::ListValue* args); 119 120 // For requesting the device list. 121 void HandleRequestDeviceList(const base::ListValue* args); 122 123 // For opening URLs (relative to the Google Cloud Print base URL) in a new 124 // tab. 125 void HandleOpenCloudPrintURL(const base::ListValue* args); 126 127 // For showing sync login UI. 128 void HandleShowSyncUI(const base::ListValue* args); 129 130 // For when the IP address of the printer has been resolved for registration. 131 void StartRegisterHTTP(scoped_ptr<PrivetHTTPClient> http_client); 132 133 // For when the confirm operation on the cloudprint server has finished 134 // executing. 135 void OnConfirmDone(GCDApiFlow::Status status); 136 137 // Signal to the web interface an error has ocurred while registering. 138 void SendRegisterError(); 139 140 // Singal to the web interface that registration has finished. 141 void SendRegisterDone(const std::string& service_name); 142 143 // Set the visibility of the page. 144 void SetIsVisible(bool visible); 145 146 // Get the sync account email. 147 std::string GetSyncAccount(); 148 149 // Reset and cancel the current registration. 150 void ResetCurrentRegistration(); 151 152 // Creates |PrivetV3HTTPClient| privet from |PrivetHTTPClient| and calls 153 // callback. 154 void PrivetClientToV3(const PrivetClientCallback& callback, 155 scoped_ptr<PrivetHTTPClient> client); 156 157 // Announcement hasn't been sent for a certain time after registration 158 // finished. Consider it failed. 159 // TODO(noamsml): Re-resolve service first. 160 void OnAnnouncementTimeoutReached(); 161 162 void CheckUserLoggedIn(); 163 164 void CheckListingDone(); 165 166#if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE) 167 void StartCloudPrintConnector(); 168 void OnCloudPrintPrefsChanged(); 169 void ShowCloudPrintSetupDialog(const base::ListValue* args); 170 void HandleDisableCloudPrintConnector(const base::ListValue* args); 171 void SetupCloudPrintConnectorSection(); 172 void RemoveCloudPrintConnectorSection(); 173 void RefreshCloudPrintStatusFromService(); 174#endif 175 176#if defined(ENABLE_WIFI_BOOTSTRAPPING) 177 void StartWifiBootstrapping(); 178 void OnBootstrappingDeviceChanged( 179 bool available, 180 const wifi::BootstrappingDeviceDescription& description); 181#endif 182 183 // A map of current device descriptions provided by the PrivetDeviceLister. 184 DeviceDescriptionMap device_descriptions_; 185 186 // The service discovery client used listen for devices on the local network. 187 scoped_refptr<ServiceDiscoverySharedClient> service_discovery_client_; 188 189 // A factory for creating the privet HTTP Client. 190 scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_; 191 192 // An object representing the resolution process for the privet_http_factory. 193 scoped_ptr<PrivetHTTPResolution> privet_resolution_; 194 195 // The current HTTP client (used for the current operation). 196 scoped_ptr<PrivetV1HTTPClient> current_http_client_; 197 198 // The current register operation. Only one allowed at any time. 199 scoped_ptr<PrivetRegisterOperation> current_register_operation_; 200 201 // The current Privet v3 setup operation. Only one allowed at any time. 202 scoped_ptr<PrivetV3SetupFlow> current_setup_operation_; 203 204 // The current confirm call used during the registration flow. 205 scoped_ptr<GCDApiFlow> confirm_api_call_flow_; 206 207 // The device lister used to list devices on the local network. 208 scoped_ptr<PrivetDeviceLister> privet_lister_; 209 210 // Whether or not the page is marked as visible. 211 bool is_visible_; 212 213 // List of printers from cloud print. 214 scoped_ptr<GCDApiFlow> cloud_print_printer_list_; 215 scoped_ptr<GCDApiFlow> cloud_device_list_; 216 std::vector<Device> cloud_devices_; 217 int failed_list_count_; 218 int succeded_list_count_; 219 ResultCallback device_code_callback_; 220 221#if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE) 222 StringPrefMember cloud_print_connector_email_; 223 BooleanPrefMember cloud_print_connector_enabled_; 224 bool cloud_print_connector_ui_enabled_; 225#endif 226 227#if defined(ENABLE_WIFI_BOOTSTRAPPING) 228 scoped_ptr<wifi::WifiManager> wifi_manager_; 229 scoped_ptr<wifi::BootstrappingDeviceLister> bootstrapping_device_lister_; 230#endif 231 232 DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUIHandler); 233}; 234 235#undef CLOUD_PRINT_CONNECTOR_UI_AVAILABLE 236 237} // namespace local_discovery 238#endif // CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_ 239