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

Side by Side Diff: extensions/renderer/resources/stash_client.js

Issue 2410743002: Remove the mojo serial interfaces and related infrastructure. (Closed)
Patch Set: Created 4 years, 2 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
OLDNEW
(Empty)
1 // Copyright 2015 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 define('stash_client', [
6 'async_waiter',
7 'content/public/renderer/frame_interfaces',
8 'extensions/common/mojo/stash.mojom',
9 'mojo/public/js/buffer',
10 'mojo/public/js/codec',
11 'mojo/public/js/core',
12 'mojo/public/js/router',
13 ], function(asyncWaiter, frameInterfaces, stashMojom, bufferModule,
14 codec, core, routerModule) {
15 /**
16 * @module stash_client
17 */
18
19 var service = new stashMojom.StashService.proxyClass(new routerModule.Router(
20 frameInterfaces.getInterface(stashMojom.StashService.name)));
21
22 /**
23 * A callback invoked to obtain objects to stash from a particular client.
24 * @callback module:stash_client.StashCallback
25 * @return {!Promise<!Array<!Object>>|!Array<!Object>} An array of objects to
26 * stash or a promise that will resolve to an array of objects to stash.
27 * The exact type of each object should match the type passed alongside
28 * this callback.
29 */
30
31 /**
32 * A stash client registration.
33 * @constructor
34 * @private
35 * @alias module:stash_client~Registration
36 */
37 function Registration(id, type, callback) {
38 /**
39 * The client id.
40 * @type {string}
41 * @private
42 */
43 this.id_ = id;
44
45 /**
46 * The type of the objects to be stashed.
47 * @type {!Object}
48 * @private
49 */
50 this.type_ = type;
51
52 /**
53 * The callback to invoke to obtain the objects to stash.
54 * @type {module:stash_client.StashCallback}
55 * @private
56 */
57 this.callback_ = callback;
58 }
59
60 /**
61 * Serializes and returns this client's stashable objects.
62 * @return
63 * {!Promise<!Array<module:extensions/common/stash.mojom.StashedObject>>} The
64 * serialized stashed objects.
65 */
66 Registration.prototype.serialize = function() {
67 return Promise.resolve(this.callback_()).then($Function.bind(
68 function(stashedObjects) {
69 if (!stashedObjects)
70 return [];
71 return $Array.map(stashedObjects, function(stashed) {
72 var builder = new codec.MessageBuilder(
73 0, codec.align(this.type_.encodedSize));
74 builder.encodeStruct(this.type_, stashed.serialization);
75 var encoded = builder.finish();
76 return new stashMojom.StashedObject({
77 id: this.id_,
78 data: new Uint8Array(encoded.buffer.arrayBuffer),
79 stashed_handles: encoded.handles,
80 monitor_handles: stashed.monitorHandles,
81 });
82 }, this);
83 }, this)).catch(function(e) { return []; });
84 };
85
86 /**
87 * The registered stash clients.
88 * @type {!Array<!Registration>}
89 */
90 var clients = [];
91
92 /**
93 * Registers a client to provide objects to stash during shut-down.
94 *
95 * @param {string} id The id of the client. This can be passed to retrieve to
96 * retrieve the stashed objects.
97 * @param {!Object} type The type of the objects that callback will return.
98 * @param {module:stash_client.StashCallback} callback The callback that
99 * returns objects to stash.
100 * @alias module:stash_client.registerClient
101 */
102 function registerClient(id, type, callback) {
103 clients.push(new Registration(id, type, callback));
104 }
105
106 var retrievedStash = service.retrieveStash().then(function(result) {
107 if (!result || !result.stash)
108 return {};
109 var stashById = {};
110 $Array.forEach(result.stash, function(stashed) {
111 if (!stashById[stashed.id])
112 stashById[stashed.id] = [];
113 stashById[stashed.id].push(stashed);
114 });
115 return stashById;
116 }, function() {
117 // If the stash is not available, act as if the stash was empty.
118 return {};
119 });
120
121 /**
122 * Retrieves the objects that were stashed with the given |id|, deserializing
123 * them into structs with type |type|.
124 *
125 * @param {string} id The id of the client. This should be unique to this
126 * client and should be passed as the id to registerClient().
127 * @param {!Object} type The mojo struct type that was serialized into the
128 * each stashed object.
129 * @return {!Promise<!Array<!Object>>} The stashed objects. The exact type of
130 * each object is that of the |type| parameter.
131 * @alias module:stash_client.retrieve
132 */
133 function retrieve(id, type) {
134 return retrievedStash.then(function(stash) {
135 var stashedObjects = stash[id];
136 if (!stashedObjects)
137 return Promise.resolve([]);
138
139 return Promise.all($Array.map(stashedObjects, function(stashed) {
140 var encodedData = new ArrayBuffer(stashed.data.length);
141 new Uint8Array(encodedData).set(stashed.data);
142 var reader = new codec.MessageReader(new codec.Message(
143 new bufferModule.Buffer(encodedData), stashed.stashed_handles));
144 var decoded = reader.decodeStruct(type);
145 return decoded;
146 }));
147 });
148 }
149
150 function saveStashForTesting() {
151 Promise.all($Array.map(clients, function(client) {
152 return client.serialize();
153 })).then(function(stashedObjects) {
154 var flattenedObjectsToStash = [];
155 $Array.forEach(stashedObjects, function(stashedObjects) {
156 flattenedObjectsToStash =
157 $Array.concat(flattenedObjectsToStash, stashedObjects);
158 });
159 service.addToStash(flattenedObjectsToStash);
160 });
161 }
162
163 return {
164 registerClient: registerClient,
165 retrieve: retrieve,
166 saveStashForTesting: saveStashForTesting,
167 };
168 });
OLDNEW
« no previous file with comments | « extensions/renderer/resources/serial_service.js ('k') | extensions/test/data/data_receiver_unittest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698