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