1// Copyright (c) 2012 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/api/messaging/extension_message_port.h"
6
7#include "chrome/browser/extensions/extension_host.h"
8#include "chrome/browser/extensions/extension_process_manager.h"
9#include "chrome/browser/extensions/extension_system.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/common/extensions/background_info.h"
12#include "chrome/common/extensions/extension_messages.h"
13#include "content/public/browser/render_process_host.h"
14
15namespace extensions {
16
17ExtensionMessagePort::ExtensionMessagePort(content::RenderProcessHost* process,
18                                           int routing_id,
19                                           const std::string& extension_id)
20     : process_(process),
21       routing_id_(routing_id),
22       extension_id_(extension_id),
23       background_host_ptr_(NULL) {
24}
25
26void ExtensionMessagePort::DispatchOnConnect(
27    int dest_port_id,
28    const std::string& channel_name,
29    const base::DictionaryValue& source_tab,
30    const std::string& source_extension_id,
31    const std::string& target_extension_id,
32    const GURL& source_url) {
33  ExtensionMsg_ExternalConnectionInfo info;
34  info.target_id = target_extension_id;
35  info.source_id = source_extension_id;
36  info.source_url = source_url;
37  process_->Send(new ExtensionMsg_DispatchOnConnect(
38      routing_id_, dest_port_id, channel_name, source_tab, info));
39}
40
41void ExtensionMessagePort::DispatchOnDisconnect(
42    int source_port_id,
43    const std::string& error_message) {
44  process_->Send(new ExtensionMsg_DispatchOnDisconnect(
45      routing_id_, source_port_id, error_message));
46}
47
48void ExtensionMessagePort::DispatchOnMessage(const std::string& message,
49                                             int target_port_id) {
50    process_->Send(new ExtensionMsg_DeliverMessage(
51        routing_id_, target_port_id, message));
52}
53
54void ExtensionMessagePort::IncrementLazyKeepaliveCount() {
55  Profile* profile =
56      Profile::FromBrowserContext(process_->GetBrowserContext());
57  ExtensionProcessManager* pm =
58      ExtensionSystem::Get(profile)->process_manager();
59  ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id_);
60  if (host && BackgroundInfo::HasLazyBackgroundPage(host->extension()))
61    pm->IncrementLazyKeepaliveCount(host->extension());
62
63  // Keep track of the background host, so when we decrement, we only do so if
64  // the host hasn't reloaded.
65  background_host_ptr_ = host;
66}
67
68void ExtensionMessagePort::DecrementLazyKeepaliveCount() {
69  Profile* profile =
70      Profile::FromBrowserContext(process_->GetBrowserContext());
71  ExtensionProcessManager* pm =
72      ExtensionSystem::Get(profile)->process_manager();
73  ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id_);
74  if (host && host == background_host_ptr_)
75    pm->DecrementLazyKeepaliveCount(host->extension());
76}
77
78content::RenderProcessHost* ExtensionMessagePort::GetRenderProcessHost() {
79  return process_;
80}
81
82}  // namespace extensions
83