• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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/browser/extensions/extension_message_handler.h"
6 
7 #include "chrome/browser/extensions/extension_message_service.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/common/extensions/extension_messages.h"
10 #include "content/browser/child_process_security_policy.h"
11 #include "content/browser/renderer_host/render_process_host.h"
12 #include "content/browser/renderer_host/render_view_host.h"
13 #include "content/browser/renderer_host/render_view_host_delegate.h"
14 
ExtensionMessageHandler(RenderViewHost * render_view_host)15 ExtensionMessageHandler::ExtensionMessageHandler(
16     RenderViewHost* render_view_host)
17     : RenderViewHostObserver(render_view_host) {
18 }
19 
~ExtensionMessageHandler()20 ExtensionMessageHandler::~ExtensionMessageHandler() {
21 }
22 
OnMessageReceived(const IPC::Message & message)23 bool ExtensionMessageHandler::OnMessageReceived(
24     const IPC::Message& message) {
25   bool handled = true;
26   IPC_BEGIN_MESSAGE_MAP(ExtensionMessageHandler, message)
27     IPC_MESSAGE_HANDLER(ExtensionHostMsg_PostMessage, OnPostMessage)
28     IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
29     IPC_MESSAGE_UNHANDLED(handled = false)
30   IPC_END_MESSAGE_MAP()
31   return handled;
32 }
33 
OnPostMessage(int port_id,const std::string & message)34 void ExtensionMessageHandler::OnPostMessage(int port_id,
35                                             const std::string& message) {
36   Profile* profile = render_view_host()->process()->profile();
37   if (profile->GetExtensionMessageService()) {
38     profile->GetExtensionMessageService()->PostMessageFromRenderer(
39         port_id, message);
40   }
41 }
42 
OnRequest(const ExtensionHostMsg_DomMessage_Params & params)43 void ExtensionMessageHandler::OnRequest(
44     const ExtensionHostMsg_DomMessage_Params& params) {
45   if (!ChildProcessSecurityPolicy::GetInstance()->
46           HasExtensionBindings(render_view_host()->process()->id())) {
47     // This can happen if someone uses window.open() to open an extension URL
48     // from a non-extension context.
49     Send(new ExtensionMsg_Response(
50         routing_id(), params.request_id, false, std::string(),
51         "Access to extension API denied."));
52     return;
53   }
54 
55   render_view_host()->delegate()->ProcessWebUIMessage(params);
56 }
57