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/process_manager.h"
11 #include "extensions/common/extension_messages.h"
12 #include "extensions/common/manifest_handlers/background_info.h"
14 namespace extensions
{
16 ExtensionMessagePort::ExtensionMessagePort(content::RenderProcessHost
* process
,
18 const std::string
& extension_id
)
20 routing_id_(routing_id
),
21 extension_id_(extension_id
),
22 background_host_ptr_(NULL
) {
25 void ExtensionMessagePort::DispatchOnConnect(
27 const std::string
& channel_name
,
28 scoped_ptr
<base::DictionaryValue
> source_tab
,
32 int guest_render_frame_routing_id
,
33 const std::string
& source_extension_id
,
34 const std::string
& target_extension_id
,
35 const GURL
& source_url
,
36 const std::string
& tls_channel_id
) {
37 ExtensionMsg_TabConnectionInfo source
;
39 source
.tab
.Swap(source_tab
.get());
40 source
.frame_id
= source_frame_id
;
42 ExtensionMsg_ExternalConnectionInfo info
;
43 info
.target_id
= target_extension_id
;
44 info
.source_id
= source_extension_id
;
45 info
.source_url
= source_url
;
46 info
.target_frame_id
= target_frame_id
;
47 info
.guest_process_id
= guest_process_id
;
48 info
.guest_render_frame_routing_id
= guest_render_frame_routing_id
;
50 process_
->Send(new ExtensionMsg_DispatchOnConnect(
51 routing_id_
, dest_port_id
, channel_name
, source
, info
, tls_channel_id
));
54 void ExtensionMessagePort::DispatchOnDisconnect(
56 const std::string
& error_message
) {
57 process_
->Send(new ExtensionMsg_DispatchOnDisconnect(
58 routing_id_
, source_port_id
, error_message
));
61 void ExtensionMessagePort::DispatchOnMessage(const Message
& message
,
63 process_
->Send(new ExtensionMsg_DeliverMessage(
64 routing_id_
, target_port_id
, message
));
67 void ExtensionMessagePort::IncrementLazyKeepaliveCount() {
69 Profile::FromBrowserContext(process_
->GetBrowserContext());
70 extensions::ProcessManager
* pm
= ProcessManager::Get(profile
);
71 ExtensionHost
* host
= pm
->GetBackgroundHostForExtension(extension_id_
);
72 if (host
&& BackgroundInfo::HasLazyBackgroundPage(host
->extension()))
73 pm
->IncrementLazyKeepaliveCount(host
->extension());
75 // Keep track of the background host, so when we decrement, we only do so if
76 // the host hasn't reloaded.
77 background_host_ptr_
= host
;
80 void ExtensionMessagePort::DecrementLazyKeepaliveCount() {
82 Profile::FromBrowserContext(process_
->GetBrowserContext());
83 extensions::ProcessManager
* pm
= ProcessManager::Get(profile
);
84 ExtensionHost
* host
= pm
->GetBackgroundHostForExtension(extension_id_
);
85 if (host
&& host
== background_host_ptr_
)
86 pm
->DecrementLazyKeepaliveCount(host
->extension());
89 content::RenderProcessHost
* ExtensionMessagePort::GetRenderProcessHost() {
93 } // namespace extensions