OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/extension_base.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/lazy_instance.h" |
| 9 #include "base/stringprintf.h" |
| 10 #include "base/string_util.h" |
| 11 #include "chrome/common/extensions/extension.h" |
| 12 #include "chrome/common/extensions/extension_set.h" |
| 13 #include "chrome/renderer/extensions/bindings_utils.h" |
| 14 #include "chrome/renderer/extensions/extension_dispatcher.h" |
| 15 #include "content/renderer/render_view.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 19 #include "ui/base/resource/resource_bundle.h" |
| 20 #include "v8/include/v8.h" |
| 21 |
| 22 using WebKit::WebFrame; |
| 23 using WebKit::WebView; |
| 24 |
| 25 namespace { |
| 26 |
| 27 typedef std::map<int, std::string> StringMap; |
| 28 static base::LazyInstance<StringMap> g_string_map(base::LINKER_INITIALIZED); |
| 29 |
| 30 } |
| 31 |
| 32 // static |
| 33 const char* ExtensionBase::GetStringResource(int resource_id) { |
| 34 StringMap* strings = g_string_map.Pointer(); |
| 35 StringMap::iterator it = strings->find(resource_id); |
| 36 if (it == strings->end()) { |
| 37 it = strings->insert(std::make_pair( |
| 38 resource_id, |
| 39 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 40 resource_id).as_string())).first; |
| 41 } |
| 42 return it->second.c_str(); |
| 43 } |
| 44 |
| 45 const Extension* ExtensionBase::GetExtensionForCurrentContext() const { |
| 46 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); |
| 47 if (!renderview) |
| 48 return NULL; // this can happen as a tab is closing. |
| 49 |
| 50 GURL url = renderview->webview()->mainFrame()->document().url(); |
| 51 const ExtensionSet* extensions = extension_dispatcher_->extensions(); |
| 52 if (!extensions->ExtensionBindingsAllowed(url)) |
| 53 return NULL; |
| 54 |
| 55 return extensions->GetByURL(url); |
| 56 } |
| 57 |
| 58 bool ExtensionBase::CheckPermissionForCurrentContext( |
| 59 const std::string& function_name) const { |
| 60 const ::Extension* extension = GetExtensionForCurrentContext(); |
| 61 if (extension && |
| 62 extension_dispatcher_->IsExtensionActive(extension->id()) && |
| 63 extension->HasAPIPermission(function_name)) |
| 64 return true; |
| 65 |
| 66 static const char kMessage[] = |
| 67 "You do not have permission to use '%s'. Be sure to declare" |
| 68 " in your manifest what permissions you need."; |
| 69 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
| 70 |
| 71 v8::ThrowException(v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
| 72 return false; |
| 73 } |
| 74 |
| 75 v8::Handle<v8::FunctionTemplate> |
| 76 ExtensionBase::GetNativeFunction(v8::Handle<v8::String> name) { |
| 77 if (name->Equals(v8::String::New("GetChromeHidden"))) { |
| 78 return v8::FunctionTemplate::New(GetChromeHidden); |
| 79 } |
| 80 |
| 81 if (name->Equals(v8::String::New("Print"))) { |
| 82 return v8::FunctionTemplate::New(Print); |
| 83 } |
| 84 |
| 85 return v8::Handle<v8::FunctionTemplate>(); |
| 86 } |
| 87 |
| 88 v8::Handle<v8::Value> ExtensionBase::GetChromeHidden( |
| 89 const v8::Arguments& args) { |
| 90 return bindings_utils::GetChromeHiddenForContext(v8::Context::GetCurrent()); |
| 91 } |
| 92 |
| 93 v8::Handle<v8::Value> ExtensionBase::Print(const v8::Arguments& args) { |
| 94 if (args.Length() < 1) |
| 95 return v8::Undefined(); |
| 96 |
| 97 std::vector<std::string> components; |
| 98 for (int i = 0; i < args.Length(); ++i) |
| 99 components.push_back(*v8::String::Utf8Value(args[i]->ToString())); |
| 100 |
| 101 LOG(ERROR) << JoinString(components, ','); |
| 102 return v8::Undefined(); |
| 103 } |
OLD | NEW |