Commit 32b3a84b authored by kalman's avatar kalman Committed by Commit bot

Move the Extension Port implementation out of messaging.js into its own file port.js.

messaging.js is too large to handle right now.

R=rdevlin.cronin@chromium.org
BUG=475536

Review URL: https://codereview.chromium.org/1097583004

Cr-Commit-Position: refs/heads/master@{#325585}
parent 5491de18
...@@ -919,6 +919,7 @@ ...@@ -919,6 +919,7 @@
'renderer/resources/permissions_custom_bindings.js', 'renderer/resources/permissions_custom_bindings.js',
'renderer/resources/platform_app.css', 'renderer/resources/platform_app.css',
'renderer/resources/platform_app.js', 'renderer/resources/platform_app.js',
'renderer/resources/port.js',
'renderer/resources/runtime_custom_bindings.js', 'renderer/resources/runtime_custom_bindings.js',
'renderer/resources/schema_utils.js', 'renderer/resources/schema_utils.js',
'renderer/resources/send_request.js', 'renderer/resources/send_request.js',
......
...@@ -510,6 +510,7 @@ std::vector<std::pair<std::string, int> > Dispatcher::GetJsResources() { ...@@ -510,6 +510,7 @@ std::vector<std::pair<std::string, int> > Dispatcher::GetJsResources() {
resources.push_back(std::make_pair("messaging", IDR_MESSAGING_JS)); resources.push_back(std::make_pair("messaging", IDR_MESSAGING_JS));
resources.push_back(std::make_pair("messaging_utils", resources.push_back(std::make_pair("messaging_utils",
IDR_MESSAGING_UTILS_JS)); IDR_MESSAGING_UTILS_JS));
resources.push_back(std::make_pair("port", IDR_PORT_JS));
resources.push_back(std::make_pair(kSchemaUtils, IDR_SCHEMA_UTILS_JS)); resources.push_back(std::make_pair(kSchemaUtils, IDR_SCHEMA_UTILS_JS));
resources.push_back(std::make_pair("sendRequest", IDR_SEND_REQUEST_JS)); resources.push_back(std::make_pair("sendRequest", IDR_SEND_REQUEST_JS));
resources.push_back(std::make_pair("setIcon", IDR_SET_ICON_JS)); resources.push_back(std::make_pair("setIcon", IDR_SET_ICON_JS));
......
...@@ -42,6 +42,7 @@ ...@@ -42,6 +42,7 @@
<include name="IDR_MESSAGING_UTILS_JS" file="messaging_utils.js" type="BINDATA" /> <include name="IDR_MESSAGING_UTILS_JS" file="messaging_utils.js" type="BINDATA" />
<include name="IDR_MIME_HANDLER_PRIVATE_CUSTOM_BINDINGS_JS" file="mime_handler_private_custom_bindings.js" type="BINDATA" /> <include name="IDR_MIME_HANDLER_PRIVATE_CUSTOM_BINDINGS_JS" file="mime_handler_private_custom_bindings.js" type="BINDATA" />
<include name="IDR_MIME_HANDLER_MOJOM_JS" file="${mojom_root}\extensions\common\api\mime_handler.mojom.js" use_base_dir="false" type="BINDATA" /> <include name="IDR_MIME_HANDLER_MOJOM_JS" file="${mojom_root}\extensions\common\api\mime_handler.mojom.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_PORT_JS" file="port.js" type="BINDATA" />
<include name="IDR_SCHEMA_UTILS_JS" file="schema_utils.js" type="BINDATA" /> <include name="IDR_SCHEMA_UTILS_JS" file="schema_utils.js" type="BINDATA" />
<include name="IDR_SEND_REQUEST_JS" file="send_request.js" type="BINDATA" /> <include name="IDR_SEND_REQUEST_JS" file="send_request.js" type="BINDATA" />
<include name="IDR_SERIAL_CUSTOM_BINDINGS_JS" file="serial_custom_bindings.js" type="BINDATA" /> <include name="IDR_SERIAL_CUSTOM_BINDINGS_JS" file="serial_custom_bindings.js" type="BINDATA" />
......
...@@ -6,14 +6,13 @@ ...@@ -6,14 +6,13 @@
// TODO(kalman): factor requiring chrome out of here. // TODO(kalman): factor requiring chrome out of here.
var chrome = requireNative('chrome').GetChrome(); var chrome = requireNative('chrome').GetChrome();
var Event = require('event_bindings').Event;
var lastError = require('lastError'); var lastError = require('lastError');
var logActivity = requireNative('activityLogger'); var logActivity = requireNative('activityLogger');
var logging = requireNative('logging'); var logging = requireNative('logging');
var messagingNatives = requireNative('messaging_natives'); var messagingNatives = requireNative('messaging_natives');
var Port = require('port').Port;
var processNatives = requireNative('process'); var processNatives = requireNative('process');
var unloadEvent = require('unload_event'); var unloadEvent = require('unload_event');
var utils = require('utils');
var messagingUtils = require('messaging_utils'); var messagingUtils = require('messaging_utils');
// The reserved channel name for the sendRequest/send(Native)Message APIs. // The reserved channel name for the sendRequest/send(Native)Message APIs.
...@@ -33,64 +32,6 @@ ...@@ -33,64 +32,6 @@
// channel. // channel.
function getOppositePortId(portId) { return portId ^ 1; } function getOppositePortId(portId) { return portId ^ 1; }
// Port object. Represents a connection to another script context through
// which messages can be passed.
function PortImpl(portId, opt_name) {
this.portId_ = portId;
this.name = opt_name;
var portSchema = {name: 'port', $ref: 'runtime.Port'};
var options = {unmanaged: true};
this.onDisconnect = new Event(null, [portSchema], options);
this.onMessage = new Event(
null,
[{name: 'message', type: 'any', optional: true}, portSchema],
options);
this.onDestroy_ = null;
}
// Sends a message asynchronously to the context on the other end of this
// port.
PortImpl.prototype.postMessage = function(msg) {
// JSON.stringify doesn't support a root object which is undefined.
if (msg === undefined)
msg = null;
msg = $JSON.stringify(msg);
if (msg === undefined) {
// JSON.stringify can fail with unserializable objects. Log an error and
// drop the message.
//
// TODO(kalman/mpcomplete): it would be better to do the same validation
// here that we do for runtime.sendMessage (and variants), i.e. throw an
// schema validation Error, but just maintain the old behaviour until
// there's a good reason not to (http://crbug.com/263077).
console.error('Illegal argument to Port.postMessage');
return;
}
messagingNatives.PostMessage(this.portId_, msg);
};
// Disconnects the port from the other end.
PortImpl.prototype.disconnect = function() {
messagingNatives.CloseChannel(this.portId_, true);
this.destroy_();
};
PortImpl.prototype.destroy_ = function() {
var portId = this.portId_;
if (this.onDestroy_)
this.onDestroy_();
privates(this.onDisconnect).impl.destroy_();
privates(this.onMessage).impl.destroy_();
messagingNatives.PortRelease(portId);
unloadEvent.removeListener(portReleasers[portId]);
delete ports[portId];
delete portReleasers[portId];
};
// Returns true if the specified port id is in this context. This is used by // Returns true if the specified port id is in this context. This is used by
// the C++ to avoid creating the javascript message for all the contexts that // the C++ to avoid creating the javascript message for all the contexts that
// don't care about a particular message. // don't care about a particular message.
...@@ -113,6 +54,14 @@ ...@@ -113,6 +54,14 @@
return port; return port;
}; };
// Called when a Port is destroyed. Does general accounting cleanup.
function onPortDestroyed(port) {
var portId = privates(port).impl.portId_;
unloadEvent.removeListener(portReleasers[portId]);
delete ports[portId];
delete portReleasers[portId];
}
// Helper function for dispatchOnRequest. // Helper function for dispatchOnRequest.
function handleSendRequestError(isSendMessage, function handleSendRequestError(isSendMessage,
responseCallbackPreserved, responseCallbackPreserved,
...@@ -200,6 +149,7 @@ ...@@ -200,6 +149,7 @@
privates(port).impl.onDestroy_ = function() { privates(port).impl.onDestroy_ = function() {
port.onMessage.removeListener(messageListener); port.onMessage.removeListener(messageListener);
onPortDestroyed(port);
}; };
port.onMessage.addListener(messageListener); port.onMessage.addListener(messageListener);
...@@ -358,6 +308,7 @@ ...@@ -358,6 +308,7 @@
privates(port).impl.onDestroy_ = function() { privates(port).impl.onDestroy_ = function() {
port.onDisconnect.removeListener(disconnectListener); port.onDisconnect.removeListener(disconnectListener);
port.onMessage.removeListener(messageListener); port.onMessage.removeListener(messageListener);
onPortDestroyed(port);
}; };
port.onDisconnect.addListener(disconnectListener); port.onDisconnect.addListener(disconnectListener);
port.onMessage.addListener(messageListener); port.onMessage.addListener(messageListener);
...@@ -373,20 +324,9 @@ ...@@ -373,20 +324,9 @@
return alignedArgs; return alignedArgs;
} }
var Port = utils.expose('Port', PortImpl, { functions: [
'disconnect',
'postMessage'
],
properties: [
'name',
'onDisconnect',
'onMessage'
] });
exports.kRequestChannel = kRequestChannel; exports.kRequestChannel = kRequestChannel;
exports.kMessageChannel = kMessageChannel; exports.kMessageChannel = kMessageChannel;
exports.kNativeMessageChannel = kNativeMessageChannel; exports.kNativeMessageChannel = kNativeMessageChannel;
exports.Port = Port;
exports.createPort = createPort; exports.createPort = createPort;
exports.sendMessageImpl = sendMessageImpl; exports.sendMessageImpl = sendMessageImpl;
exports.sendMessageUpdateArguments = sendMessageUpdateArguments; exports.sendMessageUpdateArguments = sendMessageUpdateArguments;
......
// Copyright 2015 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.
var Event = require('event_bindings').Event;
var messagingNatives = requireNative('messaging_natives');
var utils = require('utils');
// Port object. Represents a connection to another script context through
// which messages can be passed.
function Port(portId, opt_name) {
this.portId_ = portId;
this.name = opt_name;
var portSchema = {name: 'port', $ref: 'runtime.Port'};
var messageSchema = {name: 'message', type: 'any', optional: true};
var options = {unmanaged: true};
this.onDisconnect = new Event(null, [portSchema], options);
this.onMessage = new Event(null, [messageSchema, portSchema], options);
this.onDestroy_ = null;
}
// Sends a message asynchronously to the context on the other end of this
// port.
Port.prototype.postMessage = function(msg) {
// JSON.stringify doesn't support a root object which is undefined.
if (msg === undefined)
msg = null;
msg = $JSON.stringify(msg);
if (msg === undefined) {
// JSON.stringify can fail with unserializable objects. Log an error and
// drop the message.
//
// TODO(kalman/mpcomplete): it would be better to do the same validation
// here that we do for runtime.sendMessage (and variants), i.e. throw an
// schema validation Error, but just maintain the old behaviour until
// there's a good reason not to (http://crbug.com/263077).
console.error('Illegal argument to Port.postMessage');
return;
}
messagingNatives.PostMessage(this.portId_, msg);
};
// Disconnects the port from the other end.
Port.prototype.disconnect = function() {
messagingNatives.CloseChannel(this.portId_, true);
this.destroy_();
};
Port.prototype.destroy_ = function() {
// Note: it's not necessary to destroy the onDisconnect/onMessage events
// because they're unmanaged.
if (this.onDestroy_)
this.onDestroy_();
messagingNatives.PortRelease(this.portId_);
};
exports.Port = utils.expose('Port', Port, {
functions: ['disconnect', 'postMessage'],
properties: ['name', 'onDisconnect', 'onMessage']
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment