Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / messaging / extension_message_port.cc
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/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"
13
14 namespace extensions {
15
16 ExtensionMessagePort::ExtensionMessagePort(content::RenderProcessHost* process,
17                                            int routing_id,
18                                            const std::string& extension_id)
19      : process_(process),
20        routing_id_(routing_id),
21        extension_id_(extension_id),
22        background_host_ptr_(NULL) {
23 }
24
25 void ExtensionMessagePort::DispatchOnConnect(
26     int dest_port_id,
27     const std::string& channel_name,
28     const base::DictionaryValue& source_tab,
29     const std::string& source_extension_id,
30     const std::string& target_extension_id,
31     const GURL& source_url,
32     const std::string& tls_channel_id) {
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       tls_channel_id));
40 }
41
42 void ExtensionMessagePort::DispatchOnDisconnect(
43     int source_port_id,
44     const std::string& error_message) {
45   process_->Send(new ExtensionMsg_DispatchOnDisconnect(
46       routing_id_, source_port_id, error_message));
47 }
48
49 void ExtensionMessagePort::DispatchOnMessage(const Message& message,
50                                              int target_port_id) {
51   process_->Send(new ExtensionMsg_DeliverMessage(
52       routing_id_, target_port_id, message));
53 }
54
55 void ExtensionMessagePort::IncrementLazyKeepaliveCount() {
56   Profile* profile =
57       Profile::FromBrowserContext(process_->GetBrowserContext());
58   extensions::ProcessManager* pm = ProcessManager::Get(profile);
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
68 void ExtensionMessagePort::DecrementLazyKeepaliveCount() {
69   Profile* profile =
70       Profile::FromBrowserContext(process_->GetBrowserContext());
71   extensions::ProcessManager* pm = ProcessManager::Get(profile);
72   ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id_);
73   if (host && host == background_host_ptr_)
74     pm->DecrementLazyKeepaliveCount(host->extension());
75 }
76
77 content::RenderProcessHost* ExtensionMessagePort::GetRenderProcessHost() {
78   return process_;
79 }
80
81 }  // namespace extensions