Chromium Code Reviews| Index: chrome/renderer/resources/extensions/log_private_custom_bindings.js |
| diff --git a/chrome/renderer/resources/extensions/log_private_custom_bindings.js b/chrome/renderer/resources/extensions/log_private_custom_bindings.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..114e65470e286661174339d6086501afce8ef046 |
| --- /dev/null |
| +++ b/chrome/renderer/resources/extensions/log_private_custom_bindings.js |
| @@ -0,0 +1,102 @@ |
| +// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
|
Zachary Kuznia
2014/06/25 00:05:53
nit: no (c)
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// Custom binding for the logPrivate API. |
| +var binding = require('binding').Binding.create('logPrivate'); |
| + |
| +var fileSystemNatives = requireNative('file_system_natives'); |
| +var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem; |
| +var lastError = require('lastError'); |
| +var sendRequest = require('sendRequest'); |
| +var GetModuleSystem = requireNative('v8_context').GetModuleSystem; |
| +// TODO(sammc): Don't require extension. See http://crbug.com/235689. |
| +var GetExtensionViews = requireNative('runtime').GetExtensionViews; |
| + |
| +// Fallback to using the current window if no background page is running. |
| +var backgroundPage = GetExtensionViews(-1, 'BACKGROUND')[0] || window; |
| +var backgroundPageModuleSystem = GetModuleSystem(backgroundPage); |
| + |
| +// 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 bindFileEntryCallback = function(functionName, apiFunctions) { |
| + apiFunctions.setCustomCallback(functionName, |
| + function(name, request, response) { |
| + if (request.callback && response) { |
| + var callback = request.callback; |
| + request.callback = null; |
| + |
| + var entry = null; |
| + var hasError = false; |
| + |
| + var getEntryError = function(fileError) { |
| + if (!hasError) { |
| + hasError = true; |
| + lastError.run( |
| + 'logPrivate.' + functionName, |
| + 'Error getting fileEntry, code: ' + fileError.code, |
| + request.stack, |
| + callback); |
| + } |
| + } |
| + |
| + // Get asynchronously get the FileEntry for returned result. |
| + var fileSystemId = response.entry.fileSystemId; |
| + var baseName = response.entry.baseName; |
| + var id = response.entry.id; |
| + var fs = GetIsolatedFileSystem(fileSystemId); |
| + |
| + try { |
| + var getEntryCallback = function(fileEntry) { |
| + if (hasError) |
| + return; |
| + |
| + entryIdManager.registerEntry(id, fileEntry); |
| + sendRequest.safeCallbackApply( |
| + 'logPrivate.' + functionName, request, callback, |
| + [fileEntry]); |
| + }; |
| + // TODO(koz): fs.root.getFile() makes a trip to the browser process, |
| + // but it might be possible avoid that by calling |
| + // WebDOMFileSystem::createV8Entry(). |
| + fs.root.getFile(baseName, {}, getEntryCallback, getEntryError); |
| + } catch (e) { |
| + if (!hasError) { |
| + hasError = true; |
| + lastError.run('logPrivate.' + functionName, |
| + 'Error getting fileEntry: ' + e.stack, |
| + request.stack, |
| + callback); |
| + } |
| + } |
| + |
| + } |
| + }); |
| + }; |
| + var entryIdManager = require('entryIdManager'); |
| +} else { |
| + // Force the logPrivate API to be loaded in the background page. Using |
| + // backgroundPageModuleSystem.require('logPrivate') is insufficient as |
| + // requireNative is only allowed while lazily loading an API. |
| + backgroundPage.chrome.fileSystem; |
| + var bindFileEntryCallback = backgroundPageModuleSystem.require( |
| + 'logPrivate').bindFileEntryCallback; |
| + var entryIdManager = backgroundPageModuleSystem.require('entryIdManager'); |
| +} |
| + |
| +binding.registerCustomHook(function(bindingsAPI) { |
| + var apiFunctions = bindingsAPI.apiFunctions; |
| + var fileSystem = bindingsAPI.compiledApi; |
| + |
| + $Array.forEach(['dumpLogs'], |
| + function(functionName) { |
| + bindFileEntryCallback(functionName, apiFunctions); |
| + }); |
| + |
| +}); |
| + |
| +exports.bindFileEntryCallback = bindFileEntryCallback; |
| +exports.binding = binding.generate(); |