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.
5 #include "chrome/browser/extensions/api/messaging/extension_message_port.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "content/public/browser/render_process_host.h"
9 #include "extensions/browser/extension_host.h"
10 #include "extensions/browser/extension_system.h"
11 #include "extensions/browser/process_manager.h"
12 #include "extensions/common/extension_messages.h"
13 #include "extensions/common/manifest_handlers/background_info.h"
15 namespace extensions
{
17 ExtensionMessagePort::ExtensionMessagePort(content::RenderProcessHost
* process
,
19 const std::string
& extension_id
)
21 routing_id_(routing_id
),
22 extension_id_(extension_id
),
23 background_host_ptr_(NULL
) {
26 void ExtensionMessagePort::DispatchOnConnect(
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 const std::string
& tls_channel_id
) {
34 ExtensionMsg_ExternalConnectionInfo info
;
35 info
.target_id
= target_extension_id
;
36 info
.source_id
= source_extension_id
;
37 info
.source_url
= source_url
;
38 process_
->Send(new ExtensionMsg_DispatchOnConnect(
39 routing_id_
, dest_port_id
, channel_name
, source_tab
, info
,
43 void ExtensionMessagePort::DispatchOnDisconnect(
45 const std::string
& error_message
) {
46 process_
->Send(new ExtensionMsg_DispatchOnDisconnect(
47 routing_id_
, source_port_id
, error_message
));
50 void ExtensionMessagePort::DispatchOnMessage(const Message
& message
,
52 process_
->Send(new ExtensionMsg_DeliverMessage(
53 routing_id_
, target_port_id
, message
));
56 void ExtensionMessagePort::IncrementLazyKeepaliveCount() {
58 Profile::FromBrowserContext(process_
->GetBrowserContext());
59 extensions::ProcessManager
* pm
=
60 ExtensionSystem::Get(profile
)->process_manager();
61 ExtensionHost
* host
= pm
->GetBackgroundHostForExtension(extension_id_
);
62 if (host
&& BackgroundInfo::HasLazyBackgroundPage(host
->extension()))
63 pm
->IncrementLazyKeepaliveCount(host
->extension());
65 // Keep track of the background host, so when we decrement, we only do so if
66 // the host hasn't reloaded.
67 background_host_ptr_
= host
;
70 void ExtensionMessagePort::DecrementLazyKeepaliveCount() {
72 Profile::FromBrowserContext(process_
->GetBrowserContext());
73 extensions::ProcessManager
* pm
=
74 ExtensionSystem::Get(profile
)->process_manager();
75 ExtensionHost
* host
= pm
->GetBackgroundHostForExtension(extension_id_
);
76 if (host
&& host
== background_host_ptr_
)
77 pm
->DecrementLazyKeepaliveCount(host
->extension());
80 content::RenderProcessHost
* ExtensionMessagePort::GetRenderProcessHost() {
84 } // namespace extensions