Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: chrome/renderer/extensions/chrome_v8_extension.cc

Issue 8540012: Enable extension APIs for content scripts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 if (!extension_dispatcher_->IsExtensionActive(extension->id()) &&
155 ExtensionAPI::GetInstance()->IsPrivileged(function_name)) {
156 static const char kMessage[] =
157 "%s can only be used in an extension process.";
158 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str());
159 v8::ThrowException(
160 v8::Exception::Error(v8::String::New(error_msg.c_str())));
161 return false;
162 }
163
164 return true;
142 } 165 }
143 166
144 v8::Handle<v8::FunctionTemplate> 167 v8::Handle<v8::FunctionTemplate>
145 ChromeV8Extension::GetNativeFunction(v8::Handle<v8::String> name) { 168 ChromeV8Extension::GetNativeFunction(v8::Handle<v8::String> name) {
146 if (name->Equals(v8::String::New("GetChromeHidden"))) { 169 if (name->Equals(v8::String::New("GetChromeHidden"))) {
147 return v8::FunctionTemplate::New(GetChromeHidden); 170 return v8::FunctionTemplate::New(GetChromeHidden);
148 } 171 }
149 172
150 if (name->Equals(v8::String::New("Print"))) { 173 if (name->Equals(v8::String::New("Print"))) {
151 return v8::FunctionTemplate::New(Print); 174 return v8::FunctionTemplate::New(Print);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 const v8::Handle<v8::Context>& context) { 214 const v8::Handle<v8::Context>& context) {
192 v8::Local<v8::Object> global = context->Global(); 215 v8::Local<v8::Object> global = context->Global();
193 v8::Local<v8::Value> hidden = global->GetHiddenValue( 216 v8::Local<v8::Value> hidden = global->GetHiddenValue(
194 v8::String::New(kChromeHidden)); 217 v8::String::New(kChromeHidden));
195 218
196 if (hidden.IsEmpty() || hidden->IsUndefined()) { 219 if (hidden.IsEmpty() || hidden->IsUndefined()) {
197 hidden = v8::Object::New(); 220 hidden = v8::Object::New();
198 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden); 221 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden);
199 222
200 #ifndef NDEBUG 223 #ifndef NDEBUG
201 // Tell extension_process_bindings.js to validate callbacks and events 224 // Tell schema_generated_bindings.js to validate callbacks and events
202 // against their schema definitions in api/extension_api.json. 225 // against their schema definitions in api/extension_api.json.
203 v8::Local<v8::Object>::Cast(hidden) 226 v8::Local<v8::Object>::Cast(hidden)
204 ->Set(v8::String::New(kValidateCallbacks), v8::True()); 227 ->Set(v8::String::New(kValidateCallbacks), v8::True());
205 #endif 228 #endif
206 } 229 }
207 230
208 DCHECK(hidden->IsObject()); 231 DCHECK(hidden->IsObject());
209 return v8::Local<v8::Object>::Cast(hidden); 232 return v8::Local<v8::Object>::Cast(hidden);
210 } 233 }
211 234
212 v8::Handle<v8::Value> ChromeV8Extension::Print(const v8::Arguments& args) { 235 v8::Handle<v8::Value> ChromeV8Extension::Print(const v8::Arguments& args) {
213 if (args.Length() < 1) 236 if (args.Length() < 1)
214 return v8::Undefined(); 237 return v8::Undefined();
215 238
216 std::vector<std::string> components; 239 std::vector<std::string> components;
217 for (int i = 0; i < args.Length(); ++i) 240 for (int i = 0; i < args.Length(); ++i)
218 components.push_back(*v8::String::Utf8Value(args[i]->ToString())); 241 components.push_back(*v8::String::Utf8Value(args[i]->ToString()));
219 242
220 LOG(ERROR) << JoinString(components, ','); 243 LOG(ERROR) << JoinString(components, ',');
221 return v8::Undefined(); 244 return v8::Undefined();
222 } 245 }
OLDNEW
« no previous file with comments | « chrome/renderer/extensions/chrome_v8_extension.h ('k') | chrome/renderer/extensions/event_bindings.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698