summaryrefslogtreecommitdiffstats
path: root/chrome/service/cloud_print/cloud_print_proxy_backend.h
blob: 603d4cdb340b1f1de647b651923231ff163a64e1 (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
// Copyright (c) 2011 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_PROXY_BACKEND_H_
#define CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_PROXY_BACKEND_H_
#pragma once

#include <string>

#include "base/threading/thread.h"
#include "printing/backend/print_backend.h"

class CloudPrintProxyService;
class GURL;
class DictionaryValue;

namespace gaia {
struct OAuthClientInfo;
}

// CloudPrintProxyFrontend is the interface used by CloudPrintProxyBackend to
// communicate with the entity that created it and, presumably, is interested in
// cloud print proxy related activity.
// NOTE: All methods will be invoked by a CloudPrintProxyBackend on the same
// thread used to create that CloudPrintProxyBackend.
class CloudPrintProxyFrontend {
 public:
  CloudPrintProxyFrontend() {}

  // There is a list of printers available that can be registered.
  virtual void OnPrinterListAvailable(
      const printing::PrinterList& printer_list) = 0;
  // We successfully authenticated with the cloud print server. This callback
  // allows the frontend to persist the tokens.
  virtual void OnAuthenticated(const std::string& robot_oauth_refresh_token,
                               const std::string& robot_email,
                               const std::string& user_email) = 0;
  // We have invalid/expired credentials.
  virtual void OnAuthenticationFailed() = 0;
  // The print system could not be initialized.
  virtual void OnPrintSystemUnavailable() = 0;

 protected:
  // Don't delete through SyncFrontend interface.
  virtual ~CloudPrintProxyFrontend() {
  }
 private:
  DISALLOW_COPY_AND_ASSIGN(CloudPrintProxyFrontend);
};

class CloudPrintProxyBackend {
 public:
  // It is OK for print_system_settings to be NULL. In this case system should
  // use system default settings.
  CloudPrintProxyBackend(
      CloudPrintProxyFrontend* frontend,
      const GURL& cloud_print_server_url,
      const DictionaryValue* print_sys_settings,
      const gaia::OAuthClientInfo& oauth_client_info,
      bool enable_job_poll);
  ~CloudPrintProxyBackend();

  // Called when the user enables Google Cloud Print.
  // |last_robot_refresh_token|, |last_robot_email| and |last_user_email| are
  // the previously persisted credentials if any. We will use this is the passed
  // in LSID belongs to the same user as |last_user_email|.
  bool InitializeWithLsid(const std::string& lsid,
                          const std::string& proxy_id,
                          const std::string& last_robot_refresh_token,
                          const std::string& last_robot_email,
                          const std::string& last_user_email);
  // Legacy mechanism when we have saved user credentials but no saved robot
  // credentials.
  bool InitializeWithToken(const std::string& cloud_print_token,
                           const std::string& proxy_id);
  // Called when we have saved robot credentials.
  bool InitializeWithRobotToken(const std::string& robot_oauth_refresh_token,
                                const std::string& robot_email,
                                const std::string& proxy_id);
  // Called when an external entity passed in the auth code for the robot.
  bool InitializeWithRobotAuthCode(const std::string& robot_oauth_auth_code,
                                   const std::string& robot_email,
                                   const std::string& proxy_id);
  void Shutdown();
  void RegisterPrinters(const printing::PrinterList& printer_list);

 private:
  // The real guts of SyncBackendHost, to keep the public client API clean.
  class Core;
  // A thread we dedicate for use to perform initialization and
  // authentication.
  base::Thread core_thread_;
  // Our core, which communicates with AuthWatcher for GAIA authentication and
  // which contains printer registration code.
  scoped_refptr<Core> core_;
  // A reference to the MessageLoop used to construct |this|, so we know how
  // to safely talk back to the SyncFrontend.
  MessageLoop* const frontend_loop_;
  // The frontend which is responsible for displaying UI and updating Prefs
  CloudPrintProxyFrontend* frontend_;

  friend class base::RefCountedThreadSafe<CloudPrintProxyBackend::Core>;

  DISALLOW_COPY_AND_ASSIGN(CloudPrintProxyBackend);
};

#endif  // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_PROXY_BACKEND_H_