| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/chrome_v8_extension.h" | 5 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "chrome/common/extensions/extension.h" | 11 #include "chrome/common/extensions/extension.h" |
| 12 #include "chrome/common/extensions/extension_set.h" | 12 #include "chrome/common/extensions/extension_set.h" |
| 13 #include "chrome/common/extensions/api/extension_api.h" |
| 13 #include "chrome/renderer/extensions/chrome_v8_context.h" | 14 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 14 #include "chrome/renderer/extensions/extension_dispatcher.h" | 15 #include "chrome/renderer/extensions/extension_dispatcher.h" |
| 15 #include "content/public/renderer/render_view.h" | 16 #include "content/public/renderer/render_view.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 17 #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/WebFrame.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 20 | 21 |
| 22 using extensions::ExtensionAPI; |
| 21 using WebKit::WebFrame; | 23 using WebKit::WebFrame; |
| 22 using WebKit::WebView; | 24 using WebKit::WebView; |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 const char kChromeHidden[] = "chromeHidden"; | 28 const char kChromeHidden[] = "chromeHidden"; |
| 27 | 29 |
| 28 #ifndef NDEBUG | 30 #ifndef NDEBUG |
| 29 const char kValidateCallbacks[] = "validateCallbacks"; | 31 const char kValidateCallbacks[] = "validateCallbacks"; |
| 30 #endif | 32 #endif |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 return NULL; // this can happen as a tab is closing. | 119 return NULL; // this can happen as a tab is closing. |
| 118 | 120 |
| 119 GURL url = renderview->GetWebView()->mainFrame()->document().url(); | 121 GURL url = renderview->GetWebView()->mainFrame()->document().url(); |
| 120 const ExtensionSet* extensions = extension_dispatcher_->extensions(); | 122 const ExtensionSet* extensions = extension_dispatcher_->extensions(); |
| 121 if (!extensions->ExtensionBindingsAllowed(url)) | 123 if (!extensions->ExtensionBindingsAllowed(url)) |
| 122 return NULL; | 124 return NULL; |
| 123 | 125 |
| 124 return extensions->GetByURL(url); | 126 return extensions->GetByURL(url); |
| 125 } | 127 } |
| 126 | 128 |
| 127 bool ChromeV8Extension::CheckPermissionForCurrentRenderView( | 129 bool ChromeV8Extension::CheckCurrentContextAccessToExtensionAPI( |
| 128 const std::string& function_name) const { | 130 const std::string& function_name) const { |
| 129 const ::Extension* extension = GetExtensionForCurrentRenderView(); | 131 ChromeV8Context* context = |
| 130 if (extension && | 132 extension_dispatcher_->v8_context_set().GetCurrent(); |
| 131 extension_dispatcher_->IsExtensionActive(extension->id()) && | 133 if (!context) { |
| 132 extension->HasAPIPermission(function_name)) | 134 DLOG(ERROR) << "Not in a v8::Context"; |
| 133 return true; | 135 return false; |
| 136 } |
| 134 | 137 |
| 135 static const char kMessage[] = | 138 const ::Extension* extension = NULL; |
| 136 "You do not have permission to use '%s'. Be sure to declare" | 139 if (!context->extension_id().empty()) { |
| 137 " in your manifest what permissions you need."; | 140 extension = |
| 138 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); | 141 extension_dispatcher_->extensions()->GetByID(context->extension_id()); |
| 142 } |
| 139 | 143 |
| 140 v8::ThrowException(v8::Exception::Error(v8::String::New(error_msg.c_str()))); | 144 if (!extension || !extension->HasAPIPermission(function_name)) { |
| 141 return false; | 145 static const char kMessage[] = |
| 146 "You do not have permission to use '%s'. Be sure to declare" |
| 147 " in your manifest what permissions you need."; |
| 148 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
| 149 v8::ThrowException( |
| 150 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
| 151 return false; |
| 152 } |
| 153 |
| 154 ExtensionAPI* api = ExtensionAPI::GetInstance(); |
| 155 if (!extension_dispatcher_->IsExtensionActive(extension->id()) && |
| 156 !api->IsFullNameUnprivileged(function_name)) { |
| 157 static const char kMessage[] = |
| 158 "%s can only be used in an extension process."; |
| 159 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
| 160 v8::ThrowException( |
| 161 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
| 162 return false; |
| 163 } |
| 164 |
| 165 return true; |
| 142 } | 166 } |
| 143 | 167 |
| 144 v8::Handle<v8::FunctionTemplate> | 168 v8::Handle<v8::FunctionTemplate> |
| 145 ChromeV8Extension::GetNativeFunction(v8::Handle<v8::String> name) { | 169 ChromeV8Extension::GetNativeFunction(v8::Handle<v8::String> name) { |
| 146 if (name->Equals(v8::String::New("GetChromeHidden"))) { | 170 if (name->Equals(v8::String::New("GetChromeHidden"))) { |
| 147 return v8::FunctionTemplate::New(GetChromeHidden); | 171 return v8::FunctionTemplate::New(GetChromeHidden); |
| 148 } | 172 } |
| 149 | 173 |
| 150 if (name->Equals(v8::String::New("Print"))) { | 174 if (name->Equals(v8::String::New("Print"))) { |
| 151 return v8::FunctionTemplate::New(Print); | 175 return v8::FunctionTemplate::New(Print); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 if (args.Length() < 1) | 237 if (args.Length() < 1) |
| 214 return v8::Undefined(); | 238 return v8::Undefined(); |
| 215 | 239 |
| 216 std::vector<std::string> components; | 240 std::vector<std::string> components; |
| 217 for (int i = 0; i < args.Length(); ++i) | 241 for (int i = 0; i < args.Length(); ++i) |
| 218 components.push_back(*v8::String::Utf8Value(args[i]->ToString())); | 242 components.push_back(*v8::String::Utf8Value(args[i]->ToString())); |
| 219 | 243 |
| 220 LOG(ERROR) << JoinString(components, ','); | 244 LOG(ERROR) << JoinString(components, ','); |
| 221 return v8::Undefined(); | 245 return v8::Undefined(); |
| 222 } | 246 } |
| OLD | NEW |