| 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 <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 { | 228 { |
| 229 std::set<std::string> permissions; | 229 std::set<std::string> permissions; |
| 230 permissions.insert("storage"); | 230 permissions.insert("storage"); |
| 231 permissions.insert("history"); | 231 permissions.insert("history"); |
| 232 extension = CreateExtensionWithPermissions(permissions); | 232 extension = CreateExtensionWithPermissions(permissions); |
| 233 } | 233 } |
| 234 | 234 |
| 235 scoped_ptr<ExtensionAPI> extension_api( | 235 scoped_ptr<ExtensionAPI> extension_api( |
| 236 ExtensionAPI::CreateWithDefaultConfiguration()); | 236 ExtensionAPI::CreateWithDefaultConfiguration()); |
| 237 | 237 |
| 238 scoped_ptr<std::set<std::string> > privileged_apis = | 238 std::set<std::string> privileged_apis = extension_api->GetAPIsForContext( |
| 239 extension_api->GetAPIsForContext( | 239 Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), GURL()); |
| 240 Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), GURL()); | |
| 241 | 240 |
| 242 scoped_ptr<std::set<std::string> > unprivileged_apis = | 241 std::set<std::string> unprivileged_apis = extension_api->GetAPIsForContext( |
| 243 extension_api->GetAPIsForContext( | 242 Feature::UNBLESSED_EXTENSION_CONTEXT, extension.get(), GURL()); |
| 244 Feature::UNBLESSED_EXTENSION_CONTEXT, extension.get(), GURL()); | |
| 245 | 243 |
| 246 scoped_ptr<std::set<std::string> > content_script_apis = | 244 std::set<std::string> content_script_apis = extension_api->GetAPIsForContext( |
| 247 extension_api->GetAPIsForContext( | 245 Feature::CONTENT_SCRIPT_CONTEXT, extension.get(), GURL()); |
| 248 Feature::CONTENT_SCRIPT_CONTEXT, extension.get(), GURL()); | |
| 249 | 246 |
| 250 // "storage" is completely unprivileged. | 247 // "storage" is completely unprivileged. |
| 251 EXPECT_EQ(1u, privileged_apis->count("storage")); | 248 EXPECT_EQ(1u, privileged_apis.count("storage")); |
| 252 EXPECT_EQ(1u, unprivileged_apis->count("storage")); | 249 EXPECT_EQ(1u, unprivileged_apis.count("storage")); |
| 253 EXPECT_EQ(1u, content_script_apis->count("storage")); | 250 EXPECT_EQ(1u, content_script_apis.count("storage")); |
| 254 | 251 |
| 255 // "extension" is partially unprivileged. | 252 // "extension" is partially unprivileged. |
| 256 EXPECT_EQ(1u, privileged_apis->count("extension")); | 253 EXPECT_EQ(1u, privileged_apis.count("extension")); |
| 257 EXPECT_EQ(1u, unprivileged_apis->count("extension")); | 254 EXPECT_EQ(1u, unprivileged_apis.count("extension")); |
| 258 EXPECT_EQ(1u, content_script_apis->count("extension")); | 255 EXPECT_EQ(1u, content_script_apis.count("extension")); |
| 259 | 256 |
| 260 // "history" is entirely privileged. | 257 // "history" is entirely privileged. |
| 261 EXPECT_EQ(1u, privileged_apis->count("history")); | 258 EXPECT_EQ(1u, privileged_apis.count("history")); |
| 262 EXPECT_EQ(0u, unprivileged_apis->count("history")); | 259 EXPECT_EQ(0u, unprivileged_apis.count("history")); |
| 263 EXPECT_EQ(0u, content_script_apis->count("history")); | 260 EXPECT_EQ(0u, content_script_apis.count("history")); |
| 264 } | 261 } |
| 265 | 262 |
| 266 TEST(ExtensionAPI, ExtensionWithDependencies) { | 263 TEST(ExtensionAPI, ExtensionWithDependencies) { |
| 267 // Extension with the "ttsEngine" permission but not the "tts" permission; it | 264 // Extension with the "ttsEngine" permission but not the "tts" permission; it |
| 268 // must load TTS. | 265 // must load TTS. |
| 269 { | 266 { |
| 270 scoped_refptr<Extension> extension = | 267 scoped_refptr<Extension> extension = |
| 271 CreateExtensionWithPermission("ttsEngine"); | 268 CreateExtensionWithPermission("ttsEngine"); |
| 272 scoped_ptr<ExtensionAPI> api( | 269 scoped_ptr<ExtensionAPI> api( |
| 273 ExtensionAPI::CreateWithDefaultConfiguration()); | 270 ExtensionAPI::CreateWithDefaultConfiguration()); |
| 274 scoped_ptr<std::set<std::string> > apis = api->GetAPIsForContext( | 271 std::set<std::string> apis = api->GetAPIsForContext( |
| 275 Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), GURL()); | 272 Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), GURL()); |
| 276 EXPECT_EQ(1u, apis->count("ttsEngine")); | 273 EXPECT_EQ(1u, apis.count("ttsEngine")); |
| 277 EXPECT_EQ(1u, apis->count("tts")); | 274 EXPECT_EQ(1u, apis.count("tts")); |
| 278 } | 275 } |
| 279 | 276 |
| 280 // Conversely, extension with the "tts" permission but not the "ttsEngine" | 277 // Conversely, extension with the "tts" permission but not the "ttsEngine" |
| 281 // permission shouldn't get the "ttsEngine" permission. | 278 // permission shouldn't get the "ttsEngine" permission. |
| 282 { | 279 { |
| 283 scoped_refptr<Extension> extension = | 280 scoped_refptr<Extension> extension = |
| 284 CreateExtensionWithPermission("tts"); | 281 CreateExtensionWithPermission("tts"); |
| 285 scoped_ptr<ExtensionAPI> api( | 282 scoped_ptr<ExtensionAPI> api( |
| 286 ExtensionAPI::CreateWithDefaultConfiguration()); | 283 ExtensionAPI::CreateWithDefaultConfiguration()); |
| 287 scoped_ptr<std::set<std::string> > apis = api->GetAPIsForContext( | 284 std::set<std::string> apis = api->GetAPIsForContext( |
| 288 Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), GURL()); | 285 Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), GURL()); |
| 289 EXPECT_EQ(0u, apis->count("ttsEngine")); | 286 EXPECT_EQ(0u, apis.count("ttsEngine")); |
| 290 EXPECT_EQ(1u, apis->count("tts")); | 287 EXPECT_EQ(1u, apis.count("tts")); |
| 291 } | 288 } |
| 292 } | 289 } |
| 293 | 290 |
| 294 bool MatchesURL( | 291 bool MatchesURL( |
| 295 ExtensionAPI* api, const std::string& api_name, const std::string& url) { | 292 ExtensionAPI* api, const std::string& api_name, const std::string& url) { |
| 296 scoped_ptr<std::set<std::string> > apis = | 293 std::set<std::string> apis = |
| 297 api->GetAPIsForContext(Feature::WEB_PAGE_CONTEXT, NULL, GURL(url)); | 294 api->GetAPIsForContext(Feature::WEB_PAGE_CONTEXT, NULL, GURL(url)); |
| 298 return apis->count(api_name); | 295 return apis.count(api_name); |
| 299 } | 296 } |
| 300 | 297 |
| 301 TEST(ExtensionAPI, URLMatching) { | 298 TEST(ExtensionAPI, URLMatching) { |
| 302 scoped_ptr<ExtensionAPI> api(ExtensionAPI::CreateWithDefaultConfiguration()); | 299 scoped_ptr<ExtensionAPI> api(ExtensionAPI::CreateWithDefaultConfiguration()); |
| 303 | 300 |
| 304 // "app" API is available to all URLs that content scripts can be injected. | 301 // "app" API is available to all URLs that content scripts can be injected. |
| 305 EXPECT_TRUE(MatchesURL(api.get(), "app", "http://example.com/example.html")); | 302 EXPECT_TRUE(MatchesURL(api.get(), "app", "http://example.com/example.html")); |
| 306 EXPECT_TRUE(MatchesURL(api.get(), "app", "https://blah.net")); | 303 EXPECT_TRUE(MatchesURL(api.get(), "app", "https://blah.net")); |
| 307 EXPECT_TRUE(MatchesURL(api.get(), "app", "file://somefile.html")); | 304 EXPECT_TRUE(MatchesURL(api.get(), "app", "file://somefile.html")); |
| 308 | 305 |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 GetDictionaryFromList(dict, "parameters", 0, &sub_dict); | 481 GetDictionaryFromList(dict, "parameters", 0, &sub_dict); |
| 485 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); | 482 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); |
| 486 EXPECT_EQ("test.foo.TestType", type); | 483 EXPECT_EQ("test.foo.TestType", type); |
| 487 GetDictionaryFromList(dict, "parameters", 1, &sub_dict); | 484 GetDictionaryFromList(dict, "parameters", 1, &sub_dict); |
| 488 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); | 485 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); |
| 489 EXPECT_EQ("fully.qualified.Type", type); | 486 EXPECT_EQ("fully.qualified.Type", type); |
| 490 } | 487 } |
| 491 | 488 |
| 492 } // namespace | 489 } // namespace |
| 493 } // namespace extensions | 490 } // namespace extensions |
| OLD | NEW |