OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/browser/extensions/api/declarative_content/content_condition.h" | 5 #include "chrome/browser/extensions/api/declarative_content/content_condition.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/extensions/api/declarative_content/content_constants.h" | 12 #include "chrome/browser/extensions/api/declarative_content/content_constants.h" |
13 | 13 |
14 namespace extensions { | 14 namespace extensions { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // TODO(jyasskin): improve error messaging to give more meaningful messages | 18 // TODO(jyasskin): improve error messaging to give more meaningful messages |
19 // to the extension developer. | 19 // to the extension developer. |
20 // Error messages: | 20 // Error messages: |
21 const char kExpectedDictionary[] = "A condition has to be a dictionary."; | 21 const char kExpectedDictionary[] = "A condition has to be a dictionary."; |
22 const char kConditionWithoutInstanceType[] = "A condition had no instanceType"; | 22 const char kConditionWithoutInstanceType[] = "A condition had no instanceType"; |
23 const char kExpectedOtherConditionType[] = "Expected a condition of type " | 23 const char kExpectedOtherConditionType[] = "Expected a condition of type " |
24 "declarativeContent.PageStateMatcher"; | 24 "declarativeContent.PageStateMatcher"; |
25 const char kUnknownConditionAttribute[] = "Unknown condition attribute '%s'"; | 25 const char kUnknownConditionAttribute[] = "Unknown condition attribute '%s'"; |
26 | 26 |
27 } // namespace | 27 } // namespace |
28 | 28 |
29 ContentCondition::ContentCondition( | 29 ContentCondition::ContentCondition( |
30 std::vector<scoped_ptr<const ContentPredicate>> predicates) | 30 std::vector<std::unique_ptr<const ContentPredicate>> predicates) |
31 : predicates(std::move(predicates)) {} | 31 : predicates(std::move(predicates)) {} |
32 | 32 |
33 ContentCondition::~ContentCondition() {} | 33 ContentCondition::~ContentCondition() {} |
34 | 34 |
35 scoped_ptr<ContentCondition> CreateContentCondition( | 35 std::unique_ptr<ContentCondition> CreateContentCondition( |
36 const Extension* extension, | 36 const Extension* extension, |
37 const std::map<std::string, ContentPredicateFactory*>& predicate_factories, | 37 const std::map<std::string, ContentPredicateFactory*>& predicate_factories, |
38 const base::Value& api_condition, | 38 const base::Value& api_condition, |
39 std::string* error) { | 39 std::string* error) { |
40 const base::DictionaryValue* api_condition_dict = nullptr; | 40 const base::DictionaryValue* api_condition_dict = nullptr; |
41 if (!api_condition.GetAsDictionary(&api_condition_dict)) { | 41 if (!api_condition.GetAsDictionary(&api_condition_dict)) { |
42 *error = kExpectedDictionary; | 42 *error = kExpectedDictionary; |
43 return scoped_ptr<ContentCondition>(); | 43 return std::unique_ptr<ContentCondition>(); |
44 } | 44 } |
45 | 45 |
46 // Verify that we are dealing with a Condition whose type we understand. | 46 // Verify that we are dealing with a Condition whose type we understand. |
47 std::string instance_type; | 47 std::string instance_type; |
48 if (!api_condition_dict->GetString( | 48 if (!api_condition_dict->GetString( |
49 declarative_content_constants::kInstanceType, &instance_type)) { | 49 declarative_content_constants::kInstanceType, &instance_type)) { |
50 *error = kConditionWithoutInstanceType; | 50 *error = kConditionWithoutInstanceType; |
51 return scoped_ptr<ContentCondition>(); | 51 return std::unique_ptr<ContentCondition>(); |
52 } | 52 } |
53 if (instance_type != declarative_content_constants::kPageStateMatcherType) { | 53 if (instance_type != declarative_content_constants::kPageStateMatcherType) { |
54 *error = kExpectedOtherConditionType; | 54 *error = kExpectedOtherConditionType; |
55 return scoped_ptr<ContentCondition>(); | 55 return std::unique_ptr<ContentCondition>(); |
56 } | 56 } |
57 | 57 |
58 std::vector<scoped_ptr<const ContentPredicate>> predicates; | 58 std::vector<std::unique_ptr<const ContentPredicate>> predicates; |
59 for (base::DictionaryValue::Iterator iter(*api_condition_dict); | 59 for (base::DictionaryValue::Iterator iter(*api_condition_dict); |
60 !iter.IsAtEnd(); iter.Advance()) { | 60 !iter.IsAtEnd(); iter.Advance()) { |
61 const std::string& predicate_name = iter.key(); | 61 const std::string& predicate_name = iter.key(); |
62 const base::Value& predicate_value = iter.value(); | 62 const base::Value& predicate_value = iter.value(); |
63 if (predicate_name == declarative_content_constants::kInstanceType) | 63 if (predicate_name == declarative_content_constants::kInstanceType) |
64 continue; | 64 continue; |
65 | 65 |
66 const auto loc = predicate_factories.find(predicate_name); | 66 const auto loc = predicate_factories.find(predicate_name); |
67 if (loc != predicate_factories.end()) | 67 if (loc != predicate_factories.end()) |
68 predicates.push_back(loc->second->CreatePredicate(extension, | 68 predicates.push_back(loc->second->CreatePredicate(extension, |
69 predicate_value, | 69 predicate_value, |
70 error)); | 70 error)); |
71 else | 71 else |
72 *error = base::StringPrintf(kUnknownConditionAttribute, | 72 *error = base::StringPrintf(kUnknownConditionAttribute, |
73 predicate_name.c_str()); | 73 predicate_name.c_str()); |
74 | 74 |
75 if (!error->empty()) | 75 if (!error->empty()) |
76 return scoped_ptr<ContentCondition>(); | 76 return std::unique_ptr<ContentCondition>(); |
77 } | 77 } |
78 | 78 |
79 return scoped_ptr<ContentCondition>( | 79 return std::unique_ptr<ContentCondition>( |
80 new ContentCondition(std::move(predicates))); | 80 new ContentCondition(std::move(predicates))); |
81 } | 81 } |
82 | 82 |
83 } // namespace extensions | 83 } // namespace extensions |
OLD | NEW |