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 = require('binding').Binding.create('runtime'); | 7 var binding = 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 process = requireNative('process'); | 11 var process = requireNative('process'); |
12 var forEach = require('utils').forEach; | 12 var forEach = require('utils').forEach; |
13 | 13 |
14 var backgroundPage = window; | 14 var WINDOW = {}; |
| 15 try { |
| 16 WINDOW = window; |
| 17 } catch (e) { |
| 18 // Running in SW context. |
| 19 // TODO(lazyboy): Synchronous access to background page is not possible from |
| 20 // service worker context. Decide what we should do in this case for the class |
| 21 // of APIs that require access to background page or window object |
| 22 } |
| 23 |
| 24 var backgroundPage = WINDOW; |
15 var backgroundRequire = require; | 25 var backgroundRequire = require; |
16 var contextType = process.GetContextType(); | 26 var contextType = process.GetContextType(); |
17 if (contextType == 'BLESSED_EXTENSION' || | 27 if (contextType == 'BLESSED_EXTENSION' || |
18 contextType == 'UNBLESSED_EXTENSION') { | 28 contextType == 'UNBLESSED_EXTENSION') { |
19 var manifest = runtimeNatives.GetManifest(); | 29 var manifest = runtimeNatives.GetManifest(); |
20 if (manifest.app && manifest.app.background) { | 30 if (manifest.app && manifest.app.background) { |
21 // Get the background page if one exists. Otherwise, default to the current | 31 // Get the background page if one exists. Otherwise, default to the current |
22 // window. | 32 // window. |
23 backgroundPage = runtimeNatives.GetExtensionViews(-1, 'BACKGROUND')[0]; | 33 backgroundPage = runtimeNatives.GetExtensionViews(-1, 'BACKGROUND')[0]; |
24 if (backgroundPage) { | 34 if (backgroundPage) { |
25 var GetModuleSystem = requireNative('v8_context').GetModuleSystem; | 35 var GetModuleSystem = requireNative('v8_context').GetModuleSystem; |
26 backgroundRequire = GetModuleSystem(backgroundPage).require; | 36 backgroundRequire = GetModuleSystem(backgroundPage).require; |
27 } else { | 37 } else { |
28 backgroundPage = window; | 38 backgroundPage = WINDOW; |
29 } | 39 } |
30 } | 40 } |
31 } | 41 } |
32 | 42 |
33 // For packaged apps, all windows use the bindFileEntryCallback from the | 43 // For packaged apps, all windows use the bindFileEntryCallback from the |
34 // background page so their FileEntry objects have the background page's context | 44 // background page so their FileEntry objects have the background page's context |
35 // as their own. This allows them to be used from other windows (including the | 45 // as their own. This allows them to be used from other windows (including the |
36 // background page) after the original window is closed. | 46 // background page) after the original window is closed. |
37 if (window == backgroundPage) { | 47 if (WINDOW == backgroundPage) { |
38 var lastError = require('lastError'); | 48 var lastError = require('lastError'); |
39 var fileSystemNatives = requireNative('file_system_natives'); | 49 var fileSystemNatives = requireNative('file_system_natives'); |
40 var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem; | 50 var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem; |
41 var bindDirectoryEntryCallback = function(functionName, apiFunctions) { | 51 var bindDirectoryEntryCallback = function(functionName, apiFunctions) { |
42 apiFunctions.setCustomCallback(functionName, | 52 apiFunctions.setCustomCallback(functionName, |
43 function(name, request, callback, response) { | 53 function(name, request, callback, response) { |
44 if (callback) { | 54 if (callback) { |
45 if (!response) { | 55 if (!response) { |
46 callback(); | 56 callback(); |
47 return; | 57 return; |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 var bg = runtimeNatives.GetExtensionViews(-1, 'BACKGROUND')[0] || null; | 207 var bg = runtimeNatives.GetExtensionViews(-1, 'BACKGROUND')[0] || null; |
198 callback(bg); | 208 callback(bg); |
199 } | 209 } |
200 }); | 210 }); |
201 | 211 |
202 bindDirectoryEntryCallback('getPackageDirectoryEntry', apiFunctions); | 212 bindDirectoryEntryCallback('getPackageDirectoryEntry', apiFunctions); |
203 }); | 213 }); |
204 | 214 |
205 exports.$set('bindDirectoryEntryCallback', bindDirectoryEntryCallback); | 215 exports.$set('bindDirectoryEntryCallback', bindDirectoryEntryCallback); |
206 exports.$set('binding', binding.generate()); | 216 exports.$set('binding', binding.generate()); |
OLD | NEW |