Chromium Code Reviews| 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 bindings for the app API. | 5 // Custom binding for the app API. |
| 6 | 6 |
| 7 var appNatives = requireNative('app'); | 7 var appNatives = requireNative('app'); |
| 8 var chrome = requireNative('chrome').GetChrome(); | |
| 9 var GetAvailability = requireNative('v8_context').GetAvailability; | |
| 10 var DCHECK = requireNative('logging').DCHECK; | |
| 8 | 11 |
| 9 // This becomes chrome.app | 12 // This becomes chrome.app |
| 10 var app = { | 13 var app = { |
| 11 getIsInstalled: appNatives.GetIsInstalled, | 14 getIsInstalled: appNatives.GetIsInstalled, |
| 12 install: appNatives.Install, | 15 install: appNatives.Install, |
| 13 getDetails: appNatives.GetDetails, | 16 getDetails: appNatives.GetDetails, |
| 14 getDetailsForFrame: appNatives.GetDetailsForFrame, | 17 getDetailsForFrame: appNatives.GetDetailsForFrame, |
| 15 runningState: appNatives.GetRunningState | 18 runningState: appNatives.GetRunningState |
| 16 }; | 19 }; |
| 17 | 20 |
| 18 // Tricky; "getIsInstalled" is actually exposed as the getter "isInstalled", | 21 // Tricky; "getIsInstalled" is actually exposed as the getter "isInstalled", |
| 19 // but we don't have a way to express this in the schema JSON (nor is it | 22 // but we don't have a way to express this in the schema JSON (nor is it |
| 20 // worth it for this one special case). | 23 // worth it for this one special case). |
| 21 // | 24 // |
| 22 // So, define it manually, and let the getIsInstalled function act as its | 25 // So, define it manually, and let the getIsInstalled function act as its |
| 23 // documentation. | 26 // documentation. |
| 24 app.__defineGetter__('isInstalled', appNatives.GetIsInstalled); | 27 app.__defineGetter__('isInstalled', appNatives.GetIsInstalled); |
| 25 | 28 |
| 26 // Called by app_bindings.cc. | 29 // Called by app_binding.cc. |
| 27 // This becomes chromeHidden.app | 30 // This becomes chromeHidden.app |
| 28 var chromeHiddenApp = { | 31 var chromeHiddenApp = { |
| 29 onGetAppNotifyChannelResponse: function(channelId, error, callbackId) { | 32 onGetAppNotifyChannelResponse: function(channelId, error, callbackId) { |
| 30 if (callbackId) { | 33 if (callbackId) { |
| 31 callbacks[callbackId](channelId, error); | 34 callbacks[callbackId](channelId, error); |
| 32 delete callbacks[callbackId]; | 35 delete callbacks[callbackId]; |
| 33 } | 36 } |
| 34 }, | 37 }, |
| 35 | 38 |
| 36 onInstallStateResponse: function(state, callbackId) { | 39 onInstallStateResponse: function(state, callbackId) { |
| 37 if (callbackId) { | 40 if (callbackId) { |
| 38 callbacks[callbackId](state); | 41 callbacks[callbackId](state); |
| 39 delete callbacks[callbackId]; | 42 delete callbacks[callbackId]; |
| 40 } | 43 } |
| 41 } | 44 } |
| 42 }; | 45 }; |
| 43 | 46 |
| 44 // appNotification stuff. | 47 // appNotification stuff. |
| 45 // | 48 // |
| 46 // TODO(kalman): move this stuff to its own custom bindings. | 49 // TODO(kalman): move this stuff to its own custom binding. |
| 47 // It will be bit tricky since I'll need to look into why there are | 50 // It will be bit tricky since I'll need to look into why there are |
| 48 // permissions defined for app notifications, yet this always sets it up? | 51 // permissions defined for app notifications, yet this always sets it up? |
| 49 var callbacks = {}; | 52 var callbacks = {}; |
| 50 var nextCallbackId = 1; | 53 var nextCallbackId = 1; |
| 51 | 54 |
| 52 // This becomes chrome.appNotifications. | 55 // This becomes chrome.appNotifications. |
| 53 var appNotifications = { | 56 var appNotifications = { |
| 54 getChannel: function getChannel(clientId, callback) { | 57 getChannel: function getChannel(clientId, callback) { |
| 55 var callbackId = 0; | 58 var callbackId = 0; |
| 56 if (callback) { | 59 if (callback) { |
| 57 callbackId = nextCallbackId++; | 60 callbackId = nextCallbackId++; |
| 58 callbacks[callbackId] = callback; | 61 callbacks[callbackId] = callback; |
| 59 } | 62 } |
| 60 appNatives.GetAppNotifyChannel(clientId, callbackId); | 63 appNatives.GetAppNotifyChannel(clientId, callbackId); |
| 61 } | 64 } |
| 62 }; | 65 }; |
| 63 | 66 |
| 64 app.installState = function getInstallState(callback) { | 67 app.installState = function getInstallState(callback) { |
| 65 var callbackId = nextCallbackId++; | 68 var callbackId = nextCallbackId++; |
| 66 callbacks[callbackId] = callback; | 69 callbacks[callbackId] = callback; |
| 67 appNatives.GetInstallState(callbackId); | 70 appNatives.GetInstallState(callbackId); |
| 68 }; | 71 }; |
| 69 | 72 |
| 70 // These must match the names in InstallAppBindings() in | 73 function defineCheckAvailabilityGetter(name, apiName, target) { |
|
not at google - send to devlin
2013/02/13 01:45:49
defineLazyGetter?
Comment why it's necessary?
cduvall
2013/02/15 00:40:28
This actually doesn't need the lazy getter anymore
| |
| 74 exports.__defineGetter__(name, function() { | |
| 75 var availability = GetAvailability(apiName); | |
| 76 if (!availability.is_available) { | |
| 77 // If this API is not available, return undefined. An exception is not | |
| 78 // thrown like the other APIs because it is not guaranteed that this will | |
| 79 // be loaded in a try catch block. | |
| 80 return; | |
|
not at google - send to devlin
2013/02/13 01:45:49
might as well say return undefined;
cduvall
2013/02/15 00:40:28
Done.
| |
| 81 } | |
| 82 return target; | |
| 83 }); | |
| 84 } | |
| 85 | |
| 86 // These must match the names in InstallAppbinding() in | |
| 71 // chrome/renderer/extensions/dispatcher.cc. | 87 // chrome/renderer/extensions/dispatcher.cc. |
| 72 exports.chromeApp = app; | 88 defineCheckAvailabilityGetter('chromeApp', 'app', app); |
| 73 exports.chromeAppNotifications = appNotifications; | 89 defineCheckAvailabilityGetter('chromeAppNotifications', |
| 74 exports.chromeHiddenApp = chromeHiddenApp; | 90 'app', |
| 91 appNotifications); | |
| 92 defineCheckAvailabilityGetter('chromeHiddenApp', 'app', chromeHiddenApp); | |
| OLD | NEW |