Commit e7369ba0 authored by rdevlin.cronin's avatar rdevlin.cronin Committed by Commit bot

[Extensions Bindings] Move directory util out of runtime_custom_bindings

The chrome.runtime API has a method called `getPackageDirectoryEntry`
which has custom bindings to retrieve the app's directory entry (a la
HTML5 filesystem) and return it. The custom bindings for this are
quite strange, since it needs to be done from the app's background
page.

Move this to the file_entry_binding_util.js file, where the similar
getFileBindingsForApi() function is implemented. In addition to grouping
similar logic, this solves a native bindings issue where with trying to
require an API directly (which is disallowed).

BUG=653596
TBR=michaelpg@chromium.org (moving a resource)

Review-Url: https://codereview.chromium.org/2895493004
Cr-Commit-Position: refs/heads/master@{#473977}
parent f035886c
...@@ -259,8 +259,6 @@ void ChromeExtensionsDispatcherDelegate::PopulateSourceMap( ...@@ -259,8 +259,6 @@ void ChromeExtensionsDispatcherDelegate::PopulateSourceMap(
IDR_CHROME_DIRECT_SETTING_JS); IDR_CHROME_DIRECT_SETTING_JS);
// Platform app sources that are not API-specific.. // Platform app sources that are not API-specific..
source_map->RegisterSource("fileEntryBindingUtil",
IDR_FILE_ENTRY_BINDING_UTIL_JS);
source_map->RegisterSource("chromeWebViewInternal", source_map->RegisterSource("chromeWebViewInternal",
IDR_CHROME_WEB_VIEW_INTERNAL_CUSTOM_BINDINGS_JS); IDR_CHROME_WEB_VIEW_INTERNAL_CUSTOM_BINDINGS_JS);
source_map->RegisterSource("chromeWebView", IDR_CHROME_WEB_VIEW_JS); source_map->RegisterSource("chromeWebView", IDR_CHROME_WEB_VIEW_JS);
......
...@@ -47,7 +47,6 @@ ...@@ -47,7 +47,6 @@
<include name="IDR_DEVELOPER_PRIVATE_CUSTOM_BINDINGS_JS" file="extensions\developer_private_custom_bindings.js" type="BINDATA" /> <include name="IDR_DEVELOPER_PRIVATE_CUSTOM_BINDINGS_JS" file="extensions\developer_private_custom_bindings.js" type="BINDATA" />
<include name="IDR_DOWNLOADS_CUSTOM_BINDINGS_JS" file="extensions\downloads_custom_bindings.js" type="BINDATA" /> <include name="IDR_DOWNLOADS_CUSTOM_BINDINGS_JS" file="extensions\downloads_custom_bindings.js" type="BINDATA" />
<include name="IDR_FEEDBACK_PRIVATE_CUSTOM_BINDINGS_JS" file="extensions\feedback_private_custom_bindings.js" type="BINDATA" /> <include name="IDR_FEEDBACK_PRIVATE_CUSTOM_BINDINGS_JS" file="extensions\feedback_private_custom_bindings.js" type="BINDATA" />
<include name="IDR_FILE_ENTRY_BINDING_UTIL_JS" file="extensions\file_entry_binding_util.js" type="BINDATA" />
<include name="IDR_FILE_SYSTEM_CUSTOM_BINDINGS_JS" file="extensions\file_system_custom_bindings.js" type="BINDATA" /> <include name="IDR_FILE_SYSTEM_CUSTOM_BINDINGS_JS" file="extensions\file_system_custom_bindings.js" type="BINDATA" />
<include name="IDR_GCM_CUSTOM_BINDINGS_JS" file="extensions\gcm_custom_bindings.js" type="BINDATA" /> <include name="IDR_GCM_CUSTOM_BINDINGS_JS" file="extensions\gcm_custom_bindings.js" type="BINDATA" />
<include name="IDR_IDENTITY_CUSTOM_BINDINGS_JS" file="extensions\identity_custom_bindings.js" type="BINDATA" /> <include name="IDR_IDENTITY_CUSTOM_BINDINGS_JS" file="extensions\identity_custom_bindings.js" type="BINDATA" />
......
This diff is collapsed.
...@@ -63,6 +63,7 @@ ...@@ -63,6 +63,7 @@
<include name="IDR_DECLARATIVE_WEBREQUEST_CUSTOM_BINDINGS_JS" file="declarative_webrequest_custom_bindings.js" type="BINDATA" /> <include name="IDR_DECLARATIVE_WEBREQUEST_CUSTOM_BINDINGS_JS" file="declarative_webrequest_custom_bindings.js" type="BINDATA" />
<include name="IDR_DISPLAY_SOURCE_CUSTOM_BINDINGS_JS" file="display_source_custom_bindings.js" type="BINDATA" /> <include name="IDR_DISPLAY_SOURCE_CUSTOM_BINDINGS_JS" file="display_source_custom_bindings.js" type="BINDATA" />
<include name="IDR_EXTENSION_CUSTOM_BINDINGS_JS" file="extension_custom_bindings.js" type="BINDATA" /> <include name="IDR_EXTENSION_CUSTOM_BINDINGS_JS" file="extension_custom_bindings.js" type="BINDATA" />
<include name="IDR_FILE_ENTRY_BINDING_UTIL_JS" file="file_entry_binding_util.js" type="BINDATA" />
<include name="IDR_GREASEMONKEY_API_JS" file="greasemonkey_api.js" type="BINDATA" /> <include name="IDR_GREASEMONKEY_API_JS" file="greasemonkey_api.js" type="BINDATA" />
<include name="IDR_I18N_CUSTOM_BINDINGS_JS" file="i18n_custom_bindings.js" type="BINDATA" /> <include name="IDR_I18N_CUSTOM_BINDINGS_JS" file="i18n_custom_bindings.js" type="BINDATA" />
<include name="IDR_MOJO_PRIVATE_CUSTOM_BINDINGS_JS" file="mojo_private_custom_bindings.js" type="BINDATA" /> <include name="IDR_MOJO_PRIVATE_CUSTOM_BINDINGS_JS" file="mojo_private_custom_bindings.js" type="BINDATA" />
......
...@@ -10,6 +10,16 @@ var GetModuleSystem = requireNative('v8_context').GetModuleSystem; ...@@ -10,6 +10,16 @@ var GetModuleSystem = requireNative('v8_context').GetModuleSystem;
var GetExtensionViews = requireNative('runtime').GetExtensionViews; var GetExtensionViews = requireNative('runtime').GetExtensionViews;
var safeCallbackApply = require('uncaught_exception_handler').safeCallbackApply; var safeCallbackApply = require('uncaught_exception_handler').safeCallbackApply;
var WINDOW = {};
try {
WINDOW = window;
} catch (e) {
// Running in SW context.
// TODO(lazyboy): Synchronous access to background page is not possible from
// service worker context. Decide what we should do in this case for the class
// of APIs that require access to background page or window object
}
// For a given |apiName|, generates object with two elements that are used // For a given |apiName|, generates object with two elements that are used
// in file system relayed APIs: // in file system relayed APIs:
// * 'bindFileEntryCallback' function that provides mapping between JS objects // * 'bindFileEntryCallback' function that provides mapping between JS objects
...@@ -18,14 +28,14 @@ var safeCallbackApply = require('uncaught_exception_handler').safeCallbackApply; ...@@ -18,14 +28,14 @@ var safeCallbackApply = require('uncaught_exception_handler').safeCallbackApply;
// previously saved file entries. // previously saved file entries.
function getFileBindingsForApi(apiName) { function getFileBindingsForApi(apiName) {
// Fallback to using the current window if no background page is running. // Fallback to using the current window if no background page is running.
var backgroundPage = GetExtensionViews(-1, -1, 'BACKGROUND')[0] || window; var backgroundPage = GetExtensionViews(-1, -1, 'BACKGROUND')[0] || WINDOW;
var backgroundPageModuleSystem = GetModuleSystem(backgroundPage); var backgroundPageModuleSystem = GetModuleSystem(backgroundPage);
// All windows use the bindFileEntryCallback from the background page so their // All windows use the bindFileEntryCallback from the background page so their
// FileEntry objects have the background page's context as their own. This // FileEntry objects have the background page's context as their own. This
// allows them to be used from other windows (including the background page) // allows them to be used from other windows (including the background page)
// after the original window is closed. // after the original window is closed.
if (window == backgroundPage) { if (WINDOW == backgroundPage) {
var bindFileEntryCallback = function(functionName, apiFunctions) { var bindFileEntryCallback = function(functionName, apiFunctions) {
apiFunctions.setCustomCallback(functionName, apiFunctions.setCustomCallback(functionName,
function(name, request, callback, response) { function(name, request, callback, response) {
...@@ -118,4 +128,51 @@ function getFileBindingsForApi(apiName) { ...@@ -118,4 +128,51 @@ function getFileBindingsForApi(apiName) {
entryIdManager: entryIdManager}; entryIdManager: entryIdManager};
} }
function getBindDirectoryEntryCallback() {
// Get the background page if one exists. Otherwise, default to the current
// window.
var backgroundPage = GetExtensionViews(-1, -1, 'BACKGROUND')[0] || WINDOW;
// For packaged apps, all windows use the bindFileEntryCallback from the
// background page so their FileEntry objects have the background page's
// context as their own. This allows them to be used from other windows
// (including the background page) after the original window is closed.
if (WINDOW == backgroundPage) {
return function(name, request, callback, response) {
if (callback) {
if (!response) {
callback();
return;
}
var fileSystemId = response.fileSystemId;
var baseName = response.baseName;
var fs = GetIsolatedFileSystem(fileSystemId);
try {
fs.root.getDirectory(baseName, {}, callback, function(fileError) {
lastError.run('runtime.' + functionName,
'Error getting Entry, code: ' + fileError.code,
request.stack,
callback);
});
} catch (e) {
lastError.run('runtime.' + functionName,
'Error: ' + e.stack,
request.stack,
callback);
}
}
}
} else {
var backgroundPageModuleSystem = GetModuleSystem(backgroundPage);
// Force the runtime API to be loaded in the background page. Using
// backgroundPageModuleSystem.require('runtime') is insufficient as
// requireNative is only allowed while lazily loading an API.
backgroundPage.chrome.runtime;
return backgroundPageModuleSystem.require('fileEntryBindingUtil')
.getBindDirectoryEntryCallback();
}
}
exports.$set('getFileBindingsForApi', getFileBindingsForApi); exports.$set('getFileBindingsForApi', getFileBindingsForApi);
exports.$set('getBindDirectoryEntryCallback', getBindDirectoryEntryCallback);
...@@ -11,81 +11,8 @@ var runtimeNatives = requireNative('runtime'); ...@@ -11,81 +11,8 @@ var runtimeNatives = requireNative('runtime');
var messagingNatives = requireNative('messaging_natives'); var messagingNatives = requireNative('messaging_natives');
var process = requireNative('process'); var process = requireNative('process');
var utils = require('utils'); var utils = require('utils');
var getBindDirectoryEntryCallback =
var WINDOW = {}; require('fileEntryBindingUtil').getBindDirectoryEntryCallback;
try {
WINDOW = window;
} catch (e) {
// Running in SW context.
// TODO(lazyboy): Synchronous access to background page is not possible from
// service worker context. Decide what we should do in this case for the class
// of APIs that require access to background page or window object
}
var backgroundPage = WINDOW;
var backgroundRequire = require;
var contextType = process.GetContextType();
if (contextType == 'BLESSED_EXTENSION' ||
contextType == 'UNBLESSED_EXTENSION') {
var manifest = runtimeNatives.GetManifest();
if (manifest.app && manifest.app.background) {
// Get the background page if one exists. Otherwise, default to the current
// window.
backgroundPage = runtimeNatives.GetExtensionViews(-1, -1, 'BACKGROUND')[0];
if (backgroundPage) {
var GetModuleSystem = requireNative('v8_context').GetModuleSystem;
backgroundRequire = GetModuleSystem(backgroundPage).require;
} else {
backgroundPage = WINDOW;
}
}
}
// For packaged apps, all windows use the bindFileEntryCallback from the
// background page so their FileEntry objects have the background page's context
// as their own. This allows them to be used from other windows (including the
// background page) after the original window is closed.
if (WINDOW == backgroundPage) {
var lastError = require('lastError');
var fileSystemNatives = requireNative('file_system_natives');
var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem;
var bindDirectoryEntryCallback = function(functionName, apiFunctions) {
apiFunctions.setCustomCallback(functionName,
function(name, request, callback, response) {
if (callback) {
if (!response) {
callback();
return;
}
var fileSystemId = response.fileSystemId;
var baseName = response.baseName;
var fs = GetIsolatedFileSystem(fileSystemId);
try {
fs.root.getDirectory(baseName, {}, callback, function(fileError) {
lastError.run('runtime.' + functionName,
'Error getting Entry, code: ' + fileError.code,
request.stack,
callback);
});
} catch (e) {
lastError.run('runtime.' + functionName,
'Error: ' + e.stack,
request.stack,
callback);
}
}
});
};
} else {
// Force the runtime API to be loaded in the background page. Using
// backgroundPageModuleSystem.require('runtime') is insufficient as
// requireNative is only allowed while lazily loading an API.
backgroundPage.chrome.runtime;
var bindDirectoryEntryCallback =
backgroundRequire('runtime').bindDirectoryEntryCallback;
}
binding.registerCustomHook(function(binding, id, contextType) { binding.registerCustomHook(function(binding, id, contextType) {
var apiFunctions = binding.apiFunctions; var apiFunctions = binding.apiFunctions;
...@@ -184,9 +111,9 @@ binding.registerCustomHook(function(binding, id, contextType) { ...@@ -184,9 +111,9 @@ binding.registerCustomHook(function(binding, id, contextType) {
} }
}); });
bindDirectoryEntryCallback('getPackageDirectoryEntry', apiFunctions); apiFunctions.setCustomCallback('getPackageDirectoryEntry',
getBindDirectoryEntryCallback());
}); });
exports.$set('bindDirectoryEntryCallback', bindDirectoryEntryCallback);
if (!apiBridge) if (!apiBridge)
exports.$set('binding', binding.generate()); exports.$set('binding', binding.generate());
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment