OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/extension_process_bindings.h" | 5 #include "chrome/renderer/extensions/extension_process_bindings.h" |
6 | 6 |
7 #include "base/singleton.h" | 7 #include "base/singleton.h" |
8 #include "chrome/common/extensions/extension.h" | 8 #include "chrome/common/extensions/extension.h" |
9 #include "chrome/common/extensions/url_pattern.h" | 9 #include "chrome/common/extensions/url_pattern.h" |
10 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
11 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
| 12 #include "chrome/renderer/extension_groups.h" |
12 #include "chrome/renderer/extensions/bindings_utils.h" | 13 #include "chrome/renderer/extensions/bindings_utils.h" |
13 #include "chrome/renderer/extensions/event_bindings.h" | 14 #include "chrome/renderer/extensions/event_bindings.h" |
14 #include "chrome/renderer/extensions/js_only_v8_extensions.h" | 15 #include "chrome/renderer/extensions/js_only_v8_extensions.h" |
15 #include "chrome/renderer/extensions/renderer_extension_bindings.h" | 16 #include "chrome/renderer/extensions/renderer_extension_bindings.h" |
16 #include "chrome/renderer/render_view.h" | 17 #include "chrome/renderer/render_view.h" |
17 #include "grit/common_resources.h" | 18 #include "grit/common_resources.h" |
18 #include "grit/renderer_resources.h" | 19 #include "grit/renderer_resources.h" |
19 #include "webkit/api/public/WebFrame.h" | 20 #include "webkit/api/public/WebFrame.h" |
| 21 #include "webkit/api/public/WebScriptSource.h" |
20 #include "webkit/api/public/WebURL.h" | 22 #include "webkit/api/public/WebURL.h" |
21 #include "webkit/api/public/WebKit.h" | 23 #include "webkit/api/public/WebKit.h" |
22 | 24 |
23 using bindings_utils::GetStringResource; | 25 using bindings_utils::GetStringResource; |
24 using bindings_utils::ContextInfo; | 26 using bindings_utils::ContextInfo; |
25 using bindings_utils::ContextList; | 27 using bindings_utils::ContextList; |
26 using bindings_utils::GetContexts; | 28 using bindings_utils::GetContexts; |
27 using bindings_utils::GetPendingRequestMap; | 29 using bindings_utils::GetPendingRequestMap; |
28 using bindings_utils::PendingRequest; | 30 using bindings_utils::PendingRequest; |
29 using bindings_utils::PendingRequestMap; | 31 using bindings_utils::PendingRequestMap; |
30 using bindings_utils::ExtensionBase; | 32 using bindings_utils::ExtensionBase; |
| 33 using WebKit::WebScriptSource; |
| 34 using WebKit::WebString; |
31 | 35 |
32 namespace { | 36 namespace { |
33 | 37 |
34 // A map of extension ID to vector of page action ids. | 38 // A map of extension ID to vector of page action ids. |
35 typedef std::map< std::string, std::vector<std::string> > PageActionIdMap; | 39 typedef std::map< std::string, std::vector<std::string> > PageActionIdMap; |
36 | 40 |
37 // A map of permission name to whether its enabled for this extension. | 41 // A map of permission name to whether its enabled for this extension. |
38 typedef std::map<std::string, bool> PermissionsMap; | 42 typedef std::map<std::string, bool> PermissionsMap; |
39 | 43 |
40 // A map of extension ID to permissions map. | 44 // A map of extension ID to permissions map. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 } else if (name->Equals(v8::String::New("GetExtensionViews"))) { | 102 } else if (name->Equals(v8::String::New("GetExtensionViews"))) { |
99 return v8::FunctionTemplate::New(GetExtensionViews); | 103 return v8::FunctionTemplate::New(GetExtensionViews); |
100 } else if (name->Equals(v8::String::New("GetNextRequestId"))) { | 104 } else if (name->Equals(v8::String::New("GetNextRequestId"))) { |
101 return v8::FunctionTemplate::New(GetNextRequestId); | 105 return v8::FunctionTemplate::New(GetNextRequestId); |
102 } else if (name->Equals(v8::String::New("OpenChannelToTab"))) { | 106 } else if (name->Equals(v8::String::New("OpenChannelToTab"))) { |
103 return v8::FunctionTemplate::New(OpenChannelToTab); | 107 return v8::FunctionTemplate::New(OpenChannelToTab); |
104 } else if (name->Equals(v8::String::New("GetCurrentPageActions"))) { | 108 } else if (name->Equals(v8::String::New("GetCurrentPageActions"))) { |
105 return v8::FunctionTemplate::New(GetCurrentPageActions); | 109 return v8::FunctionTemplate::New(GetCurrentPageActions); |
106 } else if (name->Equals(v8::String::New("StartRequest"))) { | 110 } else if (name->Equals(v8::String::New("StartRequest"))) { |
107 return v8::FunctionTemplate::New(StartRequest); | 111 return v8::FunctionTemplate::New(StartRequest); |
| 112 } else if (name->Equals(v8::String::New("ExecuteScript"))) { |
| 113 return v8::FunctionTemplate::New(ExecuteScript); |
| 114 } else if (name->Equals(v8::String::New("ExecuteScriptWithUrl"))) { |
| 115 return v8::FunctionTemplate::New(ExecuteScriptWithUrl); |
108 } | 116 } |
109 | 117 |
110 return ExtensionBase::GetNativeFunction(name); | 118 return ExtensionBase::GetNativeFunction(name); |
111 } | 119 } |
112 | 120 |
113 private: | 121 private: |
| 122 static v8::Handle<v8::Value> ExecuteScript(const v8::Arguments& args) { |
| 123 if (args.Length() != 1 || !args[0]->IsString()) |
| 124 return v8::Boolean::New(false); |
| 125 |
| 126 std::string script_source = *v8::String::Utf8Value(args[0]->ToString()); |
| 127 WebScriptSource web_script = WebScriptSource( |
| 128 WebString::fromUTF8(script_source)); |
| 129 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); |
| 130 if (!renderview) |
| 131 return v8::Boolean::New(false); |
| 132 |
| 133 renderview->webview()->GetMainFrame()->executeScriptInNewWorld( |
| 134 &web_script, 1, EXTENSION_GROUP_CONTENT_SCRIPTS); |
| 135 return v8::Boolean::New(true); |
| 136 } |
| 137 |
| 138 static v8::Handle<v8::Value> ExecuteScriptWithUrl(const v8::Arguments& args) { |
| 139 if (args.Length() != 2) |
| 140 return v8::Undefined(); |
| 141 |
| 142 if (!args[0]->IsInt32() || !args[1]->IsString()) |
| 143 return v8::Undefined(); |
| 144 |
| 145 int request_id = args[0]->Int32Value(); |
| 146 std::string script_url = *v8::String::Utf8Value(args[1]->ToString()); |
| 147 SendExtensionRequest("extension.executeScriptWithUrl", script_url, |
| 148 request_id, true); |
| 149 return v8::Boolean::New(true); |
| 150 } |
| 151 |
114 static v8::Handle<v8::Value> GetExtensionAPIDefinition( | 152 static v8::Handle<v8::Value> GetExtensionAPIDefinition( |
115 const v8::Arguments& args) { | 153 const v8::Arguments& args) { |
116 return v8::String::New(GetStringResource<IDR_EXTENSION_API_JSON>()); | 154 return v8::String::New(GetStringResource<IDR_EXTENSION_API_JSON>()); |
117 } | 155 } |
118 | 156 |
119 static v8::Handle<v8::Value> GetExtensionViews(const v8::Arguments& args) { | 157 static v8::Handle<v8::Value> GetExtensionViews(const v8::Arguments& args) { |
120 if (args.Length() != 2) | 158 if (args.Length() != 2) |
121 return v8::Undefined(); | 159 return v8::Undefined(); |
122 | 160 |
123 if (!args[0]->IsInt32() || !args[1]->IsString()) | 161 if (!args[0]->IsInt32() || !args[1]->IsString()) |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 page_action_vector->Set(v8::Integer::New(i), | 264 page_action_vector->Set(v8::Integer::New(i), |
227 v8::String::New(page_action_id.c_str())); | 265 v8::String::New(page_action_id.c_str())); |
228 } | 266 } |
229 | 267 |
230 return page_action_vector; | 268 return page_action_vector; |
231 } | 269 } |
232 | 270 |
233 // Starts an API request to the browser, with an optional callback. The | 271 // Starts an API request to the browser, with an optional callback. The |
234 // callback will be dispatched to EventBindings::HandleResponse. | 272 // callback will be dispatched to EventBindings::HandleResponse. |
235 static v8::Handle<v8::Value> StartRequest(const v8::Arguments& args) { | 273 static v8::Handle<v8::Value> StartRequest(const v8::Arguments& args) { |
236 // Get the current RenderView so that we can send a routed IPC message from | |
237 // the correct source. | |
238 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); | |
239 if (!renderview) | |
240 return v8::Undefined(); | |
241 | |
242 if (args.Length() != 4 || !args[0]->IsString() || !args[1]->IsString() || | 274 if (args.Length() != 4 || !args[0]->IsString() || !args[1]->IsString() || |
243 !args[2]->IsInt32() || !args[3]->IsBoolean()) | 275 !args[2]->IsInt32() || !args[3]->IsBoolean()) |
244 return v8::Undefined(); | 276 return v8::Undefined(); |
245 | 277 |
246 std::string name = *v8::String::AsciiValue(args[0]); | 278 std::string name = *v8::String::AsciiValue(args[0]); |
247 if (GetFunctionNameSet()->find(name) == GetFunctionNameSet()->end()) { | 279 if (GetFunctionNameSet()->find(name) == GetFunctionNameSet()->end()) { |
248 NOTREACHED() << "Unexpected function " << name; | 280 NOTREACHED() << "Unexpected function " << name; |
249 return v8::Undefined(); | 281 return v8::Undefined(); |
250 } | 282 } |
251 | 283 |
252 if (!ExtensionProcessBindings::CurrentContextHasPermission(name)) { | 284 if (!ExtensionProcessBindings::CurrentContextHasPermission(name)) { |
253 #if EXTENSION_TIME_TO_BREAK_API | 285 #if EXTENSION_TIME_TO_BREAK_API |
254 return ExtensionProcessBindings::ThrowPermissionDeniedException(name); | 286 return ExtensionProcessBindings::ThrowPermissionDeniedException(name); |
255 #else | 287 #else |
256 ExtensionProcessBindings::ThrowPermissionDeniedException(name); | 288 ExtensionProcessBindings::ThrowPermissionDeniedException(name); |
257 #endif | 289 #endif |
258 } | 290 } |
259 | 291 |
260 std::string json_args = *v8::String::Utf8Value(args[1]); | 292 std::string json_args = *v8::String::Utf8Value(args[1]); |
261 int request_id = args[2]->Int32Value(); | 293 int request_id = args[2]->Int32Value(); |
262 bool has_callback = args[3]->BooleanValue(); | 294 bool has_callback = args[3]->BooleanValue(); |
| 295 SendExtensionRequest(name, json_args, request_id, has_callback); |
263 | 296 |
| 297 return v8::Undefined(); |
| 298 } |
| 299 |
| 300 static bool SendExtensionRequest(std::string name, |
| 301 std::string json_args, |
| 302 int request_id, |
| 303 bool has_callback) { |
| 304 // Get the current RenderView so that we can send a routed IPC message from |
| 305 // the correct source. |
| 306 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); |
| 307 if (!renderview) |
| 308 return false; |
264 v8::Persistent<v8::Context> current_context = | 309 v8::Persistent<v8::Context> current_context = |
265 v8::Persistent<v8::Context>::New(v8::Context::GetCurrent()); | 310 v8::Persistent<v8::Context>::New(v8::Context::GetCurrent()); |
266 DCHECK(!current_context.IsEmpty()); | 311 DCHECK(!current_context.IsEmpty()); |
267 GetPendingRequestMap()[request_id].reset(new PendingRequest( | 312 GetPendingRequestMap()[request_id].reset(new PendingRequest( |
268 current_context, name)); | 313 current_context, name)); |
269 | 314 |
270 renderview->SendExtensionRequest(name, json_args, request_id, has_callback); | 315 renderview->SendExtensionRequest(name, json_args, request_id, has_callback); |
271 | 316 return true; |
272 return v8::Undefined(); | |
273 } | 317 } |
274 }; | 318 }; |
275 | 319 |
276 } // namespace | 320 } // namespace |
277 | 321 |
278 v8::Extension* ExtensionProcessBindings::Get() { | 322 v8::Extension* ExtensionProcessBindings::Get() { |
279 static v8::Extension* extension = new ExtensionImpl(); | 323 static v8::Extension* extension = new ExtensionImpl(); |
280 return extension; | 324 return extension; |
281 } | 325 } |
282 | 326 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 | 449 |
406 v8::Local<v8::Function> function = | 450 v8::Local<v8::Function> function = |
407 v8::Local<v8::Function>::Cast(console_error); | 451 v8::Local<v8::Function>::Cast(console_error); |
408 v8::Local<v8::Value> argv[] = { v8::String::New(error_msg.c_str()) }; | 452 v8::Local<v8::Value> argv[] = { v8::String::New(error_msg.c_str()) }; |
409 if (!function.IsEmpty()) | 453 if (!function.IsEmpty()) |
410 function->Call(console->ToObject(), arraysize(argv), argv); | 454 function->Call(console->ToObject(), arraysize(argv), argv); |
411 | 455 |
412 return v8::Undefined(); | 456 return v8::Undefined(); |
413 #endif | 457 #endif |
414 } | 458 } |
OLD | NEW |