| 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 runtime API. | 5 // Custom binding for the runtime API. |
| 6 | 6 |
| 7 var binding = apiBridge || require('binding').Binding.create('runtime'); | 7 var binding = apiBridge || require('binding').Binding.create('runtime'); |
| 8 | 8 |
| 9 var messaging = require('messaging'); | 9 var messaging = require('messaging'); |
| 10 var runtimeNatives = requireNative('runtime'); | 10 var runtimeNatives = requireNative('runtime'); |
| 11 var messagingNatives = requireNative('messaging_natives'); | 11 var messagingNatives = requireNative('messaging_natives'); |
| 12 var process = requireNative('process'); | 12 var process = requireNative('process'); |
| 13 var utils = require('utils'); | 13 var utils = require('utils'); |
| 14 | 14 var getBindDirectoryEntryCallback = |
| 15 var WINDOW = {}; | 15 require('fileEntryBindingUtil').getBindDirectoryEntryCallback; |
| 16 try { | |
| 17 WINDOW = window; | |
| 18 } catch (e) { | |
| 19 // Running in SW context. | |
| 20 // TODO(lazyboy): Synchronous access to background page is not possible from | |
| 21 // service worker context. Decide what we should do in this case for the class | |
| 22 // of APIs that require access to background page or window object | |
| 23 } | |
| 24 | |
| 25 var backgroundPage = WINDOW; | |
| 26 var backgroundRequire = require; | |
| 27 var contextType = process.GetContextType(); | |
| 28 | |
| 29 if (contextType == 'BLESSED_EXTENSION' || | |
| 30 contextType == 'UNBLESSED_EXTENSION') { | |
| 31 var manifest = runtimeNatives.GetManifest(); | |
| 32 if (manifest.app && manifest.app.background) { | |
| 33 // Get the background page if one exists. Otherwise, default to the current | |
| 34 // window. | |
| 35 backgroundPage = runtimeNatives.GetExtensionViews(-1, -1, 'BACKGROUND')[0]; | |
| 36 if (backgroundPage) { | |
| 37 var GetModuleSystem = requireNative('v8_context').GetModuleSystem; | |
| 38 backgroundRequire = GetModuleSystem(backgroundPage).require; | |
| 39 } else { | |
| 40 backgroundPage = WINDOW; | |
| 41 } | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 // For packaged apps, all windows use the bindFileEntryCallback from the | |
| 46 // background page so their FileEntry objects have the background page's context | |
| 47 // as their own. This allows them to be used from other windows (including the | |
| 48 // background page) after the original window is closed. | |
| 49 if (WINDOW == backgroundPage) { | |
| 50 var lastError = require('lastError'); | |
| 51 var fileSystemNatives = requireNative('file_system_natives'); | |
| 52 var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem; | |
| 53 var bindDirectoryEntryCallback = function(functionName, apiFunctions) { | |
| 54 apiFunctions.setCustomCallback(functionName, | |
| 55 function(name, request, callback, response) { | |
| 56 if (callback) { | |
| 57 if (!response) { | |
| 58 callback(); | |
| 59 return; | |
| 60 } | |
| 61 var fileSystemId = response.fileSystemId; | |
| 62 var baseName = response.baseName; | |
| 63 var fs = GetIsolatedFileSystem(fileSystemId); | |
| 64 | |
| 65 try { | |
| 66 fs.root.getDirectory(baseName, {}, callback, function(fileError) { | |
| 67 lastError.run('runtime.' + functionName, | |
| 68 'Error getting Entry, code: ' + fileError.code, | |
| 69 request.stack, | |
| 70 callback); | |
| 71 }); | |
| 72 } catch (e) { | |
| 73 lastError.run('runtime.' + functionName, | |
| 74 'Error: ' + e.stack, | |
| 75 request.stack, | |
| 76 callback); | |
| 77 } | |
| 78 } | |
| 79 }); | |
| 80 }; | |
| 81 } else { | |
| 82 // Force the runtime API to be loaded in the background page. Using | |
| 83 // backgroundPageModuleSystem.require('runtime') is insufficient as | |
| 84 // requireNative is only allowed while lazily loading an API. | |
| 85 backgroundPage.chrome.runtime; | |
| 86 var bindDirectoryEntryCallback = | |
| 87 backgroundRequire('runtime').bindDirectoryEntryCallback; | |
| 88 } | |
| 89 | 16 |
| 90 binding.registerCustomHook(function(binding, id, contextType) { | 17 binding.registerCustomHook(function(binding, id, contextType) { |
| 91 var apiFunctions = binding.apiFunctions; | 18 var apiFunctions = binding.apiFunctions; |
| 92 var runtime = binding.compiledApi; | 19 var runtime = binding.compiledApi; |
| 93 | 20 |
| 94 // | 21 // |
| 95 // Unprivileged APIs. | 22 // Unprivileged APIs. |
| 96 // | 23 // |
| 97 | 24 |
| 98 if (id != '') | 25 if (id != '') |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 | 104 |
| 178 apiFunctions.setCustomCallback('getBackgroundPage', | 105 apiFunctions.setCustomCallback('getBackgroundPage', |
| 179 function(name, request, callback, response) { | 106 function(name, request, callback, response) { |
| 180 if (callback) { | 107 if (callback) { |
| 181 var bg = | 108 var bg = |
| 182 runtimeNatives.GetExtensionViews(-1, -1, 'BACKGROUND')[0] || null; | 109 runtimeNatives.GetExtensionViews(-1, -1, 'BACKGROUND')[0] || null; |
| 183 callback(bg); | 110 callback(bg); |
| 184 } | 111 } |
| 185 }); | 112 }); |
| 186 | 113 |
| 187 bindDirectoryEntryCallback('getPackageDirectoryEntry', apiFunctions); | 114 apiFunctions.setCustomCallback('getPackageDirectoryEntry', |
| 115 getBindDirectoryEntryCallback()); |
| 188 }); | 116 }); |
| 189 | 117 |
| 190 exports.$set('bindDirectoryEntryCallback', bindDirectoryEntryCallback); | |
| 191 if (!apiBridge) | 118 if (!apiBridge) |
| 192 exports.$set('binding', binding.generate()); | 119 exports.$set('binding', binding.generate()); |
| OLD | NEW |