local_discovery_ui_handler.h revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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 "base/prefs/pref_member.h"
14#include "chrome/browser/local_discovery/cloud_device_list.h"
15#include "chrome/browser/local_discovery/cloud_print_printer_list.h"
16#include "chrome/browser/local_discovery/privet_device_lister.h"
17#include "chrome/browser/local_discovery/privet_http.h"
18#include "components/signin/core/browser/signin_manager.h"
19#include "content/public/browser/web_ui_message_handler.h"
20
21#if defined(ENABLE_FULL_PRINTING) && !defined(OS_CHROMEOS)
22#define CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
23#endif
24
25#if defined(ENABLE_WIFI_BOOTSTRAPPING)
26#include "chrome/browser/local_discovery/wifi/bootstrapping_device_lister.h"
27#include "chrome/browser/local_discovery/wifi/wifi_manager.h"
28#endif
29
30// TODO(noamsml): Factor out full registration flow into single class
31namespace local_discovery {
32
33class PrivetConfirmApiCallFlow;
34class PrivetHTTPAsynchronousFactory;
35class PrivetHTTPResolution;
36class ServiceDiscoverySharedClient;
37
38// UI Handler for chrome://devices/
39// It listens to local discovery notifications and passes those notifications
40// into the Javascript to update the page.
41class LocalDiscoveryUIHandler : public content::WebUIMessageHandler,
42                                public PrivetRegisterOperation::Delegate,
43                                public PrivetDeviceLister::Delegate,
44                                public CloudDeviceListDelegate,
45                                public SigninManagerBase::Observer {
46 public:
47  LocalDiscoveryUIHandler();
48  virtual ~LocalDiscoveryUIHandler();
49
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 base::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(bool added,
74                             const std::string& name,
75                             const DeviceDescription& description) OVERRIDE;
76
77  virtual void DeviceRemoved(const std::string& name) OVERRIDE;
78
79  virtual void DeviceCacheFlushed() OVERRIDE;
80
81  // CloudDeviceListDelegate implementation.
82  virtual void OnDeviceListReady(const std::vector<Device>& devices) OVERRIDE;
83  virtual void OnDeviceListUnavailable() OVERRIDE;
84
85  // SigninManagerBase::Observer implementation.
86  virtual void GoogleSigninSucceeded(const std::string& username,
87                                     const std::string& password) OVERRIDE;
88
89  virtual void GoogleSignedOut(const std::string& username) OVERRIDE;
90
91 private:
92  typedef std::map<std::string, DeviceDescription> DeviceDescriptionMap;
93
94  // Message handlers:
95  // For when the page is ready to receive device notifications.
96  void HandleStart(const base::ListValue* args);
97
98  // For when a visibility change occurs.
99  void HandleIsVisible(const base::ListValue* args);
100
101  // For when a user choice is made.
102  void HandleRegisterDevice(const base::ListValue* args);
103
104  // For when a cancellation is made.
105  void HandleCancelRegistration(const base::ListValue* args);
106
107  // For requesting the device list.
108  void HandleRequestDeviceList(const base::ListValue* args);
109
110  // For opening URLs (relative to the Google Cloud Print base URL) in a new
111  // tab.
112  void HandleOpenCloudPrintURL(const base::ListValue* args);
113
114  // For showing sync login UI.
115  void HandleShowSyncUI(const base::ListValue* args);
116
117  // For when the IP address of the printer has been resolved for registration.
118  void StartRegisterHTTP(scoped_ptr<PrivetHTTPClient> http_client);
119
120  // For when the confirm operation on the cloudprint server has finished
121  // executing.
122  void OnConfirmDone(GCDApiFlow::Status status);
123
124  // Signal to the web interface an error has ocurred while registering.
125  void SendRegisterError();
126
127  // Singal to the web interface that registration has finished.
128  void SendRegisterDone(const std::string& service_name,
129                        const DeviceDescription& device);
130
131  // Set the visibility of the page.
132  void SetIsVisible(bool visible);
133
134  // Get the sync account email.
135  std::string GetSyncAccount();
136
137  // Reset and cancel the current registration.
138  void ResetCurrentRegistration();
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  void CheckListingDone();
148
149  scoped_ptr<GCDApiFlow> CreateApiFlow(scoped_ptr<GCDApiFlow::Request> request);
150
151#if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
152  void StartCloudPrintConnector();
153  void OnCloudPrintPrefsChanged();
154  void ShowCloudPrintSetupDialog(const base::ListValue* args);
155  void HandleDisableCloudPrintConnector(const base::ListValue* args);
156  void SetupCloudPrintConnectorSection();
157  void RemoveCloudPrintConnectorSection();
158  void RefreshCloudPrintStatusFromService();
159#endif
160
161#if defined(ENABLE_WIFI_BOOTSTRAPPING)
162  void StartWifiBootstrapping();
163  void OnBootstrappingDeviceChanged(
164      bool available,
165      const wifi::BootstrappingDeviceDescription& description);
166#endif
167
168  // The current HTTP client (used for the current operation).
169  scoped_ptr<PrivetHTTPClient> current_http_client_;
170
171  // The current register operation. Only one allowed at any time.
172  scoped_ptr<PrivetRegisterOperation> current_register_operation_;
173
174  // The current confirm call used during the registration flow.
175  scoped_ptr<GCDApiFlow> confirm_api_call_flow_;
176
177  // The device lister used to list devices on the local network.
178  scoped_ptr<PrivetDeviceLister> privet_lister_;
179
180  // The service discovery client used listen for devices on the local network.
181  scoped_refptr<ServiceDiscoverySharedClient> service_discovery_client_;
182
183  // A factory for creating the privet HTTP Client.
184  scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_;
185
186  // An object representing the resolution process for the privet_http_factory.
187  scoped_ptr<PrivetHTTPResolution> privet_resolution_;
188
189  // A map of current device descriptions provided by the PrivetDeviceLister.
190  DeviceDescriptionMap device_descriptions_;
191
192  // Whether or not the page is marked as visible.
193  bool is_visible_;
194
195  // List of printers from cloud print.
196  scoped_ptr<GCDApiFlow> cloud_print_printer_list_;
197  scoped_ptr<GCDApiFlow> cloud_device_list_;
198  std::vector<Device> cloud_devices_;
199  int failed_list_count_;
200  int succeded_list_count_;
201
202#if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
203  StringPrefMember cloud_print_connector_email_;
204  BooleanPrefMember cloud_print_connector_enabled_;
205  bool cloud_print_connector_ui_enabled_;
206#endif
207
208#if defined(ENABLE_WIFI_BOOTSTRAPPING)
209  scoped_ptr<wifi::WifiManager> wifi_manager_;
210  scoped_ptr<wifi::BootstrappingDeviceLister> bootstrapping_device_lister_;
211#endif
212
213  DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUIHandler);
214};
215
216#undef CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
217
218}  // namespace local_discovery
219#endif  // CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_
220