| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/common/extensions/api/extension_api.h" | 5 #include "chrome/common/extensions/api/extension_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/string_split.h" | 13 #include "base/string_split.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
| 17 #include "chrome/common/extensions/extension_permission_set.h" | 17 #include "chrome/common/extensions/extension_permission_set.h" |
| 18 #include "googleurl/src/gurl.h" |
| 18 #include "grit/common_resources.h" | 19 #include "grit/common_resources.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 20 | 21 |
| 21 namespace extensions { | 22 namespace extensions { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Adds any APIs listed in "dependencies" found in |schema| but not in | 26 // Adds any APIs listed in "dependencies" found in |schema| but not in |
| 26 // |reference| to |out|. | 27 // |reference| to |out|. |
| 27 void GetMissingDependencies( | 28 void GetMissingDependencies( |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 loaded->Remove(loaded->GetSize() - 1, &value); | 89 loaded->Remove(loaded->GetSize() - 1, &value); |
| 89 CHECK(value->IsType(Value::TYPE_DICTIONARY)); | 90 CHECK(value->IsType(Value::TYPE_DICTIONARY)); |
| 90 const DictionaryValue* schema = static_cast<const DictionaryValue*>(value); | 91 const DictionaryValue* schema = static_cast<const DictionaryValue*>(value); |
| 91 CHECK(schema->GetString("namespace", &schema_namespace)); | 92 CHECK(schema->GetString("namespace", &schema_namespace)); |
| 92 schemas_[schema_namespace] = linked_ptr<const DictionaryValue>(schema); | 93 schemas_[schema_namespace] = linked_ptr<const DictionaryValue>(schema); |
| 93 } | 94 } |
| 94 } | 95 } |
| 95 | 96 |
| 96 ExtensionAPI::ExtensionAPI() { | 97 ExtensionAPI::ExtensionAPI() { |
| 97 static int kJsonApiResourceIds[] = { | 98 static int kJsonApiResourceIds[] = { |
| 99 IDR_EXTENSION_API_JSON_APP, |
| 98 IDR_EXTENSION_API_JSON_BOOKMARKS, | 100 IDR_EXTENSION_API_JSON_BOOKMARKS, |
| 99 IDR_EXTENSION_API_JSON_BROWSERACTION, | 101 IDR_EXTENSION_API_JSON_BROWSERACTION, |
| 100 IDR_EXTENSION_API_JSON_CHROMEAUTHPRIVATE, | 102 IDR_EXTENSION_API_JSON_CHROMEAUTHPRIVATE, |
| 101 IDR_EXTENSION_API_JSON_CHROMEOSINFOPRIVATE, | 103 IDR_EXTENSION_API_JSON_CHROMEOSINFOPRIVATE, |
| 102 IDR_EXTENSION_API_JSON_CHROMEPRIVATE, | 104 IDR_EXTENSION_API_JSON_CHROMEPRIVATE, |
| 103 IDR_EXTENSION_API_JSON_CONTENTSETTINGS, | 105 IDR_EXTENSION_API_JSON_CONTENTSETTINGS, |
| 104 IDR_EXTENSION_API_JSON_CONTEXTMENUS, | 106 IDR_EXTENSION_API_JSON_CONTEXTMENUS, |
| 105 IDR_EXTENSION_API_JSON_COOKIES, | 107 IDR_EXTENSION_API_JSON_COOKIES, |
| 106 IDR_EXTENSION_API_JSON_DEBUGGER, | 108 IDR_EXTENSION_API_JSON_DEBUGGER, |
| 107 IDR_EXTENSION_API_JSON_DEVTOOLS, | 109 IDR_EXTENSION_API_JSON_DEVTOOLS, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 continue; | 172 continue; |
| 171 } | 173 } |
| 172 | 174 |
| 173 // Only need to look at functions/events; even though there are unprivileged | 175 // Only need to look at functions/events; even though there are unprivileged |
| 174 // properties (e.g. in extensions), access to those never reaches C++ land. | 176 // properties (e.g. in extensions), access to those never reaches C++ land. |
| 175 if (HasUnprivilegedChild(it->second.get(), "functions") || | 177 if (HasUnprivilegedChild(it->second.get(), "functions") || |
| 176 HasUnprivilegedChild(it->second.get(), "events")) { | 178 HasUnprivilegedChild(it->second.get(), "events")) { |
| 177 partially_unprivileged_apis_.insert(it->first); | 179 partially_unprivileged_apis_.insert(it->first); |
| 178 } | 180 } |
| 179 } | 181 } |
| 182 |
| 183 // Populate |url_matching_apis_|. |
| 184 for (SchemaMap::const_iterator it = schemas_.begin(); |
| 185 it != schemas_.end(); ++it) { |
| 186 ListValue* matches = NULL; |
| 187 { |
| 188 Value* matches_value = NULL; |
| 189 if (!it->second->Get("matches", &matches_value)) |
| 190 continue; |
| 191 CHECK_EQ(Value::TYPE_LIST, matches_value->GetType()); |
| 192 matches = static_cast<ListValue*>(matches_value); |
| 193 } |
| 194 URLPatternSet pattern_set; |
| 195 for (size_t i = 0; i < matches->GetSize(); ++i) { |
| 196 std::string pattern; |
| 197 CHECK(matches->GetString(i, &pattern)); |
| 198 pattern_set.AddPattern( |
| 199 URLPattern(UserScript::kValidUserScriptSchemes, pattern)); |
| 200 } |
| 201 url_matching_apis_[it->first] = pattern_set; |
| 202 } |
| 180 } | 203 } |
| 181 | 204 |
| 182 ExtensionAPI::~ExtensionAPI() { | 205 ExtensionAPI::~ExtensionAPI() { |
| 183 } | 206 } |
| 184 | 207 |
| 185 bool ExtensionAPI::IsPrivileged(const std::string& full_name) const { | 208 bool ExtensionAPI::IsPrivileged(const std::string& full_name) const { |
| 186 std::string api_name; | 209 std::string api_name; |
| 187 std::string child_name; | 210 std::string child_name; |
| 188 | 211 |
| 189 { | 212 { |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 if (permissions.HasAnyAccessToAPI(it->first)) | 327 if (permissions.HasAnyAccessToAPI(it->first)) |
| 305 (*out)[it->first] = it->second; | 328 (*out)[it->first] = it->second; |
| 306 } | 329 } |
| 307 } | 330 } |
| 308 | 331 |
| 309 bool ExtensionAPI::IsWholeAPIPrivileged(const std::string& api_name) const { | 332 bool ExtensionAPI::IsWholeAPIPrivileged(const std::string& api_name) const { |
| 310 return !completely_unprivileged_apis_.count(api_name) && | 333 return !completely_unprivileged_apis_.count(api_name) && |
| 311 !partially_unprivileged_apis_.count(api_name); | 334 !partially_unprivileged_apis_.count(api_name); |
| 312 } | 335 } |
| 313 | 336 |
| 337 bool ExtensionAPI::MatchesURL( |
| 338 const std::string& api_name, const GURL& url) const { |
| 339 std::map<std::string, URLPatternSet>::const_iterator it = |
| 340 url_matching_apis_.find(api_name); |
| 341 return it != url_matching_apis_.end() && it->second.MatchesURL(url); |
| 342 } |
| 343 |
| 344 void ExtensionAPI::GetSchemasForURL(const GURL& url, SchemaMap* out) const { |
| 345 for (std::map<std::string, URLPatternSet>::const_iterator it = |
| 346 url_matching_apis_.begin(); it != url_matching_apis_.end(); ++it) { |
| 347 if (it->second.MatchesURL(url)) |
| 348 (*out)[it->first] = schemas_.find(it->first)->second; |
| 349 } |
| 350 } |
| 351 |
| 314 } // namespace extensions | 352 } // namespace extensions |
| OLD | NEW |