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