Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(947)

Side by Side Diff: chrome/test/data/extensions/api_test/messaging/externally_connectable/sites/assertions.js

Issue 16174005: Implement externally_connectable! Web pages can now communicate directly with (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: no find copies Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 window.assertions = (function() {
6
7 // Codes for test results. Must match ExternallyConnectableMessagingTest::Result
8 // in c/b/extensions/extension_messages_apitest.cc.
9 var results = {
10 OK: 0,
11 NAMESPACE_NOT_DEFINED: 1,
12 FUNCTION_NOT_DEFINED: 2,
13 COULD_NOT_ESTABLISH_CONNECTION_ERROR: 3,
14 OTHER_ERROR: 4,
15 INCORRECT_RESPONSE: 5
16 };
17
18 return {
19 canConnectAndSendMessages: function(extensionId) {
20 if (!chrome.runtime) {
21 reply(results.NAMESPACE_NOT_DEFINED);
22 return;
23 }
24
25 if (!chrome.runtime.sendMessage || !chrome.runtime.connect) {
26 reply(results.FUNCTION_NOT_DEFINED);
27 return;
28 }
29
30 // Make the messages sent vaguely complex, but unambiguously JSON-ifiable.
31 var message = [{'a': {'b': 10}}, 20, 'c\x10\x11'];
32
33 function checkLastError(reply) {
34 if (!chrome.runtime.lastError)
35 return true;
36 var kCouldNotEstablishConnection =
37 'Could not establish connection. Receiving end does not exist.';
38 if (chrome.runtime.lastError.message == kCouldNotEstablishConnection)
39 reply(results.COULD_NOT_ESTABLISH_CONNECTION_ERROR);
40 else
41 reply(results.OTHER_ERROR);
42 return false;
43 }
44
45 function checkResponse(response, reply) {
46 var responseJson = JSON.stringify(response);
47 var messageJson = JSON.stringify(message);
48 if (responseJson == messageJson)
49 return true;
50 console.warn('Expected ' + messageJson + ' got ' + responseJson);
51 reply(results.INCORRECT_RESPONSE);
52 return false;
53 }
54
55 function canSendMessage(reply) {
56 chrome.runtime.sendMessage(extensionId, message, function(response) {
57 if (checkLastError(reply) && checkResponse(response, reply))
58 reply(results.OK);
59 });
60 }
61
62 function canConnectAndSendMessages(reply) {
63 var port = chrome.runtime.connect(extensionId);
64 port.postMessage(message, function() {
65 checkLastError(reply);
66 });
67 port.postMessage(message, function() {
68 checkLastError(reply);
69 });
70 var pendingResponses = 2;
71 port.onMessage.addListener(function(response) {
Jeffrey Yasskin 2013/06/08 00:28:09 Do you need to add this listener before posting yo
not at google - send to devlin 2013/06/08 02:02:33 JS is defined by its single threadedness.
72 pendingResponses--;
73 if (checkLastError(reply) &&
Jeffrey Yasskin 2013/06/08 00:28:09 If the first response produces a lastError, but th
not at google - send to devlin 2013/06/08 02:02:33 Done.
74 checkResponse(response, reply) &&
75 pendingResponses == 0) {
76 reply(results.OK);
77 }
78 });
79 }
80
81 var done = domAutomationController.send.bind(domAutomationController);
82 canSendMessage(function(result) {
83 if (result != results.OK)
84 done(result);
85 else
86 canConnectAndSendMessages(done);
87 });
88 },
89
90 isDefined: function(name) {
91 var result = results.OK;
92 if (!chrome.runtime)
93 result = results.NAMESPACE_NOT_DEFINED;
94 else if (!chrome.runtime[name])
95 result = results.FUNCTION_NOT_DEFINED;
96 domAutomationController.send(result);
97 }
98 };
99
100 }()); // window.assertions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698