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. 4 5 #ifndef CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_CONNECTOR_H_ 6 #define CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_CONNECTOR_H_ 7 8 #include <list> 9 #include <map> 10 #include <string> 11 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" 17 18 namespace cloud_print { 19 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 PrintServerWatcherDelegate, 30 private PrinterJobHandlerDelegate, 31 private CloudPrintURLFetcherDelegate { 32 public: 33 class Client { 34 public: 35 virtual void OnAuthFailed() = 0; 36 protected: 37 virtual ~Client() {} 38 }; 39 40 CloudPrintConnector(Client* client, const ConnectorSettings& settings); 41 42 bool Start(); 43 void Stop(); 44 bool IsRunning(); 45 46 // Return list of printer ids registered with CloudPrint. 47 void GetPrinterIds(std::list<std::string>* printer_ids); 48 49 // Check for jobs for specific printer. If printer id is empty 50 // jobs will be checked for all available printers. 51 void CheckForJobs(const std::string& reason, const std::string& printer_id); 52 53 private: 54 friend class base::RefCountedThreadSafe<CloudPrintConnector>; 55 56 // Prototype for a response handler. 57 typedef CloudPrintURLFetcher::ResponseAction 58 (CloudPrintConnector::*ResponseHandler)( 59 const net::URLFetcher* source, 60 const GURL& url, 61 DictionaryValue* json_data, 62 bool succeeded); 63 64 enum PendingTaskType { 65 PENDING_PRINTERS_NONE, 66 PENDING_PRINTERS_AVAILABLE, 67 PENDING_PRINTER_REGISTER, 68 PENDING_PRINTER_DELETE 69 }; 70 71 // TODO(vitalybuka): Consider delete pending_tasks_ and just use MessageLoop. 72 struct PendingTask { 73 PendingTaskType type; 74 // Optional members, depending on type. 75 std::string printer_id; // For pending delete. 76 printing::PrinterBasicInfo printer_info; // For pending registration. 77 78 PendingTask() : type(PENDING_PRINTERS_NONE) {} 79 ~PendingTask() {} 80 }; 81 82 virtual ~CloudPrintConnector(); 83 // PrintServerWatcherDelegate implementation 84 virtual void OnPrinterAdded() OVERRIDE; 85 // PrinterJobHandler::Delegate implementation 86 virtual void OnPrinterDeleted(const std::string& printer_name) OVERRIDE; 87 virtual void OnAuthError() OVERRIDE; 88 89 // CloudPrintURLFetcher::Delegate implementation. 90 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( 91 const net::URLFetcher* source, 92 const GURL& url, 93 const std::string& data) OVERRIDE; 94 virtual CloudPrintURLFetcher::ResponseAction HandleJSONData( 95 const net::URLFetcher* source, 96 const GURL& url, 97 base::DictionaryValue* json_data, 98 bool succeeded) OVERRIDE; 99 virtual CloudPrintURLFetcher::ResponseAction OnRequestAuthError() OVERRIDE; 100 virtual std::string GetAuthHeader() OVERRIDE; 101 102 // Begin response handlers 103 CloudPrintURLFetcher::ResponseAction HandlePrinterListResponse( 104 const net::URLFetcher* source, 105 const GURL& url, 106 DictionaryValue* json_data, 107 bool succeeded); 108 109 CloudPrintURLFetcher::ResponseAction HandlePrinterDeleteResponse( 110 const net::URLFetcher* source, 111 const GURL& url, 112 DictionaryValue* json_data, 113 bool succeeded); 114 115 CloudPrintURLFetcher::ResponseAction HandleRegisterPrinterResponse( 116 const net::URLFetcher* source, 117 const GURL& url, 118 DictionaryValue* json_data, 119 bool succeeded); 120 // End response handlers 121 122 // Helper functions for network requests. 123 void StartGetRequest(const GURL& url, 124 int max_retries, 125 ResponseHandler handler); 126 void StartPostRequest(const GURL& url, 127 int max_retries, 128 const std::string& mime_type, 129 const std::string& post_data, 130 ResponseHandler handler); 131 132 // Reports a diagnostic message to the server. 133 void ReportUserMessage(const std::string& message_id, 134 const std::string& failure_message); 135 136 bool RemovePrinterFromList(const std::string& printer_name, 137 printing::PrinterList* printer_list); 138 139 void InitJobHandlerForPrinter(DictionaryValue* printer_data); 140 141 void AddPendingAvailableTask(); 142 void AddPendingDeleteTask(const std::string& id); 143 void AddPendingRegisterTask(const printing::PrinterBasicInfo& info); 144 void AddPendingTask(const PendingTask& task); 145 void ProcessPendingTask(); 146 void ContinuePendingTaskProcessing(); 147 void OnPrintersAvailable(); 148 void OnPrinterRegister(const printing::PrinterBasicInfo& info); 149 void OnPrinterDelete(const std::string& name); 150 151 void OnReceivePrinterCaps( 152 bool succeeded, 153 const std::string& printer_name, 154 const printing::PrinterCapsAndDefaults& caps_and_defaults); 155 156 // Register printer from the list. 157 void RegisterPrinters(const printing::PrinterList& printers); 158 159 bool IsSamePrinter(const std::string& name1, const std::string& name2) const; 160 bool InitPrintSystem(); 161 162 // CloudPrintConnector client. 163 Client* client_; 164 // Connector settings. 165 ConnectorSettings settings_; 166 // Pointer to current print system. 167 scoped_refptr<PrintSystem> print_system_; 168 // Watcher for print system updates. 169 scoped_refptr<PrintSystem::PrintServerWatcher> 170 print_server_watcher_; 171 // A map of printer id to job handler. 172 typedef std::map<std::string, scoped_refptr<PrinterJobHandler> > 173 JobHandlerMap; 174 JobHandlerMap job_handler_map_; 175 // Next response handler. 176 ResponseHandler next_response_handler_; 177 // The list of pending tasks to be done in the background. 178 std::list<PendingTask> pending_tasks_; 179 // The CloudPrintURLFetcher instance for the current request. 180 scoped_refptr<CloudPrintURLFetcher> request_; 181 // The CloudPrintURLFetcher instance for the user message request. 182 scoped_refptr<CloudPrintURLFetcher> user_message_request_; 183 184 DISALLOW_COPY_AND_ASSIGN(CloudPrintConnector); 185 }; 186 187 } // namespace cloud_print 188 189 #endif // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_CONNECTOR_H_ 190 191