OLD | NEW |
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 #include "chrome/common/extensions/manifest.h" | 5 #include "chrome/common/extensions/manifest.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/string_split.h" | 10 #include "base/string_split.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 return i != map.end(); | 90 return i != map.end(); |
91 } | 91 } |
92 | 92 |
93 // Returns true if the given |key| can be specified by the manifest |type|. | 93 // Returns true if the given |key| can be specified by the manifest |type|. |
94 bool CanAccessKey(const std::string& key, Manifest::Type type) const { | 94 bool CanAccessKey(const std::string& key, Manifest::Type type) const { |
95 RestrictionMap::const_iterator i = map.find(key); | 95 RestrictionMap::const_iterator i = map.find(key); |
96 return (i != map.end() && (type & i->second) != 0); | 96 return (i != map.end() && (type & i->second) != 0); |
97 } | 97 } |
98 | 98 |
99 RestrictionMap map; | 99 RestrictionMap map; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(Restrictions); |
100 }; | 102 }; |
101 | 103 |
102 base::LazyInstance<Restrictions> g_restrictions; | 104 static base::LazyInstance<Restrictions> g_restrictions = |
| 105 LAZY_INSTANCE_INITIALIZER; |
103 | 106 |
104 } // namespace | 107 } // namespace |
105 | 108 |
106 // static | 109 // static |
107 std::set<std::string> Manifest::GetAllKnownKeys() { | 110 std::set<std::string> Manifest::GetAllKnownKeys() { |
108 std::set<std::string> keys; | 111 std::set<std::string> keys; |
109 const RestrictionMap& map = g_restrictions.Get().map; | 112 const RestrictionMap& map = g_restrictions.Get().map; |
110 for (RestrictionMap::const_iterator i = map.begin(); i != map.end(); i++) | 113 for (RestrictionMap::const_iterator i = map.begin(); i != map.end(); i++) |
111 keys.insert(i->first); | 114 keys.insert(i->first); |
112 return keys; | 115 return keys; |
113 } | 116 } |
114 | 117 |
115 Manifest::Manifest(DictionaryValue* value) : value_(value) {} | 118 Manifest::Manifest(DictionaryValue* value) : value_(value) {} |
116 Manifest::~Manifest() {} | 119 Manifest::~Manifest() {} |
117 | 120 |
118 bool Manifest::ValidateManifest(string16* error) const { | 121 bool Manifest::ValidateManifest(string16* error) const { |
119 Restrictions restrictions = g_restrictions.Get(); | 122 const Restrictions& restrictions = g_restrictions.Get(); |
120 Type type = GetType(); | 123 Type type = GetType(); |
121 | 124 |
122 for (DictionaryValue::key_iterator key = value_->begin_keys(); | 125 for (DictionaryValue::key_iterator key = value_->begin_keys(); |
123 key != value_->end_keys(); ++key) { | 126 key != value_->end_keys(); ++key) { |
124 // When validating the extension manifests, we ignore keys that are not | 127 // When validating the extension manifests, we ignore keys that are not |
125 // recognized for forward compatibility. | 128 // recognized for forward compatibility. |
126 if (!restrictions.IsKnownKey(*key)) { | 129 if (!restrictions.IsKnownKey(*key)) { |
127 // TODO(aa): Consider having an error here in the case of strict error | 130 // TODO(aa): Consider having an error here in the case of strict error |
128 // checking to let developers know when they screw up. | 131 // checking to let developers know when they screw up. |
129 continue; | 132 continue; |
130 } | 133 } |
131 | 134 |
132 if (!restrictions.CanAccessKey(*key, type)) { | 135 if (!restrictions.CanAccessKey(*key, type)) { |
133 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | 136 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( |
134 errors::kFeatureNotAllowed, *key); | 137 errors::kFeatureNotAllowed, *key); |
135 return false; | 138 return false; |
136 } | 139 } |
137 } | 140 } |
138 | 141 |
139 return true; | 142 return true; |
140 } | 143 } |
141 | 144 |
142 bool Manifest::HasKey(const std::string& key) const { | 145 bool Manifest::HasKey(const std::string& key) const { |
143 Restrictions restrictions = g_restrictions.Get(); | 146 const Restrictions& restrictions = g_restrictions.Get(); |
144 return restrictions.CanAccessKey(key, GetType()) && value_->HasKey(key); | 147 return restrictions.CanAccessKey(key, GetType()) && value_->HasKey(key); |
145 } | 148 } |
146 | 149 |
147 bool Manifest::Get( | 150 bool Manifest::Get( |
148 const std::string& path, Value** out_value) const { | 151 const std::string& path, Value** out_value) const { |
149 return CanAccessPath(path) && value_->Get(path, out_value); | 152 return CanAccessPath(path) && value_->Get(path, out_value); |
150 } | 153 } |
151 | 154 |
152 bool Manifest::GetBoolean( | 155 bool Manifest::GetBoolean( |
153 const std::string& path, bool* out_value) const { | 156 const std::string& path, bool* out_value) const { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 } | 221 } |
219 | 222 |
220 bool Manifest::IsHostedApp() const { | 223 bool Manifest::IsHostedApp() const { |
221 return GetType() == kTypeHostedApp; | 224 return GetType() == kTypeHostedApp; |
222 } | 225 } |
223 | 226 |
224 bool Manifest::CanAccessPath(const std::string& path) const { | 227 bool Manifest::CanAccessPath(const std::string& path) const { |
225 std::vector<std::string> components; | 228 std::vector<std::string> components; |
226 base::SplitString(path, '.', &components); | 229 base::SplitString(path, '.', &components); |
227 | 230 |
228 Restrictions restrictions = g_restrictions.Get(); | 231 const Restrictions& restrictions = g_restrictions.Get(); |
229 return restrictions.CanAccessKey(components[0], GetType()); | 232 return restrictions.CanAccessKey(components[0], GetType()); |
230 } | 233 } |
231 | 234 |
232 } // namespace extensions | 235 } // namespace extensions |
OLD | NEW |