[Media Router] Add integration tests and e2e tests for media router and presentation...
[chromium-blink-merge.git] / chrome / service / cloud_print / cloud_print_connector.h
blob6d052474a74835ec343a54ef90930c4e753037e7
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_
8 #include <list>
9 #include <map>
10 #include <string>
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 {
32 public:
33 class Client {
34 public:
35 virtual void OnAuthFailed() = 0;
36 virtual void OnXmppPingUpdated(int ping_timeout) = 0;
37 protected:
38 virtual ~Client() {}
41 CloudPrintConnector(Client* client, const ConnectorSettings& settings);
43 bool Start();
44 void Stop();
45 bool IsRunning();
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);
57 private:
58 friend class base::RefCountedThreadSafe<CloudPrintConnector>;
60 // Prototype for a response handler.
61 typedef CloudPrintURLFetcher::ResponseAction
62 (CloudPrintConnector::*ResponseHandler)(
63 const net::URLFetcher* source,
64 const GURL& url,
65 base::DictionaryValue* json_data,
66 bool succeeded);
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.
76 struct PendingTask {
77 PendingTaskType type;
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) {}
83 ~PendingTask() {}
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,
96 const GURL& url,
97 const std::string& data) override;
98 CloudPrintURLFetcher::ResponseAction HandleJSONData(
99 const net::URLFetcher* source,
100 const GURL& url,
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,
109 const GURL& url,
110 base::DictionaryValue* json_data,
111 bool succeeded);
113 CloudPrintURLFetcher::ResponseAction HandlePrinterListResponseSettingsUpdate(
114 const net::URLFetcher* source,
115 const GURL& url,
116 base::DictionaryValue* json_data,
117 bool succeeded);
119 CloudPrintURLFetcher::ResponseAction HandlePrinterDeleteResponse(
120 const net::URLFetcher* source,
121 const GURL& url,
122 base::DictionaryValue* json_data,
123 bool succeeded);
125 CloudPrintURLFetcher::ResponseAction HandleRegisterPrinterResponse(
126 const net::URLFetcher* source,
127 const GURL& url,
128 base::DictionaryValue* json_data,
129 bool succeeded);
130 // End response handlers
132 // Helper functions for network requests.
133 void StartGetRequest(const GURL& url,
134 int max_retries,
135 ResponseHandler handler);
136 void StartPostRequest(CloudPrintURLFetcher::RequestType type,
137 const GURL& url,
138 int max_retries,
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(
165 bool succeeded,
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();
176 void ReportStats();
178 // CloudPrintConnector client.
179 Client* 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> >
189 JobHandlerMap;
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_