local_discovery_ui_handler.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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 "chrome/browser/local_discovery/cloud_print_printer_list.h" 14#include "chrome/browser/local_discovery/privet_device_lister.h" 15#include "chrome/browser/local_discovery/privet_http.h" 16#include "content/public/browser/web_ui_message_handler.h" 17 18// TODO(noamsml): Factor out full registration flow into single class 19namespace local_discovery { 20 21class PrivetConfirmApiCallFlow; 22class PrivetHTTPAsynchronousFactory; 23class PrivetHTTPResolution; 24class ServiceDiscoverySharedClient; 25 26// UI Handler for chrome://devices/ 27// It listens to local discovery notifications and passes those notifications 28// into the Javascript to update the page. 29class LocalDiscoveryUIHandler : public content::WebUIMessageHandler, 30 public PrivetRegisterOperation::Delegate, 31 public PrivetDeviceLister::Delegate, 32 public CloudPrintPrinterList::Delegate { 33 public: 34 class Factory { 35 public: 36 virtual ~Factory() {} 37 virtual LocalDiscoveryUIHandler* CreateLocalDiscoveryUIHandler() = 0; 38 }; 39 40 LocalDiscoveryUIHandler(); 41 // This constructor should only used by tests. 42 explicit LocalDiscoveryUIHandler( 43 scoped_ptr<PrivetDeviceLister> privet_lister); 44 virtual ~LocalDiscoveryUIHandler(); 45 46 static LocalDiscoveryUIHandler* Create(); 47 static void SetFactory(Factory* factory); 48 static bool GetHasVisible(); 49 50 // WebUIMessageHandler implementation. 51 virtual void RegisterMessages() OVERRIDE; 52 53 // PrivetRegisterOperation::Delegate implementation. 54 virtual void OnPrivetRegisterClaimToken( 55 PrivetRegisterOperation* operation, 56 const std::string& token, 57 const GURL& url) OVERRIDE; 58 59 virtual void OnPrivetRegisterError( 60 PrivetRegisterOperation* operation, 61 const std::string& action, 62 PrivetRegisterOperation::FailureReason reason, 63 int printer_http_code, 64 const DictionaryValue* json) OVERRIDE; 65 66 virtual void OnPrivetRegisterDone( 67 PrivetRegisterOperation* operation, 68 const std::string& device_id) OVERRIDE; 69 70 // PrivetDeviceLister::Delegate implementation. 71 virtual void DeviceChanged( 72 bool added, 73 const std::string& name, 74 const DeviceDescription& description) OVERRIDE; 75 76 virtual void DeviceRemoved(const std::string& name) OVERRIDE; 77 78 virtual void DeviceCacheFlushed() OVERRIDE; 79 80 // CloudPrintPrinterList::Delegate implementation. 81 virtual void OnCloudPrintPrinterListReady() OVERRIDE; 82 83 virtual void OnCloudPrintPrinterListUnavailable() OVERRIDE; 84 85 private: 86 typedef std::map<std::string, DeviceDescription> DeviceDescriptionMap; 87 88 // Message handlers: 89 // For when the page is ready to recieve device notifications. 90 void HandleStart(const base::ListValue* args); 91 92 // For when a visibility change occurs. 93 void HandleIsVisible(const base::ListValue* args); 94 95 // For when a user choice is made. 96 void HandleRegisterDevice(const base::ListValue* args); 97 98 // For when a cancelation is made. 99 void HandleCancelRegistration(const base::ListValue* args); 100 101 // For requesting the printer list. 102 void HandleRequestPrinterList(const base::ListValue* args); 103 104 // For opening URLs (relative to the Google Cloud Print base URL) in a new 105 // tab. 106 void HandleOpenCloudPrintURL(const base::ListValue* args); 107 108 // For showing sync login UI. 109 void HandleShowSyncUI(const base::ListValue* args); 110 111 // For when the IP address of the printer has been resolved for registration. 112 void StartRegisterHTTP( 113 scoped_ptr<PrivetHTTPClient> http_client); 114 115 // For when the confirm operation on the cloudprint server has finished 116 // executing. 117 void OnConfirmDone(CloudPrintBaseApiFlow::Status status); 118 119 // Signal to the web interface an error has ocurred while registering. 120 void SendRegisterError(); 121 122 // Singal to the web interface that registration has finished. 123 void SendRegisterDone(); 124 125 // Set the visibility of the page. 126 void SetIsVisible(bool visible); 127 128 // Get the sync account email. 129 std::string GetSyncAccount(); 130 131 // Get the base cloud print URL. 132 std::string GetCloudPrintBaseUrl(); 133 134 // Reset and cancel the current registration. 135 void ResetCurrentRegistration(); 136 137 scoped_ptr<base::DictionaryValue> CreatePrinterInfo( 138 const CloudPrintPrinterList::PrinterDetails& description); 139 140 // Announcement hasn't been sent for a certain time after registration 141 // finished. Consider it failed. 142 // TODO(noamsml): Re-resolve service first. 143 void OnAnnouncementTimeoutReached(); 144 145 void CheckUserLoggedIn(); 146 147 // The current HTTP client (used for the current operation). 148 scoped_ptr<PrivetHTTPClient> current_http_client_; 149 150 // The current register operation. Only one allowed at any time. 151 scoped_ptr<PrivetRegisterOperation> current_register_operation_; 152 153 // The current confirm call used during the registration flow. 154 scoped_ptr<PrivetConfirmApiCallFlow> confirm_api_call_flow_; 155 156 // The device lister used to list devices on the local network. 157 scoped_ptr<PrivetDeviceLister> privet_lister_; 158 159 // The service discovery client used listen for devices on the local network. 160 scoped_refptr<ServiceDiscoverySharedClient> service_discovery_client_; 161 162 // A factory for creating the privet HTTP Client. 163 scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_; 164 165 // An object representing the resolution process for the privet_http_factory. 166 scoped_ptr<PrivetHTTPResolution> privet_resolution_; 167 168 // A map of current device descriptions provided by the PrivetDeviceLister. 169 DeviceDescriptionMap device_descriptions_; 170 171 // Whether or not the page is marked as visible. 172 bool is_visible_; 173 174 // Device whose state must be updated to "registered" to complete 175 // registration. 176 std::string new_register_device_; 177 178 // List of printers from cloud print. 179 scoped_ptr<CloudPrintPrinterList> cloud_print_printer_list_; 180 181 // Announcement timeout for registration. 182 base::CancelableCallback<void()> registration_announce_timeout_; 183 184 DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUIHandler); 185}; 186 187} // namespace local_discovery 188#endif // CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_ 189