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').LogActivity; | |
19 | |
20 function wrapForLogging(fun) { | |
21 var id = extensionId; | |
22 var slicer = Array.prototype.slice; | |
23 return (function() { | |
24 logActivity("API", id, "app." + fun.name, slicer.call(arguments)); | |
25 return fun.apply(this, arguments); | |
26 }); | |
27 } | |
16 | 28 |
17 // This becomes chrome.app | 29 // This becomes chrome.app |
18 var app = { | 30 if (!extensionId) { |
Matt Perry
2013/04/09 20:56:59
When can the extensionId be NULL?
felt
2013/04/10 00:21:38
If this is a website, extensionId is null and we d
| |
19 getIsInstalled: appNatives.GetIsInstalled, | 31 var app = { |
Matt Perry
2013/04/09 20:56:59
I think you need to declare "var app;" at toplevel
felt
2013/04/10 00:21:38
It works fine actually. But maybe it's more clear
| |
20 install: appNatives.Install, | 32 getIsInstalled: appNatives.GetIsInstalled, |
21 getDetails: appNatives.GetDetails, | 33 install: appNatives.Install, |
22 getDetailsForFrame: appNatives.GetDetailsForFrame, | 34 getDetails: appNatives.GetDetails, |
23 runningState: appNatives.GetRunningState | 35 getDetailsForFrame: appNatives.GetDetailsForFrame, |
24 }; | 36 runningState: appNatives.GetRunningState |
37 }; | |
38 } else { | |
39 var app = { | |
40 getIsInstalled: wrapForLogging(appNatives.GetIsInstalled), | |
41 install: wrapForLogging(appNatives.Install), | |
42 getDetails: wrapForLogging(appNatives.GetDetails), | |
43 getDetailsForFrame: wrapForLogging(appNatives.GetDetailsForFrame), | |
44 runningState: wrapForLogging(appNatives.GetRunningState) | |
45 }; | |
46 } | |
25 | 47 |
26 // Tricky; "getIsInstalled" is actually exposed as the getter "isInstalled", | 48 // 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 | 49 // 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). | 50 // worth it for this one special case). |
29 // | 51 // |
30 // So, define it manually, and let the getIsInstalled function act as its | 52 // So, define it manually, and let the getIsInstalled function act as its |
31 // documentation. | 53 // documentation. |
32 app.__defineGetter__('isInstalled', appNatives.GetIsInstalled); | 54 if (!extensionId) |
55 app.__defineGetter__('isInstalled', appNatives.GetIsInstalled); | |
56 else | |
57 app.__defineGetter__('isInstalled', | |
58 wrapForLogging(appNatives.GetIsInstalled)); | |
33 | 59 |
34 // Called by app_bindings.cc. | 60 // Called by app_bindings.cc. |
35 // This becomes chromeHidden.app | 61 // This becomes chromeHidden.app |
36 var chromeHiddenApp = { | 62 var chromeHiddenApp = { |
37 onInstallStateResponse: function(state, callbackId) { | 63 onInstallStateResponse: function(state, callbackId) { |
38 if (callbackId) { | 64 if (callbackId) { |
39 callbacks[callbackId](state); | 65 callbacks[callbackId](state); |
40 delete callbacks[callbackId]; | 66 delete callbacks[callbackId]; |
41 } | 67 } |
42 } | 68 } |
43 }; | 69 }; |
44 | 70 |
45 // TODO(kalman): move this stuff to its own custom bindings. | 71 // TODO(kalman): move this stuff to its own custom bindings. |
46 var callbacks = {}; | 72 var callbacks = {}; |
47 var nextCallbackId = 1; | 73 var nextCallbackId = 1; |
48 | 74 |
49 app.installState = function getInstallState(callback) { | 75 app.installState = function getInstallState(callback) { |
50 var callbackId = nextCallbackId++; | 76 var callbackId = nextCallbackId++; |
51 callbacks[callbackId] = callback; | 77 callbacks[callbackId] = callback; |
52 appNatives.GetInstallState(callbackId); | 78 appNatives.GetInstallState(callbackId); |
53 }; | 79 }; |
80 if (extensionId) | |
81 app.installState = wrapForLogging(app.installState); | |
54 | 82 |
55 // These must match the names in InstallAppbinding() in | 83 // These must match the names in InstallAppbinding() in |
56 // chrome/renderer/extensions/dispatcher.cc. | 84 // chrome/renderer/extensions/dispatcher.cc. |
57 exports.chromeApp = app; | 85 exports.chromeApp = app; |
58 exports.chromeHiddenApp = chromeHiddenApp; | 86 exports.chromeHiddenApp = chromeHiddenApp; |
OLD | NEW |