| 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 "extensions/common/api/declarative/declarative_manifest_data.h" | 5 #include "extensions/common/api/declarative/declarative_manifest_data.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 // Converts a rule defined in the manifest into a JSON internal format. The | 56 // Converts a rule defined in the manifest into a JSON internal format. The |
| 57 // difference is that actions and conditions use a "type" key to define the | 57 // difference is that actions and conditions use a "type" key to define the |
| 58 // type of rule/condition, while the internal format uses a "instanceType" key | 58 // type of rule/condition, while the internal format uses a "instanceType" key |
| 59 // for this. This function walks through all the conditions and rules to swap | 59 // for this. This function walks through all the conditions and rules to swap |
| 60 // the manifest key for the internal key. | 60 // the manifest key for the internal key. |
| 61 bool ConvertManifestRule(const linked_ptr<DeclarativeManifestData::Rule>& rule, | 61 bool ConvertManifestRule(const linked_ptr<DeclarativeManifestData::Rule>& rule, |
| 62 ErrorBuilder* error_builder) { | 62 ErrorBuilder* error_builder) { |
| 63 auto convert_list = | 63 auto convert_list = |
| 64 [error_builder](std::vector<scoped_ptr<base::Value>>& list) { | 64 [error_builder](std::vector<std::unique_ptr<base::Value>>& list) { |
| 65 for (const scoped_ptr<base::Value>& value : list) { | 65 for (const std::unique_ptr<base::Value>& value : list) { |
| 66 base::DictionaryValue* dictionary = nullptr; | 66 base::DictionaryValue* dictionary = nullptr; |
| 67 if (!value->GetAsDictionary(&dictionary)) { | 67 if (!value->GetAsDictionary(&dictionary)) { |
| 68 error_builder->Append("expected dictionary, got %s", | 68 error_builder->Append("expected dictionary, got %s", |
| 69 ValueTypeToString(value.get())); | 69 ValueTypeToString(value.get())); |
| 70 return false; | 70 return false; |
| 71 } | 71 } |
| 72 std::string type; | 72 std::string type; |
| 73 if (!dictionary->GetString("type", &type)) { | 73 if (!dictionary->GetString("type", &type)) { |
| 74 error_builder->Append("'type' is required and must be a string"); | 74 error_builder->Append("'type' is required and must be a string"); |
| 75 return false; | 75 return false; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 91 } | 91 } |
| 92 | 92 |
| 93 // static | 93 // static |
| 94 DeclarativeManifestData* DeclarativeManifestData::Get( | 94 DeclarativeManifestData* DeclarativeManifestData::Get( |
| 95 const Extension* extension) { | 95 const Extension* extension) { |
| 96 return static_cast<DeclarativeManifestData*>( | 96 return static_cast<DeclarativeManifestData*>( |
| 97 extension->GetManifestData(manifest_keys::kEventRules)); | 97 extension->GetManifestData(manifest_keys::kEventRules)); |
| 98 } | 98 } |
| 99 | 99 |
| 100 // static | 100 // static |
| 101 scoped_ptr<DeclarativeManifestData> DeclarativeManifestData::FromValue( | 101 std::unique_ptr<DeclarativeManifestData> DeclarativeManifestData::FromValue( |
| 102 const base::Value& value, | 102 const base::Value& value, |
| 103 base::string16* error) { | 103 base::string16* error) { |
| 104 // The following is an example of how an event programmatic rule definition | 104 // The following is an example of how an event programmatic rule definition |
| 105 // translates to a manifest definition. | 105 // translates to a manifest definition. |
| 106 // | 106 // |
| 107 // From javascript: | 107 // From javascript: |
| 108 // | 108 // |
| 109 // chrome.declarativeContent.onPageChanged.addRules([{ | 109 // chrome.declarativeContent.onPageChanged.addRules([{ |
| 110 // actions: [ | 110 // actions: [ |
| 111 // new chrome.declarativeContent.ShowPageAction() | 111 // new chrome.declarativeContent.ShowPageAction() |
| (...skipping 15 matching lines...) Expand all Loading... |
| 127 // "type" : "declarativeContent.PageStateMatcher" | 127 // "type" : "declarativeContent.PageStateMatcher" |
| 128 // }] | 128 // }] |
| 129 // }] | 129 // }] |
| 130 // | 130 // |
| 131 // The javascript objects get translated into JSON objects with a "type" | 131 // The javascript objects get translated into JSON objects with a "type" |
| 132 // field to indicate the instance type. Instead of adding rules to a | 132 // field to indicate the instance type. Instead of adding rules to a |
| 133 // specific event list, each rule has an "event" field to indicate which | 133 // specific event list, each rule has an "event" field to indicate which |
| 134 // event it applies to. | 134 // event it applies to. |
| 135 // | 135 // |
| 136 ErrorBuilder error_builder(error); | 136 ErrorBuilder error_builder(error); |
| 137 scoped_ptr<DeclarativeManifestData> result(new DeclarativeManifestData()); | 137 std::unique_ptr<DeclarativeManifestData> result( |
| 138 new DeclarativeManifestData()); |
| 138 const base::ListValue* list = nullptr; | 139 const base::ListValue* list = nullptr; |
| 139 if (!value.GetAsList(&list)) { | 140 if (!value.GetAsList(&list)) { |
| 140 error_builder.Append("'event_rules' expected list, got %s", | 141 error_builder.Append("'event_rules' expected list, got %s", |
| 141 ValueTypeToString(&value)); | 142 ValueTypeToString(&value)); |
| 142 return scoped_ptr<DeclarativeManifestData>(); | 143 return std::unique_ptr<DeclarativeManifestData>(); |
| 143 } | 144 } |
| 144 | 145 |
| 145 for (size_t i = 0; i < list->GetSize(); ++i) { | 146 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 146 const base::DictionaryValue* dict = nullptr; | 147 const base::DictionaryValue* dict = nullptr; |
| 147 if (!list->GetDictionary(i, &dict)) { | 148 if (!list->GetDictionary(i, &dict)) { |
| 148 const base::Value* value = nullptr; | 149 const base::Value* value = nullptr; |
| 149 if (list->Get(i, &value)) | 150 if (list->Get(i, &value)) |
| 150 error_builder.Append("expected dictionary, got %s", | 151 error_builder.Append("expected dictionary, got %s", |
| 151 ValueTypeToString(value)); | 152 ValueTypeToString(value)); |
| 152 else | 153 else |
| 153 error_builder.Append("expected dictionary"); | 154 error_builder.Append("expected dictionary"); |
| 154 return scoped_ptr<DeclarativeManifestData>(); | 155 return std::unique_ptr<DeclarativeManifestData>(); |
| 155 } | 156 } |
| 156 std::string event; | 157 std::string event; |
| 157 if (!dict->GetString("event", &event)) { | 158 if (!dict->GetString("event", &event)) { |
| 158 error_builder.Append("'event' is required"); | 159 error_builder.Append("'event' is required"); |
| 159 return scoped_ptr<DeclarativeManifestData>(); | 160 return std::unique_ptr<DeclarativeManifestData>(); |
| 160 } | 161 } |
| 161 | 162 |
| 162 linked_ptr<Rule> rule(new Rule()); | 163 linked_ptr<Rule> rule(new Rule()); |
| 163 if (!Rule::Populate(*dict, rule.get())) { | 164 if (!Rule::Populate(*dict, rule.get())) { |
| 164 error_builder.Append("rule failed to populate"); | 165 error_builder.Append("rule failed to populate"); |
| 165 return scoped_ptr<DeclarativeManifestData>(); | 166 return std::unique_ptr<DeclarativeManifestData>(); |
| 166 } | 167 } |
| 167 | 168 |
| 168 if (!ConvertManifestRule(rule, &error_builder)) | 169 if (!ConvertManifestRule(rule, &error_builder)) |
| 169 return scoped_ptr<DeclarativeManifestData>(); | 170 return std::unique_ptr<DeclarativeManifestData>(); |
| 170 | 171 |
| 171 result->event_rules_map_[event].push_back(rule); | 172 result->event_rules_map_[event].push_back(rule); |
| 172 } | 173 } |
| 173 return result; | 174 return result; |
| 174 } | 175 } |
| 175 | 176 |
| 176 std::vector<linked_ptr<DeclarativeManifestData::Rule>>& | 177 std::vector<linked_ptr<DeclarativeManifestData::Rule>>& |
| 177 DeclarativeManifestData::RulesForEvent(const std::string& event) { | 178 DeclarativeManifestData::RulesForEvent(const std::string& event) { |
| 178 return event_rules_map_[event]; | 179 return event_rules_map_[event]; |
| 179 } | 180 } |
| 180 | 181 |
| 181 } // namespace extensions | 182 } // namespace extensions |
| OLD | NEW |