OLD | NEW |
(Empty) | |
| 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/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/extensions/native_message_process.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/extensions/extension_messages.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/render_process_host.h" |
| 15 |
| 16 namespace extensions { |
| 17 |
| 18 ExtensionMessagePort* MessagePort::AsExtensionMessagePort() { |
| 19 DCHECK(!IsNative()); |
| 20 return static_cast<ExtensionMessagePort*>(this); |
| 21 } |
| 22 |
| 23 NativeMessagePort* MessagePort::AsNativeMessagePort() { |
| 24 DCHECK(IsNative()); |
| 25 return static_cast<NativeMessagePort*>(this); |
| 26 } |
| 27 |
| 28 ExtensionMessagePort::ExtensionMessagePort(content::RenderProcessHost* process, |
| 29 int routing_id, |
| 30 const std::string& extension_id) |
| 31 : process_(process), |
| 32 routing_id_(routing_id), |
| 33 extension_id_(extension_id), |
| 34 background_host_ptr_(NULL) { |
| 35 } |
| 36 |
| 37 void ExtensionMessagePort::DispatchOnConnect( |
| 38 int dest_port_id, |
| 39 const std::string& channel_name, |
| 40 const std::string& tab_json, |
| 41 const std::string& source_extension_id, |
| 42 const std::string& target_extension_id) { |
| 43 process_->Send(new ExtensionMsg_DispatchOnConnect( |
| 44 routing_id_, dest_port_id, channel_name, |
| 45 tab_json, source_extension_id, target_extension_id)); |
| 46 } |
| 47 |
| 48 void ExtensionMessagePort::DispatchOnDisconnect(int source_port_id, |
| 49 bool connection_error) { |
| 50 process_->Send(new ExtensionMsg_DispatchOnDisconnect( |
| 51 routing_id_, source_port_id, connection_error)); |
| 52 } |
| 53 |
| 54 void ExtensionMessagePort::DispatchOnMessage(const std::string& message, |
| 55 int target_port_id) { |
| 56 process_->Send(new ExtensionMsg_DeliverMessage( |
| 57 routing_id_, target_port_id, message)); |
| 58 } |
| 59 |
| 60 void ExtensionMessagePort::IncrementLazyKeepaliveCount() { |
| 61 Profile* profile = |
| 62 Profile::FromBrowserContext(process_->GetBrowserContext()); |
| 63 ExtensionProcessManager* pm = |
| 64 ExtensionSystem::Get(profile)->process_manager(); |
| 65 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id_); |
| 66 if (host && host->extension()->has_lazy_background_page()) |
| 67 pm->IncrementLazyKeepaliveCount(host->extension()); |
| 68 |
| 69 // Keep track of the background host, so when we decrement, we only do so if |
| 70 // the host hasn't reloaded. |
| 71 background_host_ptr_ = host; |
| 72 } |
| 73 |
| 74 void ExtensionMessagePort::DecrementLazyKeepaliveCount() { |
| 75 Profile* profile = |
| 76 Profile::FromBrowserContext(process_->GetBrowserContext()); |
| 77 ExtensionProcessManager* pm = |
| 78 ExtensionSystem::Get(profile)->process_manager(); |
| 79 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id_); |
| 80 if (host && host == background_host_ptr_) |
| 81 pm->DecrementLazyKeepaliveCount(host->extension()); |
| 82 } |
| 83 |
| 84 NativeMessagePort::NativeMessagePort(NativeMessageProcess* native_process) |
| 85 : native_process_(native_process) { |
| 86 } |
| 87 |
| 88 NativeMessagePort::~NativeMessagePort() { |
| 89 content::BrowserThread::DeleteSoon(content::BrowserThread::FILE, |
| 90 FROM_HERE, |
| 91 native_process_); |
| 92 } |
| 93 |
| 94 void NativeMessagePort::DispatchOnMessage(const std::string& message, |
| 95 int target_port_id) { |
| 96 content::BrowserThread::PostTask( |
| 97 content::BrowserThread::FILE, |
| 98 FROM_HERE, |
| 99 base::Bind(&NativeMessageProcess::Send, |
| 100 base::Unretained(native_process_), |
| 101 message)); |
| 102 } |
| 103 |
| 104 } // namespace extensions |
OLD | NEW |