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_BROWSER_PRINTING_CLOUD_PRINT_CLOUD_PRINT_PROXY_SERVICE_H_
6#define CHROME_BROWSER_PRINTING_CLOUD_PRINT_CLOUD_PRINT_PROXY_SERVICE_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/callback_forward.h"
13#include "base/memory/weak_ptr.h"
14#include "base/observer_list.h"
15#include "base/prefs/pref_change_registrar.h"
16#include "components/keyed_service/core/keyed_service.h"
17
18class Profile;
19class ServiceProcessControl;
20
21namespace base {
22class DictionaryValue;
23}  // namespace base
24
25namespace cloud_print {
26struct CloudPrintProxyInfo;
27}  // namespace cloud_print
28
29// Layer between the browser user interface and the cloud print proxy code
30// running in the service process.
31class CloudPrintProxyService : public KeyedService {
32 public:
33  explicit CloudPrintProxyService(Profile* profile);
34  virtual ~CloudPrintProxyService();
35
36  typedef base::Callback<void(const std::vector<std::string>&)>
37      PrintersCallback;
38
39  // Initializes the object. This should be called every time an object of this
40  // class is constructed.
41  void Initialize();
42
43  // Returns list of printer names available for registration.
44  void GetPrinters(const PrintersCallback& callback);
45
46  // Enables/disables cloud printing for the user
47  virtual void EnableForUserWithRobot(
48      const std::string& robot_auth_code,
49      const std::string& robot_email,
50      const std::string& user_email,
51      const base::DictionaryValue& user_settings);
52  virtual void DisableForUser();
53
54  // Query the service process for the status of the cloud print proxy and
55  // update the browser prefs.
56  void RefreshStatusFromService();
57
58  // Disable the service if the policy to do so is set, and once the
59  // disablement is verified, quit the browser. Returns true if the policy is
60  // not set or the connector was not enabled.
61  bool EnforceCloudPrintConnectorPolicyAndQuit();
62
63  std::string proxy_id() const { return proxy_id_; }
64
65 private:
66  // NotificationDelegate implementation for the token expired notification.
67  class TokenExpiredNotificationDelegate;
68  friend class TokenExpiredNotificationDelegate;
69
70  // Methods that send an IPC to the service.
71  void GetCloudPrintProxyPrinters(const PrintersCallback& callback);
72  void RefreshCloudPrintProxyStatus();
73  void EnableCloudPrintProxyWithRobot(
74      const std::string& robot_auth_code,
75      const std::string& robot_email,
76      const std::string& user_email,
77      const base::DictionaryValue* user_preferences);
78  void DisableCloudPrintProxy();
79
80  // Callback that gets the cloud print proxy info.
81  void ProxyInfoCallback(
82    const cloud_print::CloudPrintProxyInfo& proxy_info);
83
84  // Invoke a task that gets run after the service process successfully
85  // launches. The task typically involves sending an IPC to the service
86  // process.
87  bool InvokeServiceTask(const base::Closure& task);
88
89  // Checks the policy. Returns true if nothing needs to be done (the policy is
90  // not set or the connector is not enabled).
91  bool ApplyCloudPrintConnectorPolicy();
92
93  Profile* profile_;
94  std::string proxy_id_;
95
96  // Virtual for testing.
97  virtual ServiceProcessControl* GetServiceProcessControl();
98
99  // For watching for connector policy changes.
100  PrefChangeRegistrar pref_change_registrar_;
101
102  // If set, continue trying to disable the connector, and quit the process
103  // once successful.
104  bool enforcing_connector_policy_;
105
106  base::WeakPtrFactory<CloudPrintProxyService> weak_factory_;
107
108  DISALLOW_COPY_AND_ASSIGN(CloudPrintProxyService);
109};
110
111#endif  // CHROME_BROWSER_PRINTING_CLOUD_PRINT_CLOUD_PRINT_PROXY_SERVICE_H_
112