| 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 var fileSystemNatives = requireNative('file_system_natives'); | 5 var fileSystemNatives = requireNative('file_system_natives'); |
| 6 var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem; | 6 var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem; |
| 7 var lastError = require('lastError'); | 7 var lastError = require('lastError'); |
| 8 var GetModuleSystem = requireNative('v8_context').GetModuleSystem; | 8 var GetModuleSystem = requireNative('v8_context').GetModuleSystem; |
| 9 // TODO(sammc): Don't require extension. See http://crbug.com/235689. | 9 // TODO(sammc): Don't require extension. See http://crbug.com/235689. |
| 10 var GetExtensionViews = requireNative('runtime').GetExtensionViews; | 10 var GetExtensionViews = requireNative('runtime').GetExtensionViews; |
| 11 var safeCallbackApply = require('uncaught_exception_handler').safeCallbackApply; | 11 var safeCallbackApply = require('uncaught_exception_handler').safeCallbackApply; |
| 12 | 12 |
| 13 var WINDOW = {}; |
| 14 try { |
| 15 WINDOW = window; |
| 16 } catch (e) { |
| 17 // Running in SW context. |
| 18 // TODO(lazyboy): Synchronous access to background page is not possible from |
| 19 // service worker context. Decide what we should do in this case for the class |
| 20 // of APIs that require access to background page or window object |
| 21 } |
| 22 |
| 13 // For a given |apiName|, generates object with two elements that are used | 23 // For a given |apiName|, generates object with two elements that are used |
| 14 // in file system relayed APIs: | 24 // in file system relayed APIs: |
| 15 // * 'bindFileEntryCallback' function that provides mapping between JS objects | 25 // * 'bindFileEntryCallback' function that provides mapping between JS objects |
| 16 // into actual FileEntry|DirectoryEntry objects. | 26 // into actual FileEntry|DirectoryEntry objects. |
| 17 // * 'entryIdManager' object that implements methods for keeping the tracks of | 27 // * 'entryIdManager' object that implements methods for keeping the tracks of |
| 18 // previously saved file entries. | 28 // previously saved file entries. |
| 19 function getFileBindingsForApi(apiName) { | 29 function getFileBindingsForApi(apiName) { |
| 20 // Fallback to using the current window if no background page is running. | 30 // Fallback to using the current window if no background page is running. |
| 21 var backgroundPage = GetExtensionViews(-1, -1, 'BACKGROUND')[0] || window; | 31 var backgroundPage = GetExtensionViews(-1, -1, 'BACKGROUND')[0] || WINDOW; |
| 22 var backgroundPageModuleSystem = GetModuleSystem(backgroundPage); | 32 var backgroundPageModuleSystem = GetModuleSystem(backgroundPage); |
| 23 | 33 |
| 24 // All windows use the bindFileEntryCallback from the background page so their | 34 // All windows use the bindFileEntryCallback from the background page so their |
| 25 // FileEntry objects have the background page's context as their own. This | 35 // FileEntry objects have the background page's context as their own. This |
| 26 // allows them to be used from other windows (including the background page) | 36 // allows them to be used from other windows (including the background page) |
| 27 // after the original window is closed. | 37 // after the original window is closed. |
| 28 if (window == backgroundPage) { | 38 if (WINDOW == backgroundPage) { |
| 29 var bindFileEntryCallback = function(functionName, apiFunctions) { | 39 var bindFileEntryCallback = function(functionName, apiFunctions) { |
| 30 apiFunctions.setCustomCallback(functionName, | 40 apiFunctions.setCustomCallback(functionName, |
| 31 function(name, request, callback, response) { | 41 function(name, request, callback, response) { |
| 32 if (callback) { | 42 if (callback) { |
| 33 if (!response) { | 43 if (!response) { |
| 34 callback(); | 44 callback(); |
| 35 return; | 45 return; |
| 36 } | 46 } |
| 37 | 47 |
| 38 var entries = []; | 48 var entries = []; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 backgroundPage.chrome.fileSystem; | 121 backgroundPage.chrome.fileSystem; |
| 112 var bindFileEntryCallback = | 122 var bindFileEntryCallback = |
| 113 backgroundPageModuleSystem.require('fileEntryBindingUtil') | 123 backgroundPageModuleSystem.require('fileEntryBindingUtil') |
| 114 .getFileBindingsForApi(apiName).bindFileEntryCallback; | 124 .getFileBindingsForApi(apiName).bindFileEntryCallback; |
| 115 var entryIdManager = backgroundPageModuleSystem.require('entryIdManager'); | 125 var entryIdManager = backgroundPageModuleSystem.require('entryIdManager'); |
| 116 } | 126 } |
| 117 return {bindFileEntryCallback: bindFileEntryCallback, | 127 return {bindFileEntryCallback: bindFileEntryCallback, |
| 118 entryIdManager: entryIdManager}; | 128 entryIdManager: entryIdManager}; |
| 119 } | 129 } |
| 120 | 130 |
| 131 function getBindDirectoryEntryCallback() { |
| 132 // Get the background page if one exists. Otherwise, default to the current |
| 133 // window. |
| 134 var backgroundPage = GetExtensionViews(-1, -1, 'BACKGROUND')[0] || WINDOW; |
| 135 |
| 136 // For packaged apps, all windows use the bindFileEntryCallback from the |
| 137 // background page so their FileEntry objects have the background page's |
| 138 // context as their own. This allows them to be used from other windows |
| 139 // (including the background page) after the original window is closed. |
| 140 if (WINDOW == backgroundPage) { |
| 141 return function(name, request, callback, response) { |
| 142 if (callback) { |
| 143 if (!response) { |
| 144 callback(); |
| 145 return; |
| 146 } |
| 147 var fileSystemId = response.fileSystemId; |
| 148 var baseName = response.baseName; |
| 149 var fs = GetIsolatedFileSystem(fileSystemId); |
| 150 |
| 151 try { |
| 152 fs.root.getDirectory(baseName, {}, callback, function(fileError) { |
| 153 lastError.run('runtime.' + functionName, |
| 154 'Error getting Entry, code: ' + fileError.code, |
| 155 request.stack, |
| 156 callback); |
| 157 }); |
| 158 } catch (e) { |
| 159 lastError.run('runtime.' + functionName, |
| 160 'Error: ' + e.stack, |
| 161 request.stack, |
| 162 callback); |
| 163 } |
| 164 } |
| 165 } |
| 166 } else { |
| 167 var backgroundPageModuleSystem = GetModuleSystem(backgroundPage); |
| 168 // Force the runtime API to be loaded in the background page. Using |
| 169 // backgroundPageModuleSystem.require('runtime') is insufficient as |
| 170 // requireNative is only allowed while lazily loading an API. |
| 171 backgroundPage.chrome.runtime; |
| 172 return backgroundPageModuleSystem.require('fileEntryBindingUtil') |
| 173 .getBindDirectoryEntryCallback(); |
| 174 } |
| 175 } |
| 176 |
| 121 exports.$set('getFileBindingsForApi', getFileBindingsForApi); | 177 exports.$set('getFileBindingsForApi', getFileBindingsForApi); |
| 178 exports.$set('getBindDirectoryEntryCallback', getBindDirectoryEntryCallback); |
| OLD | NEW |