local_discovery_ui_handler.h revision 3551c9c881056c480085172ff9840cab31610854
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 11#include "chrome/browser/local_discovery/privet_confirm_api_flow.h" 12#include "chrome/browser/local_discovery/privet_device_lister.h" 13#include "chrome/browser/local_discovery/privet_http.h" 14#include "chrome/browser/local_discovery/privet_http_asynchronous_factory.h" 15#include "chrome/browser/local_discovery/service_discovery_host_client.h" 16#include "chrome/common/local_discovery/service_discovery_client.h" 17#include "content/public/browser/user_metrics.h" 18#include "content/public/browser/web_ui_message_handler.h" 19 20namespace local_discovery { 21 22// UI Handler for chrome://devices/ 23// It listens to local discovery notifications and passes those notifications 24// into the Javascript to update the page. 25class LocalDiscoveryUIHandler : public content::WebUIMessageHandler, 26 public PrivetRegisterOperation::Delegate, 27 public PrivetDeviceLister::Delegate, 28 public PrivetInfoOperation::Delegate { 29 public: 30 class Factory { 31 public: 32 virtual ~Factory() {} 33 virtual LocalDiscoveryUIHandler* CreateLocalDiscoveryUIHandler() = 0; 34 }; 35 36 LocalDiscoveryUIHandler(); 37 // This constructor should only used by tests. 38 explicit LocalDiscoveryUIHandler( 39 scoped_ptr<PrivetDeviceLister> privet_lister); 40 virtual ~LocalDiscoveryUIHandler(); 41 42 static LocalDiscoveryUIHandler* Create(); 43 static void SetFactory(Factory* factory); 44 45 // WebUIMessageHandler implementation. 46 virtual void RegisterMessages() OVERRIDE; 47 48 // PrivetRegisterOperation::Delegate implementation. 49 virtual void OnPrivetRegisterClaimToken( 50 PrivetRegisterOperation* operation, 51 const std::string& token, 52 const GURL& url) OVERRIDE; 53 54 virtual void OnPrivetRegisterError( 55 PrivetRegisterOperation* operation, 56 const std::string& action, 57 PrivetRegisterOperation::FailureReason reason, 58 int printer_http_code, 59 const DictionaryValue* json) OVERRIDE; 60 61 virtual void OnPrivetRegisterDone( 62 PrivetRegisterOperation* operation, 63 const std::string& device_id) OVERRIDE; 64 65 // PrivetDeviceLister::Delegate implementation. 66 virtual void DeviceChanged( 67 bool added, 68 const std::string& name, 69 const DeviceDescription& description) OVERRIDE; 70 virtual void DeviceRemoved(const std::string& name) OVERRIDE; 71 72 // PrivetInfoOperation::Delegate implementation: 73 virtual void OnPrivetInfoDone( 74 PrivetInfoOperation* operation, 75 int http_code, 76 const base::DictionaryValue* json_value) OVERRIDE; 77 78 private: 79 // Message handlers: 80 // For registering a device. 81 void HandleRegisterDevice(const base::ListValue* args); 82 // For when the page is ready to recieve device notifications. 83 84 void HandleStart(const base::ListValue* args); 85 // For when info for a device is requested. 86 void HandleInfoRequested(const base::ListValue* args); 87 88 // For when the IP address of the printer has been resolved for registration. 89 void StartRegisterHTTP(scoped_ptr<PrivetHTTPClient> http_client); 90 91 // For when the IP address of the printer has been resolved for registration. 92 void StartInfoHTTP(scoped_ptr<PrivetHTTPClient> http_client); 93 94 // For when the confirm operation on the cloudprint server has finished 95 // executing. 96 void OnConfirmDone(PrivetConfirmApiCallFlow::Status status); 97 98 // Log an error to the web interface. 99 void LogRegisterErrorToWeb(const std::string& error); 100 101 // Log a successful registration to the web inteface. 102 void LogRegisterDoneToWeb(const std::string& id); 103 104 // Log an error to the web interface. 105 void LogInfoErrorToWeb(const std::string& error); 106 107 // The current HTTP client (used for the current operation). 108 scoped_ptr<PrivetHTTPClient> current_http_client_; 109 110 // The current info operation (operations are currently exclusive). 111 scoped_ptr<PrivetInfoOperation> current_info_operation_; 112 113 // The current register operation. Only one allowed at any time. 114 scoped_ptr<PrivetRegisterOperation> current_register_operation_; 115 116 // The current confirm call used during the registration flow. 117 scoped_ptr<PrivetConfirmApiCallFlow> confirm_api_call_flow_; 118 119 // The device lister used to list devices on the local network. 120 scoped_ptr<PrivetDeviceLister> privet_lister_; 121 122 // The service discovery client used listen for devices on the local network. 123 scoped_refptr<ServiceDiscoveryHostClient> service_discovery_client_; 124 125 // A factory for creating the privet HTTP Client. 126 scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_; 127 128 // An object representing the resolution process for the privet_http_factory. 129 scoped_ptr<PrivetHTTPAsynchronousFactory::Resolution> privet_resolution_; 130 131 // A map of current device descriptions provided by the PrivetDeviceLister. 132 std::map<std::string, DeviceDescription> device_descriptions_; 133 134 DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUIHandler); 135}; 136 137} // namespace local_discovery 138#endif // CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_ 139