• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
31 namespace local_discovery {
32 
33 class PrivetConfirmApiCallFlow;
34 class PrivetHTTPAsynchronousFactory;
35 class PrivetHTTPResolution;
36 class PrivetV1HTTPClient;
37 class ServiceDiscoverySharedClient;
38 
39 // UI Handler for chrome://devices/
40 // It listens to local discovery notifications and passes those notifications
41 // into the Javascript to update the page.
42 class LocalDiscoveryUIHandler : public content::WebUIMessageHandler,
43                                 public PrivetRegisterOperation::Delegate,
44                                 public PrivetDeviceLister::Delegate,
45                                 public CloudDeviceListDelegate,
46                                 public SigninManagerBase::Observer {
47  public:
48   LocalDiscoveryUIHandler();
49   virtual ~LocalDiscoveryUIHandler();
50 
51   static bool GetHasVisible();
52 
53   // WebUIMessageHandler implementation.
54   virtual void RegisterMessages() OVERRIDE;
55   // PrivetRegisterOperation::Delegate implementation.
56   virtual void OnPrivetRegisterClaimToken(
57       PrivetRegisterOperation* operation,
58       const std::string& token,
59       const GURL& url) OVERRIDE;
60   virtual void OnPrivetRegisterError(
61       PrivetRegisterOperation* operation,
62       const std::string& action,
63       PrivetRegisterOperation::FailureReason reason,
64       int printer_http_code,
65       const base::DictionaryValue* json) OVERRIDE;
66   virtual void OnPrivetRegisterDone(
67       PrivetRegisterOperation* operation,
68       const std::string& device_id) OVERRIDE;
69 
70   // PrivetDeviceLister::Delegate implementation.
71   virtual void DeviceChanged(bool added,
72                              const std::string& name,
73                              const DeviceDescription& description) OVERRIDE;
74   virtual void DeviceRemoved(const std::string& name) OVERRIDE;
75   virtual void DeviceCacheFlushed() OVERRIDE;
76 
77   // CloudDeviceListDelegate implementation.
78   virtual void OnDeviceListReady(const std::vector<Device>& devices) OVERRIDE;
79   virtual void OnDeviceListUnavailable() OVERRIDE;
80 
81   // SigninManagerBase::Observer implementation.
82   virtual void GoogleSigninSucceeded(const std::string& username,
83                                      const std::string& password) OVERRIDE;
84   virtual void GoogleSignedOut(const std::string& username) OVERRIDE;
85 
86  private:
87   typedef std::map<std::string, DeviceDescription> DeviceDescriptionMap;
88 
89   // Message handlers:
90   // For when the page is ready to receive device notifications.
91   void HandleStart(const base::ListValue* args);
92 
93   // For when a visibility change occurs.
94   void HandleIsVisible(const base::ListValue* args);
95 
96   // For when a user choice is made.
97   void HandleRegisterDevice(const base::ListValue* args);
98 
99   // For when a cancellation is made.
100   void HandleCancelRegistration(const base::ListValue* args);
101 
102   // For requesting the device list.
103   void HandleRequestDeviceList(const base::ListValue* args);
104 
105   // For opening URLs (relative to the Google Cloud Print base URL) in a new
106   // tab.
107   void HandleOpenCloudPrintURL(const base::ListValue* args);
108 
109   // For showing sync login UI.
110   void HandleShowSyncUI(const base::ListValue* args);
111 
112   // For when the IP address of the printer has been resolved for registration.
113   void StartRegisterHTTP(scoped_ptr<PrivetHTTPClient> http_client);
114 
115   // For when the confirm operation on the cloudprint server has finished
116   // executing.
117   void OnConfirmDone(GCDApiFlow::Status status);
118 
119   // Signal to the web interface an error has ocurred while registering.
120   void SendRegisterError();
121 
122   // Singal to the web interface that registration has finished.
123   void SendRegisterDone(const std::string& service_name,
124                         const DeviceDescription& device);
125 
126   // Set the visibility of the page.
127   void SetIsVisible(bool visible);
128 
129   // Get the sync account email.
130   std::string GetSyncAccount();
131 
132   // Reset and cancel the current registration.
133   void ResetCurrentRegistration();
134 
135   // Announcement hasn't been sent for a certain time after registration
136   // finished. Consider it failed.
137   // TODO(noamsml): Re-resolve service first.
138   void OnAnnouncementTimeoutReached();
139 
140   void CheckUserLoggedIn();
141 
142   void CheckListingDone();
143 
144   scoped_ptr<GCDApiFlow> CreateApiFlow();
145 
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();
154 #endif
155 
156 #if defined(ENABLE_WIFI_BOOTSTRAPPING)
157   void StartWifiBootstrapping();
158   void OnBootstrappingDeviceChanged(
159       bool available,
160       const wifi::BootstrappingDeviceDescription& description);
161 #endif
162 
163   // A map of current device descriptions provided by the PrivetDeviceLister.
164   DeviceDescriptionMap device_descriptions_;
165 
166   // The service discovery client used listen for devices on the local network.
167   scoped_refptr<ServiceDiscoverySharedClient> service_discovery_client_;
168 
169   // A factory for creating the privet HTTP Client.
170   scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_;
171 
172   // An object representing the resolution process for the privet_http_factory.
173   scoped_ptr<PrivetHTTPResolution> privet_resolution_;
174 
175   // The current HTTP client (used for the current operation).
176   scoped_ptr<PrivetV1HTTPClient> current_http_client_;
177 
178   // The current register operation. Only one allowed at any time.
179   scoped_ptr<PrivetRegisterOperation> current_register_operation_;
180 
181   // The current confirm call used during the registration flow.
182   scoped_ptr<GCDApiFlow> confirm_api_call_flow_;
183 
184   // The device lister used to list devices on the local network.
185   scoped_ptr<PrivetDeviceLister> privet_lister_;
186 
187   // Whether or not the page is marked as visible.
188   bool is_visible_;
189 
190   // List of printers from cloud print.
191   scoped_ptr<GCDApiFlow> cloud_print_printer_list_;
192   scoped_ptr<GCDApiFlow> cloud_device_list_;
193   std::vector<Device> cloud_devices_;
194   int failed_list_count_;
195   int succeded_list_count_;
196 
197 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
198   StringPrefMember cloud_print_connector_email_;
199   BooleanPrefMember cloud_print_connector_enabled_;
200   bool cloud_print_connector_ui_enabled_;
201 #endif
202 
203 #if defined(ENABLE_WIFI_BOOTSTRAPPING)
204   scoped_ptr<wifi::WifiManager> wifi_manager_;
205   scoped_ptr<wifi::BootstrappingDeviceLister> bootstrapping_device_lister_;
206 #endif
207 
208   DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUIHandler);
209 };
210 
211 #undef CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
212 
213 }  // namespace local_discovery
214 #endif  // CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_
215