Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / extensions / renderer / extension_frame_helper.cc
blobfe581c331fd845868386be85175a7fe91f97244b
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/constants.h"
10 #include "extensions/common/extension_messages.h"
11 #include "extensions/common/manifest_handlers/background_info.h"
12 #include "extensions/renderer/console.h"
13 #include "extensions/renderer/content_watcher.h"
14 #include "extensions/renderer/dispatcher.h"
15 #include "extensions/renderer/messaging_bindings.h"
16 #include "extensions/renderer/script_context.h"
17 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
18 #include "third_party/WebKit/public/web/WebDocument.h"
19 #include "third_party/WebKit/public/web/WebLocalFrame.h"
21 namespace extensions {
23 namespace {
25 base::LazyInstance<std::set<const ExtensionFrameHelper*>> g_frame_helpers =
26 LAZY_INSTANCE_INITIALIZER;
28 // Returns true if the render frame corresponding with |frame_helper| matches
29 // the given criteria.
30 bool RenderFrameMatches(const ExtensionFrameHelper* frame_helper,
31 ViewType match_view_type,
32 int match_window_id,
33 const std::string& match_extension_id) {
34 if (match_view_type != VIEW_TYPE_INVALID &&
35 frame_helper->view_type() != match_view_type)
36 return false;
37 GURL url = frame_helper->render_frame()->GetWebFrame()->document().url();
38 if (!url.SchemeIs(kExtensionScheme))
39 return false;
40 if (url.host() != match_extension_id)
41 return false;
42 if (match_window_id != extension_misc::kUnknownWindowId &&
43 frame_helper->browser_window_id() != match_window_id)
44 return false;
45 return true;
48 } // namespace
50 ExtensionFrameHelper::ExtensionFrameHelper(content::RenderFrame* render_frame,
51 Dispatcher* extension_dispatcher)
52 : content::RenderFrameObserver(render_frame),
53 content::RenderFrameObserverTracker<ExtensionFrameHelper>(render_frame),
54 view_type_(VIEW_TYPE_INVALID),
55 tab_id_(-1),
56 browser_window_id_(-1),
57 extension_dispatcher_(extension_dispatcher),
58 did_create_current_document_element_(false) {
59 g_frame_helpers.Get().insert(this);
62 ExtensionFrameHelper::~ExtensionFrameHelper() {
63 g_frame_helpers.Get().erase(this);
66 // static
67 std::vector<content::RenderFrame*> ExtensionFrameHelper::GetExtensionFrames(
68 const std::string& extension_id,
69 int browser_window_id,
70 ViewType view_type) {
71 std::vector<content::RenderFrame*> render_frames;
72 for (const ExtensionFrameHelper* helper : g_frame_helpers.Get()) {
73 if (RenderFrameMatches(helper, view_type, browser_window_id, extension_id))
74 render_frames.push_back(helper->render_frame());
76 return render_frames;
79 // static
80 content::RenderFrame* ExtensionFrameHelper::GetBackgroundPageFrame(
81 const std::string& extension_id) {
82 for (const ExtensionFrameHelper* helper : g_frame_helpers.Get()) {
83 if (RenderFrameMatches(helper, VIEW_TYPE_EXTENSION_BACKGROUND_PAGE,
84 extension_misc::kUnknownWindowId, extension_id)) {
85 blink::WebLocalFrame* web_frame = helper->render_frame()->GetWebFrame();
86 // Check if this is the top frame.
87 if (web_frame->top() == web_frame)
88 return helper->render_frame();
91 return nullptr;
94 // static
95 bool ExtensionFrameHelper::IsContextForEventPage(const ScriptContext* context) {
96 content::RenderFrame* render_frame = context->GetRenderFrame();
97 return context->extension() && render_frame &&
98 BackgroundInfo::HasLazyBackgroundPage(context->extension()) &&
99 ExtensionFrameHelper::Get(render_frame)->view_type() ==
100 VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
103 void ExtensionFrameHelper::DidCreateDocumentElement() {
104 did_create_current_document_element_ = true;
105 extension_dispatcher_->DidCreateDocumentElement(
106 render_frame()->GetWebFrame());
109 void ExtensionFrameHelper::DidCreateNewDocument() {
110 did_create_current_document_element_ = false;
113 void ExtensionFrameHelper::DidMatchCSS(
114 const blink::WebVector<blink::WebString>& newly_matching_selectors,
115 const blink::WebVector<blink::WebString>& stopped_matching_selectors) {
116 extension_dispatcher_->content_watcher()->DidMatchCSS(
117 render_frame()->GetWebFrame(), newly_matching_selectors,
118 stopped_matching_selectors);
121 void ExtensionFrameHelper::DidCreateScriptContext(
122 v8::Local<v8::Context> context,
123 int extension_group,
124 int world_id) {
125 extension_dispatcher_->DidCreateScriptContext(
126 render_frame()->GetWebFrame(), context, extension_group, world_id);
129 void ExtensionFrameHelper::WillReleaseScriptContext(
130 v8::Local<v8::Context> context,
131 int world_id) {
132 extension_dispatcher_->WillReleaseScriptContext(
133 render_frame()->GetWebFrame(), context, world_id);
136 bool ExtensionFrameHelper::OnMessageReceived(const IPC::Message& message) {
137 bool handled = true;
138 IPC_BEGIN_MESSAGE_MAP(ExtensionFrameHelper, message)
139 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnConnect,
140 OnExtensionDispatchOnConnect)
141 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnExtensionDeliverMessage)
142 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnDisconnect,
143 OnExtensionDispatchOnDisconnect)
144 IPC_MESSAGE_HANDLER(ExtensionMsg_SetTabId, OnExtensionSetTabId)
145 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateBrowserWindowId,
146 OnUpdateBrowserWindowId)
147 IPC_MESSAGE_HANDLER(ExtensionMsg_NotifyRenderViewType,
148 OnNotifyRendererViewType)
149 IPC_MESSAGE_HANDLER(ExtensionMsg_Response, OnExtensionResponse)
150 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnExtensionMessageInvoke)
151 IPC_MESSAGE_UNHANDLED(handled = false)
152 IPC_END_MESSAGE_MAP()
153 return handled;
156 void ExtensionFrameHelper::OnExtensionDispatchOnConnect(
157 int target_port_id,
158 const std::string& channel_name,
159 const ExtensionMsg_TabConnectionInfo& source,
160 const ExtensionMsg_ExternalConnectionInfo& info,
161 const std::string& tls_channel_id) {
162 MessagingBindings::DispatchOnConnect(
163 extension_dispatcher_->script_context_set(),
164 target_port_id,
165 channel_name,
166 source,
167 info,
168 tls_channel_id,
169 render_frame());
172 void ExtensionFrameHelper::OnExtensionDeliverMessage(int target_id,
173 const Message& message) {
174 MessagingBindings::DeliverMessage(
175 extension_dispatcher_->script_context_set(), target_id, message,
176 render_frame());
179 void ExtensionFrameHelper::OnExtensionDispatchOnDisconnect(
180 int port_id,
181 const std::string& error_message) {
182 MessagingBindings::DispatchOnDisconnect(
183 extension_dispatcher_->script_context_set(), port_id, error_message,
184 render_frame());
187 void ExtensionFrameHelper::OnExtensionSetTabId(int tab_id) {
188 CHECK_EQ(tab_id_, -1);
189 CHECK_GE(tab_id, 0);
190 tab_id_ = tab_id;
193 void ExtensionFrameHelper::OnUpdateBrowserWindowId(int browser_window_id) {
194 browser_window_id_ = browser_window_id;
197 void ExtensionFrameHelper::OnNotifyRendererViewType(ViewType type) {
198 // TODO(devlin): It'd be really nice to be able to
199 // DCHECK_EQ(VIEW_TYPE_INVALID, view_type_) here.
200 view_type_ = type;
203 void ExtensionFrameHelper::OnExtensionResponse(int request_id,
204 bool success,
205 const base::ListValue& response,
206 const std::string& error) {
207 extension_dispatcher_->OnExtensionResponse(request_id,
208 success,
209 response,
210 error);
213 void ExtensionFrameHelper::OnExtensionMessageInvoke(
214 const std::string& extension_id,
215 const std::string& module_name,
216 const std::string& function_name,
217 const base::ListValue& args,
218 bool user_gesture) {
219 extension_dispatcher_->InvokeModuleSystemMethod(render_frame(), extension_id,
220 module_name, function_name,
221 args, user_gesture);
224 } // namespace extensions