Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: extensions/common/permissions/set_disjunction_permission.h

Issue 201203006: Move basic API permissions code out of //chrome (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_COMMON_EXTENSIONS_PERMISSIONS_SET_DISJUNCTION_PERMISSION_H_ 5 #ifndef EXTENSIONS_COMMON_PERMISSIONS_SET_DISJUNCTION_PERMISSION_H_
6 #define CHROME_COMMON_EXTENSIONS_PERMISSIONS_SET_DISJUNCTION_PERMISSION_H_ 6 #define EXTENSIONS_COMMON_PERMISSIONS_SET_DISJUNCTION_PERMISSION_H_
7 7
8 #include <algorithm> 8 #include <algorithm>
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
11 11
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "extensions/common/extension_messages.h" 14 #include "extensions/common/extension_messages.h"
15 #include "extensions/common/permissions/api_permission.h" 15 #include "extensions/common/permissions/api_permission.h"
16 #include "ipc/ipc_message.h" 16 #include "ipc/ipc_message.h"
17 #include "ipc/ipc_message_utils.h" 17 #include "ipc/ipc_message_utils.h"
18 18
19 namespace extensions { 19 namespace extensions {
20 20
21 // An abstract base class for permissions that are represented by the 21 // An abstract base class for permissions that are represented by the
22 // disjunction of a set of conditions. Each condition is represented by a 22 // disjunction of a set of conditions. Each condition is represented by a
23 // |PermissionDataType| (e.g. SocketPermissionData). If an 23 // |PermissionDataType| (e.g. SocketPermissionData). If an
24 // APIPermission::CheckParam matches any of the conditions in the set, the 24 // APIPermission::CheckParam matches any of the conditions in the set, the
25 // permission is granted. 25 // permission is granted.
26 // 26 //
27 // For an example of how to use this class, see SocketPermission. 27 // For an example of how to use this class, see SocketPermission.
28 template <class PermissionDataType, class DerivedType> 28 template <class PermissionDataType, class DerivedType>
29 class SetDisjunctionPermission : public APIPermission { 29 class SetDisjunctionPermission : public APIPermission {
30 public: 30 public:
31 explicit SetDisjunctionPermission(const APIPermissionInfo* info) 31 explicit SetDisjunctionPermission(const APIPermissionInfo* info)
32 : APIPermission(info) { 32 : APIPermission(info) {}
33 }
34 33
35 ~SetDisjunctionPermission() { 34 ~SetDisjunctionPermission() {}
36 }
37 35
38 // APIPermission overrides 36 // APIPermission overrides
39 virtual bool HasMessages() const OVERRIDE { 37 virtual bool HasMessages() const OVERRIDE { return !data_set_.empty(); }
40 return !data_set_.empty();
41 }
42 38
43 virtual bool Check(const APIPermission::CheckParam* param) const OVERRIDE { 39 virtual bool Check(const APIPermission::CheckParam* param) const OVERRIDE {
44 for (typename std::set<PermissionDataType>::const_iterator i = 40 for (typename std::set<PermissionDataType>::const_iterator i =
45 data_set_.begin(); i != data_set_.end(); ++i) { 41 data_set_.begin();
42 i != data_set_.end();
43 ++i) {
46 if (i->Check(param)) 44 if (i->Check(param))
47 return true; 45 return true;
48 } 46 }
49 return false; 47 return false;
50 } 48 }
51 49
52 virtual bool Contains(const APIPermission* rhs) const OVERRIDE { 50 virtual bool Contains(const APIPermission* rhs) const OVERRIDE {
53 CHECK(rhs->info() == info()); 51 CHECK(rhs->info() == info());
54 const SetDisjunctionPermission* perm = 52 const SetDisjunctionPermission* perm =
55 static_cast<const SetDisjunctionPermission*>(rhs); 53 static_cast<const SetDisjunctionPermission*>(rhs);
56 return std::includes( 54 return std::includes(data_set_.begin(),
57 data_set_.begin(), data_set_.end(), 55 data_set_.end(),
58 perm->data_set_.begin(), perm->data_set_.end()); 56 perm->data_set_.begin(),
57 perm->data_set_.end());
59 } 58 }
60 59
61 virtual bool Equal(const APIPermission* rhs) const OVERRIDE { 60 virtual bool Equal(const APIPermission* rhs) const OVERRIDE {
62 CHECK(rhs->info() == info()); 61 CHECK(rhs->info() == info());
63 const SetDisjunctionPermission* perm = 62 const SetDisjunctionPermission* perm =
64 static_cast<const SetDisjunctionPermission*>(rhs); 63 static_cast<const SetDisjunctionPermission*>(rhs);
65 return data_set_ == perm->data_set_; 64 return data_set_ == perm->data_set_;
66 } 65 }
67 66
68 virtual APIPermission* Clone() const OVERRIDE { 67 virtual APIPermission* Clone() const OVERRIDE {
69 SetDisjunctionPermission* result = new DerivedType(info()); 68 SetDisjunctionPermission* result = new DerivedType(info());
70 result->data_set_ = data_set_; 69 result->data_set_ = data_set_;
71 return result; 70 return result;
72 } 71 }
73 72
74 virtual APIPermission* Diff(const APIPermission* rhs) const OVERRIDE { 73 virtual APIPermission* Diff(const APIPermission* rhs) const OVERRIDE {
75 CHECK(rhs->info() == info()); 74 CHECK(rhs->info() == info());
76 const SetDisjunctionPermission* perm = 75 const SetDisjunctionPermission* perm =
77 static_cast<const SetDisjunctionPermission*>(rhs); 76 static_cast<const SetDisjunctionPermission*>(rhs);
78 scoped_ptr<SetDisjunctionPermission> result(new DerivedType(info())); 77 scoped_ptr<SetDisjunctionPermission> result(new DerivedType(info()));
79 std::set_difference( 78 std::set_difference(data_set_.begin(),
80 data_set_.begin(), data_set_.end(), 79 data_set_.end(),
81 perm->data_set_.begin(), perm->data_set_.end(), 80 perm->data_set_.begin(),
82 std::inserter<std::set<PermissionDataType> >( 81 perm->data_set_.end(),
83 result->data_set_, result->data_set_.begin())); 82 std::inserter<std::set<PermissionDataType> >(
83 result->data_set_, result->data_set_.begin()));
84 return result->data_set_.empty() ? NULL : result.release(); 84 return result->data_set_.empty() ? NULL : result.release();
85 } 85 }
86 86
87 virtual APIPermission* Union(const APIPermission* rhs) const OVERRIDE { 87 virtual APIPermission* Union(const APIPermission* rhs) const OVERRIDE {
88 CHECK(rhs->info() == info()); 88 CHECK(rhs->info() == info());
89 const SetDisjunctionPermission* perm = 89 const SetDisjunctionPermission* perm =
90 static_cast<const SetDisjunctionPermission*>(rhs); 90 static_cast<const SetDisjunctionPermission*>(rhs);
91 scoped_ptr<SetDisjunctionPermission> result(new DerivedType(info())); 91 scoped_ptr<SetDisjunctionPermission> result(new DerivedType(info()));
92 std::set_union( 92 std::set_union(data_set_.begin(),
93 data_set_.begin(), data_set_.end(), 93 data_set_.end(),
94 perm->data_set_.begin(), perm->data_set_.end(), 94 perm->data_set_.begin(),
95 std::inserter<std::set<PermissionDataType> >( 95 perm->data_set_.end(),
96 result->data_set_, result->data_set_.begin())); 96 std::inserter<std::set<PermissionDataType> >(
97 result->data_set_, result->data_set_.begin()));
97 return result.release(); 98 return result.release();
98 } 99 }
99 100
100 virtual APIPermission* Intersect(const APIPermission* rhs) const OVERRIDE { 101 virtual APIPermission* Intersect(const APIPermission* rhs) const OVERRIDE {
101 CHECK(rhs->info() == info()); 102 CHECK(rhs->info() == info());
102 const SetDisjunctionPermission* perm = 103 const SetDisjunctionPermission* perm =
103 static_cast<const SetDisjunctionPermission*>(rhs); 104 static_cast<const SetDisjunctionPermission*>(rhs);
104 scoped_ptr<SetDisjunctionPermission> result(new DerivedType(info())); 105 scoped_ptr<SetDisjunctionPermission> result(new DerivedType(info()));
105 std::set_intersection( 106 std::set_intersection(data_set_.begin(),
106 data_set_.begin(), data_set_.end(), 107 data_set_.end(),
107 perm->data_set_.begin(), perm->data_set_.end(), 108 perm->data_set_.begin(),
108 std::inserter<std::set<PermissionDataType> >( 109 perm->data_set_.end(),
109 result->data_set_, result->data_set_.begin())); 110 std::inserter<std::set<PermissionDataType> >(
111 result->data_set_, result->data_set_.begin()));
110 return result->data_set_.empty() ? NULL : result.release(); 112 return result->data_set_.empty() ? NULL : result.release();
111 } 113 }
112 114
113 virtual bool FromValue(const base::Value* value, 115 virtual bool FromValue(const base::Value* value,
114 std::string* error) OVERRIDE { 116 std::string* error) OVERRIDE {
115 data_set_.clear(); 117 data_set_.clear();
116 const base::ListValue* list = NULL; 118 const base::ListValue* list = NULL;
117 119
118 if (!value || !value->GetAsList(&list) || list->GetSize() == 0) { 120 if (!value || !value->GetAsList(&list) || list->GetSize() == 0) {
119 if (error) 121 if (error)
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 virtual void Log(std::string* log) const OVERRIDE { 162 virtual void Log(std::string* log) const OVERRIDE {
161 IPC::LogParam(data_set_, log); 163 IPC::LogParam(data_set_, log);
162 } 164 }
163 165
164 protected: 166 protected:
165 std::set<PermissionDataType> data_set_; 167 std::set<PermissionDataType> data_set_;
166 }; 168 };
167 169
168 } // namespace extensions 170 } // namespace extensions
169 171
170 #endif // CHROME_COMMON_EXTENSIONS_PERMISSIONS_SET_DISJUNCTION_PERMISSION_H_ 172 #endif // EXTENSIONS_COMMON_PERMISSIONS_SET_DISJUNCTION_PERMISSION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698