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 |
| 44 bool Start(); |
| 45 void Stop(); |
| 46 bool IsRunning(); |
| 47 |
| 48 // Register printer from the list. |
| 49 void RegisterPrinters(const printing::PrinterList& printers); |
| 50 |
| 51 // Check for jobs for specific printer. If printer id is empty |
| 52 // jobs will be checked for all available printers. |
| 53 void CheckForJobs(const std::string& reason, const std::string& printer_id); |
| 54 |
| 55 // cloud_print::PrintServerWatcherDelegate implementation |
| 56 virtual void OnPrinterAdded(); |
| 57 // PrinterJobHandler::Delegate implementation |
| 58 virtual void OnPrinterDeleted(const std::string& printer_name); |
| 59 virtual void OnAuthError(); |
| 60 |
| 61 // CloudPrintURLFetcher::Delegate implementation. |
| 62 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( |
| 63 const URLFetcher* source, |
| 64 const GURL& url, |
| 65 const std::string& data); |
| 66 |
| 67 virtual CloudPrintURLFetcher::ResponseAction HandleJSONData( |
| 68 const URLFetcher* source, |
| 69 const GURL& url, |
| 70 DictionaryValue* json_data, |
| 71 bool succeeded); |
| 72 virtual CloudPrintURLFetcher::ResponseAction OnRequestAuthError(); |
| 73 virtual std::string GetAuthHeader(); |
| 74 |
| 75 private: |
| 76 // Prototype for a response handler. |
| 77 typedef CloudPrintURLFetcher::ResponseAction |
| 78 (CloudPrintConnector::*ResponseHandler)( |
| 79 const URLFetcher* source, |
| 80 const GURL& url, |
| 81 DictionaryValue* json_data, |
| 82 bool succeeded); |
| 83 |
| 84 // Begin response handlers |
| 85 CloudPrintURLFetcher::ResponseAction HandlePrinterListResponse( |
| 86 const URLFetcher* source, |
| 87 const GURL& url, |
| 88 DictionaryValue* json_data, |
| 89 bool succeeded); |
| 90 |
| 91 CloudPrintURLFetcher::ResponseAction HandlePrinterDeleteResponse( |
| 92 const URLFetcher* source, |
| 93 const GURL& url, |
| 94 DictionaryValue* json_data, |
| 95 bool succeeded); |
| 96 |
| 97 CloudPrintURLFetcher::ResponseAction HandleRegisterPrinterResponse( |
| 98 const URLFetcher* source, |
| 99 const GURL& url, |
| 100 DictionaryValue* json_data, |
| 101 bool succeeded); |
| 102 // End response handlers |
| 103 |
| 104 // Helper functions for network requests. |
| 105 void StartGetRequest(const GURL& url, |
| 106 int max_retries, |
| 107 ResponseHandler handler); |
| 108 void StartPostRequest(const GURL& url, |
| 109 int max_retries, |
| 110 const std::string& mime_type, |
| 111 const std::string& post_data, |
| 112 ResponseHandler handler); |
| 113 |
| 114 // Reports a diagnostic message to the server. |
| 115 void ReportUserMessage(const std::string& message_id, |
| 116 const std::string& failure_message); |
| 117 |
| 118 bool RemovePrinterFromList(const std::string& printer_name, |
| 119 printing::PrinterList* printer_list); |
| 120 |
| 121 void InitJobHandlerForPrinter(DictionaryValue* printer_data); |
| 122 |
| 123 enum PendingTaskType { |
| 124 PEDNING_PRINTERS_AVAILABLE, |
| 125 PEDNING_PRINTER_REGISTER, |
| 126 PEDNING_PRINTER_DELETE |
| 127 }; |
| 128 |
| 129 struct PendingTask { |
| 130 PendingTaskType type; |
| 131 // Optional members, depending on type. |
| 132 std::string printer_id; // For pending delete. |
| 133 printing::PrinterBasicInfo printer_info; // For pending registration. |
| 134 |
| 135 PendingTask() {} |
| 136 ~PendingTask() {} |
| 137 }; |
| 138 |
| 139 void AddPendingAvailableTask(); |
| 140 void AddPendingDeleteTask(const std::string& id); |
| 141 void AddPendingRegisterTask(const printing::PrinterBasicInfo& info); |
| 142 void AddPendingTask(const PendingTask& task); |
| 143 void ProcessPendingTask(); |
| 144 void ContinuePendingTaskProcessing(); |
| 145 void OnPrintersAvailable(); |
| 146 void OnPrinterRegister(const printing::PrinterBasicInfo& info); |
| 147 void OnPrinterDelete(const std::string& name); |
| 148 |
| 149 void OnReceivePrinterCaps( |
| 150 bool succeeded, |
| 151 const std::string& printer_name, |
| 152 const printing::PrinterCapsAndDefaults& caps_and_defaults); |
| 153 |
| 154 bool IsSamePrinter(const std::string& name1, const std::string& name2) const; |
| 155 |
| 156 // CloudPrintConnector client. |
| 157 Client* client_; |
| 158 // Print system settings. |
| 159 scoped_ptr<DictionaryValue> print_system_settings_; |
| 160 // Pointer to current print system. |
| 161 scoped_refptr<cloud_print::PrintSystem> print_system_; |
| 162 // Watcher for print system updates. |
| 163 scoped_refptr<cloud_print::PrintSystem::PrintServerWatcher> |
| 164 print_server_watcher_; |
| 165 // Id of the Cloud Print proxy. |
| 166 std::string proxy_id_; |
| 167 // Cloud Print server url. |
| 168 GURL cloud_print_server_url_; |
| 169 // A map of printer id to job handler. |
| 170 typedef std::map<std::string, scoped_refptr<PrinterJobHandler> > |
| 171 JobHandlerMap; |
| 172 JobHandlerMap job_handler_map_; |
| 173 // Next response handler. |
| 174 ResponseHandler next_response_handler_; |
| 175 // The list of peding tasks to be done in the background. |
| 176 std::list<PendingTask> pedning_tasks_; |
| 177 // The CloudPrintURLFetcher instance for the current request. |
| 178 scoped_refptr<CloudPrintURLFetcher> request_; |
| 179 // The CloudPrintURLFetcher instance for the user message request. |
| 180 scoped_refptr<CloudPrintURLFetcher> user_message_request_; |
| 181 |
| 182 DISALLOW_COPY_AND_ASSIGN(CloudPrintConnector); |
| 183 }; |
| 184 |
| 185 #endif // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_CONNECTOR_H_ |
| 186 |
OLD | NEW |