| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/renderer/extensions/custom_bindings_util.h" | |
| 6 | |
| 7 #include <map> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/string_util.h" | |
| 11 #include "chrome/common/extensions/api/extension_api.h" | |
| 12 #include "chrome/common/extensions/extension.h" | |
| 13 #include "chrome/renderer/extensions/chrome_v8_extension.h" | |
| 14 #include "chrome/renderer/extensions/chrome_private_custom_bindings.h" | |
| 15 #include "chrome/renderer/extensions/context_menus_custom_bindings.h" | |
| 16 #include "chrome/renderer/extensions/experimental.socket_custom_bindings.h" | |
| 17 #include "chrome/renderer/extensions/extension_custom_bindings.h" | |
| 18 #include "chrome/renderer/extensions/extension_dispatcher.h" | |
| 19 #include "chrome/renderer/extensions/file_browser_handler_custom_bindings.h" | |
| 20 #include "chrome/renderer/extensions/file_browser_private_custom_bindings.h" | |
| 21 #include "chrome/renderer/extensions/i18n_custom_bindings.h" | |
| 22 #include "chrome/renderer/extensions/page_actions_custom_bindings.h" | |
| 23 #include "chrome/renderer/extensions/page_capture_custom_bindings.h" | |
| 24 #include "chrome/renderer/extensions/tabs_custom_bindings.h" | |
| 25 #include "chrome/renderer/extensions/tts_custom_bindings.h" | |
| 26 #include "chrome/renderer/extensions/web_request_custom_bindings.h" | |
| 27 #include "grit/renderer_resources.h" | |
| 28 #include "v8/include/v8.h" | |
| 29 | |
| 30 namespace extensions { | |
| 31 | |
| 32 namespace custom_bindings_util { | |
| 33 | |
| 34 // Extracts the name of an API from the name of the V8 extension which contains | |
| 35 // custom bindings for it (see kCustomBindingNames). | |
| 36 std::string GetAPIName(const std::string& v8_extension_name) { | |
| 37 // Extract the name of the API from the v8 extension name. | |
| 38 // This is "${api_name}" in "extensions/${api_name}_custom_bindings.js". | |
| 39 std::string prefix = "extensions/"; | |
| 40 const bool kCaseSensitive = true; | |
| 41 if (!StartsWithASCII(v8_extension_name, prefix, kCaseSensitive)) | |
| 42 return ""; | |
| 43 | |
| 44 std::string suffix = "_custom_bindings.js"; | |
| 45 if (!EndsWith(v8_extension_name, suffix, kCaseSensitive)) | |
| 46 return ""; | |
| 47 | |
| 48 // By convention, filenames are use unix_hacker_style, but the APIs we expose | |
| 49 // to javascript use camelCase. | |
| 50 std::string not_camelcase = v8_extension_name.substr( | |
| 51 prefix.size(), | |
| 52 v8_extension_name.size() - prefix.size() - suffix.size()); | |
| 53 | |
| 54 std::string camelcase; | |
| 55 bool next_to_upper = false; | |
| 56 for (std::string::iterator it = not_camelcase.begin(); | |
| 57 it != not_camelcase.end(); ++it) { | |
| 58 if (*it == '_') { | |
| 59 next_to_upper = true; | |
| 60 } else if (next_to_upper) { | |
| 61 camelcase.push_back(base::ToUpperASCII(*it)); | |
| 62 next_to_upper = false; | |
| 63 } else { | |
| 64 camelcase.push_back(*it); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 return camelcase; | |
| 69 } | |
| 70 | |
| 71 bool AllowAPIInjection(const std::string& api_name, | |
| 72 const Extension& extension, | |
| 73 ExtensionDispatcher* extension_dispatcher) { | |
| 74 CHECK(api_name != ""); | |
| 75 | |
| 76 // As in ExtensionAPI::GetSchemasForExtension, we need to allow any bindings | |
| 77 // for an API that the extension *might* have permission to use. | |
| 78 bool allowed = | |
| 79 extension.required_permission_set()->HasAnyAccessToAPI(api_name) || | |
| 80 extension.optional_permission_set()->HasAnyAccessToAPI(api_name); | |
| 81 | |
| 82 if (extension_dispatcher->is_extension_process()) { | |
| 83 return allowed; | |
| 84 } else { | |
| 85 return allowed && | |
| 86 !ExtensionAPI::GetInstance()->IsWholeAPIPrivileged(api_name); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 } // namespace custom_bindings_util | |
| 91 | |
| 92 } // namespace extensions | |
| OLD | NEW |