0a044147627b7a4edb1f09e4ee7328652906884e
[platform/framework/web/crosswalk.git] / src / components / policy / core / common / cloud / cloud_policy_manager.h
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 COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_MANAGER_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_MANAGER_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/prefs/pref_member.h"
15 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
16 #include "components/policy/core/common/cloud/cloud_policy_core.h"
17 #include "components/policy/core/common/cloud/cloud_policy_store.h"
18 #include "components/policy/core/common/cloud/component_cloud_policy_service.h"
19 #include "components/policy/core/common/configuration_policy_provider.h"
20 #include "components/policy/policy_export.h"
21
22 namespace base {
23 class FilePath;
24 class SequencedTaskRunner;
25 }
26
27 namespace net {
28 class URLRequestContextGetter;
29 }
30
31 namespace policy {
32
33 // CloudPolicyManager is the main switching central between cloud policy and the
34 // upper layers of the policy stack. It wires up a CloudPolicyCore to the
35 // ConfigurationPolicyProvider interface.
36 //
37 // This class contains the base functionality, there are subclasses that add
38 // functionality specific to user-level and device-level cloud policy, such as
39 // blocking on initial user policy fetch or device enrollment.
40 class POLICY_EXPORT CloudPolicyManager
41     : public ConfigurationPolicyProvider,
42       public CloudPolicyStore::Observer,
43       public ComponentCloudPolicyService::Delegate {
44  public:
45   // |task_runner| is the runner for policy refresh tasks.
46   // |file_task_runner| is used for file operations. Currently this must be the
47   // FILE BrowserThread.
48   // |io_task_runner| is used for network IO. Currently this must be the IO
49   // BrowserThread.
50   CloudPolicyManager(
51       const PolicyNamespaceKey& policy_ns_key,
52       CloudPolicyStore* cloud_policy_store,
53       const scoped_refptr<base::SequencedTaskRunner>& task_runner,
54       const scoped_refptr<base::SequencedTaskRunner>& file_task_runner,
55       const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
56   virtual ~CloudPolicyManager();
57
58   CloudPolicyCore* core() { return &core_; }
59   const CloudPolicyCore* core() const { return &core_; }
60
61   // ConfigurationPolicyProvider:
62   virtual void Shutdown() OVERRIDE;
63   virtual bool IsInitializationComplete(PolicyDomain domain) const OVERRIDE;
64   virtual void RefreshPolicies() OVERRIDE;
65
66   // CloudPolicyStore::Observer:
67   virtual void OnStoreLoaded(CloudPolicyStore* cloud_policy_store) OVERRIDE;
68   virtual void OnStoreError(CloudPolicyStore* cloud_policy_store) OVERRIDE;
69
70   // ComponentCloudPolicyService::Delegate:
71   virtual void OnComponentCloudPolicyUpdated() OVERRIDE;
72
73  protected:
74   // Check whether fully initialized and if so, publish policy by calling
75   // ConfigurationPolicyStore::UpdatePolicy().
76   void CheckAndPublishPolicy();
77
78   void CreateComponentCloudPolicyService(
79       const base::FilePath& policy_cache_path,
80       const scoped_refptr<net::URLRequestContextGetter>& request_context);
81
82   void ClearAndDestroyComponentCloudPolicyService();
83
84   // Convenience accessors to core() components.
85   CloudPolicyClient* client() { return core_.client(); }
86   const CloudPolicyClient* client() const { return core_.client(); }
87   CloudPolicyStore* store() { return core_.store(); }
88   const CloudPolicyStore* store() const { return core_.store(); }
89   CloudPolicyService* service() { return core_.service(); }
90   const CloudPolicyService* service() const { return core_.service(); }
91   ComponentCloudPolicyService* component_policy_service() const {
92     return component_policy_service_.get();
93   }
94
95  private:
96   // Completion handler for policy refresh operations.
97   void OnRefreshComplete(bool success);
98
99   CloudPolicyCore core_;
100   scoped_ptr<ComponentCloudPolicyService> component_policy_service_;
101
102   // Whether there's a policy refresh operation pending, in which case all
103   // policy update notifications are deferred until after it completes.
104   bool waiting_for_policy_refresh_;
105
106   scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
107   scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
108
109   DISALLOW_COPY_AND_ASSIGN(CloudPolicyManager);
110 };
111
112 }  // namespace policy
113
114 #endif  // COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_MANAGER_H_