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