summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/message_handler.cc
blob: 5a21dcb5389fa4530167b1ffca5f039d250a27ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 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/browser/extensions/message_handler.h"

#include "chrome/browser/extensions/api/messaging/message_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/view_type_utils.h"
#include "chrome/common/extensions/extension_messages.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"

using content::WebContents;

namespace extensions {

MessageHandler::MessageHandler(
    content::RenderViewHost* render_view_host)
    : content::RenderViewHostObserver(render_view_host) {
}

MessageHandler::~MessageHandler() {
}

bool MessageHandler::OnMessageReceived(
    const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(MessageHandler, message)
    IPC_MESSAGE_HANDLER(ExtensionHostMsg_PostMessage, OnPostMessage)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void MessageHandler::RenderViewHostInitialized() {
  WebContents* web_contents =
      WebContents::FromRenderViewHost(render_view_host());
  Send(new ExtensionMsg_NotifyRenderViewType(
      routing_id(), chrome::GetViewType(web_contents)));
}

void MessageHandler::OnPostMessage(int port_id,
                                            const std::string& message) {
  Profile* profile = Profile::FromBrowserContext(
      render_view_host()->GetProcess()->GetBrowserContext());
  MessageService* message_service =
      ExtensionSystem::Get(profile)->message_service();
  if (message_service) {
    message_service->PostMessage(port_id, message);
  }
}

}  // namespace extensions