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/renderer/extensions/api_definitions_natives.h" | 5 #include "chrome/renderer/extensions/api_definitions_natives.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
| 9 namespace { |
| 10 const char kInvalidExtensionNamespace[] = "Invalid extension namespace"; |
| 11 } |
| 12 |
7 namespace extensions { | 13 namespace extensions { |
8 | 14 |
9 ApiDefinitionsNatives::ApiDefinitionsNatives( | 15 ApiDefinitionsNatives::ApiDefinitionsNatives( |
10 ExtensionDispatcher* extension_dispatcher) | 16 ExtensionDispatcher* extension_dispatcher) |
11 : ChromeV8Extension(extension_dispatcher) { | 17 : ChromeV8Extension(extension_dispatcher) { |
12 RouteFunction("GetExtensionAPIDefinition", | 18 RouteFunction("GetExtensionAPIDefinition", |
13 base::Bind(&ApiDefinitionsNatives::GetExtensionAPIDefinition, | 19 base::Bind(&ApiDefinitionsNatives::GetExtensionAPIDefinition, |
14 base::Unretained(this))); | 20 base::Unretained(this))); |
15 } | 21 } |
16 | 22 |
17 v8::Handle<v8::Value> ApiDefinitionsNatives::GetExtensionAPIDefinition( | 23 v8::Handle<v8::Value> ApiDefinitionsNatives::GetExtensionAPIDefinition( |
18 const v8::Arguments& args) { | 24 const v8::Arguments& args) { |
19 ChromeV8Context* v8_context = | 25 ChromeV8Context* v8_context = |
20 extension_dispatcher()->v8_context_set().GetCurrent(); | 26 extension_dispatcher()->v8_context_set().GetCurrent(); |
21 CHECK(v8_context); | 27 CHECK(v8_context); |
| 28 |
| 29 std::set<std::string> available_apis(v8_context->GetAvailableExtensionAPIs()); |
| 30 if (args.Length() == 0) { |
| 31 return extension_dispatcher()->v8_schema_registry()->GetSchemas( |
| 32 available_apis); |
| 33 } |
| 34 // Build set of APIs requested by the user. |
| 35 std::set<std::string> requested_apis; |
| 36 for (int i = 0; i < args.Length(); ++i) { |
| 37 if (!args[i]->IsString()) { |
| 38 v8::ThrowException(v8::String::New(kInvalidExtensionNamespace)); |
| 39 return v8::Undefined(); |
| 40 } |
| 41 requested_apis.insert(*v8::String::Utf8Value(args[i]->ToString())); |
| 42 } |
| 43 |
| 44 // Filter those that are unknown. |
| 45 std::set<std::string> apis_to_check; |
| 46 std::set_intersection(requested_apis.begin(), requested_apis.end(), |
| 47 available_apis.begin(), available_apis.end(), |
| 48 std::inserter(apis_to_check, apis_to_check.begin())); |
22 return extension_dispatcher()->v8_schema_registry()->GetSchemas( | 49 return extension_dispatcher()->v8_schema_registry()->GetSchemas( |
23 v8_context->GetAvailableExtensionAPIs()); | 50 apis_to_check); |
24 } | 51 } |
25 | 52 |
26 } // namespace extensions | 53 } // namespace extensions |
OLD | NEW |