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 runtime API. | 5 // Custom binding for the runtime API. |
6 | 6 |
7 var binding = require('binding').Binding.create('runtime'); | 7 var binding = require('binding').Binding.create('runtime'); |
8 | 8 |
9 var extensionNatives = requireNative('extension'); | 9 var extensionNatives = requireNative('extension'); |
10 var miscBindings = require('miscellaneous_bindings'); | 10 var miscBindings = require('miscellaneous_bindings'); |
11 var runtimeNatives = requireNative('runtime'); | 11 var runtimeNatives = requireNative('runtime'); |
12 var unloadEvent = require('unload_event'); | 12 var unloadEvent = require('unload_event'); |
| 13 var process = requireNative('process'); |
| 14 |
| 15 var backgroundPage = window; |
| 16 var backgroundRequire = require; |
| 17 var contextType = process.GetContextType(); |
| 18 if (contextType == 'BLESSED_EXTENSION' || |
| 19 contextType == 'UNBLESSED_EXTENSION') { |
| 20 var manifest = runtimeNatives.GetManifest(); |
| 21 if (manifest.app && manifest.app.background) { |
| 22 backgroundPage = extensionNatives.GetExtensionViews(-1, 'BACKGROUND')[0]; |
| 23 var GetModuleSystem = requireNative('v8_context').GetModuleSystem; |
| 24 backgroundRequire = GetModuleSystem(backgroundPage).require; |
| 25 } |
| 26 } |
| 27 |
| 28 // For packaged apps, all windows use the bindFileEntryCallback from the |
| 29 // background page so their FileEntry objects have the background page's context |
| 30 // as their own. This allows them to be used from other windows (including the |
| 31 // background page) after the original window is closed. |
| 32 if (window == backgroundPage) { |
| 33 var lastError = require('lastError'); |
| 34 var fileSystemNatives = requireNative('file_system_natives'); |
| 35 var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem; |
| 36 var bindDirectoryEntryCallback = function(functionName, apiFunctions) { |
| 37 apiFunctions.setCustomCallback(functionName, |
| 38 function(name, request, response) { |
| 39 if (request.callback && response) { |
| 40 var callback = request.callback; |
| 41 request.callback = null; |
| 42 |
| 43 var fileSystemId = response.fileSystemId; |
| 44 var baseName = response.baseName; |
| 45 var fs = GetIsolatedFileSystem(fileSystemId); |
| 46 |
| 47 try { |
| 48 fs.root.getDirectory(baseName, {}, callback, function(fileError) { |
| 49 lastError.run('runtime.' + functionName, |
| 50 'Error getting Entry, code: ' + fileError.code, |
| 51 request.stack, |
| 52 callback); |
| 53 }); |
| 54 } catch (e) { |
| 55 lastError.run('runtime.' + functionName, |
| 56 'Error: ' + e.stack, |
| 57 request.stack, |
| 58 callback); |
| 59 } |
| 60 } |
| 61 }); |
| 62 }; |
| 63 } else { |
| 64 // Force the runtime API to be loaded in the background page. Using |
| 65 // backgroundPageModuleSystem.require('runtime') is insufficient as |
| 66 // requireNative is only allowed while lazily loading an API. |
| 67 backgroundPage.runtime; |
| 68 var bindDirectoryEntryCallback = backgroundRequire( |
| 69 'runtime').bindDirectoryEntryCallback; |
| 70 } |
13 | 71 |
14 binding.registerCustomHook(function(binding, id, contextType) { | 72 binding.registerCustomHook(function(binding, id, contextType) { |
15 var apiFunctions = binding.apiFunctions; | 73 var apiFunctions = binding.apiFunctions; |
16 var runtime = binding.compiledApi; | 74 var runtime = binding.compiledApi; |
17 | 75 |
18 // | 76 // |
19 // Unprivileged APIs. | 77 // Unprivileged APIs. |
20 // | 78 // |
21 | 79 |
22 runtime.id = id; | 80 runtime.id = id; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 176 |
119 apiFunctions.setCustomCallback('getBackgroundPage', | 177 apiFunctions.setCustomCallback('getBackgroundPage', |
120 function(name, request, response) { | 178 function(name, request, response) { |
121 if (request.callback) { | 179 if (request.callback) { |
122 var bg = extensionNatives.GetExtensionViews(-1, 'BACKGROUND')[0] || null; | 180 var bg = extensionNatives.GetExtensionViews(-1, 'BACKGROUND')[0] || null; |
123 request.callback(bg); | 181 request.callback(bg); |
124 } | 182 } |
125 request.callback = null; | 183 request.callback = null; |
126 }); | 184 }); |
127 | 185 |
| 186 bindDirectoryEntryCallback('getPackageDirectoryEntry', apiFunctions); |
128 }); | 187 }); |
129 | 188 |
| 189 exports.bindDirectoryEntryCallback = bindDirectoryEntryCallback; |
130 exports.binding = binding.generate(); | 190 exports.binding = binding.generate(); |
OLD | NEW |