 Chromium Code Reviews
 Chromium Code Reviews Issue 2895493004:
  [Extensions Bindings] Move directory util out of runtime_custom_bindings  (Closed)
    
  
    Issue 2895493004:
  [Extensions Bindings] Move directory util out of runtime_custom_bindings  (Closed) 
  | 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 var backgroundPageModuleSystem = GetModuleSystem(backgroundPage); | |
| 
lazyboy
2017/05/19 21:03:51
Should we move this close to where it is required
 
Devlin
2017/05/20 00:50:40
Done.
 | |
| 136 | |
| 137 // For packaged apps, all windows use the bindFileEntryCallback from the | |
| 138 // background page so their FileEntry objects have the background page's | |
| 139 // context as their own. This allows them to be used from other windows | |
| 140 // (including the background page) after the original window is closed. | |
| 141 if (WINDOW == backgroundPage) { | |
| 142 return function(name, request, callback, response) { | |
| 143 if (callback) { | |
| 144 if (!response) { | |
| 145 callback(); | |
| 146 return; | |
| 147 } | |
| 148 var fileSystemId = response.fileSystemId; | |
| 149 var baseName = response.baseName; | |
| 150 var fs = GetIsolatedFileSystem(fileSystemId); | |
| 151 | |
| 152 try { | |
| 153 fs.root.getDirectory(baseName, {}, callback, function(fileError) { | |
| 154 lastError.run('runtime.' + functionName, | |
| 155 'Error getting Entry, code: ' + fileError.code, | |
| 156 request.stack, | |
| 157 callback); | |
| 158 }); | |
| 159 } catch (e) { | |
| 160 lastError.run('runtime.' + functionName, | |
| 161 'Error: ' + e.stack, | |
| 162 request.stack, | |
| 163 callback); | |
| 164 } | |
| 165 } | |
| 166 } | |
| 167 } else { | |
| 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().bindDirectoryEntryCallback; | |
| 174 } | |
| 175 } | |
| 176 | |
| 121 exports.$set('getFileBindingsForApi', getFileBindingsForApi); | 177 exports.$set('getFileBindingsForApi', getFileBindingsForApi); | 
| 178 exports.$set('getBindDirectoryEntryCallback', getBindDirectoryEntryCallback); | |
| OLD | NEW |