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
3 // found in the LICENSE file.
5 // Custom binding for the app API.
7 var GetAvailability = requireNative('v8_context').GetAvailability;
8 if (!GetAvailability('app').is_available) {
10 exports.onInstallStateResponse = function(){};
14 var appNatives = requireNative('app');
15 var process = requireNative('process');
16 var extensionId = process.GetExtensionId();
17 var logActivity = requireNative('activityLogger');
19 function wrapForLogging(fun) {
21 return fun; // nothing interesting to log without an extension
24 // TODO(ataly): We need to make sure we use the right prototype for
25 // fun.apply. Array slice can either be rewritten or similarly defined.
26 logActivity.LogAPICall(extensionId, "app." + fun.name,
27 $Array.slice(arguments));
28 return $Function.apply(fun, this, arguments);
32 // This becomes chrome.app
34 getIsInstalled: wrapForLogging(appNatives.GetIsInstalled),
35 getDetails: wrapForLogging(appNatives.GetDetails),
36 getDetailsForFrame: wrapForLogging(appNatives.GetDetailsForFrame),
37 runningState: wrapForLogging(appNatives.GetRunningState)
40 // Tricky; "getIsInstalled" is actually exposed as the getter "isInstalled",
41 // but we don't have a way to express this in the schema JSON (nor is it
42 // worth it for this one special case).
44 // So, define it manually, and let the getIsInstalled function act as its
46 app.__defineGetter__('isInstalled', wrapForLogging(appNatives.GetIsInstalled));
48 // Called by app_bindings.cc.
49 function onInstallStateResponse(state, callbackId) {
50 var callback = callbacks[callbackId];
51 delete callbacks[callbackId];
52 if (typeof(callback) == 'function') {
56 console.error('Exception in chrome.app.installState response handler: ' +
62 // TODO(kalman): move this stuff to its own custom bindings.
64 var nextCallbackId = 1;
66 app.installState = function getInstallState(callback) {
67 var callbackId = nextCallbackId++;
68 callbacks[callbackId] = callback;
69 appNatives.GetInstallState(callbackId);
72 app.installState = wrapForLogging(app.installState);
74 exports.binding = app;
75 exports.onInstallStateResponse = onInstallStateResponse;