Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / components / ownership / owner_settings_service.h
1 // Copyright 2014 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_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_
6 #define COMPONENTS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/callback_forward.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread_checker.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "components/ownership/ownership_export.h"
19 #include "policy/proto/device_management_backend.pb.h"
20
21 namespace base {
22 class TaskRunner;
23 }
24
25 namespace ownership {
26 class OwnerKeyUtil;
27 class PrivateKey;
28 class PublicKey;
29
30 // This class is a common interface for platform-specific classes
31 // which deal with ownership, keypairs and owner-related settings.
32 class OWNERSHIP_EXPORT OwnerSettingsService : public KeyedService {
33  public:
34   typedef base::Callback<void(std::string policy_blob)>
35       AssembleAndSignPolicyAsyncCallback;
36
37   typedef base::Callback<void(bool is_owner)> IsOwnerCallback;
38
39   explicit OwnerSettingsService(
40       const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util);
41   virtual ~OwnerSettingsService();
42
43   base::WeakPtr<OwnerSettingsService> as_weak_ptr() {
44     return weak_factory_.GetWeakPtr();
45   }
46
47   // Returns whether current user is owner or not. When this method
48   // is called too early, incorrect result can be returned because
49   // private key loading may be in progress.
50   bool IsOwner();
51
52   // Determines whether current user is owner or not, responds via
53   // |callback|.
54   void IsOwnerAsync(const IsOwnerCallback& callback);
55
56   // Assembles and signs |policy| on the |task_runner|, responds on
57   // the original thread via |callback|.
58   bool AssembleAndSignPolicyAsync(
59       base::TaskRunner* task_runner,
60       scoped_ptr<enterprise_management::PolicyData> policy,
61       const AssembleAndSignPolicyAsyncCallback& callback);
62
63   // Signs |settings| with the private half of the owner key and sends
64   // the resulting policy blob for storage. The
65   // result of the operation is reported through |callback|.
66   virtual void SignAndStorePolicyAsync(
67       scoped_ptr<enterprise_management::PolicyData> policy,
68       const base::Closure& callback) = 0;
69
70  protected:
71   void ReloadKeypair();
72
73   void OnKeypairLoaded(const scoped_refptr<PublicKey>& public_key,
74                        const scoped_refptr<PrivateKey>& private_key);
75
76   // Platform-specific keypair loading algorithm.
77   virtual void ReloadKeypairImpl(const base::Callback<
78       void(const scoped_refptr<PublicKey>& public_key,
79            const scoped_refptr<PrivateKey>& private_key)>& callback) = 0;
80
81   // Plafrom-specific actions which should be performed when keypair is loaded.
82   virtual void OnPostKeypairLoadedActions() = 0;
83
84   scoped_refptr<ownership::PublicKey> public_key_;
85
86   scoped_refptr<ownership::PrivateKey> private_key_;
87
88   scoped_refptr<ownership::OwnerKeyUtil> owner_key_util_;
89
90   std::vector<IsOwnerCallback> pending_is_owner_callbacks_;
91
92   base::ThreadChecker thread_checker_;
93
94  private:
95   base::WeakPtrFactory<OwnerSettingsService> weak_factory_;
96
97   DISALLOW_COPY_AND_ASSIGN(OwnerSettingsService);
98 };
99
100 }  // namespace ownership
101
102 #endif  // COMPONENTS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_