| 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" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
| 16 #include "base/stl_util.h" | |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "components/json_schema/json_schema_constants.h" | 18 #include "components/json_schema/json_schema_constants.h" |
| 19 #include "components/json_schema/json_schema_validator.h" | 19 #include "components/json_schema/json_schema_validator.h" |
| 20 #include "components/policy/core/common/schema_internal.h" | 20 #include "components/policy/core/common/schema_internal.h" |
| 21 #include "third_party/re2/re2/re2.h" | 21 #include "third_party/re2/re2/re2.h" |
| 22 | 22 |
| 23 namespace schema = json_schema_constants; | 23 namespace schema = json_schema_constants; |
| 24 | 24 |
| 25 namespace policy { | 25 namespace policy { |
| 26 | 26 |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 | 240 |
| 241 // Assigns the IDs in |id_map| to the pending references in the | 241 // Assigns the IDs in |id_map| to the pending references in the |
| 242 // |reference_list|. If an ID is missing then |error| is set and false is | 242 // |reference_list|. If an ID is missing then |error| is set and false is |
| 243 // returned; otherwise returns true. | 243 // returned; otherwise returns true. |
| 244 static bool ResolveReferences(const IdMap& id_map, | 244 static bool ResolveReferences(const IdMap& id_map, |
| 245 const ReferenceList& reference_list, | 245 const ReferenceList& reference_list, |
| 246 std::string* error); | 246 std::string* error); |
| 247 | 247 |
| 248 // Cache for CompileRegex(), will memorize return value of every call to | 248 // Cache for CompileRegex(), will memorize return value of every call to |
| 249 // CompileRegex() and return results directly next time. | 249 // CompileRegex() and return results directly next time. |
| 250 mutable std::map<std::string, re2::RE2*> regex_cache_; | 250 mutable base::ScopedPtrMap<std::string, scoped_ptr<re2::RE2>> regex_cache_; |
| 251 STLValueDeleter<std::map<std::string, re2::RE2*> > regex_cache_deleter_; | |
| 252 | 251 |
| 253 SchemaData schema_data_; | 252 SchemaData schema_data_; |
| 254 std::vector<std::string> strings_; | 253 std::vector<std::string> strings_; |
| 255 std::vector<SchemaNode> schema_nodes_; | 254 std::vector<SchemaNode> schema_nodes_; |
| 256 std::vector<PropertyNode> property_nodes_; | 255 std::vector<PropertyNode> property_nodes_; |
| 257 std::vector<PropertiesNode> properties_nodes_; | 256 std::vector<PropertiesNode> properties_nodes_; |
| 258 std::vector<RestrictionNode> restriction_nodes_; | 257 std::vector<RestrictionNode> restriction_nodes_; |
| 259 std::vector<int> int_enums_; | 258 std::vector<int> int_enums_; |
| 260 std::vector<const char*> string_enums_; | 259 std::vector<const char*> string_enums_; |
| 261 | 260 |
| 262 DISALLOW_COPY_AND_ASSIGN(InternalStorage); | 261 DISALLOW_COPY_AND_ASSIGN(InternalStorage); |
| 263 }; | 262 }; |
| 264 | 263 |
| 265 Schema::InternalStorage::InternalStorage() | 264 Schema::InternalStorage::InternalStorage() { |
| 266 : regex_cache_deleter_(®ex_cache_) {} | 265 } |
| 267 | 266 |
| 268 Schema::InternalStorage::~InternalStorage() {} | 267 Schema::InternalStorage::~InternalStorage() { |
| 268 } |
| 269 | 269 |
| 270 // static | 270 // static |
| 271 scoped_refptr<const Schema::InternalStorage> Schema::InternalStorage::Wrap( | 271 scoped_refptr<const Schema::InternalStorage> Schema::InternalStorage::Wrap( |
| 272 const SchemaData* data) { | 272 const SchemaData* data) { |
| 273 InternalStorage* storage = new InternalStorage(); | 273 InternalStorage* storage = new InternalStorage(); |
| 274 storage->schema_data_.schema_nodes = data->schema_nodes; | 274 storage->schema_data_.schema_nodes = data->schema_nodes; |
| 275 storage->schema_data_.property_nodes = data->property_nodes; | 275 storage->schema_data_.property_nodes = data->property_nodes; |
| 276 storage->schema_data_.properties_nodes = data->properties_nodes; | 276 storage->schema_data_.properties_nodes = data->properties_nodes; |
| 277 storage->schema_data_.restriction_nodes = data->restriction_nodes; | 277 storage->schema_data_.restriction_nodes = data->restriction_nodes; |
| 278 storage->schema_data_.int_enums = data->int_enums; | 278 storage->schema_data_.int_enums = data->int_enums; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 data->property_nodes = vector_as_array(&storage->property_nodes_); | 335 data->property_nodes = vector_as_array(&storage->property_nodes_); |
| 336 data->properties_nodes = vector_as_array(&storage->properties_nodes_); | 336 data->properties_nodes = vector_as_array(&storage->properties_nodes_); |
| 337 data->restriction_nodes = vector_as_array(&storage->restriction_nodes_); | 337 data->restriction_nodes = vector_as_array(&storage->restriction_nodes_); |
| 338 data->int_enums = vector_as_array(&storage->int_enums_); | 338 data->int_enums = vector_as_array(&storage->int_enums_); |
| 339 data->string_enums = vector_as_array(&storage->string_enums_); | 339 data->string_enums = vector_as_array(&storage->string_enums_); |
| 340 return storage; | 340 return storage; |
| 341 } | 341 } |
| 342 | 342 |
| 343 re2::RE2* Schema::InternalStorage::CompileRegex( | 343 re2::RE2* Schema::InternalStorage::CompileRegex( |
| 344 const std::string& pattern) const { | 344 const std::string& pattern) const { |
| 345 std::map<std::string, re2::RE2*>::iterator it = regex_cache_.find(pattern); | 345 base::ScopedPtrMap<std::string, scoped_ptr<re2::RE2>>::const_iterator it = |
| 346 regex_cache_.find(pattern); |
| 346 if (it == regex_cache_.end()) { | 347 if (it == regex_cache_.end()) { |
| 347 re2::RE2* compiled = new re2::RE2(pattern); | 348 scoped_ptr<re2::RE2> compiled(new re2::RE2(pattern)); |
| 348 regex_cache_[pattern] = compiled; | 349 re2::RE2* compiled_ptr = compiled.get(); |
| 349 return compiled; | 350 regex_cache_.insert(pattern, compiled.Pass()); |
| 351 return compiled_ptr; |
| 350 } | 352 } |
| 351 return it->second; | 353 return it->second; |
| 352 } | 354 } |
| 353 | 355 |
| 354 // static | 356 // static |
| 355 void Schema::InternalStorage::DetermineStorageSizes( | 357 void Schema::InternalStorage::DetermineStorageSizes( |
| 356 const base::DictionaryValue& schema, | 358 const base::DictionaryValue& schema, |
| 357 StorageSizes* sizes) { | 359 StorageSizes* sizes) { |
| 358 std::string ref_string; | 360 std::string ref_string; |
| 359 if (schema.GetString(schema::kRef, &ref_string)) { | 361 if (schema.GetString(schema::kRef, &ref_string)) { |
| (...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1099 return false; | 1101 return false; |
| 1100 } else { | 1102 } else { |
| 1101 int index = rnode->string_pattern_restriction.pattern_index; | 1103 int index = rnode->string_pattern_restriction.pattern_index; |
| 1102 DCHECK(index == rnode->string_pattern_restriction.pattern_index_backup); | 1104 DCHECK(index == rnode->string_pattern_restriction.pattern_index_backup); |
| 1103 re2::RE2* regex = storage_->CompileRegex(*storage_->string_enums(index)); | 1105 re2::RE2* regex = storage_->CompileRegex(*storage_->string_enums(index)); |
| 1104 return re2::RE2::PartialMatch(str, *regex); | 1106 return re2::RE2::PartialMatch(str, *regex); |
| 1105 } | 1107 } |
| 1106 } | 1108 } |
| 1107 | 1109 |
| 1108 } // namespace policy | 1110 } // namespace policy |
| OLD | NEW |