OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/browser_plugin/chrome_browser_plugin_observer.h" |
| 6 |
| 7 #include <stdio.h> |
| 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/common/chrome_browser_plugin_messages.h" |
| 12 #include "content/public/renderer/browser_plugin/browser_plugin.h" |
| 13 #include "content/public/renderer/browser_plugin/browser_plugin_method_binding.h
" |
| 14 #include "content/public/renderer/v8_value_converter.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| 19 #include "v8/include/v8.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Method bindings. |
| 24 const char kMethodExecuteScript[] = "executeScript"; |
| 25 |
| 26 // Events. |
| 27 const char kEventExecuteScriptResponse[] = "scriptresponse"; |
| 28 |
| 29 // Parameters/properties on events. |
| 30 const char kResult[] = "result"; |
| 31 const char kRequestId[] = "requestId"; |
| 32 |
| 33 class BrowserPluginBindingExecuteScript |
| 34 : public content::BrowserPluginMethodBinding { |
| 35 public: |
| 36 BrowserPluginBindingExecuteScript( |
| 37 ChromeBrowserPluginObserver* browser_plugin_observer) |
| 38 : browser_plugin_observer_(browser_plugin_observer) { |
| 39 } |
| 40 |
| 41 virtual ~BrowserPluginBindingExecuteScript() {} |
| 42 |
| 43 virtual bool MatchesName(const NPIdentifier& name) const OVERRIDE { |
| 44 return WebKit::WebBindings::getStringIdentifier( |
| 45 kMethodExecuteScript) == name; |
| 46 } |
| 47 |
| 48 virtual bool Invoke(content::BrowserPlugin* browser_plugin, |
| 49 const NPVariant* args, |
| 50 NPVariant* result) OVERRIDE { |
| 51 v8::Handle<v8::Value> value = WebKit::WebBindings::toV8Value(&args[0]); |
| 52 if (value.IsEmpty() || !browser_plugin->GetContainer() || |
| 53 !browser_plugin->GetContainer()->element().document().frame()) |
| 54 return false; |
| 55 WebKit::WebFrame* frame = browser_plugin->GetContainer()-> |
| 56 element().document().frame(); |
| 57 v8::HandleScope handle_scope; |
| 58 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
| 59 v8::Context::Scope context_scope(context); |
| 60 scoped_ptr<content::V8ValueConverter> converter( |
| 61 content::V8ValueConverter::create()); |
| 62 base::Value* result_value = converter->FromV8Value(value, context); |
| 63 base::ListValue list; |
| 64 list.Set(0, result_value ? result_value : |
| 65 base::Value::CreateNullValue()); |
| 66 int request_id = browser_plugin_observer_->GetNextRequestID(); |
| 67 |
| 68 browser_plugin_observer_->Send(new ChromeBrowserPluginHostMsg_ExecuteScript( |
| 69 browser_plugin->GetRenderView()->GetRoutingID(), |
| 70 browser_plugin_observer_->instance_id(), |
| 71 request_id, |
| 72 list)); |
| 73 |
| 74 INT32_TO_NPVARIANT(request_id, *result); |
| 75 return true; |
| 76 } |
| 77 |
| 78 virtual uint32 GetArgCount() const OVERRIDE { return 1; } |
| 79 |
| 80 private: |
| 81 |
| 82 ChromeBrowserPluginObserver* browser_plugin_observer_; |
| 83 DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingExecuteScript); |
| 84 }; |
| 85 |
| 86 } // namespace |
| 87 |
| 88 ChromeBrowserPluginObserver::ChromeBrowserPluginObserver( |
| 89 content::BrowserPlugin* browser_plugin) : |
| 90 content::BrowserPluginObserver(browser_plugin), |
| 91 next_request_id_(0) { |
| 92 browser_plugin->AddMethodBinding(new BrowserPluginBindingExecuteScript(this)); |
| 93 browser_plugin->RequestMessage( |
| 94 ChromeBrowserPluginHostMsg_ExecuteScript_Response::ID); |
| 95 } |
| 96 |
| 97 ChromeBrowserPluginObserver::~ChromeBrowserPluginObserver() { |
| 98 } |
| 99 |
| 100 int ChromeBrowserPluginObserver::GetNextRequestID() { |
| 101 return next_request_id_++; |
| 102 } |
| 103 |
| 104 bool ChromeBrowserPluginObserver::OnMessageReceived( |
| 105 const IPC::Message& message) { |
| 106 bool handled = true; |
| 107 IPC_BEGIN_MESSAGE_MAP(ChromeBrowserPluginObserver, message) |
| 108 IPC_MESSAGE_HANDLER(ChromeBrowserPluginHostMsg_ExecuteScript_Response, |
| 109 OnExecuteScriptResponse) |
| 110 IPC_MESSAGE_UNHANDLED(handled = false) |
| 111 IPC_END_MESSAGE_MAP() |
| 112 return handled; |
| 113 } |
| 114 |
| 115 void ChromeBrowserPluginObserver::OnExecuteScriptResponse( |
| 116 int instance_id, |
| 117 int request_id, |
| 118 const base::ListValue& result) { |
| 119 std::map<std::string, base::Value*> props; |
| 120 props[kRequestId] = base::Value::CreateIntegerValue(request_id); |
| 121 props[kResult] = result.DeepCopy(); |
| 122 browser_plugin()->TriggerEvent(kEventExecuteScriptResponse, &props); |
| 123 } |
OLD | NEW |