| Index: chrome/renderer/browser_plugin/chrome_browser_plugin_observer.cc
|
| diff --git a/chrome/renderer/browser_plugin/chrome_browser_plugin_observer.cc b/chrome/renderer/browser_plugin/chrome_browser_plugin_observer.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..544bf3bd472a7ae8b383c3603d8882ff91058486
|
| --- /dev/null
|
| +++ b/chrome/renderer/browser_plugin/chrome_browser_plugin_observer.cc
|
| @@ -0,0 +1,123 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/renderer/browser_plugin/chrome_browser_plugin_observer.h"
|
| +
|
| +#include <stdio.h>
|
| +
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "base/values.h"
|
| +#include "chrome/common/chrome_browser_plugin_messages.h"
|
| +#include "content/public/renderer/browser_plugin/browser_plugin.h"
|
| +#include "content/public/renderer/browser_plugin/browser_plugin_method_binding.h"
|
| +#include "content/public/renderer/v8_value_converter.h"
|
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
|
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
|
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
|
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h"
|
| +#include "v8/include/v8.h"
|
| +
|
| +namespace {
|
| +
|
| +// Method bindings.
|
| +const char kMethodExecuteScript[] = "executeScript";
|
| +
|
| +// Events.
|
| +const char kEventExecuteScriptResponse[] = "scriptresponse";
|
| +
|
| +// Parameters/properties on events.
|
| +const char kResult[] = "result";
|
| +const char kRequestId[] = "requestId";
|
| +
|
| +class BrowserPluginBindingExecuteScript
|
| + : public content::BrowserPluginMethodBinding {
|
| + public:
|
| + BrowserPluginBindingExecuteScript(
|
| + ChromeBrowserPluginObserver* browser_plugin_observer)
|
| + : browser_plugin_observer_(browser_plugin_observer) {
|
| + }
|
| +
|
| + virtual ~BrowserPluginBindingExecuteScript() {}
|
| +
|
| + virtual bool MatchesName(const NPIdentifier& name) const OVERRIDE {
|
| + return WebKit::WebBindings::getStringIdentifier(
|
| + kMethodExecuteScript) == name;
|
| + }
|
| +
|
| + virtual bool Invoke(content::BrowserPlugin* browser_plugin,
|
| + const NPVariant* args,
|
| + NPVariant* result) OVERRIDE {
|
| + v8::Handle<v8::Value> value = WebKit::WebBindings::toV8Value(&args[0]);
|
| + if (value.IsEmpty() || !browser_plugin->GetContainer() ||
|
| + !browser_plugin->GetContainer()->element().document().frame())
|
| + return false;
|
| + WebKit::WebFrame* frame = browser_plugin->GetContainer()->
|
| + element().document().frame();
|
| + v8::HandleScope handle_scope;
|
| + v8::Local<v8::Context> context = frame->mainWorldScriptContext();
|
| + v8::Context::Scope context_scope(context);
|
| + scoped_ptr<content::V8ValueConverter> converter(
|
| + content::V8ValueConverter::create());
|
| + base::Value* result_value = converter->FromV8Value(value, context);
|
| + base::ListValue list;
|
| + list.Set(0, result_value ? result_value :
|
| + base::Value::CreateNullValue());
|
| + int request_id = browser_plugin_observer_->GetNextRequestID();
|
| +
|
| + browser_plugin_observer_->Send(new ChromeBrowserPluginHostMsg_ExecuteScript(
|
| + browser_plugin->GetRenderView()->GetRoutingID(),
|
| + browser_plugin_observer_->instance_id(),
|
| + request_id,
|
| + list));
|
| +
|
| + INT32_TO_NPVARIANT(request_id, *result);
|
| + return true;
|
| + }
|
| +
|
| + virtual uint32 GetArgCount() const OVERRIDE { return 1; }
|
| +
|
| + private:
|
| +
|
| + ChromeBrowserPluginObserver* browser_plugin_observer_;
|
| + DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingExecuteScript);
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +ChromeBrowserPluginObserver::ChromeBrowserPluginObserver(
|
| + content::BrowserPlugin* browser_plugin) :
|
| + content::BrowserPluginObserver(browser_plugin),
|
| + next_request_id_(0) {
|
| + browser_plugin->AddMethodBinding(new BrowserPluginBindingExecuteScript(this));
|
| + browser_plugin->RequestMessage(
|
| + ChromeBrowserPluginHostMsg_ExecuteScript_Response::ID);
|
| +}
|
| +
|
| +ChromeBrowserPluginObserver::~ChromeBrowserPluginObserver() {
|
| +}
|
| +
|
| +int ChromeBrowserPluginObserver::GetNextRequestID() {
|
| + return next_request_id_++;
|
| +}
|
| +
|
| +bool ChromeBrowserPluginObserver::OnMessageReceived(
|
| + const IPC::Message& message) {
|
| + bool handled = true;
|
| + IPC_BEGIN_MESSAGE_MAP(ChromeBrowserPluginObserver, message)
|
| + IPC_MESSAGE_HANDLER(ChromeBrowserPluginHostMsg_ExecuteScript_Response,
|
| + OnExecuteScriptResponse)
|
| + IPC_MESSAGE_UNHANDLED(handled = false)
|
| + IPC_END_MESSAGE_MAP()
|
| + return handled;
|
| +}
|
| +
|
| +void ChromeBrowserPluginObserver::OnExecuteScriptResponse(
|
| + int instance_id,
|
| + int request_id,
|
| + const base::ListValue& result) {
|
| + std::map<std::string, base::Value*> props;
|
| + props[kRequestId] = base::Value::CreateIntegerValue(request_id);
|
| + props[kResult] = result.DeepCopy();
|
| + browser_plugin()->TriggerEvent(kEventExecuteScriptResponse, &props);
|
| +}
|
|
|