| 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 "base/stl_util-inl.h" | 8 #include "base/stl_util-inl.h" |
| 9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
| 10 #include "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
| 11 #include "chrome/renderer/extensions/bindings_utils.h" | 11 #include "chrome/renderer/extensions/bindings_utils.h" |
| 12 #include "chrome/renderer/extensions/event_bindings.h" | 12 #include "chrome/renderer/extensions/event_bindings.h" |
| 13 #include "chrome/renderer/extensions/renderer_extension_bindings.h" | 13 #include "chrome/renderer/extensions/renderer_extension_bindings.h" |
| 14 #include "chrome/renderer/js_only_v8_extensions.h" | 14 #include "chrome/renderer/js_only_v8_extensions.h" |
| 15 #include "chrome/renderer/render_view.h" | 15 #include "chrome/renderer/render_view.h" |
| 16 #include "grit/renderer_resources.h" | 16 #include "grit/renderer_resources.h" |
| 17 #include "webkit/api/public/WebScriptSource.h" | |
| 18 #include "webkit/glue/webframe.h" | 17 #include "webkit/glue/webframe.h" |
| 19 | 18 |
| 20 using WebKit::WebScriptSource; | |
| 21 using WebKit::WebString; | |
| 22 | |
| 23 namespace { | 19 namespace { |
| 24 | 20 |
| 25 const char kExtensionName[] = "chrome/ExtensionProcessBindings"; | 21 const char kExtensionName[] = "chrome/ExtensionProcessBindings"; |
| 26 const char* kExtensionDeps[] = { | 22 const char* kExtensionDeps[] = { |
| 27 BaseJsV8Extension::kName, | 23 BaseJsV8Extension::kName, |
| 28 EventBindings::kName, | 24 EventBindings::kName, |
| 29 JsonSchemaJsV8Extension::kName, | 25 JsonSchemaJsV8Extension::kName, |
| 30 RendererExtensionBindings::kName, | 26 RendererExtensionBindings::kName, |
| 31 }; | 27 }; |
| 32 | 28 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 return v8::FunctionTemplate::New(StartRequest, name); | 94 return v8::FunctionTemplate::New(StartRequest, name); |
| 99 | 95 |
| 100 return v8::Handle<v8::FunctionTemplate>(); | 96 return v8::Handle<v8::FunctionTemplate>(); |
| 101 } | 97 } |
| 102 | 98 |
| 103 private: | 99 private: |
| 104 static v8::Handle<v8::Value> RegisterExtension(const v8::Arguments& args) { | 100 static v8::Handle<v8::Value> RegisterExtension(const v8::Arguments& args) { |
| 105 RenderView* renderview = GetRenderViewForCurrentContext(); | 101 RenderView* renderview = GetRenderViewForCurrentContext(); |
| 106 DCHECK(renderview); | 102 DCHECK(renderview); |
| 107 GURL url = renderview->webview()->GetMainFrame()->GetURL(); | 103 GURL url = renderview->webview()->GetMainFrame()->GetURL(); |
| 108 DCHECK(url.scheme() == chrome::kExtensionScheme); | 104 // Don't check the URL scheme in unit tests. |
| 105 if (RenderThread::current()) |
| 106 DCHECK(url.scheme() == chrome::kExtensionScheme); |
| 109 | 107 |
| 110 v8::Persistent<v8::Context> current_context = | 108 v8::Persistent<v8::Context> current_context = |
| 111 v8::Persistent<v8::Context>::New(v8::Context::GetCurrent()); | 109 v8::Persistent<v8::Context>::New(v8::Context::GetCurrent()); |
| 112 DCHECK(!current_context.IsEmpty()); | 110 DCHECK(!current_context.IsEmpty()); |
| 113 | 111 |
| 114 std::string extension_id = url.host(); | 112 std::string extension_id = url.host(); |
| 115 GetRegisteredContexts(extension_id).push_back(current_context); | 113 GetRegisteredContexts(extension_id).push_back(current_context); |
| 116 return v8::String::New(extension_id.c_str()); | 114 return v8::String::New(extension_id.c_str()); |
| 117 } | 115 } |
| 118 | 116 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 | 209 |
| 212 v8::Extension* ExtensionProcessBindings::Get() { | 210 v8::Extension* ExtensionProcessBindings::Get() { |
| 213 return new ExtensionImpl(); | 211 return new ExtensionImpl(); |
| 214 } | 212 } |
| 215 | 213 |
| 216 void ExtensionProcessBindings::SetFunctionNames( | 214 void ExtensionProcessBindings::SetFunctionNames( |
| 217 const std::vector<std::string>& names) { | 215 const std::vector<std::string>& names) { |
| 218 ExtensionImpl::SetFunctionNames(names); | 216 ExtensionImpl::SetFunctionNames(names); |
| 219 } | 217 } |
| 220 | 218 |
| 221 void ExtensionProcessBindings::RegisterExtensionContext(WebFrame* frame) { | |
| 222 frame->ExecuteScript(WebScriptSource(WebString::fromUTF8( | |
| 223 "chrome.self.register_();"))); | |
| 224 } | |
| 225 | |
| 226 void ExtensionProcessBindings::HandleResponse(int request_id, bool success, | 219 void ExtensionProcessBindings::HandleResponse(int request_id, bool success, |
| 227 const std::string& response, | 220 const std::string& response, |
| 228 const std::string& error) { | 221 const std::string& error) { |
| 229 CallContext* call = GetPendingRequestMap()[request_id]; | 222 CallContext* call = GetPendingRequestMap()[request_id]; |
| 230 if (!call) | 223 if (!call) |
| 231 return; // The frame went away. | 224 return; // The frame went away. |
| 232 | 225 |
| 233 v8::HandleScope handle_scope; | 226 v8::HandleScope handle_scope; |
| 234 v8::Handle<v8::Value> argv[5]; | 227 v8::Handle<v8::Value> argv[5]; |
| 235 argv[0] = v8::Integer::New(request_id); | 228 argv[0] = v8::Integer::New(request_id); |
| 236 argv[1] = v8::String::New(call->name.c_str()); | 229 argv[1] = v8::String::New(call->name.c_str()); |
| 237 argv[2] = v8::Boolean::New(success); | 230 argv[2] = v8::Boolean::New(success); |
| 238 argv[3] = v8::String::New(response.c_str()); | 231 argv[3] = v8::String::New(response.c_str()); |
| 239 argv[4] = v8::String::New(error.c_str()); | 232 argv[4] = v8::String::New(error.c_str()); |
| 240 CallFunctionInContext(call->context, "chrome.handleResponse_", | 233 CallFunctionInContext(call->context, "chrome.handleResponse_", |
| 241 arraysize(argv), argv); | 234 arraysize(argv), argv); |
| 242 | 235 |
| 243 GetPendingRequestMap().erase(request_id); | 236 GetPendingRequestMap().erase(request_id); |
| 244 delete call; | 237 delete call; |
| 245 } | 238 } |
| OLD | NEW |