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 "components/signin/core/browser/signin_manager.h"
18 #include "content/public/browser/web_ui_message_handler.h"
20 #if defined(ENABLE_FULL_PRINTING) && !defined(OS_CHROMEOS)
21 #define CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
24 // TODO(noamsml): Factor out full registration flow into single class
25 namespace local_discovery {
27 class PrivetConfirmApiCallFlow;
28 class PrivetHTTPAsynchronousFactory;
29 class PrivetHTTPResolution;
30 class ServiceDiscoverySharedClient;
32 // UI Handler for chrome://devices/
33 // It listens to local discovery notifications and passes those notifications
34 // into the Javascript to update the page.
35 class LocalDiscoveryUIHandler : public content::WebUIMessageHandler,
36 public PrivetRegisterOperation::Delegate,
37 public PrivetDeviceLister::Delegate,
38 public CloudPrintPrinterList::Delegate,
39 public SigninManagerBase::Observer {
41 LocalDiscoveryUIHandler();
42 virtual ~LocalDiscoveryUIHandler();
44 static bool GetHasVisible();
46 // WebUIMessageHandler implementation.
47 virtual void RegisterMessages() OVERRIDE;
49 // PrivetRegisterOperation::Delegate implementation.
50 virtual void OnPrivetRegisterClaimToken(
51 PrivetRegisterOperation* operation,
52 const std::string& token,
53 const GURL& url) OVERRIDE;
55 virtual void OnPrivetRegisterError(
56 PrivetRegisterOperation* operation,
57 const std::string& action,
58 PrivetRegisterOperation::FailureReason reason,
59 int printer_http_code,
60 const base::DictionaryValue* json) OVERRIDE;
62 virtual void OnPrivetRegisterDone(
63 PrivetRegisterOperation* operation,
64 const std::string& device_id) OVERRIDE;
66 // PrivetDeviceLister::Delegate implementation.
67 virtual void DeviceChanged(bool added,
68 const std::string& name,
69 const DeviceDescription& description) OVERRIDE;
71 virtual void DeviceRemoved(const std::string& name) OVERRIDE;
73 virtual void DeviceCacheFlushed() OVERRIDE;
75 // CloudPrintPrinterList::Delegate implementation.
76 virtual void OnCloudPrintPrinterListReady() OVERRIDE;
78 virtual void OnCloudPrintPrinterListUnavailable() OVERRIDE;
80 // SigninManagerBase::Observer implementation.
81 virtual void GoogleSigninSucceeded(const std::string& username,
82 const std::string& password) OVERRIDE;
84 virtual void GoogleSignedOut(const std::string& username) OVERRIDE;
87 typedef std::map<std::string, DeviceDescription> DeviceDescriptionMap;
90 // For when the page is ready to receive device notifications.
91 void HandleStart(const base::ListValue* args);
93 // For when a visibility change occurs.
94 void HandleIsVisible(const base::ListValue* args);
96 // For when a user choice is made.
97 void HandleRegisterDevice(const base::ListValue* args);
99 // For when a cancelation is made.
100 void HandleCancelRegistration(const base::ListValue* args);
102 // For requesting the printer list.
103 void HandleRequestPrinterList(const base::ListValue* args);
105 // For opening URLs (relative to the Google Cloud Print base URL) in a new
107 void HandleOpenCloudPrintURL(const base::ListValue* args);
109 // For showing sync login UI.
110 void HandleShowSyncUI(const base::ListValue* args);
112 // For when the IP address of the printer has been resolved for registration.
113 void StartRegisterHTTP(
114 scoped_ptr<PrivetHTTPClient> http_client);
116 // For when the confirm operation on the cloudprint server has finished
118 void OnConfirmDone(GCDBaseApiFlow::Status status);
120 // Signal to the web interface an error has ocurred while registering.
121 void SendRegisterError();
123 // Singal to the web interface that registration has finished.
124 void SendRegisterDone(const std::string& service_name,
125 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 // Reset and cancel the current registration.
134 void ResetCurrentRegistration();
136 scoped_ptr<base::DictionaryValue> CreatePrinterInfo(
137 const CloudPrintPrinterList::PrinterDetails& description);
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();
144 void CheckUserLoggedIn();
146 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
147 void StartCloudPrintConnector();
148 void OnCloudPrintPrefsChanged();
149 void ShowCloudPrintSetupDialog(const base::ListValue* args);
150 void HandleDisableCloudPrintConnector(const base::ListValue* args);
151 void SetupCloudPrintConnectorSection();
152 void RemoveCloudPrintConnectorSection();
153 void RefreshCloudPrintStatusFromService();
156 // The current HTTP client (used for the current operation).
157 scoped_ptr<PrivetHTTPClient> current_http_client_;
159 // The current register operation. Only one allowed at any time.
160 scoped_ptr<PrivetRegisterOperation> current_register_operation_;
162 // The current confirm call used during the registration flow.
163 scoped_ptr<PrivetConfirmApiCallFlow> confirm_api_call_flow_;
165 // The device lister used to list devices on the local network.
166 scoped_ptr<PrivetDeviceLister> privet_lister_;
168 // The service discovery client used listen for devices on the local network.
169 scoped_refptr<ServiceDiscoverySharedClient> service_discovery_client_;
171 // A factory for creating the privet HTTP Client.
172 scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_;
174 // An object representing the resolution process for the privet_http_factory.
175 scoped_ptr<PrivetHTTPResolution> privet_resolution_;
177 // A map of current device descriptions provided by the PrivetDeviceLister.
178 DeviceDescriptionMap device_descriptions_;
180 // Whether or not the page is marked as visible.
183 // List of printers from cloud print.
184 scoped_ptr<CloudPrintPrinterList> cloud_print_printer_list_;
186 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
187 StringPrefMember cloud_print_connector_email_;
188 BooleanPrefMember cloud_print_connector_enabled_;
189 bool cloud_print_connector_ui_enabled_;
192 DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUIHandler);
195 #undef CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
197 } // namespace local_discovery
198 #endif // CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_