diff options
author | mad@google.com <mad@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-08 02:33:26 +0000 |
---|---|---|
committer | mad@google.com <mad@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-08 02:33:26 +0000 |
commit | d181233239400698741f9765bc2b9769f9e1f2ef (patch) | |
tree | 939d0358920a87ac9eadbe14ee3398546b00a872 /chrome/browser/automation | |
parent | e8637201ee40d8a09066ff2204a1b05594eefdf1 (diff) | |
download | chromium_src-d181233239400698741f9765bc2b9769f9e1f2ef.zip chromium_src-d181233239400698741f9765bc2b9769f9e1f2ef.tar.gz chromium_src-d181233239400698741f9765bc2b9769f9e1f2ef.tar.bz2 |
Committing for Siggi based on review:
http://codereview.chromium.org/165134
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@22858 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/automation')
4 files changed, 67 insertions, 15 deletions
diff --git a/chrome/browser/automation/extension_automation_constants.cc b/chrome/browser/automation/extension_automation_constants.cc index 6b99a41..c513945 100644 --- a/chrome/browser/automation/extension_automation_constants.cc +++ b/chrome/browser/automation/extension_automation_constants.cc @@ -21,6 +21,9 @@ const wchar_t kAutomationConnectionIdKey[] = L"connid"; const wchar_t kAutomationMessageDataKey[] = L"data"; const wchar_t kAutomationExtensionIdKey[] = L"extid"; const wchar_t kAutomationPortIdKey[] = L"portid"; +const wchar_t kAutomationChannelNameKey[] = L"chname"; +const wchar_t kAutomationTabJsonKey[] = L"tab"; + const char kAutomationPortRequestTarget[] = "__priv_prtreq"; const char kAutomationPortResponseTarget[] = "__priv_prtres"; diff --git a/chrome/browser/automation/extension_automation_constants.h b/chrome/browser/automation/extension_automation_constants.h index 06bd97d..8c29293 100644 --- a/chrome/browser/automation/extension_automation_constants.h +++ b/chrome/browser/automation/extension_automation_constants.h @@ -30,6 +30,9 @@ extern const wchar_t kAutomationConnectionIdKey[]; extern const wchar_t kAutomationMessageDataKey[]; extern const wchar_t kAutomationExtensionIdKey[]; extern const wchar_t kAutomationPortIdKey[]; +extern const wchar_t kAutomationChannelNameKey[]; +extern const wchar_t kAutomationTabJsonKey[]; + // All external port message requests should have this target. extern const char kAutomationPortRequestTarget[]; // All external port message responses have this target. @@ -43,6 +46,7 @@ enum PrivatePortCommand { OPEN_CHANNEL = 0, CHANNEL_OPENED = 1, POST_MESSAGE = 2, + CHANNEL_CLOSED = 3, }; }; // namespace automation_extension_constants diff --git a/chrome/browser/automation/extension_port_container.cc b/chrome/browser/automation/extension_port_container.cc index e814fcc..f0ff246 100644 --- a/chrome/browser/automation/extension_port_container.cc +++ b/chrome/browser/automation/extension_port_container.cc @@ -25,7 +25,7 @@ ExtensionPortContainer::ExtensionPortContainer(AutomationProvider* automation, int tab_handle) : automation_(automation), service_(NULL), port_id_(-1), tab_handle_(tab_handle) { - service_ = automation_->profile()->GetExtensionMessageService();; + service_ = automation_->profile()->GetExtensionMessageService(); DCHECK(service_); } @@ -61,16 +61,22 @@ void ExtensionPortContainer::PostMessageFromExternalPort( bool ExtensionPortContainer::Connect(const std::string &extension_id, int process_id, int routing_id, - int connection_id) { + int connection_id, + const std::string& channel_name) { DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); port_id_ = service_->OpenAutomationChannelToExtension(process_id, routing_id, extension_id, + channel_name, this); + if (port_id_ == -1) { + // In this case a disconnect message has been dispatched. + return false; + } SendConnectionResponse(connection_id, port_id_); - return port_id_ != -1; + return true; } void ExtensionPortContainer::SendConnectionResponse(int connection_id, @@ -110,7 +116,13 @@ void ExtensionPortContainer::OnExtensionMessageInvoke( if (args.GetString(0, &message) && args.GetInteger(1, &source_port_id)) OnExtensionHandleMessage(message, source_port_id); } else if (function_name == ExtensionMessageService::kDispatchOnDisconnect) { - // do nothing + DCHECK_EQ(args.GetSize(), 1u); + int port_id; + if (args.GetInteger(0, &port_id)) + OnExtensionPortDisconnected(port_id); + } else if (function_name == ExtensionMessageService::kDispatchOnConnect) { + // Do nothing. + // TODO(siggi): implement } else { NOTREACHED() << function_name << " shouldn't be called."; } @@ -119,13 +131,25 @@ void ExtensionPortContainer::OnExtensionMessageInvoke( void ExtensionPortContainer::OnExtensionHandleMessage( const std::string& message, int source_port_id) { // Compose the reply message and fire it away. - scoped_ptr<DictionaryValue> msg_dict(new DictionaryValue()); - msg_dict->SetInteger(ext::kAutomationRequestIdKey, ext::POST_MESSAGE); - msg_dict->SetInteger(ext::kAutomationPortIdKey, port_id_); - msg_dict->SetString(ext::kAutomationMessageDataKey, message); + DictionaryValue msg_dict; + msg_dict.SetInteger(ext::kAutomationRequestIdKey, ext::POST_MESSAGE); + msg_dict.SetInteger(ext::kAutomationPortIdKey, port_id_); + msg_dict.SetString(ext::kAutomationMessageDataKey, message); std::string msg_json; - JSONWriter::Write(msg_dict.get(), false, &msg_json); + JSONWriter::Write(&msg_dict, false, &msg_json); + + PostMessageToExternalPort(msg_json); +} + +void ExtensionPortContainer::OnExtensionPortDisconnected(int source_port_id) { + // Compose the disconnect message and fire it away. + DictionaryValue msg_dict; + msg_dict.SetInteger(ext::kAutomationRequestIdKey, ext::CHANNEL_CLOSED); + msg_dict.SetInteger(ext::kAutomationPortIdKey, port_id_); + + std::string msg_json; + JSONWriter::Write(&msg_dict, false, &msg_json); PostMessageToExternalPort(msg_json); } @@ -173,13 +197,18 @@ bool ExtensionPortContainer::InterceptMessageFromExternalHost( if (!got_value) return true; + std::string channel_name; + // Channel name is optional. + message_dict->GetString(ext::kAutomationChannelNameKey, &channel_name); + int routing_id = view_host->routing_id(); // Create the extension port and connect it. scoped_ptr<ExtensionPortContainer> port( new ExtensionPortContainer(automation, tab_handle)); int process_id = view_host->process()->pid(); - if (port->Connect(extension_id, process_id, routing_id, connection_id)) { + if (port->Connect(extension_id, process_id, routing_id, connection_id, + channel_name)) { // We have a successful connection. automation->AddPortContainer(port.release()); } @@ -200,6 +229,19 @@ bool ExtensionPortContainer::InterceptMessageFromExternalHost( DCHECK(port); if (port) port->PostMessageFromExternalPort(data); + } else if (command == ext::CHANNEL_CLOSED) { + int port_id = -1; + got_value = message_dict->GetInteger(ext::kAutomationPortIdKey, &port_id); + DCHECK(got_value); + if (!got_value) + return true; + + ExtensionPortContainer* port = automation->GetPortContainer(port_id); + DCHECK(port); + if (port) { + // This will delete the port and notify the other end of the disconnect. + automation->RemovePortContainer(port); + } } else { // We don't expect other messages here. NOTREACHED(); diff --git a/chrome/browser/automation/extension_port_container.h b/chrome/browser/automation/extension_port_container.h index ae86dae..6d9d149 100644 --- a/chrome/browser/automation/extension_port_container.h +++ b/chrome/browser/automation/extension_port_container.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROME_BROWSER_EXTENSION_PORT_CONTAINER_H_ -#define CHROME_BROWSER_EXTENSION_PORT_CONTAINER_H_ +#ifndef CHROME_BROWSER_AUTOMATION_EXTENSION_PORT_CONTAINER_H_ +#define CHROME_BROWSER_AUTOMATION_EXTENSION_PORT_CONTAINER_H_ #include <string> @@ -55,13 +55,16 @@ class ExtensionPortContainer : public IPC::Message::Sender { bool Connect(const std::string &extension_id, int process_id, int routing_id, - int connection_id); - // Sends a response to the + int connection_id, + const std::string& channel_name); + + // Sends a connect response to the external port. void SendConnectionResponse(int connection_id, int port_id); void OnExtensionMessageInvoke(const std::string& function_name, const ListValue& args); void OnExtensionHandleMessage(const std::string& message, int source_port_id); + void OnExtensionPortDisconnected(int source_port_id); // Our automation provider. AutomationProvider* automation_; @@ -77,4 +80,4 @@ class ExtensionPortContainer : public IPC::Message::Sender { DISALLOW_COPY_AND_ASSIGN(ExtensionPortContainer); }; -#endif // CHROME_BROWSER_EXTENSION_PORT_CONTAINER_H_ +#endif // CHROME_BROWSER_AUTOMATION_EXTENSION_PORT_CONTAINER_H_ |