1 // Copyright (c) 2012 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_SERVICE_CLOUD_PRINT_CLOUD_PRINT_CONNECTOR_H_
6 #define CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_CONNECTOR_H_
12 #include "base/threading/thread.h"
13 #include "base/values.h"
14 #include "chrome/service/cloud_print/connector_settings.h"
15 #include "chrome/service/cloud_print/print_system.h"
16 #include "chrome/service/cloud_print/printer_job_handler.h"
18 namespace cloud_print
{
20 // CloudPrintConnector handles top printer management tasks.
21 // - Matching local and cloud printers
22 // - Registration of local printers
23 // - Deleting cloud printers
24 // All tasks are posted to the common queue (PendingTasks) and executed
25 // one-by-one in FIFO order.
26 // CloudPrintConnector will notify client over Client interface.
27 class CloudPrintConnector
28 : public base::RefCountedThreadSafe
<CloudPrintConnector
>,
29 private PrintSystem::PrintServerWatcher::Delegate
,
30 private PrinterJobHandlerDelegate
,
31 private CloudPrintURLFetcherDelegate
{
35 virtual void OnAuthFailed() = 0;
36 virtual void OnXmppPingUpdated(int ping_timeout
) = 0;
41 CloudPrintConnector(Client
* client
, const ConnectorSettings
& settings
);
47 // Return list of printer ids registered with CloudPrint.
48 void GetPrinterIds(std::list
<std::string
>* printer_ids
);
50 // Check for jobs for specific printer. If printer id is empty
51 // jobs will be checked for all available printers.
52 void CheckForJobs(const std::string
& reason
, const std::string
& printer_id
);
54 // Update settings for specific printer.
55 void UpdatePrinterSettings(const std::string
& printer_id
);
58 friend class base::RefCountedThreadSafe
<CloudPrintConnector
>;
60 // Prototype for a response handler.
61 typedef CloudPrintURLFetcher::ResponseAction
62 (CloudPrintConnector::*ResponseHandler
)(
63 const net::URLFetcher
* source
,
65 base::DictionaryValue
* json_data
,
68 enum PendingTaskType
{
69 PENDING_PRINTERS_NONE
,
70 PENDING_PRINTERS_AVAILABLE
,
71 PENDING_PRINTER_REGISTER
,
72 PENDING_PRINTER_DELETE
75 // TODO(vitalybuka): Consider delete pending_tasks_ and just use MessageLoop.
78 // Optional members, depending on type.
79 std::string printer_id
; // For pending delete.
80 printing::PrinterBasicInfo printer_info
; // For pending registration.
82 PendingTask() : type(PENDING_PRINTERS_NONE
) {}
86 ~CloudPrintConnector() override
;
87 // PrintServerWatcherDelegate implementation
88 void OnPrinterAdded() override
;
89 // PrinterJobHandler::Delegate implementation
90 void OnPrinterDeleted(const std::string
& printer_name
) override
;
91 void OnAuthError() override
;
93 // CloudPrintURLFetcher::Delegate implementation.
94 CloudPrintURLFetcher::ResponseAction
HandleRawData(
95 const net::URLFetcher
* source
,
97 const std::string
& data
) override
;
98 CloudPrintURLFetcher::ResponseAction
HandleJSONData(
99 const net::URLFetcher
* source
,
101 base::DictionaryValue
* json_data
,
102 bool succeeded
) override
;
103 CloudPrintURLFetcher::ResponseAction
OnRequestAuthError() override
;
104 std::string
GetAuthHeader() override
;
106 // Begin response handlers
107 CloudPrintURLFetcher::ResponseAction
HandlePrinterListResponse(
108 const net::URLFetcher
* source
,
110 base::DictionaryValue
* json_data
,
113 CloudPrintURLFetcher::ResponseAction
HandlePrinterListResponseSettingsUpdate(
114 const net::URLFetcher
* source
,
116 base::DictionaryValue
* json_data
,
119 CloudPrintURLFetcher::ResponseAction
HandlePrinterDeleteResponse(
120 const net::URLFetcher
* source
,
122 base::DictionaryValue
* json_data
,
125 CloudPrintURLFetcher::ResponseAction
HandleRegisterPrinterResponse(
126 const net::URLFetcher
* source
,
128 base::DictionaryValue
* json_data
,
130 // End response handlers
132 // Helper functions for network requests.
133 void StartGetRequest(const GURL
& url
,
135 ResponseHandler handler
);
136 void StartPostRequest(CloudPrintURLFetcher::RequestType type
,
139 const std::string
& mime_type
,
140 const std::string
& post_data
,
141 ResponseHandler handler
);
143 // Reports a diagnostic message to the server.
144 void ReportUserMessage(const std::string
& message_id
,
145 const std::string
& failure_message
);
147 bool RemovePrinterFromList(const std::string
& printer_name
,
148 printing::PrinterList
* printer_list
);
150 void InitJobHandlerForPrinter(base::DictionaryValue
* printer_data
);
152 void UpdateSettingsFromPrintersList(base::DictionaryValue
* json_data
);
154 void AddPendingAvailableTask();
155 void AddPendingDeleteTask(const std::string
& id
);
156 void AddPendingRegisterTask(const printing::PrinterBasicInfo
& info
);
157 void AddPendingTask(const PendingTask
& task
);
158 void ProcessPendingTask();
159 void ContinuePendingTaskProcessing();
160 void OnPrintersAvailable();
161 void OnPrinterRegister(const printing::PrinterBasicInfo
& info
);
162 void OnPrinterDelete(const std::string
& name
);
164 void OnReceivePrinterCaps(
166 const std::string
& printer_name
,
167 const printing::PrinterCapsAndDefaults
& caps_and_defaults
);
169 // Register printer from the list.
170 void RegisterPrinters(const printing::PrinterList
& printers
);
172 bool IsSamePrinter(const std::string
& name1
, const std::string
& name2
) const;
173 bool InitPrintSystem();
175 void ScheduleStatsReport();
178 // CloudPrintConnector client.
180 // Connector settings.
181 ConnectorSettings settings_
;
182 // Pointer to current print system.
183 scoped_refptr
<PrintSystem
> print_system_
;
184 // Watcher for print system updates.
185 scoped_refptr
<PrintSystem::PrintServerWatcher
>
186 print_server_watcher_
;
187 // A map of printer id to job handler.
188 typedef std::map
<std::string
, scoped_refptr
<PrinterJobHandler
> >
190 JobHandlerMap job_handler_map_
;
191 // Next response handler.
192 ResponseHandler next_response_handler_
;
193 // The list of pending tasks to be done in the background.
194 std::list
<PendingTask
> pending_tasks_
;
195 // The CloudPrintURLFetcher instance for the current request.
196 scoped_refptr
<CloudPrintURLFetcher
> request_
;
197 // The CloudPrintURLFetcher instance for the user message request.
198 scoped_refptr
<CloudPrintURLFetcher
> user_message_request_
;
199 base::WeakPtrFactory
<CloudPrintConnector
> stats_ptr_factory_
;
201 DISALLOW_COPY_AND_ASSIGN(CloudPrintConnector
);
204 } // namespace cloud_print
206 #endif // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_CONNECTOR_H_