diff options
28 files changed, 153 insertions, 1294 deletions
diff --git a/chrome/browser/extensions/api/messaging/extension_message_port.cc b/chrome/browser/extensions/api/messaging/extension_message_port.cc deleted file mode 100644 index b0c4afc..0000000 --- a/chrome/browser/extensions/api/messaging/extension_message_port.cc +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/extensions/api/messaging/extension_message_port.h" - -#include "chrome/browser/extensions/extension_host.h" -#include "chrome/browser/extensions/extension_process_manager.h" -#include "chrome/browser/extensions/extension_system.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/common/extensions/extension_messages.h" -#include "content/public/browser/render_process_host.h" - -namespace extensions { - -ExtensionMessagePort::ExtensionMessagePort(content::RenderProcessHost* process, - int routing_id, - const std::string& extension_id) - : process_(process), - routing_id_(routing_id), - extension_id_(extension_id), - background_host_ptr_(NULL) { -} - -void ExtensionMessagePort::DispatchOnConnect( - int dest_port_id, - const std::string& channel_name, - const std::string& tab_json, - const std::string& source_extension_id, - const std::string& target_extension_id) { - process_->Send(new ExtensionMsg_DispatchOnConnect( - routing_id_, dest_port_id, channel_name, - tab_json, source_extension_id, target_extension_id)); -} - -void ExtensionMessagePort::DispatchOnDisconnect(int source_port_id, - bool connection_error) { - process_->Send(new ExtensionMsg_DispatchOnDisconnect( - routing_id_, source_port_id, connection_error)); -} - -void ExtensionMessagePort::DispatchOnMessage(const std::string& message, - int target_port_id) { - process_->Send(new ExtensionMsg_DeliverMessage( - routing_id_, target_port_id, message)); -} - -void ExtensionMessagePort::IncrementLazyKeepaliveCount() { - Profile* profile = - Profile::FromBrowserContext(process_->GetBrowserContext()); - ExtensionProcessManager* pm = - ExtensionSystem::Get(profile)->process_manager(); - ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id_); - if (host && host->extension()->has_lazy_background_page()) - pm->IncrementLazyKeepaliveCount(host->extension()); - - // Keep track of the background host, so when we decrement, we only do so if - // the host hasn't reloaded. - background_host_ptr_ = host; -} - -void ExtensionMessagePort::DecrementLazyKeepaliveCount() { - Profile* profile = - Profile::FromBrowserContext(process_->GetBrowserContext()); - ExtensionProcessManager* pm = - ExtensionSystem::Get(profile)->process_manager(); - ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id_); - if (host && host == background_host_ptr_) - pm->DecrementLazyKeepaliveCount(host->extension()); -} - -content::RenderProcessHost* ExtensionMessagePort::GetRenderProcessHost() { - return process_; -} - -} // namespace extensions diff --git a/chrome/browser/extensions/api/messaging/extension_message_port.h b/chrome/browser/extensions/api/messaging/extension_message_port.h deleted file mode 100644 index 69b5c74..0000000 --- a/chrome/browser/extensions/api/messaging/extension_message_port.h +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_EXTENSIONS_API_MESSAGING_EXTENSION_MESSAGE_PORT_H_ -#define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_EXTENSION_MESSAGE_PORT_H_ - -#include "chrome/browser/extensions/api/messaging/message_service.h" - -namespace content { -class RenderProcessHost; -} // namespace content - -namespace extensions { - -// A port that manages communication with an extension. -class ExtensionMessagePort : public MessageService::MessagePort { - public: - ExtensionMessagePort(content::RenderProcessHost* process, - int routing_id, - const std::string& extension_id); - virtual void DispatchOnConnect( - int dest_port_id, - const std::string& channel_name, - const std::string& tab_json, - const std::string& source_extension_id, - const std::string& target_extension_id) OVERRIDE; - virtual void DispatchOnDisconnect(int source_port_id, - bool connection_error) OVERRIDE; - virtual void DispatchOnMessage(const std::string& message, - int target_port_id) OVERRIDE; - virtual void IncrementLazyKeepaliveCount() OVERRIDE; - virtual void DecrementLazyKeepaliveCount() OVERRIDE; - virtual content::RenderProcessHost* GetRenderProcessHost() OVERRIDE; - - private: - content::RenderProcessHost* process_; - int routing_id_; - std::string extension_id_; - void* background_host_ptr_; // used in IncrementLazyKeepaliveCount -}; - -} // namespace extensions - -#endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_EXTENSION_MESSAGE_PORT_H_ diff --git a/chrome/browser/extensions/api/messaging/message_service.cc b/chrome/browser/extensions/api/messaging/message_service.cc index e45fb80..8b5bb48 100644 --- a/chrome/browser/extensions/api/messaging/message_service.cc +++ b/chrome/browser/extensions/api/messaging/message_service.cc @@ -10,8 +10,6 @@ #include "base/json/json_writer.h" #include "base/stl_util.h" #include "base/values.h" -#include "chrome/browser/extensions/api/messaging/extension_message_port.h" -#include "chrome/browser/extensions/api/messaging/native_message_port.h" #include "chrome/browser/extensions/extension_host.h" #include "chrome/browser/extensions/extension_process_manager.h" #include "chrome/browser/extensions/extension_service.h" @@ -26,7 +24,6 @@ #include "chrome/common/extensions/extension.h" #include "chrome/common/extensions/extension_messages.h" #include "chrome/common/view_type.h" -#include "content/public/browser/browser_thread.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/render_view_host.h" @@ -50,24 +47,42 @@ using content::WebContents; namespace extensions { +struct MessageService::MessagePort { + content::RenderProcessHost* process; + int routing_id; + std::string extension_id; + void* background_host_ptr; // used in IncrementLazyKeepaliveCount + + MessagePort() + : process(NULL), + routing_id(MSG_ROUTING_CONTROL), + background_host_ptr(NULL) {} + MessagePort(content::RenderProcessHost* process, + int routing_id, + const std::string& extension_id) + : process(process), + routing_id(routing_id), + extension_id(extension_id), + background_host_ptr(NULL) {} +}; + struct MessageService::MessageChannel { - scoped_ptr<MessagePort> opener; - scoped_ptr<MessagePort> receiver; + MessageService::MessagePort opener; + MessageService::MessagePort receiver; }; struct MessageService::OpenChannelParams { content::RenderProcessHost* source; std::string tab_json; - scoped_ptr<MessagePort> receiver; + MessagePort receiver; int receiver_port_id; std::string source_extension_id; std::string target_extension_id; std::string channel_name; - // Takes ownership of receiver. OpenChannelParams(content::RenderProcessHost* source, const std::string& tab_json, - MessagePort* receiver, + const MessagePort& receiver, int receiver_port_id, const std::string& source_extension_id, const std::string& target_extension_id, @@ -85,6 +100,31 @@ namespace { static base::StaticAtomicSequenceNumber g_next_channel_id; +static void DispatchOnConnect(const MessageService::MessagePort& port, + int dest_port_id, + const std::string& channel_name, + const std::string& tab_json, + const std::string& source_extension_id, + const std::string& target_extension_id) { + port.process->Send(new ExtensionMsg_DispatchOnConnect( + port.routing_id, dest_port_id, channel_name, + tab_json, source_extension_id, target_extension_id)); +} + +static void DispatchOnDisconnect(const MessageService::MessagePort& port, + int source_port_id, + bool connection_error) { + port.process->Send(new ExtensionMsg_DispatchOnDisconnect( + port.routing_id, source_port_id, connection_error)); +} + +static void DispatchOnMessage(const MessageService::MessagePort& port, + const std::string& message, + int target_port_id) { + port.process->Send(new ExtensionMsg_DeliverMessage( + port.routing_id, target_port_id, message)); +} + static content::RenderProcessHost* GetExtensionProcess( Profile* profile, const std::string& extension_id) { SiteInstance* site_instance = @@ -97,13 +137,32 @@ static content::RenderProcessHost* GetExtensionProcess( return site_instance->GetProcess(); } -} // namespace +static void IncrementLazyKeepaliveCount(MessageService::MessagePort* port) { + Profile* profile = + Profile::FromBrowserContext(port->process->GetBrowserContext()); + ExtensionProcessManager* pm = + ExtensionSystem::Get(profile)->process_manager(); + ExtensionHost* host = pm->GetBackgroundHostForExtension(port->extension_id); + if (host && host->extension()->has_lazy_background_page()) + pm->IncrementLazyKeepaliveCount(host->extension()); + + // Keep track of the background host, so when we decrement, we only do so if + // the host hasn't reloaded. + port->background_host_ptr = host; +} -content::RenderProcessHost* - MessageService::MessagePort::GetRenderProcessHost() { - return NULL; +static void DecrementLazyKeepaliveCount(MessageService::MessagePort* port) { + Profile* profile = + Profile::FromBrowserContext(port->process->GetBrowserContext()); + ExtensionProcessManager* pm = + ExtensionSystem::Get(profile)->process_manager(); + ExtensionHost* host = pm->GetBackgroundHostForExtension(port->extension_id); + if (host && host == port->background_host_ptr) + pm->DecrementLazyKeepaliveCount(host->extension()); } +} // namespace + // static void MessageService::AllocatePortIdPair(int* port1, int* port2) { int channel_id = g_next_channel_id.GetNext(); @@ -125,8 +184,7 @@ void MessageService::AllocatePortIdPair(int* port1, int* port2) { MessageService::MessageService( LazyBackgroundTaskQueue* queue) - : lazy_background_task_queue_(queue), - weak_factory_(this) { + : lazy_background_task_queue_(queue) { registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, content::NotificationService::AllBrowserContextsAndSources()); registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, @@ -152,9 +210,9 @@ void MessageService::OpenChannelToExtension( // Note: we use the source's profile here. If the source is an incognito // process, we will use the incognito EPM to find the right extension process, // which depends on whether the extension uses spanning or split mode. - MessagePort* receiver = new ExtensionMessagePort( - GetExtensionProcess(profile, target_extension_id), MSG_ROUTING_CONTROL, - target_extension_id); + MessagePort receiver(GetExtensionProcess(profile, target_extension_id), + MSG_ROUTING_CONTROL, + target_extension_id); WebContents* source_contents = tab_util::GetWebContentsByID( source_process_id, source_routing_id); @@ -166,90 +224,17 @@ void MessageService::OpenChannelToExtension( base::JSONWriter::Write(tab_value.get(), &tab_json); } - OpenChannelParams* params = new OpenChannelParams(source, tab_json, receiver, - receiver_port_id, - source_extension_id, - target_extension_id, - channel_name); + OpenChannelParams params(source, tab_json, receiver, receiver_port_id, + source_extension_id, target_extension_id, + channel_name); // The target might be a lazy background page. In that case, we have to check // if it is loaded and ready, and if not, queue up the task and load the // page. - if (MaybeAddPendingOpenChannelTask(profile, params)) { + if (MaybeAddPendingOpenChannelTask(profile, params)) return; - } - OpenChannelImpl(scoped_ptr<OpenChannelParams>(params)); -} - -void MessageService::OpenChannelToNativeApp( - int source_process_id, - int source_routing_id, - int receiver_port_id, - const std::string& source_extension_id, - const std::string& native_app_name, - const std::string& channel_name, - const std::string& connect_message) { - content::RenderProcessHost* source = - content::RenderProcessHost::FromID(source_process_id); - if (!source) - return; - - WebContents* source_contents = tab_util::GetWebContentsByID( - source_process_id, source_routing_id); - - // Include info about the opener's tab (if it was a tab). - std::string tab_json = "null"; - if (source_contents) { - scoped_ptr<DictionaryValue> tab_value(ExtensionTabUtil::CreateTabValue( - source_contents, ExtensionTabUtil::INCLUDE_PRIVACY_SENSITIVE_FIELDS)); - base::JSONWriter::Write(tab_value.get(), &tab_json); - } - - scoped_ptr<MessageChannel> channel(new MessageChannel()); - channel->opener.reset(new ExtensionMessagePort(source, MSG_ROUTING_CONTROL, - source_extension_id)); - - NativeMessageProcessHost::MessageType type = - channel_name == "chrome.extension.sendNativeMessage" ? - NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST : - NativeMessageProcessHost::TYPE_CONNECT; - - content::BrowserThread::PostTask( - content::BrowserThread::FILE, - FROM_HERE, - base::Bind(&NativeMessageProcessHost::Create, - base::WeakPtr<NativeMessageProcessHost::Client>( - weak_factory_.GetWeakPtr()), - native_app_name, connect_message, receiver_port_id, - type, - base::Bind(&MessageService::FinalizeOpenChannelToNativeApp, - weak_factory_.GetWeakPtr(), - receiver_port_id, - channel_name, - base::Passed(&channel), - tab_json))); -} - -void MessageService::FinalizeOpenChannelToNativeApp( - int receiver_port_id, - const std::string& channel_name, - scoped_ptr<MessageChannel> channel, - const std::string& tab_json, - NativeMessageProcessHost::ScopedHost native_process) { - DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); - - // Abandon the channel - if (!native_process.get()) { - LOG(ERROR) << "Failed to create native process."; - return; - } - channel->receiver.reset(new NativeMessagePort(native_process.release())); - - // Keep the opener alive until the channel is closed. - channel->opener->IncrementLazyKeepaliveCount(); - - AddChannel(channel.release(), receiver_port_id); + OpenChannelImpl(params); } void MessageService::OpenChannelToTab( @@ -263,20 +248,20 @@ void MessageService::OpenChannelToTab( Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); TabContents* contents = NULL; - scoped_ptr<MessagePort> receiver; + MessagePort receiver; if (ExtensionTabUtil::GetTabById(tab_id, profile, true, NULL, NULL, &contents, NULL)) { - receiver.reset(new ExtensionMessagePort( - contents->web_contents()->GetRenderProcessHost(), - contents->web_contents()->GetRenderViewHost()->GetRoutingID(), - extension_id)); + receiver.process = contents->web_contents()->GetRenderProcessHost(); + receiver.routing_id = + contents->web_contents()->GetRenderViewHost()->GetRoutingID(); + receiver.extension_id = extension_id; } if (contents && contents->web_contents()->GetController().NeedsReload()) { // The tab isn't loaded yet. Don't attempt to connect. Treat this as a // disconnect. - ExtensionMessagePort port(source, MSG_ROUTING_CONTROL, extension_id); - port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(receiver_port_id), true); + DispatchOnDisconnect(MessagePort(source, MSG_ROUTING_CONTROL, extension_id), + GET_OPPOSITE_PORT_ID(receiver_port_id), true); return; } @@ -291,63 +276,52 @@ void MessageService::OpenChannelToTab( base::JSONWriter::Write(tab_value.get(), &tab_json); } - scoped_ptr<OpenChannelParams> params(new OpenChannelParams(source, tab_json, - receiver.release(), - receiver_port_id, - extension_id, - extension_id, - channel_name)); - OpenChannelImpl(params.Pass()); + OpenChannelParams params(source, tab_json, receiver, receiver_port_id, + extension_id, extension_id, channel_name); + OpenChannelImpl(params); } -bool MessageService::OpenChannelImpl(scoped_ptr<OpenChannelParams> params) { - if (!params->source) +bool MessageService::OpenChannelImpl(const OpenChannelParams& params) { + if (!params.source) return false; // Closed while in flight. - if (!params->receiver.get() || !params->receiver->GetRenderProcessHost()) { + if (!params.receiver.process) { // Treat it as a disconnect. - ExtensionMessagePort port(params->source, MSG_ROUTING_CONTROL, ""); - port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(params->receiver_port_id), - true); + DispatchOnDisconnect(MessagePort(params.source, MSG_ROUTING_CONTROL, ""), + GET_OPPOSITE_PORT_ID(params.receiver_port_id), true); return false; } // Add extra paranoid CHECKs, since we have crash reports of this being NULL. // http://code.google.com/p/chromium/issues/detail?id=19067 - CHECK(params->receiver->GetRenderProcessHost()); + CHECK(params.receiver.process); MessageChannel* channel(new MessageChannel); - channel->opener.reset(new ExtensionMessagePort(params->source, - MSG_ROUTING_CONTROL, - params->source_extension_id)); - channel->receiver.reset(params->receiver.release()); + channel->opener = MessagePort(params.source, MSG_ROUTING_CONTROL, + params.source_extension_id); + channel->receiver = params.receiver; - CHECK(channel->receiver->GetRenderProcessHost()); + CHECK(params.receiver.process); - AddChannel(channel, params->receiver_port_id); + int channel_id = GET_CHANNEL_ID(params.receiver_port_id); + CHECK(channels_.find(channel_id) == channels_.end()); + channels_[channel_id] = channel; + pending_channels_.erase(channel_id); - CHECK(channel->receiver->GetRenderProcessHost()); + CHECK(params.receiver.process); // Send the connect event to the receiver. Give it the opener's port ID (the // opener has the opposite port ID). - channel->receiver->DispatchOnConnect(params->receiver_port_id, - params->channel_name, params->tab_json, - params->source_extension_id, - params->target_extension_id); + DispatchOnConnect(params.receiver, params.receiver_port_id, + params.channel_name, params.tab_json, + params.source_extension_id, params.target_extension_id); // Keep both ends of the channel alive until the channel is closed. - channel->opener->IncrementLazyKeepaliveCount(); - channel->receiver->IncrementLazyKeepaliveCount(); + IncrementLazyKeepaliveCount(&channel->opener); + IncrementLazyKeepaliveCount(&channel->receiver); return true; } -void MessageService::AddChannel(MessageChannel* channel, int receiver_port_id) { - int channel_id = GET_CHANNEL_ID(receiver_port_id); - CHECK(channels_.find(channel_id) == channels_.end()); - channels_[channel_id] = channel; - pending_channels_.erase(channel_id); -} - void MessageService::CloseChannel(int port_id, bool connection_error) { // Note: The channel might be gone already, if the other side closed first. int channel_id = GET_CHANNEL_ID(port_id); @@ -358,7 +332,7 @@ void MessageService::CloseChannel(int port_id, bool connection_error) { lazy_background_task_queue_->AddPendingTask( pending->second.first, pending->second.second, base::Bind(&MessageService::PendingCloseChannel, - weak_factory_.GetWeakPtr(), port_id, connection_error)); + base::Unretained(this), port_id, connection_error)); } return; } @@ -372,21 +346,21 @@ void MessageService::CloseChannelImpl( // Notify the other side. if (notify_other_port) { - MessagePort* port = IS_OPENER_PORT_ID(closing_port_id) ? - channel->receiver.get() : channel->opener.get(); - port->DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(closing_port_id), - connection_error); + const MessagePort& port = IS_OPENER_PORT_ID(closing_port_id) ? + channel->receiver : channel->opener; + DispatchOnDisconnect(port, GET_OPPOSITE_PORT_ID(closing_port_id), + connection_error); } - // Balance the IncrementLazyKeepaliveCount() in OpenChannelImpl. - channel->opener->DecrementLazyKeepaliveCount(); - channel->receiver->DecrementLazyKeepaliveCount(); + // Balance the addrefs in OpenChannelImpl. + DecrementLazyKeepaliveCount(&channel->opener); + DecrementLazyKeepaliveCount(&channel->receiver); delete channel_iter->second; channels_.erase(channel_iter); } -void MessageService::PostMessage( +void MessageService::PostMessageFromRenderer( int source_port_id, const std::string& message) { int channel_id = GET_CHANNEL_ID(source_port_id); MessageChannelMap::iterator iter = channels_.find(channel_id); @@ -398,22 +372,17 @@ void MessageService::PostMessage( lazy_background_task_queue_->AddPendingTask( pending->second.first, pending->second.second, base::Bind(&MessageService::PendingPostMessage, - weak_factory_.GetWeakPtr(), source_port_id, message)); + base::Unretained(this), source_port_id, message)); } return; } // Figure out which port the ID corresponds to. int dest_port_id = GET_OPPOSITE_PORT_ID(source_port_id); - MessagePort* port = IS_OPENER_PORT_ID(dest_port_id) ? - iter->second->opener.get() : iter->second->receiver.get(); + const MessagePort& port = IS_OPENER_PORT_ID(dest_port_id) ? + iter->second->opener : iter->second->receiver; - port->DispatchOnMessage(message, dest_port_id); -} - -void MessageService::PostMessageFromNativeProcess(int port_id, - const std::string& message) { - PostMessage(port_id, message); + DispatchOnMessage(port, message, dest_port_id); } void MessageService::Observe(int type, @@ -439,31 +408,28 @@ void MessageService::OnProcessClosed(content::RenderProcessHost* process) { for (MessageChannelMap::iterator it = channels_.begin(); it != channels_.end(); ) { MessageChannelMap::iterator current = it++; + // If both sides are the same renderer, and it is closing, there is no + // "other" port, so there's no need to notify it. + bool notify_other_port = + current->second->opener.process != current->second->receiver.process; - content::RenderProcessHost* opener_process = - current->second->opener->GetRenderProcessHost(); - content::RenderProcessHost* receiver_process = - current->second->receiver->GetRenderProcessHost(); - - bool notify_other_port = opener_process && - opener_process == receiver_process; - - if (opener_process == process) { + if (current->second->opener.process == process) { CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), - false, notify_other_port); - } else if (receiver_process == process) { + false, notify_other_port); + } else if (current->second->receiver.process == process) { CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first), - false, notify_other_port); + false, notify_other_port); } } } bool MessageService::MaybeAddPendingOpenChannelTask( Profile* profile, - OpenChannelParams* params) { + const OpenChannelParams& params) { ExtensionService* service = profile->GetExtensionService(); - const std::string& extension_id = params->target_extension_id; - const Extension* extension = service->extensions()->GetByID(extension_id); + const std::string& extension_id = params.target_extension_id; + const Extension* extension = service->extensions()->GetByID( + extension_id); if (extension && extension->has_lazy_background_page()) { // If the extension uses spanning incognito mode, make sure we're always // using the original profile since that is what the extension process @@ -472,13 +438,11 @@ bool MessageService::MaybeAddPendingOpenChannelTask( profile = profile->GetOriginalProfile(); if (lazy_background_task_queue_->ShouldEnqueueTask(profile, extension)) { - pending_channels_[GET_CHANNEL_ID(params->receiver_port_id)] = - PendingChannel(profile, extension_id); - scoped_ptr<OpenChannelParams> scoped_params(params); lazy_background_task_queue_->AddPendingTask(profile, extension_id, base::Bind(&MessageService::PendingOpenChannel, - weak_factory_.GetWeakPtr(), base::Passed(&scoped_params), - params->source->GetID())); + base::Unretained(this), params, params.source->GetID())); + pending_channels_[GET_CHANNEL_ID(params.receiver_port_id)] = + PendingChannel(profile, extension_id); return true; } } @@ -486,23 +450,22 @@ bool MessageService::MaybeAddPendingOpenChannelTask( return false; } -void MessageService::PendingOpenChannel(scoped_ptr<OpenChannelParams> params, +void MessageService::PendingOpenChannel(const OpenChannelParams& params_in, int source_process_id, ExtensionHost* host) { if (!host) return; // TODO(mpcomplete): notify source of disconnect? // Re-lookup the source process since it may no longer be valid. - content::RenderProcessHost* source = - content::RenderProcessHost::FromID(source_process_id); - if (!source) + OpenChannelParams params = params_in; + params.source = content::RenderProcessHost::FromID(source_process_id); + if (!params.source) return; - params->source = source; - params->receiver.reset(new ExtensionMessagePort(host->render_process_host(), - MSG_ROUTING_CONTROL, - params->target_extension_id)); - OpenChannelImpl(params.Pass()); + params.receiver = MessagePort(host->render_process_host(), + MSG_ROUTING_CONTROL, + params.target_extension_id); + OpenChannelImpl(params); } } // namespace extensions diff --git a/chrome/browser/extensions/api/messaging/native_message_port.cc b/chrome/browser/extensions/api/messaging/native_message_port.cc deleted file mode 100644 index b77f04b..0000000 --- a/chrome/browser/extensions/api/messaging/native_message_port.cc +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/extensions/api/messaging/native_message_port.h" - -#include "chrome/browser/extensions/api/messaging/native_message_process_host.h" -#include "content/public/browser/browser_thread.h" - -namespace extensions { - -NativeMessagePort::NativeMessagePort(NativeMessageProcessHost* native_process) - : native_process_(native_process) { -} - -NativeMessagePort::~NativeMessagePort() { - content::BrowserThread::DeleteSoon(content::BrowserThread::FILE, - FROM_HERE, - native_process_); -} - -void NativeMessagePort::DispatchOnMessage(const std::string& message, - int target_port_id) { - content::BrowserThread::PostTask( - content::BrowserThread::FILE, - FROM_HERE, - base::Bind(&NativeMessageProcessHost::Send, - base::Unretained(native_process_), - message)); -} - -} // namespace extensions diff --git a/chrome/browser/extensions/api/messaging/native_message_port.h b/chrome/browser/extensions/api/messaging/native_message_port.h deleted file mode 100644 index 6afce8a..0000000 --- a/chrome/browser/extensions/api/messaging/native_message_port.h +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PORT_H_ -#define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PORT_H_ - -#include "chrome/browser/extensions/api/messaging/message_service.h" - -namespace extensions { -class NativeMessageProcessHost; - -// A port that manages communication with a native application. -class NativeMessagePort : public MessageService::MessagePort { - public: - // Takes ownership of |native_process|. - explicit NativeMessagePort(NativeMessageProcessHost* native_process); - virtual ~NativeMessagePort(); - virtual void DispatchOnMessage(const std::string& message, - int target_port_id) OVERRIDE; - - private: - NativeMessageProcessHost* native_process_; -}; - -} // namespace extensions - -#endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PORT_H_ diff --git a/chrome/browser/extensions/api/messaging/native_message_process_host.cc b/chrome/browser/extensions/api/messaging/native_message_process_host.cc deleted file mode 100644 index d91c696..0000000 --- a/chrome/browser/extensions/api/messaging/native_message_process_host.cc +++ /dev/null @@ -1,223 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/extensions/api/messaging/native_message_process_host.h" - -#include "base/file_path.h" -#include "base/logging.h" -#include "base/path_service.h" -#include "base/pickle.h" -#include "base/process_util.h" -#include "base/values.h" -#include "chrome/browser/extensions/api/messaging/native_process_launcher.h" -#include "chrome/common/chrome_paths.h" -#include "chrome/common/chrome_version_info.h" -#include "chrome/common/extensions/features/feature.h" -#include "content/public/common/result_codes.h" - -namespace { - -const int kExitTimeoutMS = 5000; -const uint32 kMaxMessageDataLength = 10 * 1024 * 1024; -const char kNativeHostsDirectoryName[] = "Native Hosts"; - -} // namespace - -namespace extensions { - -NativeMessageProcessHost::NativeMessageProcessHost( - base::WeakPtr<Client> weak_client_ui, - int destination_port, - base::ProcessHandle native_process_handle, - FileHandle read_file, - FileHandle write_file, - bool is_send_message) - : weak_client_ui_(weak_client_ui), - destination_port_(destination_port), - native_process_handle_(native_process_handle), - read_file_(read_file), - write_file_(write_file), - scoped_read_file_(&read_file_), - scoped_write_file_(&write_file_), - is_send_message_(is_send_message) { - DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); - InitIO(); -} - -NativeMessageProcessHost::~NativeMessageProcessHost() { - DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); - // Give the process some time to shutdown, then try and kill it. - content::BrowserThread::PostDelayedTask( - content::BrowserThread::FILE, - FROM_HERE, - base::Bind(base::IgnoreResult(&base::KillProcess), - native_process_handle_, - content::RESULT_CODE_NORMAL_EXIT, - false /* don't wait for exit */), - base::TimeDelta::FromMilliseconds(kExitTimeoutMS)); -} - -// static -void NativeMessageProcessHost::Create(base::WeakPtr<Client> weak_client_ui, - const std::string& native_app_name, - const std::string& connection_message, - int destination_port, - MessageType type, - CreateCallback callback) { - NativeProcessLauncher launcher; - CreateWithLauncher(weak_client_ui, native_app_name, connection_message, - destination_port, type, callback, launcher); -} - -// static -void NativeMessageProcessHost::CreateWithLauncher( - base::WeakPtr<Client> weak_client_ui, - const std::string& native_app_name, - const std::string& connection_message, - int destination_port, - MessageType type, - CreateCallback callback, - const NativeProcessLauncher& launcher) { - DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); - DCHECK(type == TYPE_SEND_MESSAGE_REQUEST || type == TYPE_CONNECT); - - ScopedHost process; - if (Feature::GetCurrentChannel() > chrome::VersionInfo::CHANNEL_DEV) { - content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, - base::Bind(callback, - base::Passed(&process))); - return; - } - - FilePath native_host_program; - FilePath native_host_registry; - CHECK(PathService::Get(chrome::DIR_USER_DATA, &native_host_registry)); - native_host_registry = - native_host_registry.AppendASCII(kNativeHostsDirectoryName); - native_host_program = native_host_registry.AppendASCII(native_app_name); - - // Make sure that the client is not trying to invoke something outside of the - // proper directory. Eg. '../../dangerous_something.exe'. - if (!file_util::ContainsPath(native_host_registry, native_host_program)) { - LOG(ERROR) << "Could not find native host: " << native_app_name; - content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, - base::Bind(callback, - base::Passed(&process))); - return; - } - - FileHandle read_handle; - FileHandle write_handle; - base::ProcessHandle native_process_handle; - - if (!launcher.LaunchNativeProcess(native_host_program, - &native_process_handle, - &read_handle, - &write_handle)) { - content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, - base::Bind(callback, - base::Passed(&process))); - return; - } - - process.reset(new NativeMessageProcessHost( - weak_client_ui, destination_port, native_process_handle, read_handle, - write_handle, type == TYPE_SEND_MESSAGE_REQUEST)); - - process->SendImpl(type, connection_message); - - content::BrowserThread::PostTask( - content::BrowserThread::UI, FROM_HERE, - base::Bind(callback, base::Passed(&process))); -} - -void NativeMessageProcessHost::SendImpl(MessageType type, - const std::string& json) { - DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); - - // Make sure that the process has not died. - if (base::GetTerminationStatus(native_process_handle_, NULL) != - base::TERMINATION_STATUS_STILL_RUNNING) { - // Notify the message service that the channel should close. - content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, - base::Bind(&Client::CloseChannel, weak_client_ui_, - destination_port_, true)); - } - - WriteMessage(type, json); -} - -bool NativeMessageProcessHost::WriteMessage(MessageType type, - const std::string& message) { - Pickle pickle; - - // Pickles will always pad bytes to 32-bit alignment, so just use a unit32. - pickle.WriteUInt32(type); - - // Pickles write the length of a string before it as a uint32. - pickle.WriteString(message); - - // Make sure that the pickle doesn't do any unexpected padding. - CHECK(8 + message.length() == pickle.payload_size()); - - if (!WriteData(write_file_, const_cast<const Pickle*>(&pickle)->payload(), - pickle.payload_size())) { - LOG(ERROR) << "Error writing message to the native client."; - return false; - } - - return true; -} - -bool NativeMessageProcessHost::ReadMessage(MessageType* type, - std::string* message) { - // Read the type (uint32) and length (uint32). - char message_meta_data[8]; - if (!ReadData(read_file_, message_meta_data, 8)) { - LOG(ERROR) << "Error reading the message type and length."; - return false; - } - - Pickle pickle; - pickle.WriteBytes(message_meta_data, 8); - PickleIterator pickle_it(pickle); - uint32 uint_type; - uint32 data_length; - if (!pickle_it.ReadUInt32(&uint_type) || - !pickle_it.ReadUInt32(&data_length)) { - LOG(ERROR) << "Error getting the message type and length from the pickle."; - return false; - } - - if (uint_type >= NUM_MESSAGE_TYPES) { - LOG(ERROR) << type << " is not a valid message type."; - return false; - } - - if ((is_send_message_ && (uint_type != TYPE_SEND_MESSAGE_RESPONSE)) || - (!is_send_message_ && (uint_type != TYPE_CONNECT_MESSAGE))) { - LOG(ERROR) << "Recieved a message of type " << uint_type << ". " - << "Expecting a message of type " - << (is_send_message_ ? TYPE_SEND_MESSAGE_RESPONSE : - TYPE_CONNECT_MESSAGE); - return false; - } - *type = static_cast<MessageType>(uint_type); - - if (data_length > kMaxMessageDataLength) { - LOG(ERROR) << data_length << " is too large for the length of a message. " - << "Max message size is " << kMaxMessageDataLength; - return false; - } - - message->resize(data_length, '\0'); - if (!ReadData(read_file_, &(*message)[0], data_length)) { - LOG(ERROR) << "Error reading the json data."; - return false; - } - - return true; -} - -} // namespace extensions diff --git a/chrome/browser/extensions/api/messaging/native_message_process_host.h b/chrome/browser/extensions/api/messaging/native_message_process_host.h deleted file mode 100644 index d629b08..0000000 --- a/chrome/browser/extensions/api/messaging/native_message_process_host.h +++ /dev/null @@ -1,178 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ -#define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ - -#include "base/file_util.h" -#include "base/memory/scoped_ptr.h" -#include "base/memory/weak_ptr.h" -#include "base/message_loop.h" -#include "base/process.h" -#include "content/public/browser/browser_thread.h" - -namespace extensions { -class NativeProcessLauncher; - -// Manages the native side of a connection between an extension and a native -// process. -// -// This class must only be created, called, and deleted on the FILE thread. -// Public methods typically accept callbacks which will be invoked on the UI -// thread. -class NativeMessageProcessHost -#if defined(OS_WIN) - : public MessageLoopForIO::IOHandler { -#else - : public MessageLoopForIO::Watcher { -#endif // defined(OS_WIN) - public: - class ScopedNativeProcessClose; - -#if defined(OS_WIN) - typedef HANDLE FileHandle; - typedef base::win::ScopedHandle ScopedFileHandle; -#else - typedef int FileHandle; - typedef file_util::ScopedFD ScopedFileHandle; -#endif // defined(OS_WIN) - - typedef scoped_ptr_malloc<NativeMessageProcessHost, ScopedNativeProcessClose> - ScopedHost; - - typedef base::Callback<void(ScopedHost host)> CreateCallback; - - // Append any new types to the end. Changing the ordering will break native - // apps. - enum MessageType { - TYPE_SEND_MESSAGE_REQUEST, // Used when an extension is sending a one-off - // message to a native app. - TYPE_SEND_MESSAGE_RESPONSE, // Used by a native app to respond to a one-off - // message. - TYPE_CONNECT, // Used when an extension wants to establish a persistent - // connection with a native app. - TYPE_CONNECT_MESSAGE, // Used for messages after a connection has already - // been established. - NUM_MESSAGE_TYPES // The number of types of messages. - }; - - // Interface for classes that which to recieve messages from the native - // process. - class Client { - public: - virtual ~Client() {} - // Called on the UI thread. - virtual void PostMessageFromNativeProcess(int port_id, - const std::string& message) = 0; - virtual void CloseChannel(int port_id, bool error) = 0; - }; - - // Desctruction functor that ensures a NativeMessageProcessHost is destroyed - // on the FILE thread. - class ScopedNativeProcessClose { - public: - inline void operator()(extensions::NativeMessageProcessHost* x) const { - content::BrowserThread::DeleteSoon(content::BrowserThread::FILE, - FROM_HERE, x); - } - }; - - - virtual ~NativeMessageProcessHost(); - - // |type| must be TYPE_CONNECT or TYPE_SEND_MESSAGE_REQUEST. |callback| will - // be called with an empty ScopedHost on error. - static void Create(base::WeakPtr<Client> weak_client_ui, - const std::string& native_app_name, - const std::string& connection_message, - int destination_port, - MessageType type, - CreateCallback callback); - - // Create a NativeMessageProcessHost using the specified launcher. This allows - // for easy testing. - static void CreateWithLauncher(base::WeakPtr<Client> weak_client_ui, - const std::string& native_app_name, - const std::string& connection_message, - int destination_port, - MessageType type, - CreateCallback callback, - const NativeProcessLauncher& launcher); - - // TYPE_SEND_MESSAGE_REQUEST will be sent via the connection message in - // NativeMessageProcessHost::Create, so only TYPE_CONNECT_MESSAGE is expected. - void Send(const std::string& json) { - SendImpl(TYPE_CONNECT_MESSAGE, json); - } - - // Try and read a single message from |read_file_|. This should only be called - // in unittests when you know there is data in the file. - void ReadNowForTesting(); - - private: - NativeMessageProcessHost(base::WeakPtr<Client> weak_client_ui, - int destination_port, - base::ProcessHandle native_process_handle, - FileHandle read_fd, - FileHandle write_fd, - bool is_send_message); - - // Initialize any IO watching that needs to occur between the native process. - void InitIO(); - - // Send a message to the native process with the specified type and payload. - void SendImpl(MessageType type, const std::string& json); - - // Write a message/data to the native process. - bool WriteMessage(MessageType type, const std::string& message); - bool WriteData(FileHandle file, const char* data, size_t bytes_to_write); - - // Read a message/data from the native process. - bool ReadMessage(MessageType* type, std::string* messgae); - bool ReadData(FileHandle file, char* data, size_t bytes_to_write); - -#if defined(OS_POSIX) - // MessageLoopForIO::Watcher - virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; - // We don't need to watch for writes. - virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {} - - MessageLoopForIO::FileDescriptorWatcher read_watcher_; -#endif // defined(OS_POSIX) - -#if defined(OS_WIN) - // MessageLoopForIO::IOHandler - virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, - DWORD bytes_transfered, - DWORD error) OVERRIDE; - - MessageLoopForIO::IOContext read_context_; - MessageLoopForIO::IOContext write_context_; -#endif // defined(OS_WIN) - - - // The Client messages will be posted to. Should only be accessed from the - // UI thread. - base::WeakPtr<Client> weak_client_ui_; - - // The id of the port on the other side of this connection. This is passed to - // |weak_client_ui_| when posting messages. - int destination_port_; - - base::ProcessHandle native_process_handle_; - - FileHandle read_file_; - FileHandle write_file_; - ScopedFileHandle scoped_read_file_; - ScopedFileHandle scoped_write_file_; - - // Only looking for one response. - bool is_send_message_; - - DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); -}; - -} // namespace extensions - -#endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H__ diff --git a/chrome/browser/extensions/api/messaging/native_message_process_host_posix.cc b/chrome/browser/extensions/api/messaging/native_message_process_host_posix.cc deleted file mode 100644 index 00f7e80..0000000 --- a/chrome/browser/extensions/api/messaging/native_message_process_host_posix.cc +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/extensions/api/messaging/native_message_process_host.h" - -#include <unistd.h> - -#include "base/logging.h" -#include "base/process_util.h" -#include "content/public/browser/browser_thread.h" - -namespace extensions { - -void NativeMessageProcessHost::ReadNowForTesting() { - OnFileCanReadWithoutBlocking(read_file_); -} - -void NativeMessageProcessHost::InitIO() { - // Always watch the read end. - MessageLoopForIO::current()->WatchFileDescriptor(read_file_, - true, /* persistent */ - MessageLoopForIO::WATCH_READ, - &read_watcher_, - this); -} - -void NativeMessageProcessHost::OnFileCanReadWithoutBlocking(int fd) { - DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); - - // Make sure that the fd given to us is the same one we started with. - CHECK_EQ(fd, read_file_); - - // If this is a sendMessage request, stop trying to read after the first - // message. - if (is_send_message_) - read_watcher_.StopWatchingFileDescriptor(); - - MessageType type; - std::string message; - if (!ReadMessage(&type, &message)) { - // A read failed, is the process dead? - if (base::GetTerminationStatus(native_process_handle_, NULL) != - base::TERMINATION_STATUS_STILL_RUNNING) { - read_watcher_.StopWatchingFileDescriptor(); - // Notify the message service that the channel should close. - content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, - base::Bind(&Client::CloseChannel, weak_client_ui_, - destination_port_, true)); - } - return; - } - - content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, - base::Bind(&Client::PostMessageFromNativeProcess, weak_client_ui_, - destination_port_, message)); -} - -bool NativeMessageProcessHost::WriteData(FileHandle file, - const char* data, - size_t bytes_to_write) { - return file_util::WriteFileDescriptor(file, data, bytes_to_write); -} - -bool NativeMessageProcessHost::ReadData(FileHandle file, - char* data, - size_t bytes_to_read) { - return file_util::ReadFromFD(file, data, bytes_to_read); -} - -} // namespace extensions diff --git a/chrome/browser/extensions/api/messaging/native_message_process_host_win.cc b/chrome/browser/extensions/api/messaging/native_message_process_host_win.cc deleted file mode 100644 index a9a6819..0000000 --- a/chrome/browser/extensions/api/messaging/native_message_process_host_win.cc +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/extensions/api/messaging/native_message_process_host.h" - -#include <windows.h> - -#include "base/logging.h" -#include "base/message_pump_win.h" -#include "base/platform_file.h" -#include "base/process_util.h" -#include "content/public/browser/browser_thread.h" - -namespace extensions { - -void NativeMessageProcessHost::OnIOCompleted( - MessageLoopForIO::IOContext* context, - DWORD bytes_transfered, - DWORD error) { - NOTREACHED(); -} - -void NativeMessageProcessHost::InitIO() { - NOTREACHED(); -} - -bool NativeMessageProcessHost::WriteData(FileHandle file, - const char* data, - size_t bytes_to_write) { - NOTREACHED(); - return false; -} - -bool NativeMessageProcessHost::ReadData(FileHandle file, - char* data, - size_t bytes_to_read) { - NOTREACHED(); - return false; -} - -} // namespace extensions diff --git a/chrome/browser/extensions/api/messaging/native_messaging_apitest_posix.cc b/chrome/browser/extensions/api/messaging/native_messaging_apitest_posix.cc deleted file mode 100644 index f5b2b3f..0000000 --- a/chrome/browser/extensions/api/messaging/native_messaging_apitest_posix.cc +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/file_path.h" -#include "base/path_service.h" -#include "chrome/browser/extensions/extension_apitest.h" -#include "chrome/common/chrome_paths.h" -#include "chrome/common/chrome_version_info.h" -#include "chrome/common/extensions/features/feature.h" - -IN_PROC_BROWSER_TEST_F(ExtensionApiTest, NativeMessageBasic) { - // Override the user data dir to point to our native app. - extensions::Feature::ScopedCurrentChannel - current_channel(chrome::VersionInfo::CHANNEL_DEV); - FilePath test_user_data_dir; - ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_user_data_dir)); - test_user_data_dir = test_user_data_dir.AppendASCII("native_messaging"); - ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, test_user_data_dir)); - ASSERT_TRUE(RunExtensionTest("native_messaging")) << message_; -} diff --git a/chrome/browser/extensions/api/messaging/native_process_launcher.h b/chrome/browser/extensions/api/messaging/native_process_launcher.h deleted file mode 100644 index 28cc10e..0000000 --- a/chrome/browser/extensions/api/messaging/native_process_launcher.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_PROCESS_LAUNCHER_H_ -#define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_PROCESS_LAUNCHER_H_ - -#include "base/process.h" -#include "chrome/browser/extensions/api/messaging/native_message_process_host.h" - -class FilePath; - -namespace extensions { - -class NativeProcessLauncher { - public: - NativeProcessLauncher() {} - virtual ~NativeProcessLauncher() {} - virtual bool LaunchNativeProcess( - const FilePath& path, - base::ProcessHandle* native_process_handle, - NativeMessageProcessHost::FileHandle* read_file, - NativeMessageProcessHost::FileHandle* write_file) const; - - private: - DISALLOW_COPY_AND_ASSIGN(NativeProcessLauncher); -}; - -} // namespace extensions - -#endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_PROCESS_LAUNCHER_H_ diff --git a/chrome/browser/extensions/api/messaging/native_process_launcher_posix.cc b/chrome/browser/extensions/api/messaging/native_process_launcher_posix.cc deleted file mode 100644 index e7cbce7..0000000 --- a/chrome/browser/extensions/api/messaging/native_process_launcher_posix.cc +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/extensions/api/messaging/native_process_launcher.h" - -#include "base/command_line.h" -#include "base/eintr_wrapper.h" -#include "base/file_util.h" -#include "base/logging.h" -#include "base/process_util.h" - -namespace extensions { - -bool NativeProcessLauncher::LaunchNativeProcess( - const FilePath& path, - base::ProcessHandle* native_process_handle, - NativeMessageProcessHost::FileHandle* read_file, - NativeMessageProcessHost::FileHandle* write_file) const { - base::FileHandleMappingVector fd_map; - - int read_pipe_fds[2] = {0}; - if (HANDLE_EINTR(pipe(read_pipe_fds)) != 0) { - LOG(ERROR) << "Bad read pipe"; - return false; - } - file_util::ScopedFD read_pipe_read_fd(&read_pipe_fds[0]); - file_util::ScopedFD read_pipe_write_fd(&read_pipe_fds[1]); - fd_map.push_back(std::make_pair(*read_pipe_write_fd, STDOUT_FILENO)); - - int write_pipe_fds[2] = {0}; - if (HANDLE_EINTR(pipe(write_pipe_fds)) != 0) { - LOG(ERROR) << "Bad write pipe"; - return false; - } - file_util::ScopedFD write_pipe_read_fd(&write_pipe_fds[0]); - file_util::ScopedFD write_pipe_write_fd(&write_pipe_fds[1]); - fd_map.push_back(std::make_pair(*write_pipe_read_fd, STDIN_FILENO)); - - CommandLine line(path); - base::LaunchOptions options; - options.fds_to_remap = &fd_map; - if (!base::LaunchProcess(line, options, native_process_handle)) { - LOG(ERROR) << "Error launching process"; - return false; - } - - // We will not be reading from the write pipe, nor writing from the read pipe. - write_pipe_read_fd.reset(); - read_pipe_write_fd.reset(); - - *read_file = *read_pipe_read_fd.release(); - *write_file = *write_pipe_write_fd.release(); - - return true; -} - -} // namespace extensions diff --git a/chrome/browser/extensions/api/messaging/native_process_launcher_win.cc b/chrome/browser/extensions/api/messaging/native_process_launcher_win.cc deleted file mode 100644 index 7d1fe69..0000000 --- a/chrome/browser/extensions/api/messaging/native_process_launcher_win.cc +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/extensions/api/messaging/native_process_launcher.h" - -#include <windows.h> - -#include "base/logging.h" -#include "base/process_util.h" -#include "base/stringprintf.h" -#include "base/string_number_conversions.h" - -namespace extensions { - -bool NativeProcessLauncher::LaunchNativeProcess( - const FilePath& path, - base::ProcessHandle* native_process_handle, - NativeMessageProcessHost::FileHandle* read_file, - NativeMessageProcessHost::FileHandle* write_file) const { - NOTREACHED(); - return false; -} - -} // namespace extensions diff --git a/chrome/browser/extensions/extension_system.cc b/chrome/browser/extensions/extension_system.cc index d981b77..ecd6067 100644 --- a/chrome/browser/extensions/extension_system.cc +++ b/chrome/browser/extensions/extension_system.cc @@ -12,7 +12,6 @@ #include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/extensions/api/alarms/alarm_manager.h" #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" -#include "chrome/browser/extensions/api/messaging/message_service.h" #include "chrome/browser/extensions/component_loader.h" #include "chrome/browser/extensions/event_router.h" #include "chrome/browser/extensions/extension_devtools_manager.h" @@ -26,6 +25,7 @@ #include "chrome/browser/extensions/extension_system_factory.h" #include "chrome/browser/extensions/lazy_background_task_queue.h" #include "chrome/browser/extensions/management_policy.h" +#include "chrome/browser/extensions/api/messaging/message_service.h" #include "chrome/browser/extensions/navigation_observer.h" #include "chrome/browser/extensions/shell_window_geometry_cache.h" #include "chrome/browser/extensions/state_store.h" diff --git a/chrome/browser/extensions/message_handler.cc b/chrome/browser/extensions/message_handler.cc index 5a21dcb..3015d3d 100644 --- a/chrome/browser/extensions/message_handler.cc +++ b/chrome/browser/extensions/message_handler.cc @@ -4,8 +4,8 @@ #include "chrome/browser/extensions/message_handler.h" -#include "chrome/browser/extensions/api/messaging/message_service.h" #include "chrome/browser/extensions/extension_system.h" +#include "chrome/browser/extensions/api/messaging/message_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/view_type_utils.h" #include "chrome/common/extensions/extension_messages.h" @@ -49,7 +49,7 @@ void MessageHandler::OnPostMessage(int port_id, MessageService* message_service = ExtensionSystem::Get(profile)->message_service(); if (message_service) { - message_service->PostMessage(port_id, message); + message_service->PostMessageFromRenderer(port_id, message); } } diff --git a/chrome/browser/renderer_host/chrome_render_message_filter.cc b/chrome/browser/renderer_host/chrome_render_message_filter.cc index f7c37a0..dfff53b 100644 --- a/chrome/browser/renderer_host/chrome_render_message_filter.cc +++ b/chrome/browser/renderer_host/chrome_render_message_filter.cc @@ -89,8 +89,6 @@ bool ChromeRenderMessageFilter::OnMessageReceived(const IPC::Message& message, IPC_MESSAGE_HANDLER(ExtensionHostMsg_OpenChannelToExtension, OnOpenChannelToExtension) IPC_MESSAGE_HANDLER(ExtensionHostMsg_OpenChannelToTab, OnOpenChannelToTab) - IPC_MESSAGE_HANDLER(ExtensionHostMsg_OpenChannelToNativeApp, - OnOpenChannelToNativeApp) IPC_MESSAGE_HANDLER_DELAY_REPLY(ExtensionHostMsg_GetMessageBundle, OnGetExtensionMessageBundle) IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddListener, OnExtensionAddListener) @@ -303,40 +301,6 @@ void ChromeRenderMessageFilter::OpenChannelToExtensionOnUIThread( source_extension_id, target_extension_id, channel_name); } -void ChromeRenderMessageFilter::OnOpenChannelToNativeApp( - int routing_id, const std::string& source_extension_id, - const std::string& native_app_name, - const std::string& channel_name, - const std::string& connect_message, int* port_id) { - int port2_id; - extensions::MessageService::AllocatePortIdPair(port_id, &port2_id); - - BrowserThread::PostTask( - BrowserThread::UI, FROM_HERE, - base::Bind(&ChromeRenderMessageFilter::OpenChannelToNativeAppOnUIThread, - this, - routing_id, - port2_id, - source_extension_id, - native_app_name, - channel_name, - connect_message)); -} - -void ChromeRenderMessageFilter::OpenChannelToNativeAppOnUIThread( - int source_routing_id, - int receiver_port_id, - const std::string& source_extension_id, - const std::string& native_app_name, - const std::string& channel_name, - const std::string& connect_message) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - extensions::ExtensionSystem::Get(profile_)->message_service()-> - OpenChannelToNativeApp( - render_process_id_, source_routing_id, receiver_port_id, - source_extension_id, native_app_name, channel_name, connect_message); -} - void ChromeRenderMessageFilter::OnOpenChannelToTab( int routing_id, int tab_id, const std::string& extension_id, const std::string& channel_name, int* port_id) { diff --git a/chrome/browser/renderer_host/chrome_render_message_filter.h b/chrome/browser/renderer_host/chrome_render_message_filter.h index f18f454..19351c9 100644 --- a/chrome/browser/renderer_host/chrome_render_message_filter.h +++ b/chrome/browser/renderer_host/chrome_render_message_filter.h @@ -100,18 +100,6 @@ class ChromeRenderMessageFilter : public content::BrowserMessageFilter { const std::string& source_extension_id, const std::string& target_extension_id, const std::string& channel_name); - void OnOpenChannelToNativeApp(int routing_id, - const std::string& source_extension_id, - const std::string& native_app_name, - const std::string& channel_name, - const std::string& connect_message, - int* port_id); - void OpenChannelToNativeAppOnUIThread(int source_routing_id, - int receiver_port_id, - const std::string& source_extension_id, - const std::string& native_app_name, - const std::string& channel_name, - const std::string& connect_message); void OnOpenChannelToTab(int routing_id, int tab_id, const std::string& extension_id, const std::string& channel_name, int* port_id); diff --git a/chrome/browser/tab_contents/background_contents.cc b/chrome/browser/tab_contents/background_contents.cc index a223059..8a43d35 100644 --- a/chrome/browser/tab_contents/background_contents.cc +++ b/chrome/browser/tab_contents/background_contents.cc @@ -5,6 +5,7 @@ #include "chrome/browser/tab_contents/background_contents.h" #include "chrome/browser/background/background_contents_service.h" +#include "chrome/browser/extensions/api/messaging/message_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/renderer_preferences_util.h" #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" diff --git a/chrome/chrome_browser_extensions.gypi b/chrome/chrome_browser_extensions.gypi index 3bbde0d..52a8d1b 100644 --- a/chrome/chrome_browser_extensions.gypi +++ b/chrome/chrome_browser_extensions.gypi @@ -195,19 +195,8 @@ 'browser/extensions/api/media_galleries/media_galleries_api.h', 'browser/extensions/api/media_galleries_private/media_galleries_private_event_router.cc', 'browser/extensions/api/media_galleries_private/media_galleries_private_event_router.h', - 'browser/extensions/api/messaging/extension_message_port.cc', - 'browser/extensions/api/messaging/extension_message_port.h', 'browser/extensions/api/messaging/message_service.cc', 'browser/extensions/api/messaging/message_service.h', - 'browser/extensions/api/messaging/native_message_process_host.cc', - 'browser/extensions/api/messaging/native_message_process_host.h', - 'browser/extensions/api/messaging/native_message_process_host_posix.cc', - 'browser/extensions/api/messaging/native_message_process_host_win.cc', - 'browser/extensions/api/messaging/native_process_launcher_posix.cc', - 'browser/extensions/api/messaging/native_process_launcher_win.cc', - 'browser/extensions/api/messaging/native_process_launcher.h', - 'browser/extensions/api/messaging/native_message_port.cc', - 'browser/extensions/api/messaging/native_message_port.h', 'browser/extensions/api/metrics/metrics.cc', 'browser/extensions/api/metrics/metrics.h', 'browser/extensions/api/offscreen_tabs/offscreen_tabs_api.cc', @@ -670,14 +659,7 @@ ['include', '^browser/extensions/api/declarative_webrequest/webrequest_rule.cc'], ['include', '^browser/extensions/api/declarative_webrequest/webrequest_rules_registry.cc'], ['include', '^browser/extensions/api/extension_action/extension_page_actions_api_constants.cc'], - ['include', '^browser/extensions/api/messaging/extension_message_port.cc'], ['include', '^browser/extensions/api/messaging/message_service.cc'], - ['include', '^browser/extensions/api/messaging/native_message_process_host.cc'], - ['include', '^browser/extensions/api/messaging/native_message_process_host_posix.cc'], - ['include', '^browser/extensions/api/messaging/native_message_process_host_win.cc'], - ['include', '^browser/extensions/api/messaging/native_process_launcher_posix.cc'], - ['include', '^browser/extensions/api/messaging/native_process_launcher_win.cc'], - ['include', '^browser/extensions/api/messaging/native_message_port.cc'], ['include', '^browser/extensions/api/omnibox/omnibox_api.cc'], ['include', '^browser/extensions/api/permissions/permissions_api.cc'], ['include', '^browser/extensions/api/permissions/permissions_api_helpers.cc'], diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index e7fd7da..946a564 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1244,7 +1244,6 @@ 'browser/extensions/api/file_system/file_system_api_unittest.cc', 'browser/extensions/api/identity/web_auth_flow_unittest.cc', 'browser/extensions/api/idle/idle_api_unittest.cc', - 'browser/extensions/api/messaging/native_message_process_host_unittest_posix.cc', 'browser/extensions/api/omnibox/omnibox_unittest.cc', 'browser/extensions/api/permissions/permissions_api_helpers_unittest.cc', 'browser/extensions/api/proxy/proxy_api_helpers_unittest.cc', @@ -2850,7 +2849,6 @@ 'browser/extensions/api/media_galleries/media_galleries_apitest.cc', 'browser/extensions/api/media_galleries_private/media_galleries_private_apitest.cc', 'browser/extensions/api/metrics/metrics_apitest.cc', - 'browser/extensions/api/messaging/native_messaging_apitest_posix.cc', 'browser/extensions/api/offscreen_tabs/offscreen_tabs_apitest.cc', 'browser/extensions/api/omnibox/omnibox_apitest.cc', 'browser/extensions/api/page_capture/page_capture_apitest.cc', diff --git a/chrome/common/extensions/api/extension.json b/chrome/common/extensions/api/extension.json index f26107e..9efc496 100644 --- a/chrome/common/extensions/api/extension.json +++ b/chrome/common/extensions/api/extension.json @@ -75,31 +75,6 @@ } }, { - "name": "connectNative", - "nodoc": true, - "type": "function", - "description": "Attempts to connect a native application in the host machine. The native application must have already registered itself in the proper directory.", - "parameters": [ - { - "type": "string", - "name": "appName", - "description": "The name of the registered app to connect to." - }, - { - "name": "connectionMessage", - "description": "The object that will be passed to the registered native app on connection.", - "type": "object", - "additionalProperties": { - "type": "any" - } - } - ], - "returns": { - "$ref": "Port", - "description": "Port through which messages can be sent and received with the application" - } - }, - { "name": "sendRequest", "nodoc": true, "type": "function", @@ -147,43 +122,6 @@ ] }, { - "name": "sendNativeMessage", - "nodoc": true, - "type": "function", - "description": "Send a single message to a registered native application.", - "parameters": [ - { - "name": "registeredNativeApp", - "description": "The name of the registered native application.", - "type": "string" - }, - { - "name": "message", - "description": "The message that will be passed to the registered native application.", - "type": "object", - "additionalProperties": { - "type": "any" - } - }, - { - "type": "function", - "name": "callback", - "optional": true, - "description": "Called with the response from the native application.", - "parameters": [ - { - "name": "nativeResponse", - "type": "object", - "description": "Whatever the native application responds with.", - "additionalProperties": { - "type": "any" - } - } - ] - } - ] - }, - { "name": "getURL", "type": "function", "unprivileged": true, diff --git a/chrome/common/extensions/extension_messages.h b/chrome/common/extensions/extension_messages.h index 9bed176..6be2364 100644 --- a/chrome/common/extensions/extension_messages.h +++ b/chrome/common/extensions/extension_messages.h @@ -451,14 +451,6 @@ IPC_SYNC_MESSAGE_CONTROL4_1(ExtensionHostMsg_OpenChannelToExtension, std::string /* channel_name */, int /* port_id */) -IPC_SYNC_MESSAGE_CONTROL5_1(ExtensionHostMsg_OpenChannelToNativeApp, - int /* routing_id */, - std::string /* source_extension_id */, - std::string /* native_app_name */, - std::string /* channel_name */, - std::string /* connection_message */, - int /* port_id */) - // Get a port handle to the given tab. The handle can be used for sending // messages to the extension. IPC_SYNC_MESSAGE_CONTROL4_1(ExtensionHostMsg_OpenChannelToTab, diff --git a/chrome/renderer/extensions/extension_custom_bindings.cc b/chrome/renderer/extensions/extension_custom_bindings.cc index e3c5afc..685c180 100644 --- a/chrome/renderer/extensions/extension_custom_bindings.cc +++ b/chrome/renderer/extensions/extension_custom_bindings.cc @@ -15,7 +15,6 @@ #include "chrome/renderer/extensions/dispatcher.h" #include "chrome/renderer/extensions/extension_helper.h" #include "content/public/renderer/render_view.h" -#include "content/public/renderer/v8_value_converter.h" #include "grit/renderer_resources.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" @@ -32,7 +31,6 @@ ExtensionCustomBindings::ExtensionCustomBindings(Dispatcher* dispatcher) : ChromeV8Extension(dispatcher) { RouteStaticFunction("GetExtensionViews", &GetExtensionViews); RouteStaticFunction("OpenChannelToExtension", &OpenChannelToExtension); - RouteStaticFunction("OpenChannelToNativeApp", &OpenChannelToNativeApp); } // static @@ -125,36 +123,4 @@ v8::Handle<v8::Value> ExtensionCustomBindings::OpenChannelToExtension( return v8::Integer::New(port_id); } -// static -v8::Handle<v8::Value> ExtensionCustomBindings::OpenChannelToNativeApp( - const v8::Arguments& args) { - // Get the current RenderView so that we can send a routed IPC message from - // the correct source. - content::RenderView* renderview = GetCurrentRenderView(); - if (!renderview) - return v8::Undefined(); - - // The Javascript code should validate/fill the arguments. - CHECK(args.Length() >= 3 && - args[0]->IsString() && - args[1]->IsString() && - args[2]->IsString() && - args[3]->IsString()); - - std::string extension_id = *v8::String::Utf8Value(args[0]->ToString()); - std::string native_app_name = *v8::String::Utf8Value(args[1]->ToString()); - std::string channel_name = *v8::String::Utf8Value(args[2]->ToString()); - std::string connect_message = *v8::String::Utf8Value(args[3]->ToString()); - - int port_id = -1; - renderview->Send(new ExtensionHostMsg_OpenChannelToNativeApp( - renderview->GetRoutingID(), - extension_id, - native_app_name, - channel_name, - connect_message, - &port_id)); - return v8::Integer::New(port_id); -} - } // namespace extensions diff --git a/chrome/renderer/extensions/extension_custom_bindings.h b/chrome/renderer/extensions/extension_custom_bindings.h index 3bc29ee..682b001 100644 --- a/chrome/renderer/extensions/extension_custom_bindings.h +++ b/chrome/renderer/extensions/extension_custom_bindings.h @@ -21,9 +21,6 @@ class ExtensionCustomBindings : public ChromeV8Extension { // Creates a new messaging channel to the given extension. static v8::Handle<v8::Value> OpenChannelToExtension( const v8::Arguments& args); - - static v8::Handle<v8::Value> OpenChannelToNativeApp( - const v8::Arguments& args); }; } // namespace extensions diff --git a/chrome/renderer/resources/extensions/extension_custom_bindings.js b/chrome/renderer/resources/extensions/extension_custom_bindings.js index 551e103..be19e9c 100644 --- a/chrome/renderer/resources/extensions/extension_custom_bindings.js +++ b/chrome/renderer/resources/extensions/extension_custom_bindings.js @@ -7,7 +7,6 @@ var extensionNatives = requireNative('extension'); var GetExtensionViews = extensionNatives.GetExtensionViews; var OpenChannelToExtension = extensionNatives.OpenChannelToExtension; -var OpenChannelToNativeApp = extensionNatives.OpenChannelToNativeApp; var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); @@ -94,8 +93,6 @@ chromeHidden.registerCustomHook('extension', sendMessageUpdateArguments.bind(null, 'sendRequest')); apiFunctions.setUpdateArgumentsPreValidate('sendMessage', sendMessageUpdateArguments.bind(null, 'sendMessage')); - apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage', - sendMessageUpdateArguments.bind(null, 'sendNativeMessage')); apiFunctions.setHandleRequest('sendRequest', function(targetId, request, responseCallback) { @@ -111,13 +108,6 @@ chromeHidden.registerCustomHook('extension', chromeHidden.Port.sendMessageImpl(port, message, responseCallback); }); - apiFunctions.setHandleRequest('sendNativeMessage', - function(targetId, message, responseCallback) { - var port = chrome.extension.connectNative( - targetId, message, chromeHidden.kNativeMessageChannel); - chromeHidden.Port.sendMessageImpl(port, '', responseCallback); - }); - apiFunctions.setUpdateArgumentsPreValidate('connect', function() { // Align missing (optional) function arguments with the arguments that // schema validation is expecting, e.g. @@ -136,29 +126,10 @@ chromeHidden.registerCustomHook('extension', connectInfo = arguments[nextArg++]; if (nextArg != arguments.length) - throw new Error('Invalid arguments to connect.'); + throw new Error('Invalid arguments to connect'); return [targetId, connectInfo]; }); - apiFunctions.setUpdateArgumentsPreValidate('connectNative', function() { - var nextArg = 0; - - // appName is required. - var appName = arguments[nextArg++]; - - // connectionMessage is required. - var connectMessage = arguments[nextArg++]; - - // channelName is only passed by sendMessage - var channelName = 'connectNative'; - if (typeof(arguments[nextArg]) == 'string') - channelName = arguments[nextArg++]; - - if (nextArg != arguments.length) - throw new Error('Invalid arguments to connectNative.'); - return [appName, {name: channelName, message: connectMessage}]; - }); - apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { if (!targetId) targetId = extensionId; @@ -171,17 +142,4 @@ chromeHidden.registerCustomHook('extension', return chromeHidden.Port.createPort(portId, name); throw new Error('Error connecting to extension ' + targetId); }); - - apiFunctions.setHandleRequest('connectNative', - function(nativeAppName, connectInfo) { - // Turn the object into a string here, because it eventually will be. - var portId = OpenChannelToNativeApp(extensionId, - nativeAppName, - connectInfo.name, - JSON.stringify(connectInfo.message)); - if (portId >= 0) { - return chromeHidden.Port.createPort(portId, connectInfo.name); - } - throw new Error('Error connecting to native app: ' + nativeAppName); - }); }); diff --git a/chrome/renderer/resources/extensions/miscellaneous_bindings.js b/chrome/renderer/resources/extensions/miscellaneous_bindings.js index e9d972d..d860bab 100644 --- a/chrome/renderer/resources/extensions/miscellaneous_bindings.js +++ b/chrome/renderer/resources/extensions/miscellaneous_bindings.js @@ -27,7 +27,6 @@ // Note: sendRequest is deprecated. chromeHidden.kRequestChannel = "chrome.extension.sendRequest"; chromeHidden.kMessageChannel = "chrome.extension.sendMessage"; - chromeHidden.kNativeMessageChannel = "chrome.extension.sendNativeMessage"; // Map of port IDs to port object. var ports = {}; @@ -258,10 +257,9 @@ // Shared implementation used by tabs.sendMessage and extension.sendMessage. chromeHidden.Port.sendMessageImpl = function(port, request, responseCallback) { - if (port.name != chromeHidden.kNativeMessageChannel) - port.postMessage(request); + port.postMessage(request); - if (port.name != chromeHidden.kRequestChannel && !responseCallback) { + if (port.name == chromeHidden.kMessageChannel && !responseCallback) { // TODO(mpcomplete): Do this for the old sendRequest API too, after // verifying it doesn't break anything. // Go ahead and disconnect immediately if the sender is not expecting diff --git a/chrome/test/data/extensions/api_test/native_messaging/manifest.json b/chrome/test/data/extensions/api_test/native_messaging/manifest.json deleted file mode 100644 index 3b3d173..0000000 --- a/chrome/test/data/extensions/api_test/native_messaging/manifest.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "version": "1.0.0.0", - "manifest_version": 2, - "name": "native messaging test", - "description": "Test the basic functionality of passing native messages.", - "background": { - "scripts": ["test.js"] - } -} diff --git a/chrome/test/data/extensions/api_test/native_messaging/test.js b/chrome/test/data/extensions/api_test/native_messaging/test.js deleted file mode 100644 index cb58585..0000000 --- a/chrome/test/data/extensions/api_test/native_messaging/test.js +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -chrome.test.getConfig(function(config) { - chrome.test.runTests([ - - function sendMessageWithCallback() { - var message = {"text": "Hi there!", "number": 3}; - chrome.extension.sendNativeMessage( - 'echo.py', message, - chrome.test.callbackPass(function(nativeResponse) { - var expectedResponse = {"id": 1, "echo": message}; - chrome.test.assertEq(expectedResponse, nativeResponse); - })); - }, - - // The goal of this test, is just not to crash. - function sendMessageWithoutCallback() { - var message = {"text": "Hi there!", "number": 3}; - chrome.extension.sendNativeMessage('echo.py', message); - chrome.test.succeed(); // Mission Complete - }, - - function connect() { - var messagesToSend = [{"text": "foo"}, - {"text": "bar", "funCount": 9001}, - {}]; - var expectedResponses = [{"id": 1, "echo": messagesToSend[0]}, - {"id": 2, "echo": messagesToSend[1]}, - {"id": 3, "echo": messagesToSend[2]}]; - var currentMessage = 0; - - port = chrome.extension.connectNative('echo.py', - messagesToSend[currentMessage]); - port.onMessage.addListener(function(message) { - chrome.test.assertEq(expectedResponses[currentMessage], message); - currentMessage++; - - if (currentMessage == expectedResponses.length) - chrome.test.notifyPass(); - else - port.postMessage(messagesToSend[currentMessage]); - }); - } - ]); -}); |