summaryrefslogtreecommitdiffstats
path: root/chrome/service/cloud_print/cloud_print_connector.h
blob: 3ff77e7814365781f73341df3ab5aad01ca45e81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_CONNECTOR_H_
#define CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_CONNECTOR_H_

#include <list>
#include <map>
#include <string>

#include "base/threading/thread.h"
#include "base/values.h"
#include "chrome/service/cloud_print/connector_settings.h"
#include "chrome/service/cloud_print/print_system.h"
#include "chrome/service/cloud_print/printer_job_handler.h"

namespace cloud_print {

// CloudPrintConnector handles top printer management tasks.
//  - Matching local and cloud printers
//  - Registration of local printers
//  - Deleting cloud printers
// All tasks are posted to the common queue (PendingTasks) and executed
// one-by-one in FIFO order.
// CloudPrintConnector will notify client over Client interface.
class CloudPrintConnector
    : public base::RefCountedThreadSafe<CloudPrintConnector>,
      private PrintSystem::PrintServerWatcher::Delegate,
      private PrinterJobHandlerDelegate,
      private CloudPrintURLFetcherDelegate {
 public:
  class Client {
   public:
    virtual void OnAuthFailed() = 0;
    virtual void OnXmppPingUpdated(int ping_timeout) = 0;
   protected:
     virtual ~Client() {}
  };

  CloudPrintConnector(Client* client, const ConnectorSettings& settings);

  bool Start();
  void Stop();
  bool IsRunning();

  // Return list of printer ids registered with CloudPrint.
  void GetPrinterIds(std::list<std::string>* printer_ids);

  // Check for jobs for specific printer. If printer id is empty
  // jobs will be checked for all available printers.
  void CheckForJobs(const std::string& reason, const std::string& printer_id);

  // Update settings for specific printer.
  void UpdatePrinterSettings(const std::string& printer_id);

 private:
  friend class base::RefCountedThreadSafe<CloudPrintConnector>;

  // Prototype for a response handler.
  typedef CloudPrintURLFetcher::ResponseAction
      (CloudPrintConnector::*ResponseHandler)(
          const net::URLFetcher* source,
          const GURL& url,
          base::DictionaryValue* json_data,
          bool succeeded);

  enum PendingTaskType {
    PENDING_PRINTERS_NONE,
    PENDING_PRINTERS_AVAILABLE,
    PENDING_PRINTER_REGISTER,
    PENDING_PRINTER_DELETE
  };

  // TODO(vitalybuka): Consider delete pending_tasks_ and just use MessageLoop.
  struct PendingTask {
    PendingTaskType type;
    // Optional members, depending on type.
    std::string printer_id;  // For pending delete.
    printing::PrinterBasicInfo printer_info;  // For pending registration.

    PendingTask() : type(PENDING_PRINTERS_NONE) {}
    ~PendingTask() {}
  };

  virtual ~CloudPrintConnector();
  // PrintServerWatcherDelegate implementation
  virtual void OnPrinterAdded() OVERRIDE;
  // PrinterJobHandler::Delegate implementation
  virtual void OnPrinterDeleted(const std::string& printer_name) OVERRIDE;
  virtual void OnAuthError() OVERRIDE;

  // CloudPrintURLFetcher::Delegate implementation.
  virtual CloudPrintURLFetcher::ResponseAction HandleRawData(
      const net::URLFetcher* source,
      const GURL& url,
      const std::string& data) OVERRIDE;
  virtual CloudPrintURLFetcher::ResponseAction HandleJSONData(
      const net::URLFetcher* source,
      const GURL& url,
      base::DictionaryValue* json_data,
      bool succeeded) OVERRIDE;
  virtual CloudPrintURLFetcher::ResponseAction OnRequestAuthError() OVERRIDE;
  virtual std::string GetAuthHeader() OVERRIDE;

  // Begin response handlers
  CloudPrintURLFetcher::ResponseAction HandlePrinterListResponse(
      const net::URLFetcher* source,
      const GURL& url,
      base::DictionaryValue* json_data,
      bool succeeded);

  CloudPrintURLFetcher::ResponseAction HandlePrinterListResponseSettingsUpdate(
      const net::URLFetcher* source,
      const GURL& url,
      base::DictionaryValue* json_data,
      bool succeeded);

  CloudPrintURLFetcher::ResponseAction HandlePrinterDeleteResponse(
      const net::URLFetcher* source,
      const GURL& url,
      base::DictionaryValue* json_data,
      bool succeeded);

  CloudPrintURLFetcher::ResponseAction HandleRegisterPrinterResponse(
      const net::URLFetcher* source,
      const GURL& url,
      base::DictionaryValue* json_data,
      bool succeeded);
  // End response handlers

  // Helper functions for network requests.
  void StartGetRequest(const GURL& url,
                       int max_retries,
                       ResponseHandler handler);
  void StartPostRequest(CloudPrintURLFetcher::RequestType type,
                        const GURL& url,
                        int max_retries,
                        const std::string& mime_type,
                        const std::string& post_data,
                        ResponseHandler handler);

  // Reports a diagnostic message to the server.
  void ReportUserMessage(const std::string& message_id,
                         const std::string& failure_message);

  bool RemovePrinterFromList(const std::string& printer_name,
                             printing::PrinterList* printer_list);

  void InitJobHandlerForPrinter(base::DictionaryValue* printer_data);

  void UpdateSettingsFromPrintersList(base::DictionaryValue* json_data);

  void AddPendingAvailableTask();
  void AddPendingDeleteTask(const std::string& id);
  void AddPendingRegisterTask(const printing::PrinterBasicInfo& info);
  void AddPendingTask(const PendingTask& task);
  void ProcessPendingTask();
  void ContinuePendingTaskProcessing();
  void OnPrintersAvailable();
  void OnPrinterRegister(const printing::PrinterBasicInfo& info);
  void OnPrinterDelete(const std::string& name);

  void OnReceivePrinterCaps(
      bool succeeded,
      const std::string& printer_name,
      const printing::PrinterCapsAndDefaults& caps_and_defaults);

  // Register printer from the list.
  void RegisterPrinters(const printing::PrinterList& printers);

  bool IsSamePrinter(const std::string& name1, const std::string& name2) const;
  bool InitPrintSystem();

  void ScheduleStatsReport();
  void ReportStats();

  // CloudPrintConnector client.
  Client* client_;
  // Connector settings.
  ConnectorSettings settings_;
  // Pointer to current print system.
  scoped_refptr<PrintSystem> print_system_;
  // Watcher for print system updates.
  scoped_refptr<PrintSystem::PrintServerWatcher>
      print_server_watcher_;
  // A map of printer id to job handler.
  typedef std::map<std::string, scoped_refptr<PrinterJobHandler> >
      JobHandlerMap;
  JobHandlerMap job_handler_map_;
  // Next response handler.
  ResponseHandler next_response_handler_;
  // The list of pending tasks to be done in the background.
  std::list<PendingTask> pending_tasks_;
  // The CloudPrintURLFetcher instance for the current request.
  scoped_refptr<CloudPrintURLFetcher> request_;
  // The CloudPrintURLFetcher instance for the user message request.
  scoped_refptr<CloudPrintURLFetcher> user_message_request_;
  base::WeakPtrFactory<CloudPrintConnector> stats_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(CloudPrintConnector);
};

}  // namespace cloud_print

#endif  // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_CONNECTOR_H_