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 "chrome/browser/local_discovery/cloud_print_printer_list.h"
13 #include "chrome/browser/local_discovery/privet_device_lister.h"
14 #include "chrome/browser/local_discovery/privet_http.h"
15 #include "components/signin/core/browser/signin_manager.h"
16 #include "content/public/browser/web_ui_message_handler.h"
18 #if defined(ENABLE_PRINT_PREVIEW) && !defined(OS_CHROMEOS)
19 #define CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
22 // TODO(noamsml): Factor out full registration flow into single class
23 namespace local_discovery
{
25 class PrivetConfirmApiCallFlow
;
26 class PrivetHTTPAsynchronousFactory
;
27 class PrivetHTTPResolution
;
28 class PrivetV1HTTPClient
;
29 class ServiceDiscoverySharedClient
;
31 // UI Handler for chrome://devices/
32 // It listens to local discovery notifications and passes those notifications
33 // into the Javascript to update the page.
34 class LocalDiscoveryUIHandler
: public content::WebUIMessageHandler
,
35 public PrivetRegisterOperation::Delegate
,
36 public PrivetDeviceLister::Delegate
,
37 public CloudDeviceListDelegate
,
38 public SigninManagerBase::Observer
{
40 LocalDiscoveryUIHandler();
41 ~LocalDiscoveryUIHandler() override
;
43 static bool GetHasVisible();
45 // WebUIMessageHandler implementation.
46 void RegisterMessages() override
;
47 // PrivetRegisterOperation::Delegate implementation.
48 void OnPrivetRegisterClaimToken(PrivetRegisterOperation
* operation
,
49 const std::string
& token
,
50 const GURL
& url
) override
;
51 void OnPrivetRegisterError(PrivetRegisterOperation
* operation
,
52 const std::string
& action
,
53 PrivetRegisterOperation::FailureReason reason
,
54 int printer_http_code
,
55 const base::DictionaryValue
* json
) override
;
56 void OnPrivetRegisterDone(PrivetRegisterOperation
* operation
,
57 const std::string
& device_id
) override
;
59 // PrivetDeviceLister::Delegate implementation.
60 void DeviceChanged(bool added
,
61 const std::string
& name
,
62 const DeviceDescription
& description
) override
;
63 void DeviceRemoved(const std::string
& name
) override
;
64 void DeviceCacheFlushed() override
;
66 // CloudDeviceListDelegate implementation.
67 void OnDeviceListReady(const std::vector
<Device
>& devices
) override
;
68 void OnDeviceListUnavailable() override
;
70 // SigninManagerBase::Observer implementation.
71 void GoogleSigninSucceeded(const std::string
& account_id
,
72 const std::string
& username
,
73 const std::string
& password
) override
;
74 void GoogleSignedOut(const std::string
& account_id
,
75 const std::string
& username
) override
;
78 typedef std::map
<std::string
, DeviceDescription
> DeviceDescriptionMap
;
79 typedef base::Callback
<void(bool result
)> ResultCallback
;
82 // For when the page is ready to receive device notifications.
83 void HandleStart(const base::ListValue
* args
);
85 // For when a visibility change occurs.
86 void HandleIsVisible(const base::ListValue
* args
);
88 // For when a user choice is made.
89 void HandleRegisterDevice(const base::ListValue
* args
);
91 // For when a cancellation is made.
92 void HandleCancelRegistration(const base::ListValue
* args
);
94 // For requesting the device list.
95 void HandleRequestDeviceList(const base::ListValue
* args
);
97 // For opening URLs (relative to the Google Cloud Print base URL) in a new
99 void HandleOpenCloudPrintURL(const base::ListValue
* args
);
101 // For showing sync login UI.
102 void HandleShowSyncUI(const base::ListValue
* args
);
104 // For when the IP address of the printer has been resolved for registration.
105 void StartRegisterHTTP(scoped_ptr
<PrivetHTTPClient
> http_client
);
107 // For when the confirm operation on the cloudprint server has finished
109 void OnConfirmDone(GCDApiFlow::Status status
);
111 // Signal to the web interface an error has ocurred while registering.
112 void SendRegisterError();
114 // Singal to the web interface that registration has finished.
115 void SendRegisterDone(const std::string
& service_name
);
117 // Set the visibility of the page.
118 void SetIsVisible(bool visible
);
120 // Get the sync account email.
121 std::string
GetSyncAccount();
123 // Reset and cancel the current registration.
124 void ResetCurrentRegistration();
126 scoped_ptr
<GCDApiFlow
> CreateApiFlow();
129 // Announcement hasn't been sent for a certain time after registration
130 // finished. Consider it failed.
131 // TODO(noamsml): Re-resolve service first.
132 void OnAnnouncementTimeoutReached();
134 void CheckUserLoggedIn();
136 void CheckListingDone();
138 bool IsUserSupervisedOrOffTheRecord();
140 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
141 void StartCloudPrintConnector();
142 void OnCloudPrintPrefsChanged();
143 void ShowCloudPrintSetupDialog(const base::ListValue
* args
);
144 void HandleDisableCloudPrintConnector(const base::ListValue
* args
);
145 void SetupCloudPrintConnectorSection();
146 void RemoveCloudPrintConnectorSection();
147 void RefreshCloudPrintStatusFromService();
150 // A map of current device descriptions provided by the PrivetDeviceLister.
151 DeviceDescriptionMap device_descriptions_
;
153 // The service discovery client used listen for devices on the local network.
154 scoped_refptr
<ServiceDiscoverySharedClient
> service_discovery_client_
;
156 // A factory for creating the privet HTTP Client.
157 scoped_ptr
<PrivetHTTPAsynchronousFactory
> privet_http_factory_
;
159 // An object representing the resolution process for the privet_http_factory.
160 scoped_ptr
<PrivetHTTPResolution
> privet_resolution_
;
162 // The current HTTP client (used for the current operation).
163 scoped_ptr
<PrivetV1HTTPClient
> current_http_client_
;
165 // The current register operation. Only one allowed at any time.
166 scoped_ptr
<PrivetRegisterOperation
> current_register_operation_
;
168 // The current confirm call used during the registration flow.
169 scoped_ptr
<GCDApiFlow
> confirm_api_call_flow_
;
171 // The device lister used to list devices on the local network.
172 scoped_ptr
<PrivetDeviceLister
> privet_lister_
;
174 // Whether or not the page is marked as visible.
177 // List of printers from cloud print.
178 scoped_ptr
<GCDApiFlow
> cloud_print_printer_list_
;
179 std::vector
<Device
> cloud_devices_
;
180 int failed_list_count_
;
181 int succeded_list_count_
;
183 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
184 StringPrefMember cloud_print_connector_email_
;
185 BooleanPrefMember cloud_print_connector_enabled_
;
186 bool cloud_print_connector_ui_enabled_
;
189 DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUIHandler
);
192 #undef CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
194 } // namespace local_discovery
195 #endif // CHROME_BROWSER_UI_WEBUI_LOCAL_DISCOVERY_LOCAL_DISCOVERY_UI_HANDLER_H_