Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
|
Zachary Kuznia
2014/06/25 00:05:53
nit: no (c)
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Custom binding for the logPrivate API. | |
| 6 var binding = require('binding').Binding.create('logPrivate'); | |
| 7 | |
| 8 var fileSystemNatives = requireNative('file_system_natives'); | |
| 9 var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem; | |
| 10 var lastError = require('lastError'); | |
| 11 var sendRequest = require('sendRequest'); | |
| 12 var GetModuleSystem = requireNative('v8_context').GetModuleSystem; | |
| 13 // TODO(sammc): Don't require extension. See http://crbug.com/235689. | |
| 14 var GetExtensionViews = requireNative('runtime').GetExtensionViews; | |
| 15 | |
| 16 // Fallback to using the current window if no background page is running. | |
| 17 var backgroundPage = GetExtensionViews(-1, 'BACKGROUND')[0] || window; | |
| 18 var backgroundPageModuleSystem = GetModuleSystem(backgroundPage); | |
| 19 | |
| 20 // All windows use the bindFileEntryCallback from the background page so their | |
| 21 // FileEntry objects have the background page's context as their own. This | |
| 22 // allows them to be used from other windows (including the background page) | |
| 23 // after the original window is closed. | |
| 24 if (window == backgroundPage) { | |
| 25 var bindFileEntryCallback = function(functionName, apiFunctions) { | |
| 26 apiFunctions.setCustomCallback(functionName, | |
| 27 function(name, request, response) { | |
| 28 if (request.callback && response) { | |
| 29 var callback = request.callback; | |
| 30 request.callback = null; | |
| 31 | |
| 32 var entry = null; | |
| 33 var hasError = false; | |
| 34 | |
| 35 var getEntryError = function(fileError) { | |
| 36 if (!hasError) { | |
| 37 hasError = true; | |
| 38 lastError.run( | |
| 39 'logPrivate.' + functionName, | |
| 40 'Error getting fileEntry, code: ' + fileError.code, | |
| 41 request.stack, | |
| 42 callback); | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 // Get asynchronously get the FileEntry for returned result. | |
| 47 var fileSystemId = response.entry.fileSystemId; | |
| 48 var baseName = response.entry.baseName; | |
| 49 var id = response.entry.id; | |
| 50 var fs = GetIsolatedFileSystem(fileSystemId); | |
| 51 | |
| 52 try { | |
| 53 var getEntryCallback = function(fileEntry) { | |
| 54 if (hasError) | |
| 55 return; | |
| 56 | |
| 57 entryIdManager.registerEntry(id, fileEntry); | |
| 58 sendRequest.safeCallbackApply( | |
| 59 'logPrivate.' + functionName, request, callback, | |
| 60 [fileEntry]); | |
| 61 }; | |
| 62 // TODO(koz): fs.root.getFile() makes a trip to the browser process, | |
| 63 // but it might be possible avoid that by calling | |
| 64 // WebDOMFileSystem::createV8Entry(). | |
| 65 fs.root.getFile(baseName, {}, getEntryCallback, getEntryError); | |
| 66 } catch (e) { | |
| 67 if (!hasError) { | |
| 68 hasError = true; | |
| 69 lastError.run('logPrivate.' + functionName, | |
| 70 'Error getting fileEntry: ' + e.stack, | |
| 71 request.stack, | |
| 72 callback); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 } | |
| 77 }); | |
| 78 }; | |
| 79 var entryIdManager = require('entryIdManager'); | |
| 80 } else { | |
| 81 // Force the logPrivate API to be loaded in the background page. Using | |
| 82 // backgroundPageModuleSystem.require('logPrivate') is insufficient as | |
| 83 // requireNative is only allowed while lazily loading an API. | |
| 84 backgroundPage.chrome.fileSystem; | |
| 85 var bindFileEntryCallback = backgroundPageModuleSystem.require( | |
| 86 'logPrivate').bindFileEntryCallback; | |
| 87 var entryIdManager = backgroundPageModuleSystem.require('entryIdManager'); | |
| 88 } | |
| 89 | |
| 90 binding.registerCustomHook(function(bindingsAPI) { | |
| 91 var apiFunctions = bindingsAPI.apiFunctions; | |
| 92 var fileSystem = bindingsAPI.compiledApi; | |
| 93 | |
| 94 $Array.forEach(['dumpLogs'], | |
| 95 function(functionName) { | |
| 96 bindFileEntryCallback(functionName, apiFunctions); | |
| 97 }); | |
| 98 | |
| 99 }); | |
| 100 | |
| 101 exports.bindFileEntryCallback = bindFileEntryCallback; | |
| 102 exports.binding = binding.generate(); | |
| OLD | NEW |