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_
12 #include "base/cancelable_callback.h"
13 #include "base/prefs/pref_member.h"
14 #include "chrome/browser/local_discovery/cloud_print_printer_list.h"
15 #include "chrome/browser/local_discovery/privet_device_lister.h"
16 #include "chrome/browser/local_discovery/privet_http.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/web_ui_message_handler.h"
21 #if defined(ENABLE_FULL_PRINTING) && !defined(OS_CHROMEOS) && \
23 #define CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
26 // TODO(noamsml): Factor out full registration flow into single class
27 namespace local_discovery {
29 class PrivetConfirmApiCallFlow;
30 class PrivetHTTPAsynchronousFactory;
31 class PrivetHTTPResolution;
32 class ServiceDiscoverySharedClient;
34 // UI Handler for chrome://devices/
35 // It listens to local discovery notifications and passes those notifications
36 // into the Javascript to update the page.
37 class LocalDiscoveryUIHandler : public content::WebUIMessageHandler,
38 public PrivetRegisterOperation::Delegate,
39 public PrivetDeviceLister::Delegate,
40 public CloudPrintPrinterList::Delegate,
41 content::NotificationObserver {
43 LocalDiscoveryUIHandler();
44 virtual ~LocalDiscoveryUIHandler();
46 static bool GetHasVisible();
48 // WebUIMessageHandler implementation.
49 virtual void RegisterMessages() OVERRIDE;
51 // PrivetRegisterOperation::Delegate implementation.
52 virtual void OnPrivetRegisterClaimToken(
53 PrivetRegisterOperation* operation,
54 const std::string& token,
55 const GURL& url) OVERRIDE;
57 virtual void OnPrivetRegisterError(
58 PrivetRegisterOperation* operation,
59 const std::string& action,
60 PrivetRegisterOperation::FailureReason reason,
61 int printer_http_code,
62 const DictionaryValue* json) OVERRIDE;
64 virtual void OnPrivetRegisterDone(
65 PrivetRegisterOperation* operation,
66 const std::string& device_id) OVERRIDE;
68 // PrivetDeviceLister::Delegate implementation.
69 virtual void DeviceChanged(bool added,
70 const std::string& name,
71 const DeviceDescription& description) OVERRIDE;
73 virtual void DeviceRemoved(const std::string& name) OVERRIDE;
75 virtual void DeviceCacheFlushed() OVERRIDE;
77 // CloudPrintPrinterList::Delegate implementation.
78 virtual void OnCloudPrintPrinterListReady() OVERRIDE;
80 virtual void OnCloudPrintPrinterListUnavailable() OVERRIDE;
82 // content::NotificationObserver implementation.
83 virtual void Observe(int type,
84 const content::NotificationSource& source,
85 const content::NotificationDetails& details) OVERRIDE;
88 typedef std::map<std::string, DeviceDescription> DeviceDescriptionMap;
91 // For when the page is ready to recieve device notifications.
92 void HandleStart(const base::ListValue* args);
94 // For when a visibility change occurs.
95 void HandleIsVisible(const base::ListValue* args);
97 // For when a user choice is made.
98 void HandleRegisterDevice(const base::ListValue* args);
100 // For when a cancelation is made.
101 void HandleCancelRegistration(const base::ListValue* args);
103 // For requesting the printer list.
104 void HandleRequestPrinterList(const base::ListValue* args);
106 // For opening URLs (relative to the Google Cloud Print base URL) in a new
108 void HandleOpenCloudPrintURL(const base::ListValue* args);
110 // For showing sync login UI.
111 void HandleShowSyncUI(const base::ListValue* args);
113 // For when the IP address of the printer has been resolved for registration.
114 void StartRegisterHTTP(
115 scoped_ptr<PrivetHTTPClient> http_client);
117 // For when the confirm operation on the cloudprint server has finished
119 void OnConfirmDone(CloudPrintBaseApiFlow::Status status);
121 // Signal to the web interface an error has ocurred while registering.
122 void SendRegisterError();
124 // Singal to the web interface that registration has finished.
125 void SendRegisterDone(const DeviceDescription& device);
127 // Set the visibility of the page.
128 void SetIsVisible(bool visible);
130 // Get the sync account email.
131 std::string GetSyncAccount();
133 // Get the base cloud print URL.
134 std::string GetCloudPrintBaseUrl();
136 // Reset and cancel the current registration.
137 void ResetCurrentRegistration();
139 scoped_ptr<base::DictionaryValue> CreatePrinterInfo(
140 const CloudPrintPrinterList::PrinterDetails& description);
142 // Announcement hasn't been sent for a certain time after registration
143 // finished. Consider it failed.
144 // TODO(noamsml): Re-resolve service first.
145 void OnAnnouncementTimeoutReached();
147 void CheckUserLoggedIn();
149 void ScheduleQuery(int timeout_seconds);
151 void SendQuery(int next_timeout_seconds);
153 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
154 void StartCloudPrintConnector();
155 void OnCloudPrintPrefsChanged();
156 void ShowCloudPrintSetupDialog(const ListValue* args);
157 void HandleDisableCloudPrintConnector(const ListValue* args);
158 void SetupCloudPrintConnectorSection();
159 void RemoveCloudPrintConnectorSection();
160 void RefreshCloudPrintStatusFromService();
163 // The current HTTP client (used for the current operation).
164 scoped_ptr<PrivetHTTPClient> current_http_client_;
166 // The current register operation. Only one allowed at any time.
167 scoped_ptr<PrivetRegisterOperation> current_register_operation_;
169 // The current confirm call used during the registration flow.
170 scoped_ptr<PrivetConfirmApiCallFlow> confirm_api_call_flow_;
172 // The device lister used to list devices on the local network.
173 scoped_ptr<PrivetDeviceLister> privet_lister_;
175 // The service discovery client used listen for devices on the local network.
176 scoped_refptr<ServiceDiscoverySharedClient> service_discovery_client_;
178 // A factory for creating the privet HTTP Client.
179 scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_;
181 // An object representing the resolution process for the privet_http_factory.
182 scoped_ptr<PrivetHTTPResolution> privet_resolution_;
184 // A map of current device descriptions provided by the PrivetDeviceLister.
185 DeviceDescriptionMap device_descriptions_;
187 // Whether or not the page is marked as visible.
190 // List of printers from cloud print.
191 scoped_ptr<CloudPrintPrinterList> cloud_print_printer_list_;
193 // Callback for requery.
194 base::CancelableCallback<void()> requery_callback_;
196 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
197 StringPrefMember cloud_print_connector_email_;
198 BooleanPrefMember cloud_print_connector_enabled_;
199 bool cloud_print_connector_ui_enabled_;
202 content::NotificationRegistrar notification_registrar_;
203 DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUIHandler);
206 #undef CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
208 } // namespace local_discovery
209 #endif // CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_