OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/policy/core/common/schema.h" | 5 #include "components/policy/core/common/schema.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <climits> | 8 #include <climits> |
9 #include <map> | 9 #include <map> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/containers/scoped_ptr_map.h" | |
14 #include "base/logging.h" | 13 #include "base/logging.h" |
15 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/stl_util.h" |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "components/json_schema/json_schema_constants.h" | 17 #include "components/json_schema/json_schema_constants.h" |
18 #include "components/json_schema/json_schema_validator.h" | 18 #include "components/json_schema/json_schema_validator.h" |
19 #include "components/policy/core/common/schema_internal.h" | 19 #include "components/policy/core/common/schema_internal.h" |
20 #include "third_party/re2/re2/re2.h" | 20 #include "third_party/re2/re2/re2.h" |
21 | 21 |
22 namespace schema = json_schema_constants; | 22 namespace schema = json_schema_constants; |
23 | 23 |
24 namespace policy { | 24 namespace policy { |
25 | 25 |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 | 239 |
240 // Assigns the IDs in |id_map| to the pending references in the | 240 // Assigns the IDs in |id_map| to the pending references in the |
241 // |reference_list|. If an ID is missing then |error| is set and false is | 241 // |reference_list|. If an ID is missing then |error| is set and false is |
242 // returned; otherwise returns true. | 242 // returned; otherwise returns true. |
243 static bool ResolveReferences(const IdMap& id_map, | 243 static bool ResolveReferences(const IdMap& id_map, |
244 const ReferenceList& reference_list, | 244 const ReferenceList& reference_list, |
245 std::string* error); | 245 std::string* error); |
246 | 246 |
247 // Cache for CompileRegex(), will memorize return value of every call to | 247 // Cache for CompileRegex(), will memorize return value of every call to |
248 // CompileRegex() and return results directly next time. | 248 // CompileRegex() and return results directly next time. |
249 mutable base::ScopedPtrMap<std::string, scoped_ptr<re2::RE2>> regex_cache_; | 249 mutable std::map<std::string, scoped_ptr<re2::RE2>> regex_cache_; |
250 | 250 |
251 SchemaData schema_data_; | 251 SchemaData schema_data_; |
252 std::vector<std::string> strings_; | 252 std::vector<std::string> strings_; |
253 std::vector<SchemaNode> schema_nodes_; | 253 std::vector<SchemaNode> schema_nodes_; |
254 std::vector<PropertyNode> property_nodes_; | 254 std::vector<PropertyNode> property_nodes_; |
255 std::vector<PropertiesNode> properties_nodes_; | 255 std::vector<PropertiesNode> properties_nodes_; |
256 std::vector<RestrictionNode> restriction_nodes_; | 256 std::vector<RestrictionNode> restriction_nodes_; |
257 std::vector<int> int_enums_; | 257 std::vector<int> int_enums_; |
258 std::vector<const char*> string_enums_; | 258 std::vector<const char*> string_enums_; |
259 | 259 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 data->property_nodes = storage->property_nodes_.data(); | 334 data->property_nodes = storage->property_nodes_.data(); |
335 data->properties_nodes = storage->properties_nodes_.data(); | 335 data->properties_nodes = storage->properties_nodes_.data(); |
336 data->restriction_nodes = storage->restriction_nodes_.data(); | 336 data->restriction_nodes = storage->restriction_nodes_.data(); |
337 data->int_enums = storage->int_enums_.data(); | 337 data->int_enums = storage->int_enums_.data(); |
338 data->string_enums = storage->string_enums_.data(); | 338 data->string_enums = storage->string_enums_.data(); |
339 return storage; | 339 return storage; |
340 } | 340 } |
341 | 341 |
342 re2::RE2* Schema::InternalStorage::CompileRegex( | 342 re2::RE2* Schema::InternalStorage::CompileRegex( |
343 const std::string& pattern) const { | 343 const std::string& pattern) const { |
344 base::ScopedPtrMap<std::string, scoped_ptr<re2::RE2>>::const_iterator it = | 344 auto it = regex_cache_.find(pattern); |
345 regex_cache_.find(pattern); | |
346 if (it == regex_cache_.end()) { | 345 if (it == regex_cache_.end()) { |
347 scoped_ptr<re2::RE2> compiled(new re2::RE2(pattern)); | 346 scoped_ptr<re2::RE2> compiled(new re2::RE2(pattern)); |
348 re2::RE2* compiled_ptr = compiled.get(); | 347 re2::RE2* compiled_ptr = compiled.get(); |
349 regex_cache_.insert(pattern, compiled.Pass()); | 348 regex_cache_.insert(std::make_pair(pattern, std::move(compiled))); |
350 return compiled_ptr; | 349 return compiled_ptr; |
351 } | 350 } |
352 return it->second; | 351 return it->second.get(); |
353 } | 352 } |
354 | 353 |
355 // static | 354 // static |
356 void Schema::InternalStorage::DetermineStorageSizes( | 355 void Schema::InternalStorage::DetermineStorageSizes( |
357 const base::DictionaryValue& schema, | 356 const base::DictionaryValue& schema, |
358 StorageSizes* sizes) { | 357 StorageSizes* sizes) { |
359 std::string ref_string; | 358 std::string ref_string; |
360 if (schema.GetString(schema::kRef, &ref_string)) { | 359 if (schema.GetString(schema::kRef, &ref_string)) { |
361 // Schemas with a "$ref" attribute don't take additional storage. | 360 // Schemas with a "$ref" attribute don't take additional storage. |
362 return; | 361 return; |
(...skipping 737 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1100 return false; | 1099 return false; |
1101 } else { | 1100 } else { |
1102 int index = rnode->string_pattern_restriction.pattern_index; | 1101 int index = rnode->string_pattern_restriction.pattern_index; |
1103 DCHECK(index == rnode->string_pattern_restriction.pattern_index_backup); | 1102 DCHECK(index == rnode->string_pattern_restriction.pattern_index_backup); |
1104 re2::RE2* regex = storage_->CompileRegex(*storage_->string_enums(index)); | 1103 re2::RE2* regex = storage_->CompileRegex(*storage_->string_enums(index)); |
1105 return re2::RE2::PartialMatch(str, *regex); | 1104 return re2::RE2::PartialMatch(str, *regex); |
1106 } | 1105 } |
1107 } | 1106 } |
1108 | 1107 |
1109 } // namespace policy | 1108 } // namespace policy |
OLD | NEW |