OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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_SERVICE_CLOUD_PRINT_CLOUD_PRINT_CONNECTOR_H_ |
| 6 #define CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_CONNECTOR_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <list> |
| 10 #include <map> |
| 11 #include <string> |
| 12 |
| 13 #include "base/threading/thread.h" |
| 14 #include "base/values.h" |
| 15 #include "chrome/service/cloud_print/print_system.h" |
| 16 #include "chrome/service/cloud_print/printer_job_handler.h" |
| 17 |
| 18 // CloudPrintConnector handles top printer management tasks. |
| 19 // - Matching local and cloud printers |
| 20 // - Registration of local printers |
| 21 // - Deleting cloud printers |
| 22 // All tasks are posted to the commond queue (PendingTasks) and executed |
| 23 // one-by-one in FIFO order. |
| 24 // CloudPrintConnector will notify client over Client interface. |
| 25 class CloudPrintConnector |
| 26 : public base::RefCountedThreadSafe<CloudPrintConnector>, |
| 27 public cloud_print::PrintServerWatcherDelegate, |
| 28 public PrinterJobHandlerDelegate, |
| 29 public CloudPrintURLFetcherDelegate { |
| 30 public: |
| 31 class Client { |
| 32 public: |
| 33 virtual void OnPrintersAvailable(const printing::PrinterList& printers) = 0; |
| 34 virtual void OnAuthFailed() = 0; |
| 35 protected: |
| 36 virtual ~Client() {} |
| 37 }; |
| 38 |
| 39 CloudPrintConnector(Client* client, |
| 40 const std::string& proxy_id, |
| 41 const GURL& cloud_print_server_url, |
| 42 const DictionaryValue* print_system_settings); |
| 43 virtual ~CloudPrintConnector(); |
| 44 |
| 45 bool Start(); |
| 46 void Stop(); |
| 47 bool IsRunning(); |
| 48 |
| 49 // Register printer from the list. |
| 50 void RegisterPrinters(const printing::PrinterList& printers); |
| 51 |
| 52 // Check for jobs for specific printer. If printer id is empty |
| 53 // jobs will be checked for all available printers. |
| 54 void CheckForJobs(const std::string& reason, const std::string& printer_id); |
| 55 |
| 56 // cloud_print::PrintServerWatcherDelegate implementation |
| 57 virtual void OnPrinterAdded(); |
| 58 // PrinterJobHandler::Delegate implementation |
| 59 virtual void OnPrinterDeleted(const std::string& printer_name); |
| 60 virtual void OnAuthError(); |
| 61 |
| 62 // CloudPrintURLFetcher::Delegate implementation. |
| 63 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( |
| 64 const content::URLFetcher* source, |
| 65 const GURL& url, |
| 66 const std::string& data); |
| 67 |
| 68 virtual CloudPrintURLFetcher::ResponseAction HandleJSONData( |
| 69 const content::URLFetcher* source, |
| 70 const GURL& url, |
| 71 base::DictionaryValue* json_data, |
| 72 bool succeeded); |
| 73 virtual CloudPrintURLFetcher::ResponseAction OnRequestAuthError(); |
| 74 virtual std::string GetAuthHeader(); |
| 75 |
| 76 private: |
| 77 // Prototype for a response handler. |
| 78 typedef CloudPrintURLFetcher::ResponseAction |
| 79 (CloudPrintConnector::*ResponseHandler)( |
| 80 const content::URLFetcher* source, |
| 81 const GURL& url, |
| 82 DictionaryValue* json_data, |
| 83 bool succeeded); |
| 84 |
| 85 // Begin response handlers |
| 86 CloudPrintURLFetcher::ResponseAction HandlePrinterListResponse( |
| 87 const content::URLFetcher* source, |
| 88 const GURL& url, |
| 89 DictionaryValue* json_data, |
| 90 bool succeeded); |
| 91 |
| 92 CloudPrintURLFetcher::ResponseAction HandlePrinterDeleteResponse( |
| 93 const content::URLFetcher* source, |
| 94 const GURL& url, |
| 95 DictionaryValue* json_data, |
| 96 bool succeeded); |
| 97 |
| 98 CloudPrintURLFetcher::ResponseAction HandleRegisterPrinterResponse( |
| 99 const content::URLFetcher* source, |
| 100 const GURL& url, |
| 101 DictionaryValue* json_data, |
| 102 bool succeeded); |
| 103 // End response handlers |
| 104 |
| 105 // Helper functions for network requests. |
| 106 void StartGetRequest(const GURL& url, |
| 107 int max_retries, |
| 108 ResponseHandler handler); |
| 109 void StartPostRequest(const GURL& url, |
| 110 int max_retries, |
| 111 const std::string& mime_type, |
| 112 const std::string& post_data, |
| 113 ResponseHandler handler); |
| 114 |
| 115 // Reports a diagnostic message to the server. |
| 116 void ReportUserMessage(const std::string& message_id, |
| 117 const std::string& failure_message); |
| 118 |
| 119 bool RemovePrinterFromList(const std::string& printer_name, |
| 120 printing::PrinterList* printer_list); |
| 121 |
| 122 void InitJobHandlerForPrinter(DictionaryValue* printer_data); |
| 123 |
| 124 enum PendingTaskType { |
| 125 PENDING_PRINTERS_AVAILABLE, |
| 126 PENDING_PRINTER_REGISTER, |
| 127 PENDING_PRINTER_DELETE |
| 128 }; |
| 129 |
| 130 struct PendingTask { |
| 131 PendingTaskType type; |
| 132 // Optional members, depending on type. |
| 133 std::string printer_id; // For pending delete. |
| 134 printing::PrinterBasicInfo printer_info; // For pending registration. |
| 135 |
| 136 PendingTask() {} |
| 137 ~PendingTask() {} |
| 138 }; |
| 139 |
| 140 void AddPendingAvailableTask(); |
| 141 void AddPendingDeleteTask(const std::string& id); |
| 142 void AddPendingRegisterTask(const printing::PrinterBasicInfo& info); |
| 143 void AddPendingTask(const PendingTask& task); |
| 144 void ProcessPendingTask(); |
| 145 void ContinuePendingTaskProcessing(); |
| 146 void OnPrintersAvailable(); |
| 147 void OnPrinterRegister(const printing::PrinterBasicInfo& info); |
| 148 void OnPrinterDelete(const std::string& name); |
| 149 |
| 150 void OnReceivePrinterCaps( |
| 151 bool succeeded, |
| 152 const std::string& printer_name, |
| 153 const printing::PrinterCapsAndDefaults& caps_and_defaults); |
| 154 |
| 155 bool IsSamePrinter(const std::string& name1, const std::string& name2) const; |
| 156 |
| 157 // CloudPrintConnector client. |
| 158 Client* client_; |
| 159 // Print system settings. |
| 160 scoped_ptr<DictionaryValue> print_system_settings_; |
| 161 // Pointer to current print system. |
| 162 scoped_refptr<cloud_print::PrintSystem> print_system_; |
| 163 // Watcher for print system updates. |
| 164 scoped_refptr<cloud_print::PrintSystem::PrintServerWatcher> |
| 165 print_server_watcher_; |
| 166 // Id of the Cloud Print proxy. |
| 167 std::string proxy_id_; |
| 168 // Cloud Print server url. |
| 169 GURL cloud_print_server_url_; |
| 170 // A map of printer id to job handler. |
| 171 typedef std::map<std::string, scoped_refptr<PrinterJobHandler> > |
| 172 JobHandlerMap; |
| 173 JobHandlerMap job_handler_map_; |
| 174 // Next response handler. |
| 175 ResponseHandler next_response_handler_; |
| 176 // The list of peding tasks to be done in the background. |
| 177 std::list<PendingTask> pending_tasks_; |
| 178 // The CloudPrintURLFetcher instance for the current request. |
| 179 scoped_refptr<CloudPrintURLFetcher> request_; |
| 180 // The CloudPrintURLFetcher instance for the user message request. |
| 181 scoped_refptr<CloudPrintURLFetcher> user_message_request_; |
| 182 |
| 183 DISALLOW_COPY_AND_ASSIGN(CloudPrintConnector); |
| 184 }; |
| 185 |
| 186 #endif // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_CONNECTOR_H_ |
| 187 |
OLD | NEW |