local_discovery_ui_handler.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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_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 "chrome/browser/signin/signin_manager.h"
18#include "content/public/browser/web_ui_message_handler.h"
19
20#if defined(ENABLE_FULL_PRINTING) && !defined(OS_CHROMEOS) && \
21  !defined(OS_MACOSX)
22#define CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
23#endif
24
25// TODO(noamsml): Factor out full registration flow into single class
26namespace local_discovery {
27
28class PrivetConfirmApiCallFlow;
29class PrivetHTTPAsynchronousFactory;
30class PrivetHTTPResolution;
31class ServiceDiscoverySharedClient;
32
33// UI Handler for chrome://devices/
34// It listens to local discovery notifications and passes those notifications
35// into the Javascript to update the page.
36class LocalDiscoveryUIHandler : public content::WebUIMessageHandler,
37                                public PrivetRegisterOperation::Delegate,
38                                public PrivetDeviceLister::Delegate,
39                                public CloudPrintPrinterList::Delegate,
40                                public SigninManagerBase::Observer {
41 public:
42  LocalDiscoveryUIHandler();
43  virtual ~LocalDiscoveryUIHandler();
44
45  static bool GetHasVisible();
46
47  // WebUIMessageHandler implementation.
48  virtual void RegisterMessages() OVERRIDE;
49
50  // PrivetRegisterOperation::Delegate implementation.
51  virtual void OnPrivetRegisterClaimToken(
52      PrivetRegisterOperation* operation,
53      const std::string& token,
54      const GURL& url) OVERRIDE;
55
56  virtual void OnPrivetRegisterError(
57      PrivetRegisterOperation* operation,
58      const std::string& action,
59      PrivetRegisterOperation::FailureReason reason,
60      int printer_http_code,
61      const base::DictionaryValue* json) OVERRIDE;
62
63  virtual void OnPrivetRegisterDone(
64      PrivetRegisterOperation* operation,
65      const std::string& device_id) OVERRIDE;
66
67  // PrivetDeviceLister::Delegate implementation.
68  virtual void DeviceChanged(bool added,
69                             const std::string& name,
70                             const DeviceDescription& description) OVERRIDE;
71
72  virtual void DeviceRemoved(const std::string& name) OVERRIDE;
73
74  virtual void DeviceCacheFlushed() OVERRIDE;
75
76  // CloudPrintPrinterList::Delegate implementation.
77  virtual void OnCloudPrintPrinterListReady() OVERRIDE;
78
79  virtual void OnCloudPrintPrinterListUnavailable() OVERRIDE;
80
81  // SigninManagerBase::Observer implementation.
82  virtual void GoogleSigninSucceeded(const std::string& username,
83                                     const std::string& password) OVERRIDE;
84
85  virtual void GoogleSignedOut(const std::string& username) 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 showing sync login UI.
111  void HandleShowSyncUI(const base::ListValue* args);
112
113  // For when the IP address of the printer has been resolved for registration.
114  void StartRegisterHTTP(
115      scoped_ptr<PrivetHTTPClient> http_client);
116
117  // For when the confirm operation on the cloudprint server has finished
118  // executing.
119  void OnConfirmDone(CloudPrintBaseApiFlow::Status status);
120
121  // Signal to the web interface an error has ocurred while registering.
122  void SendRegisterError();
123
124  // Singal to the web interface that registration has finished.
125  void SendRegisterDone(const std::string& service_name,
126                        const DeviceDescription& device);
127
128  // Set the visibility of the page.
129  void SetIsVisible(bool visible);
130
131  // Get the sync account email.
132  std::string GetSyncAccount();
133
134  // Get the base cloud print URL.
135  std::string GetCloudPrintBaseUrl();
136
137  // Reset and cancel the current registration.
138  void ResetCurrentRegistration();
139
140  scoped_ptr<base::DictionaryValue> CreatePrinterInfo(
141      const CloudPrintPrinterList::PrinterDetails& description);
142
143  // Announcement hasn't been sent for a certain time after registration
144  // finished. Consider it failed.
145  // TODO(noamsml): Re-resolve service first.
146  void OnAnnouncementTimeoutReached();
147
148  void CheckUserLoggedIn();
149
150#if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
151  void StartCloudPrintConnector();
152  void OnCloudPrintPrefsChanged();
153  void ShowCloudPrintSetupDialog(const base::ListValue* args);
154  void HandleDisableCloudPrintConnector(const base::ListValue* args);
155  void SetupCloudPrintConnectorSection();
156  void RemoveCloudPrintConnectorSection();
157  void RefreshCloudPrintStatusFromService();
158#endif
159
160  // The current HTTP client (used for the current operation).
161  scoped_ptr<PrivetHTTPClient> current_http_client_;
162
163  // The current register operation. Only one allowed at any time.
164  scoped_ptr<PrivetRegisterOperation> current_register_operation_;
165
166  // The current confirm call used during the registration flow.
167  scoped_ptr<PrivetConfirmApiCallFlow> confirm_api_call_flow_;
168
169  // The device lister used to list devices on the local network.
170  scoped_ptr<PrivetDeviceLister> privet_lister_;
171
172  // The service discovery client used listen for devices on the local network.
173  scoped_refptr<ServiceDiscoverySharedClient> service_discovery_client_;
174
175  // A factory for creating the privet HTTP Client.
176  scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_;
177
178  // An object representing the resolution process for the privet_http_factory.
179  scoped_ptr<PrivetHTTPResolution> privet_resolution_;
180
181  // A map of current device descriptions provided by the PrivetDeviceLister.
182  DeviceDescriptionMap device_descriptions_;
183
184  // Whether or not the page is marked as visible.
185  bool is_visible_;
186
187  // List of printers from cloud print.
188  scoped_ptr<CloudPrintPrinterList> cloud_print_printer_list_;
189
190#if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
191  StringPrefMember cloud_print_connector_email_;
192  BooleanPrefMember cloud_print_connector_enabled_;
193  bool cloud_print_connector_ui_enabled_;
194#endif
195
196  DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUIHandler);
197};
198
199#undef CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
200
201}  // namespace local_discovery
202#endif  // CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_
203