| 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/string_piece.h" |
| 10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 12 #include "extensions/common/manifest_constants.h" | 13 #include "extensions/common/manifest_constants.h" |
| 13 | 14 |
| 14 using base::UTF8ToUTF16; | |
| 15 using base::StringPrintf; | |
| 16 | |
| 17 namespace extensions { | 15 namespace extensions { |
| 18 | 16 |
| 19 namespace { | 17 namespace { |
| 20 | 18 |
| 21 const char* ValueTypeToString(const base::Value* value) { | |
| 22 const base::Value::Type type = value->GetType(); | |
| 23 static const char* strings[] = {"null", | |
| 24 "boolean", | |
| 25 "integer", | |
| 26 "double", | |
| 27 "string", | |
| 28 "binary", | |
| 29 "dictionary", | |
| 30 "list"}; | |
| 31 CHECK(static_cast<size_t>(type) < arraysize(strings)); | |
| 32 return strings[type]; | |
| 33 } | |
| 34 | |
| 35 class ErrorBuilder { | 19 class ErrorBuilder { |
| 36 public: | 20 public: |
| 37 explicit ErrorBuilder(base::string16* error) : error_(error) {} | 21 explicit ErrorBuilder(base::string16* error) : error_(error) {} |
| 38 | 22 |
| 39 // Appends a literal string |error|. | 23 // Appends a literal string |error|. |
| 40 void Append(const char* error) { | 24 void Append(base::StringPiece error) { |
| 41 if (error_->length()) | 25 if (!error_->empty()) |
| 42 error_->append(UTF8ToUTF16("; ")); | 26 error_->append(base::ASCIIToUTF16("; ")); |
| 43 error_->append(UTF8ToUTF16(error)); | 27 error_->append(base::UTF8ToUTF16(error)); |
| 44 } | 28 } |
| 45 | 29 |
| 46 // Appends a string |error| with the first %s replaced by |sub|. | 30 // Appends a string |error| with the first %s replaced by |sub|. |
| 47 void Append(const char* error, const char* sub) { | 31 void Append(base::StringPiece error, base::StringPiece sub) { |
| 48 Append(base::StringPrintf(error, sub).c_str()); | 32 Append(base::StringPrintf(error.data(), sub.data())); |
| 49 } | 33 } |
| 50 | 34 |
| 51 private: | 35 private: |
| 52 base::string16* error_; | 36 base::string16* const error_; |
| 53 DISALLOW_COPY_AND_ASSIGN(ErrorBuilder); | 37 DISALLOW_COPY_AND_ASSIGN(ErrorBuilder); |
| 54 }; | 38 }; |
| 55 | 39 |
| 56 // Converts a rule defined in the manifest into a JSON internal format. The | 40 // 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 | 41 // 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 | 42 // 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 | 43 // for this. This function walks through all the conditions and rules to swap |
| 60 // the manifest key for the internal key. | 44 // the manifest key for the internal key. |
| 61 bool ConvertManifestRule(const linked_ptr<DeclarativeManifestData::Rule>& rule, | 45 bool ConvertManifestRule(const linked_ptr<DeclarativeManifestData::Rule>& rule, |
| 62 ErrorBuilder* error_builder) { | 46 ErrorBuilder* error_builder) { |
| 63 auto convert_list = | 47 auto convert_list = |
| 64 [error_builder](std::vector<std::unique_ptr<base::Value>>& list) { | 48 [error_builder](const std::vector<std::unique_ptr<base::Value>>& list) { |
| 65 for (const std::unique_ptr<base::Value>& value : list) { | 49 for (const std::unique_ptr<base::Value>& value : list) { |
| 66 base::DictionaryValue* dictionary = nullptr; | 50 base::DictionaryValue* dictionary = nullptr; |
| 67 if (!value->GetAsDictionary(&dictionary)) { | 51 if (!value->GetAsDictionary(&dictionary)) { |
| 68 error_builder->Append("expected dictionary, got %s", | 52 error_builder->Append("expected dictionary, got %s", |
| 69 ValueTypeToString(value.get())); | 53 base::Value::GetTypeName(value->GetType())); |
| 70 return false; | 54 return false; |
| 71 } | 55 } |
| 72 std::string type; | 56 std::string type; |
| 73 if (!dictionary->GetString("type", &type)) { | 57 if (!dictionary->GetString("type", &type)) { |
| 74 error_builder->Append("'type' is required and must be a string"); | 58 error_builder->Append("'type' is required and must be a string"); |
| 75 return false; | 59 return false; |
| 76 } | 60 } |
| 77 dictionary->Remove("type", nullptr); | 61 dictionary->Remove("type", nullptr); |
| 78 dictionary->SetString("instanceType", type); | 62 dictionary->SetString("instanceType", type); |
| 79 } | 63 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 // field to indicate the instance type. Instead of adding rules to a | 116 // 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 | 117 // specific event list, each rule has an "event" field to indicate which |
| 134 // event it applies to. | 118 // event it applies to. |
| 135 // | 119 // |
| 136 ErrorBuilder error_builder(error); | 120 ErrorBuilder error_builder(error); |
| 137 std::unique_ptr<DeclarativeManifestData> result( | 121 std::unique_ptr<DeclarativeManifestData> result( |
| 138 new DeclarativeManifestData()); | 122 new DeclarativeManifestData()); |
| 139 const base::ListValue* list = nullptr; | 123 const base::ListValue* list = nullptr; |
| 140 if (!value.GetAsList(&list)) { | 124 if (!value.GetAsList(&list)) { |
| 141 error_builder.Append("'event_rules' expected list, got %s", | 125 error_builder.Append("'event_rules' expected list, got %s", |
| 142 ValueTypeToString(&value)); | 126 base::Value::GetTypeName(value.GetType())); |
| 143 return std::unique_ptr<DeclarativeManifestData>(); | 127 return std::unique_ptr<DeclarativeManifestData>(); |
| 144 } | 128 } |
| 145 | 129 |
| 146 for (size_t i = 0; i < list->GetSize(); ++i) { | 130 for (const auto& element : *list) { |
| 147 const base::DictionaryValue* dict = nullptr; | 131 const base::DictionaryValue* dict = nullptr; |
| 148 if (!list->GetDictionary(i, &dict)) { | 132 if (!element->GetAsDictionary(&dict)) { |
| 149 const base::Value* value = nullptr; | 133 error_builder.Append("expected dictionary, got %s", |
| 150 if (list->Get(i, &value)) | 134 base::Value::GetTypeName(element->GetType())); |
| 151 error_builder.Append("expected dictionary, got %s", | |
| 152 ValueTypeToString(value)); | |
| 153 else | |
| 154 error_builder.Append("expected dictionary"); | |
| 155 return std::unique_ptr<DeclarativeManifestData>(); | 135 return std::unique_ptr<DeclarativeManifestData>(); |
| 156 } | 136 } |
| 157 std::string event; | 137 std::string event; |
| 158 if (!dict->GetString("event", &event)) { | 138 if (!dict->GetString("event", &event)) { |
| 159 error_builder.Append("'event' is required"); | 139 error_builder.Append("'event' is required"); |
| 160 return std::unique_ptr<DeclarativeManifestData>(); | 140 return std::unique_ptr<DeclarativeManifestData>(); |
| 161 } | 141 } |
| 162 | 142 |
| 163 linked_ptr<Rule> rule(new Rule()); | 143 linked_ptr<Rule> rule(new Rule()); |
| 164 if (!Rule::Populate(*dict, rule.get())) { | 144 if (!Rule::Populate(*dict, rule.get())) { |
| 165 error_builder.Append("rule failed to populate"); | 145 error_builder.Append("rule failed to populate"); |
| 166 return std::unique_ptr<DeclarativeManifestData>(); | 146 return std::unique_ptr<DeclarativeManifestData>(); |
| 167 } | 147 } |
| 168 | 148 |
| 169 if (!ConvertManifestRule(rule, &error_builder)) | 149 if (!ConvertManifestRule(rule, &error_builder)) |
| 170 return std::unique_ptr<DeclarativeManifestData>(); | 150 return std::unique_ptr<DeclarativeManifestData>(); |
| 171 | 151 |
| 172 result->event_rules_map_[event].push_back(rule); | 152 result->event_rules_map_[event].push_back(rule); |
| 173 } | 153 } |
| 174 return result; | 154 return result; |
| 175 } | 155 } |
| 176 | 156 |
| 177 std::vector<linked_ptr<DeclarativeManifestData::Rule>>& | 157 std::vector<linked_ptr<DeclarativeManifestData::Rule>>& |
| 178 DeclarativeManifestData::RulesForEvent(const std::string& event) { | 158 DeclarativeManifestData::RulesForEvent(const std::string& event) { |
| 179 return event_rules_map_[event]; | 159 return event_rules_map_[event]; |
| 180 } | 160 } |
| 181 | 161 |
| 182 } // namespace extensions | 162 } // namespace extensions |
| OLD | NEW |