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 utils = require('utils'); | 12 var utils = require('utils'); |
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 | 27 |
18 if (contextType == 'BLESSED_EXTENSION' || | 28 if (contextType == 'BLESSED_EXTENSION' || |
19 contextType == 'UNBLESSED_EXTENSION') { | 29 contextType == 'UNBLESSED_EXTENSION') { |
20 var manifest = runtimeNatives.GetManifest(); | 30 var manifest = runtimeNatives.GetManifest(); |
21 if (manifest.app && manifest.app.background) { | 31 if (manifest.app && manifest.app.background) { |
22 // Get the background page if one exists. Otherwise, default to the current | 32 // Get the background page if one exists. Otherwise, default to the current |
23 // window. | 33 // window. |
24 backgroundPage = runtimeNatives.GetExtensionViews(-1, 'BACKGROUND')[0]; | 34 backgroundPage = runtimeNatives.GetExtensionViews(-1, 'BACKGROUND')[0]; |
25 if (backgroundPage) { | 35 if (backgroundPage) { |
26 var GetModuleSystem = requireNative('v8_context').GetModuleSystem; | 36 var GetModuleSystem = requireNative('v8_context').GetModuleSystem; |
27 backgroundRequire = GetModuleSystem(backgroundPage).require; | 37 backgroundRequire = GetModuleSystem(backgroundPage).require; |
28 } else { | 38 } else { |
29 backgroundPage = window; | 39 backgroundPage = WINDOW; |
30 } | 40 } |
31 } | 41 } |
32 } | 42 } |
33 | 43 |
34 // For packaged apps, all windows use the bindFileEntryCallback from the | 44 // For packaged apps, all windows use the bindFileEntryCallback from the |
35 // background page so their FileEntry objects have the background page's context | 45 // background page so their FileEntry objects have the background page's context |
36 // as their own. This allows them to be used from other windows (including the | 46 // as their own. This allows them to be used from other windows (including the |
37 // background page) after the original window is closed. | 47 // background page) after the original window is closed. |
38 if (window == backgroundPage) { | 48 if (WINDOW == backgroundPage) { |
39 var lastError = require('lastError'); | 49 var lastError = require('lastError'); |
40 var fileSystemNatives = requireNative('file_system_natives'); | 50 var fileSystemNatives = requireNative('file_system_natives'); |
41 var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem; | 51 var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem; |
42 var bindDirectoryEntryCallback = function(functionName, apiFunctions) { | 52 var bindDirectoryEntryCallback = function(functionName, apiFunctions) { |
43 apiFunctions.setCustomCallback(functionName, | 53 apiFunctions.setCustomCallback(functionName, |
44 function(name, request, callback, response) { | 54 function(name, request, callback, response) { |
45 if (callback) { | 55 if (callback) { |
46 if (!response) { | 56 if (!response) { |
47 callback(); | 57 callback(); |
48 return; | 58 return; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 var bg = runtimeNatives.GetExtensionViews(-1, 'BACKGROUND')[0] || null; | 180 var bg = runtimeNatives.GetExtensionViews(-1, 'BACKGROUND')[0] || null; |
171 callback(bg); | 181 callback(bg); |
172 } | 182 } |
173 }); | 183 }); |
174 | 184 |
175 bindDirectoryEntryCallback('getPackageDirectoryEntry', apiFunctions); | 185 bindDirectoryEntryCallback('getPackageDirectoryEntry', apiFunctions); |
176 }); | 186 }); |
177 | 187 |
178 exports.$set('bindDirectoryEntryCallback', bindDirectoryEntryCallback); | 188 exports.$set('bindDirectoryEntryCallback', bindDirectoryEntryCallback); |
179 exports.$set('binding', binding.generate()); | 189 exports.$set('binding', binding.generate()); |
OLD | NEW |