Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / extensions / renderer / extension_frame_helper.cc
blobe5f381a75472eb65f24155a9fffd390190f16d3e
1 // Copyright 2013 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.
5 #include "extensions/renderer/extension_frame_helper.h"
7 #include "content/public/renderer/render_frame.h"
8 #include "extensions/common/api/messaging/message.h"
9 #include "extensions/common/extension_messages.h"
10 #include "extensions/renderer/console.h"
11 #include "extensions/renderer/dispatcher.h"
12 #include "extensions/renderer/messaging_bindings.h"
13 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
14 #include "third_party/WebKit/public/web/WebLocalFrame.h"
16 namespace extensions {
18 ExtensionFrameHelper::ExtensionFrameHelper(content::RenderFrame* render_frame,
19 Dispatcher* extension_dispatcher)
20 : content::RenderFrameObserver(render_frame),
21 content::RenderFrameObserverTracker<ExtensionFrameHelper>(render_frame),
22 extension_dispatcher_(extension_dispatcher) {}
24 ExtensionFrameHelper::~ExtensionFrameHelper() {
27 void ExtensionFrameHelper::DidCreateScriptContext(
28 v8::Handle<v8::Context> context,
29 int extension_group,
30 int world_id) {
31 extension_dispatcher_->DidCreateScriptContext(
32 render_frame()->GetWebFrame(), context, extension_group, world_id);
35 void ExtensionFrameHelper::WillReleaseScriptContext(
36 v8::Handle<v8::Context> context,
37 int world_id) {
38 extension_dispatcher_->WillReleaseScriptContext(
39 render_frame()->GetWebFrame(), context, world_id);
42 bool ExtensionFrameHelper::OnMessageReceived(const IPC::Message& message) {
43 bool handled = true;
44 IPC_BEGIN_MESSAGE_MAP(ExtensionFrameHelper, message)
45 IPC_MESSAGE_HANDLER(ExtensionMsg_AddMessageToConsole,
46 OnAddMessageToConsole)
47 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnConnect,
48 OnExtensionDispatchOnConnect)
49 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnExtensionDeliverMessage)
50 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnDisconnect,
51 OnExtensionDispatchOnDisconnect)
52 IPC_MESSAGE_UNHANDLED(handled = false)
53 IPC_END_MESSAGE_MAP()
54 return handled;
57 void ExtensionFrameHelper::OnAddMessageToConsole(
58 content::ConsoleMessageLevel level,
59 const std::string& message) {
60 console::AddMessage(render_frame()->GetRenderView(), level, message);
63 void ExtensionFrameHelper::OnExtensionDispatchOnConnect(
64 int target_port_id,
65 const std::string& channel_name,
66 const ExtensionMsg_TabConnectionInfo& source,
67 const ExtensionMsg_ExternalConnectionInfo& info,
68 const std::string& tls_channel_id) {
69 MessagingBindings::DispatchOnConnect(
70 extension_dispatcher_->script_context_set(),
71 target_port_id,
72 channel_name,
73 source,
74 info,
75 tls_channel_id,
76 render_frame());
79 void ExtensionFrameHelper::OnExtensionDeliverMessage(int target_id,
80 const Message& message) {
81 MessagingBindings::DeliverMessage(
82 extension_dispatcher_->script_context_set(), target_id, message,
83 render_frame());
86 void ExtensionFrameHelper::OnExtensionDispatchOnDisconnect(
87 int port_id,
88 const std::string& error_message) {
89 MessagingBindings::DispatchOnDisconnect(
90 extension_dispatcher_->script_context_set(), port_id, error_message,
91 render_frame());
94 } // namespace extensions