Bug 676375 - Implement "send URI for display" command in Sync Clients engine; r=rnewman

This commit is contained in:
Gregory Szorc 2011-08-09 09:23:55 -07:00
Родитель b61c46cf13
Коммит 2228dda3d5
2 изменённых файлов: 132 добавлений и 1 удалений

Просмотреть файл

@ -199,7 +199,8 @@ ClientEngine.prototype = {
resetEngine: { args: 1, desc: "Clear temporary local data for engine" },
wipeAll: { args: 0, desc: "Delete all client data for all engines" },
wipeEngine: { args: 1, desc: "Delete all client data for engine" },
logout: { args: 0, desc: "Log out client" }
logout: { args: 0, desc: "Log out client" },
displayURI: { args: 2, desc: "Instruct a client to display a URI" }
},
/**
@ -284,6 +285,9 @@ ClientEngine.prototype = {
case "logout":
Weave.Service.logout();
return false;
case "displayURI":
this._handleDisplayURI(args[0], args[1]);
break;
default:
this._log.debug("Received an unknown command: " + command);
break;
@ -330,6 +334,52 @@ ClientEngine.prototype = {
this._sendCommandToClient(command, args, id);
}
}
},
/**
* Send a URI to another client for display.
*
* A side effect is the score is increased dramatically to incur an
* immediate sync.
*
* If an unknown client ID is specified, sendCommand() will throw an
* Error object.
*
* @param uri
* URI (as a string) to send and display on the remote client
* @param clientId
* ID of client to send the command to. If not defined, will be sent
* to all remote clients.
*/
sendURIToClientForDisplay: function sendURIToClientForDisplay(uri, clientId) {
this._log.info("Sending URI to client: " + uri + " -> " + clientId);
this.sendCommand("displayURI", [uri, this.syncID], clientId);
Clients._tracker.score += SCORE_INCREMENT_XLARGE;
},
/**
* Handle a single received 'displayURI' command.
*
* Interested parties should observe the "weave:engine:clients:display-uri"
* topic. The callback will receive an object as the subject parameter with
* the following keys:
*
* uri URI (string) that is requested for display
* clientId ID of client that sent the command
*
* The 'data' parameter to the callback will not be defined.
*
* @param uri
* String URI that was received
* @param clientId
* ID of client that sent URI
*/
_handleDisplayURI: function _handleDisplayURI(uri, clientId) {
this._log.info("Received a URI for display: " + uri + " from " + clientId);
let subject = { uri: uri, client: clientId };
Svc.Obs.notify("weave:engine:clients:display-uri", subject);
}
};

Просмотреть файл

@ -449,6 +449,87 @@ add_test(function test_command_sync() {
}
});
add_test(function test_send_uri_to_client_for_display() {
_("Ensure sendURIToClientForDisplay() sends command properly.");
let tracker = Clients._tracker;
let store = Clients._store;
let remoteId = Utils.makeGUID();
let rec = new ClientsRec("clients", remoteId);
rec.name = "remote";
store.create(rec);
let remoteRecord = store.createRecord(remoteId, "clients");
tracker.clearChangedIDs();
let initialScore = tracker.score;
let uri = "http://www.mozilla.org/";
Clients.sendURIToClientForDisplay(uri, remoteId);
let newRecord = store._remoteClients[remoteId];
do_check_neq(newRecord, undefined);
do_check_eq(newRecord.commands.length, 1);
let command = newRecord.commands[0];
do_check_eq(command.command, "displayURI");
do_check_eq(command.args.length, 2);
do_check_eq(command.args[0], uri);
do_check_true(tracker.score > initialScore);
do_check_true(tracker.score - initialScore >= SCORE_INCREMENT_XLARGE);
_("Ensure unknown client IDs result in exception.");
let unknownId = Utils.makeGUID();
let error;
try {
Clients.sendURIToClientForDisplay(uri, unknownId);
} catch (ex) {
error = ex;
}
do_check_eq(error.message.indexOf("Unknown remote client ID: "), 0);
run_next_test();
});
add_test(function test_receive_display_uri() {
_("Ensure processing of received 'displayURI' commands works.");
// We don't set up WBOs and perform syncing because other tests verify
// the command API works as advertised. This saves us a little work.
let uri = "http://www.mozilla.org/";
let remoteId = Utils.makeGUID();
let command = {
command: "displayURI",
args: [uri, remoteId],
};
Clients.localCommands = [command];
// Received 'displayURI' command should result in the topic defined below
// being called.
let ev = "weave:engine:clients:display-uri";
let handler = function(subject, data) {
Svc.Obs.remove(ev, handler);
do_check_eq(subject.uri, uri);
do_check_eq(subject.client, remoteId);
do_check_eq(data, null);
run_next_test();
};
Svc.Obs.add(ev, handler);
do_check_true(Clients.processIncomingCommands());
});
function run_test() {
initTestLogging("Trace");
Log4Moz.repository.getLogger("Sync.Engine.Clients").level = Log4Moz.Level.Trace;