File: local_discovery_ui_handler.h

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (237 lines) | stat: -rw-r--r-- 9,128 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_
#define CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_

#include <map>
#include <string>
#include <vector>

#include "base/cancelable_callback.h"
#include "base/prefs/pref_member.h"
#include "chrome/browser/local_discovery/cloud_device_list.h"
#include "chrome/browser/local_discovery/cloud_print_printer_list.h"
#include "chrome/browser/local_discovery/privet_device_lister.h"
#include "chrome/browser/local_discovery/privet_http.h"
#include "chrome/browser/local_discovery/privetv3_setup_flow.h"
#include "components/signin/core/browser/signin_manager.h"
#include "content/public/browser/web_ui_message_handler.h"

#if defined(ENABLE_PRINT_PREVIEW) && !defined(OS_CHROMEOS)
#define CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
#endif

#if defined(ENABLE_WIFI_BOOTSTRAPPING)
#include "chrome/browser/local_discovery/wifi/bootstrapping_device_lister.h"
#include "chrome/browser/local_discovery/wifi/wifi_manager.h"
#endif

// TODO(noamsml): Factor out full registration flow into single class
namespace local_discovery {

class PrivetConfirmApiCallFlow;
class PrivetHTTPAsynchronousFactory;
class PrivetHTTPResolution;
class PrivetV1HTTPClient;
class ServiceDiscoverySharedClient;

// UI Handler for chrome://devices/
// It listens to local discovery notifications and passes those notifications
// into the Javascript to update the page.
class LocalDiscoveryUIHandler : public content::WebUIMessageHandler,
                                public PrivetRegisterOperation::Delegate,
                                public PrivetV3SetupFlow::Delegate,
                                public PrivetDeviceLister::Delegate,
                                public CloudDeviceListDelegate,
                                public SigninManagerBase::Observer {
 public:
  LocalDiscoveryUIHandler();
  ~LocalDiscoveryUIHandler() override;

  static bool GetHasVisible();

  // WebUIMessageHandler implementation.
  void RegisterMessages() override;
  // PrivetRegisterOperation::Delegate implementation.
  void OnPrivetRegisterClaimToken(PrivetRegisterOperation* operation,
                                  const std::string& token,
                                  const GURL& url) override;
  void OnPrivetRegisterError(PrivetRegisterOperation* operation,
                             const std::string& action,
                             PrivetRegisterOperation::FailureReason reason,
                             int printer_http_code,
                             const base::DictionaryValue* json) override;
  void OnPrivetRegisterDone(PrivetRegisterOperation* operation,
                            const std::string& device_id) override;

  // PrivetV3SetupFlow::Delegate implementation.
  scoped_ptr<GCDApiFlow> CreateApiFlow() override;
  void GetWiFiCredentials(const CredentialsCallback& callback) override;
  void SwitchToSetupWiFi(const ResultCallback& callback) override;
  void CreatePrivetV3Client(const std::string& service_name,
                            const PrivetClientCallback& callback) override;
  void ConfirmSecurityCode(const ResultCallback& callback) override;
  void RestoreWifi(const ResultCallback& callback) override;
  void OnSetupDone() override;
  void OnSetupError() override;

  // PrivetDeviceLister::Delegate implementation.
  void DeviceChanged(bool added,
                     const std::string& name,
                     const DeviceDescription& description) override;
  void DeviceRemoved(const std::string& name) override;
  void DeviceCacheFlushed() override;

  // CloudDeviceListDelegate implementation.
  void OnDeviceListReady(const std::vector<Device>& devices) override;
  void OnDeviceListUnavailable() override;

  // SigninManagerBase::Observer implementation.
  void GoogleSigninSucceeded(const std::string& account_id,
                             const std::string& username,
                             const std::string& password) override;
  void GoogleSignedOut(const std::string& account_id,
                       const std::string& username) override;

 private:
  typedef std::map<std::string, DeviceDescription> DeviceDescriptionMap;

  // Message handlers:
  // For when the page is ready to receive device notifications.
  void HandleStart(const base::ListValue* args);

  // For when a visibility change occurs.
  void HandleIsVisible(const base::ListValue* args);

  // For when a user choice is made.
  void HandleRegisterDevice(const base::ListValue* args);

  // For when a code is confirmed.
  void HandleConfirmCode(const base::ListValue* args);

  // For when a cancellation is made.
  void HandleCancelRegistration(const base::ListValue* args);

  // For requesting the device list.
  void HandleRequestDeviceList(const base::ListValue* args);

  // For opening URLs (relative to the Google Cloud Print base URL) in a new
  // tab.
  void HandleOpenCloudPrintURL(const base::ListValue* args);

  // For showing sync login UI.
  void HandleShowSyncUI(const base::ListValue* args);

  // For when the IP address of the printer has been resolved for registration.
  void StartRegisterHTTP(scoped_ptr<PrivetHTTPClient> http_client);

  // For when the confirm operation on the cloudprint server has finished
  // executing.
  void OnConfirmDone(GCDApiFlow::Status status);

  // Signal to the web interface an error has ocurred while registering.
  void SendRegisterError();

  // Singal to the web interface that registration has finished.
  void SendRegisterDone(const std::string& service_name);

  // Set the visibility of the page.
  void SetIsVisible(bool visible);

  // Get the sync account email.
  std::string GetSyncAccount();

  // Reset and cancel the current registration.
  void ResetCurrentRegistration();

  // Creates |PrivetV3HTTPClient| privet from |PrivetHTTPClient| and calls
  // callback.
  void PrivetClientToV3(const PrivetClientCallback& callback,
                        scoped_ptr<PrivetHTTPClient> client);

  // Announcement hasn't been sent for a certain time after registration
  // finished. Consider it failed.
  // TODO(noamsml): Re-resolve service first.
  void OnAnnouncementTimeoutReached();

  void CheckUserLoggedIn();

  void CheckListingDone();

  bool IsUserSupervisedOrOffTheRecord();

#if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
  void StartCloudPrintConnector();
  void OnCloudPrintPrefsChanged();
  void ShowCloudPrintSetupDialog(const base::ListValue* args);
  void HandleDisableCloudPrintConnector(const base::ListValue* args);
  void SetupCloudPrintConnectorSection();
  void RemoveCloudPrintConnectorSection();
  void RefreshCloudPrintStatusFromService();
#endif

#if defined(ENABLE_WIFI_BOOTSTRAPPING)
  void StartWifiBootstrapping();
  void OnBootstrappingDeviceChanged(
      bool available,
      const wifi::BootstrappingDeviceDescription& description);
#endif

  // A map of current device descriptions provided by the PrivetDeviceLister.
  DeviceDescriptionMap device_descriptions_;

  // The service discovery client used listen for devices on the local network.
  scoped_refptr<ServiceDiscoverySharedClient> service_discovery_client_;

  // A factory for creating the privet HTTP Client.
  scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_;

  // An object representing the resolution process for the privet_http_factory.
  scoped_ptr<PrivetHTTPResolution> privet_resolution_;

  // The current HTTP client (used for the current operation).
  scoped_ptr<PrivetV1HTTPClient> current_http_client_;

  // The current register operation. Only one allowed at any time.
  scoped_ptr<PrivetRegisterOperation> current_register_operation_;

  // The current Privet v3 setup operation. Only one allowed at any time.
  scoped_ptr<PrivetV3SetupFlow> current_setup_operation_;

  // The current confirm call used during the registration flow.
  scoped_ptr<GCDApiFlow> confirm_api_call_flow_;

  // The device lister used to list devices on the local network.
  scoped_ptr<PrivetDeviceLister> privet_lister_;

  // Whether or not the page is marked as visible.
  bool is_visible_;

  // List of printers from cloud print.
  scoped_ptr<GCDApiFlow> cloud_print_printer_list_;
  scoped_ptr<GCDApiFlow> cloud_device_list_;
  std::vector<Device> cloud_devices_;
  int failed_list_count_;
  int succeded_list_count_;
  ResultCallback device_code_callback_;

#if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
  StringPrefMember cloud_print_connector_email_;
  BooleanPrefMember cloud_print_connector_enabled_;
  bool cloud_print_connector_ui_enabled_;
#endif

#if defined(ENABLE_WIFI_BOOTSTRAPPING)
  scoped_ptr<wifi::WifiManager> wifi_manager_;
  scoped_ptr<wifi::BootstrappingDeviceLister> bootstrapping_device_lister_;
#endif

  DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUIHandler);
};

#undef CLOUD_PRINT_CONNECTOR_UI_AVAILABLE

}  // namespace local_discovery
#endif  // CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_