OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 require('json_schema'); | 5 require('json_schema'); |
6 require('event_bindings'); | 6 require('event_bindings'); |
| 7 var chrome = requireNative('chrome').GetChrome(); |
| 8 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 9 var forEach = require('utils').forEach; |
| 10 var GetAvailability = requireNative('v8_context').GetAvailability; |
| 11 var logging = requireNative('logging'); |
| 12 var process = requireNative('process'); |
| 13 var contextType = process.GetContextType(); |
| 14 var extensionId = process.GetExtensionId(); |
| 15 var manifestVersion = process.GetManifestVersion(); |
7 var schemaRegistry = requireNative('schema_registry'); | 16 var schemaRegistry = requireNative('schema_registry'); |
| 17 var schemaUtils = require('schemaUtils'); |
8 var sendRequest = require('sendRequest').sendRequest; | 18 var sendRequest = require('sendRequest').sendRequest; |
9 var utils = require('utils'); | 19 var utils = require('utils'); |
10 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | |
11 var chrome = requireNative('chrome').GetChrome(); | |
12 var schemaUtils = require('schemaUtils'); | |
13 var process = requireNative('process'); | |
14 var manifestVersion = process.GetManifestVersion(); | |
15 var extensionId = process.GetExtensionId(); | |
16 var contextType = process.GetContextType(); | |
17 var GetAvailability = requireNative('v8_context').GetAvailability; | |
18 var logging = requireNative('logging'); | |
19 | 20 |
20 // Stores the name and definition of each API function, with methods to | 21 // Stores the name and definition of each API function, with methods to |
21 // modify their behaviour (such as a custom way to handle requests to the | 22 // modify their behaviour (such as a custom way to handle requests to the |
22 // API, a custom callback, etc). | 23 // API, a custom callback, etc). |
23 function APIFunctions() { | 24 function APIFunctions() { |
24 this.apiFunctions_ = {}; | 25 this.apiFunctions_ = {}; |
25 this.unavailableApiFunctions_ = {}; | 26 this.unavailableApiFunctions_ = {}; |
26 } | 27 } |
27 | 28 |
28 APIFunctions.prototype.register = function(apiName, apiFunction) { | 29 APIFunctions.prototype.register = function(apiName, apiFunction) { |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 // Registers a function |hook| to run after the schema for all APIs has been | 158 // Registers a function |hook| to run after the schema for all APIs has been |
158 // generated. The hook is passed as its first argument an "API" object to | 159 // generated. The hook is passed as its first argument an "API" object to |
159 // interact with, and second the current extension ID. See where | 160 // interact with, and second the current extension ID. See where |
160 // |customHooks| is used. | 161 // |customHooks| is used. |
161 registerCustomHook: function(fn) { | 162 registerCustomHook: function(fn) { |
162 this.customHooks_.push(fn); | 163 this.customHooks_.push(fn); |
163 }, | 164 }, |
164 | 165 |
165 // TODO(kalman/cduvall): Refactor this so |runHooks_| is not needed. | 166 // TODO(kalman/cduvall): Refactor this so |runHooks_| is not needed. |
166 runHooks_: function(api) { | 167 runHooks_: function(api) { |
167 this.customHooks_.forEach(function(hook) { | 168 forEach(this.customHooks_, function(i, hook) { |
168 if (!isSchemaNodeSupported(this.schema_, platform, manifestVersion)) | 169 if (!isSchemaNodeSupported(this.schema_, platform, manifestVersion)) |
169 return; | 170 return; |
170 | 171 |
171 if (!hook) | 172 if (!hook) |
172 return; | 173 return; |
173 | 174 |
174 hook({ | 175 hook({ |
175 apiFunctions: this.apiFunctions_, | 176 apiFunctions: this.apiFunctions_, |
176 schema: this.schema_, | 177 schema: this.schema_, |
177 compiledApi: api | 178 compiledApi: api |
(...skipping 23 matching lines...) Expand all Loading... |
201 var mod = {}; | 202 var mod = {}; |
202 | 203 |
203 var namespaces = schema.namespace.split('.'); | 204 var namespaces = schema.namespace.split('.'); |
204 for (var index = 0, name; name = namespaces[index]; index++) { | 205 for (var index = 0, name; name = namespaces[index]; index++) { |
205 mod[name] = mod[name] || {}; | 206 mod[name] = mod[name] || {}; |
206 mod = mod[name]; | 207 mod = mod[name]; |
207 } | 208 } |
208 | 209 |
209 // Add types to global schemaValidator | 210 // Add types to global schemaValidator |
210 if (schema.types) { | 211 if (schema.types) { |
211 schema.types.forEach(function(t) { | 212 forEach(schema.types, function(i, t) { |
212 if (!isSchemaNodeSupported(t, platform, manifestVersion)) | 213 if (!isSchemaNodeSupported(t, platform, manifestVersion)) |
213 return; | 214 return; |
214 | 215 |
215 schemaUtils.schemaValidator.addTypes(t); | 216 schemaUtils.schemaValidator.addTypes(t); |
216 if (t.type == 'object' && this.customTypes_[t.id]) { | 217 if (t.type == 'object' && this.customTypes_[t.id]) { |
217 var parts = t.id.split("."); | 218 var parts = t.id.split("."); |
218 this.customTypes_[t.id].prototype.setSchema(t); | 219 this.customTypes_[t.id].prototype.setSchema(t); |
219 mod[parts[parts.length - 1]] = this.customTypes_[t.id]; | 220 mod[parts[parts.length - 1]] = this.customTypes_[t.id]; |
220 } | 221 } |
221 }, this); | 222 }, this); |
(...skipping 13 matching lines...) Expand all Loading... |
235 function addUnprivilegedAccessGetter(mod, name) { | 236 function addUnprivilegedAccessGetter(mod, name) { |
236 mod.__defineGetter__(name, function() { | 237 mod.__defineGetter__(name, function() { |
237 throw new Error( | 238 throw new Error( |
238 '"' + name + '" can only be used in extension processes. See ' + | 239 '"' + name + '" can only be used in extension processes. See ' + |
239 'the content scripts documentation for more details.'); | 240 'the content scripts documentation for more details.'); |
240 }); | 241 }); |
241 } | 242 } |
242 | 243 |
243 // Setup Functions. | 244 // Setup Functions. |
244 if (schema.functions) { | 245 if (schema.functions) { |
245 schema.functions.forEach(function(functionDef) { | 246 forEach(schema.functions, function(i, functionDef) { |
246 if (functionDef.name in mod) { | 247 if (functionDef.name in mod) { |
247 throw new Error('Function ' + functionDef.name + | 248 throw new Error('Function ' + functionDef.name + |
248 ' already defined in ' + schema.namespace); | 249 ' already defined in ' + schema.namespace); |
249 } | 250 } |
250 | 251 |
251 if (!isSchemaNodeSupported(functionDef, platform, manifestVersion)) { | 252 if (!isSchemaNodeSupported(functionDef, platform, manifestVersion)) { |
252 this.apiFunctions_.registerUnavailable(functionDef.name); | 253 this.apiFunctions_.registerUnavailable(functionDef.name); |
253 return; | 254 return; |
254 } | 255 } |
255 if (!isSchemaAccessAllowed(functionDef)) { | 256 if (!isSchemaAccessAllowed(functionDef)) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 this.definition.returns) { | 303 this.definition.returns) { |
303 schemaUtils.validate([retval], [this.definition.returns]); | 304 schemaUtils.validate([retval], [this.definition.returns]); |
304 } | 305 } |
305 return retval; | 306 return retval; |
306 }).bind(apiFunction); | 307 }).bind(apiFunction); |
307 }, this); | 308 }, this); |
308 } | 309 } |
309 | 310 |
310 // Setup Events | 311 // Setup Events |
311 if (schema.events) { | 312 if (schema.events) { |
312 schema.events.forEach(function(eventDef) { | 313 forEach(schema.events, function(i, eventDef) { |
313 if (eventDef.name in mod) { | 314 if (eventDef.name in mod) { |
314 throw new Error('Event ' + eventDef.name + | 315 throw new Error('Event ' + eventDef.name + |
315 ' already defined in ' + schema.namespace); | 316 ' already defined in ' + schema.namespace); |
316 } | 317 } |
317 if (!isSchemaNodeSupported(eventDef, platform, manifestVersion)) | 318 if (!isSchemaNodeSupported(eventDef, platform, manifestVersion)) |
318 return; | 319 return; |
319 if (!isSchemaAccessAllowed(eventDef)) { | 320 if (!isSchemaAccessAllowed(eventDef)) { |
320 addUnprivilegedAccessGetter(mod, eventDef.name); | 321 addUnprivilegedAccessGetter(mod, eventDef.name); |
321 return; | 322 return; |
322 } | 323 } |
(...skipping 15 matching lines...) Expand all Loading... |
338 eventName, eventDef.parameters, options); | 339 eventName, eventDef.parameters, options); |
339 } | 340 } |
340 }, this); | 341 }, this); |
341 } | 342 } |
342 | 343 |
343 function addProperties(m, parentDef) { | 344 function addProperties(m, parentDef) { |
344 var properties = parentDef.properties; | 345 var properties = parentDef.properties; |
345 if (!properties) | 346 if (!properties) |
346 return; | 347 return; |
347 | 348 |
348 utils.forEach(properties, function(propertyName, propertyDef) { | 349 forEach(properties, function(propertyName, propertyDef) { |
349 if (propertyName in m) | 350 if (propertyName in m) |
350 return; // TODO(kalman): be strict like functions/events somehow. | 351 return; // TODO(kalman): be strict like functions/events somehow. |
351 if (!isSchemaNodeSupported(propertyDef, platform, manifestVersion)) | 352 if (!isSchemaNodeSupported(propertyDef, platform, manifestVersion)) |
352 return; | 353 return; |
353 if (!isSchemaAccessAllowed(propertyDef)) { | 354 if (!isSchemaAccessAllowed(propertyDef)) { |
354 addUnprivilegedAccessGetter(m, propertyName); | 355 addUnprivilegedAccessGetter(m, propertyName); |
355 return; | 356 return; |
356 } | 357 } |
357 | 358 |
358 var value = propertyDef.value; | 359 var value = propertyDef.value; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 }); | 399 }); |
399 }; | 400 }; |
400 | 401 |
401 addProperties(mod, schema); | 402 addProperties(mod, schema); |
402 this.runHooks_(mod); | 403 this.runHooks_(mod); |
403 return mod; | 404 return mod; |
404 } | 405 } |
405 }; | 406 }; |
406 | 407 |
407 exports.Binding = Binding; | 408 exports.Binding = Binding; |
OLD | NEW |