| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 extension API. | 5 // Custom binding for the extension API. |
| 6 | 6 |
| 7 var binding = require('binding').Binding.create('extension'); | 7 var binding = require('binding').Binding.create('extension'); |
| 8 | 8 |
| 9 var messaging = require('messaging'); | 9 var messaging = require('messaging'); |
| 10 var runtimeNatives = requireNative('runtime'); | 10 var runtimeNatives = requireNative('runtime'); |
| 11 var GetExtensionViews = runtimeNatives.GetExtensionViews; | 11 var GetExtensionViews = runtimeNatives.GetExtensionViews; |
| 12 var chrome = requireNative('chrome').GetChrome(); | 12 var chrome = requireNative('chrome').GetChrome(); |
| 13 | 13 |
| 14 var inIncognitoContext = requireNative('process').InIncognitoContext(); | 14 var inIncognitoContext = requireNative('process').InIncognitoContext(); |
| 15 var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled(); | 15 var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled(); |
| 16 var contextType = requireNative('process').GetContextType(); | 16 var contextType = requireNative('process').GetContextType(); |
| 17 var manifestVersion = requireNative('process').GetManifestVersion(); | 17 var manifestVersion = requireNative('process').GetManifestVersion(); |
| 18 | 18 |
| 19 // This should match chrome.windows.WINDOW_ID_NONE. | 19 // This should match chrome.windows.WINDOW_ID_NONE. |
| 20 // | 20 // |
| 21 // We can't use chrome.windows.WINDOW_ID_NONE directly because the | 21 // We can't use chrome.windows.WINDOW_ID_NONE directly because the |
| 22 // chrome.windows API won't exist unless this extension has permission for it; | 22 // chrome.windows API won't exist unless this extension has permission for it; |
| 23 // which may not be the case. | 23 // which may not be the case. |
| 24 var WINDOW_ID_NONE = -1; | 24 var WINDOW_ID_NONE = -1; |
| 25 var TAB_ID_NONE = -1; |
| 25 | 26 |
| 26 binding.registerCustomHook(function(bindingsAPI, extensionId) { | 27 binding.registerCustomHook(function(bindingsAPI, extensionId) { |
| 27 var extension = bindingsAPI.compiledApi; | 28 var extension = bindingsAPI.compiledApi; |
| 28 if (manifestVersion < 2) { | 29 if (manifestVersion < 2) { |
| 29 chrome.self = extension; | 30 chrome.self = extension; |
| 30 extension.inIncognitoTab = inIncognitoContext; | 31 extension.inIncognitoTab = inIncognitoContext; |
| 31 } | 32 } |
| 32 extension.inIncognitoContext = inIncognitoContext; | 33 extension.inIncognitoContext = inIncognitoContext; |
| 33 | 34 |
| 34 var apiFunctions = bindingsAPI.apiFunctions; | 35 var apiFunctions = bindingsAPI.apiFunctions; |
| 35 | 36 |
| 36 apiFunctions.setHandleRequest('getViews', function(properties) { | 37 apiFunctions.setHandleRequest('getViews', function(properties) { |
| 37 var windowId = WINDOW_ID_NONE; | 38 var windowId = WINDOW_ID_NONE; |
| 39 var tabId = TAB_ID_NONE; |
| 38 var type = 'ALL'; | 40 var type = 'ALL'; |
| 39 if (properties) { | 41 if (properties) { |
| 40 if (properties.type != null) { | 42 if (properties.type != null) { |
| 41 type = properties.type; | 43 type = properties.type; |
| 42 } | 44 } |
| 43 if (properties.windowId != null) { | 45 if (properties.windowId != null) { |
| 44 windowId = properties.windowId; | 46 windowId = properties.windowId; |
| 45 } | 47 } |
| 48 if (properties.tabId != null) { |
| 49 tabId = properties.tabId; |
| 50 } |
| 46 } | 51 } |
| 47 return GetExtensionViews(windowId, type); | 52 return GetExtensionViews(windowId, type, tabId); |
| 48 }); | 53 }); |
| 49 | 54 |
| 50 apiFunctions.setHandleRequest('getBackgroundPage', function() { | 55 apiFunctions.setHandleRequest('getBackgroundPage', function() { |
| 51 return GetExtensionViews(-1, 'BACKGROUND')[0] || null; | 56 return GetExtensionViews(-1, 'BACKGROUND', -1)[0] || null; |
| 52 }); | 57 }); |
| 53 | 58 |
| 54 apiFunctions.setHandleRequest('getExtensionTabs', function(windowId) { | 59 apiFunctions.setHandleRequest('getExtensionTabs', function(windowId) { |
| 55 if (windowId == null) | 60 if (windowId == null) |
| 56 windowId = WINDOW_ID_NONE; | 61 windowId = WINDOW_ID_NONE; |
| 57 return GetExtensionViews(windowId, 'TAB'); | 62 return GetExtensionViews(windowId, 'TAB', -1); |
| 58 }); | 63 }); |
| 59 | 64 |
| 60 apiFunctions.setHandleRequest('getURL', function(path) { | 65 apiFunctions.setHandleRequest('getURL', function(path) { |
| 61 path = String(path); | 66 path = String(path); |
| 62 if (!path.length || path[0] != '/') | 67 if (!path.length || path[0] != '/') |
| 63 path = '/' + path; | 68 path = '/' + path; |
| 64 return 'chrome-extension://' + extensionId + path; | 69 return 'chrome-extension://' + extensionId + path; |
| 65 }); | 70 }); |
| 66 | 71 |
| 67 // Alias several messaging deprecated APIs to their runtime counterparts. | 72 // Alias several messaging deprecated APIs to their runtime counterparts. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 }; | 107 }; |
| 103 if (contextType == 'BLESSED_EXTENSION') { | 108 if (contextType == 'BLESSED_EXTENSION') { |
| 104 extension.onRequestExternal.addListener = function() { | 109 extension.onRequestExternal.addListener = function() { |
| 105 throw new Error(sendRequestIsDisabled); | 110 throw new Error(sendRequestIsDisabled); |
| 106 }; | 111 }; |
| 107 } | 112 } |
| 108 } | 113 } |
| 109 }); | 114 }); |
| 110 | 115 |
| 111 exports.$set('binding', binding.generate()); | 116 exports.$set('binding', binding.generate()); |
| OLD | NEW |