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 // Custom binding for the app API. | 5 // Custom binding for the app API. |
6 | 6 |
7 var GetAvailability = requireNative('v8_context').GetAvailability; | 7 var GetAvailability = requireNative('v8_context').GetAvailability; |
8 if (!GetAvailability('app').is_available) { | 8 if (!GetAvailability('app').is_available) { |
9 exports.chromeApp = {}; | 9 exports.chromeApp = {}; |
10 exports.chromeHiddenApp = {}; | 10 exports.chromeHiddenApp = {}; |
11 return; | 11 return; |
12 } | 12 } |
13 | 13 |
14 var appNatives = requireNative('app'); | 14 var appNatives = requireNative('app'); |
15 var chrome = requireNative('chrome').GetChrome(); | 15 var chrome = requireNative('chrome').GetChrome(); |
| 16 var process = requireNative('process'); |
| 17 var extensionId = process.GetExtensionId(); |
| 18 var logActivity = requireNative('activityLogger'); |
| 19 |
| 20 function wrapForLogging(fun) { |
| 21 var id = extensionId; |
| 22 return (function() { |
| 23 // TODO(ataly): We need to make sure we use the right prototype for |
| 24 // fun.apply. Array slice can either be rewritten or similarly defined. |
| 25 logActivity.LogAPICall(id, "app." + fun.name, |
| 26 Array.prototype.slice.call(arguments)); |
| 27 return fun.apply(this, arguments); |
| 28 }); |
| 29 } |
16 | 30 |
17 // This becomes chrome.app | 31 // This becomes chrome.app |
18 var app = { | 32 var app; |
19 getIsInstalled: appNatives.GetIsInstalled, | 33 if (!extensionId) { |
20 install: appNatives.Install, | 34 app = { |
21 getDetails: appNatives.GetDetails, | 35 getIsInstalled: appNatives.GetIsInstalled, |
22 getDetailsForFrame: appNatives.GetDetailsForFrame, | 36 install: appNatives.Install, |
23 runningState: appNatives.GetRunningState | 37 getDetails: appNatives.GetDetails, |
24 }; | 38 getDetailsForFrame: appNatives.GetDetailsForFrame, |
| 39 runningState: appNatives.GetRunningState |
| 40 }; |
| 41 } else { |
| 42 app = { |
| 43 getIsInstalled: wrapForLogging(appNatives.GetIsInstalled), |
| 44 install: wrapForLogging(appNatives.Install), |
| 45 getDetails: wrapForLogging(appNatives.GetDetails), |
| 46 getDetailsForFrame: wrapForLogging(appNatives.GetDetailsForFrame), |
| 47 runningState: wrapForLogging(appNatives.GetRunningState) |
| 48 }; |
| 49 } |
25 | 50 |
26 // Tricky; "getIsInstalled" is actually exposed as the getter "isInstalled", | 51 // Tricky; "getIsInstalled" is actually exposed as the getter "isInstalled", |
27 // but we don't have a way to express this in the schema JSON (nor is it | 52 // but we don't have a way to express this in the schema JSON (nor is it |
28 // worth it for this one special case). | 53 // worth it for this one special case). |
29 // | 54 // |
30 // So, define it manually, and let the getIsInstalled function act as its | 55 // So, define it manually, and let the getIsInstalled function act as its |
31 // documentation. | 56 // documentation. |
32 app.__defineGetter__('isInstalled', appNatives.GetIsInstalled); | 57 if (!extensionId) |
| 58 app.__defineGetter__('isInstalled', appNatives.GetIsInstalled); |
| 59 else |
| 60 app.__defineGetter__('isInstalled', |
| 61 wrapForLogging(appNatives.GetIsInstalled)); |
33 | 62 |
34 // Called by app_bindings.cc. | 63 // Called by app_bindings.cc. |
35 // This becomes chromeHidden.app | 64 // This becomes chromeHidden.app |
36 var chromeHiddenApp = { | 65 var chromeHiddenApp = { |
37 onInstallStateResponse: function(state, callbackId) { | 66 onInstallStateResponse: function(state, callbackId) { |
38 if (callbackId) { | 67 if (callbackId) { |
39 callbacks[callbackId](state); | 68 callbacks[callbackId](state); |
40 delete callbacks[callbackId]; | 69 delete callbacks[callbackId]; |
41 } | 70 } |
42 } | 71 } |
43 }; | 72 }; |
44 | 73 |
45 // TODO(kalman): move this stuff to its own custom bindings. | 74 // TODO(kalman): move this stuff to its own custom bindings. |
46 var callbacks = {}; | 75 var callbacks = {}; |
47 var nextCallbackId = 1; | 76 var nextCallbackId = 1; |
48 | 77 |
49 app.installState = function getInstallState(callback) { | 78 app.installState = function getInstallState(callback) { |
50 var callbackId = nextCallbackId++; | 79 var callbackId = nextCallbackId++; |
51 callbacks[callbackId] = callback; | 80 callbacks[callbackId] = callback; |
52 appNatives.GetInstallState(callbackId); | 81 appNatives.GetInstallState(callbackId); |
53 }; | 82 }; |
| 83 if (extensionId) |
| 84 app.installState = wrapForLogging(app.installState); |
54 | 85 |
55 // These must match the names in InstallAppbinding() in | 86 // These must match the names in InstallAppbinding() in |
56 // chrome/renderer/extensions/dispatcher.cc. | 87 // chrome/renderer/extensions/dispatcher.cc. |
57 exports.chromeApp = app; | 88 exports.chromeApp = app; |
58 exports.chromeHiddenApp = chromeHiddenApp; | 89 exports.chromeHiddenApp = chromeHiddenApp; |
OLD | NEW |