Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: chrome/renderer/resources/extensions/file_entry_binding_util.js

Issue 2895493004: [Extensions Bindings] Move directory util out of runtime_custom_bindings (Closed)
Patch Set: . Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 var fileSystemNatives = requireNative('file_system_natives');
6 var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem;
7 var lastError = require('lastError');
8 var GetModuleSystem = requireNative('v8_context').GetModuleSystem;
9 // TODO(sammc): Don't require extension. See http://crbug.com/235689.
10 var GetExtensionViews = requireNative('runtime').GetExtensionViews;
11 var safeCallbackApply = require('uncaught_exception_handler').safeCallbackApply;
12
13 // For a given |apiName|, generates object with two elements that are used
14 // in file system relayed APIs:
15 // * 'bindFileEntryCallback' function that provides mapping between JS objects
16 // into actual FileEntry|DirectoryEntry objects.
17 // * 'entryIdManager' object that implements methods for keeping the tracks of
18 // previously saved file entries.
19 function getFileBindingsForApi(apiName) {
20 // Fallback to using the current window if no background page is running.
21 var backgroundPage = GetExtensionViews(-1, -1, 'BACKGROUND')[0] || window;
22 var backgroundPageModuleSystem = GetModuleSystem(backgroundPage);
23
24 // All windows use the bindFileEntryCallback from the background page so their
25 // 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)
27 // after the original window is closed.
28 if (window == backgroundPage) {
29 var bindFileEntryCallback = function(functionName, apiFunctions) {
30 apiFunctions.setCustomCallback(functionName,
31 function(name, request, callback, response) {
32 if (callback) {
33 if (!response) {
34 callback();
35 return;
36 }
37
38 var entries = [];
39 var hasError = false;
40
41 var getEntryError = function(fileError) {
42 if (!hasError) {
43 hasError = true;
44 lastError.run(
45 apiName + '.' + functionName,
46 'Error getting fileEntry, code: ' + fileError.code,
47 request.stack,
48 callback);
49 }
50 }
51
52 // Loop through the response entries and asynchronously get the
53 // FileEntry for each. We use hasError to ensure that only the first
54 // error is reported. Note that an error can occur either during the
55 // loop or in the asynchronous error callback to getFile.
56 $Array.forEach(response.entries, function(entry) {
57 if (hasError)
58 return;
59 var fileSystemId = entry.fileSystemId;
60 var baseName = entry.baseName;
61 var id = entry.id;
62 var fs = GetIsolatedFileSystem(fileSystemId);
63
64 try {
65 var getEntryCallback = function(fileEntry) {
66 if (hasError)
67 return;
68 entryIdManager.registerEntry(id, fileEntry);
69 entries.push(fileEntry);
70 // Once all entries are ready, pass them to the callback. In the
71 // event of an error, this condition will never be satisfied so
72 // the callback will not be called with any entries.
73 if (entries.length == response.entries.length) {
74 if (response.multiple) {
75 safeCallbackApply(apiName + '.' + functionName, request,
76 callback, [entries]);
77 } else {
78 safeCallbackApply(
79 apiName + '.' + functionName, request, callback,
80 [entries[0]]);
81 }
82 }
83 }
84 // TODO(koz): fs.root.getFile() makes a trip to the browser
85 // process, but it might be possible avoid that by calling
86 // WebDOMFileSystem::createV8Entry().
87 if (entry.isDirectory) {
88 fs.root.getDirectory(baseName, {}, getEntryCallback,
89 getEntryError);
90 } else {
91 fs.root.getFile(baseName, {}, getEntryCallback, getEntryError);
92 }
93 } catch (e) {
94 if (!hasError) {
95 hasError = true;
96 lastError.run(apiName + '.' + functionName,
97 'Error getting fileEntry: ' + e.stack,
98 request.stack,
99 callback);
100 }
101 }
102 });
103 }
104 });
105 };
106 var entryIdManager = require('entryIdManager');
107 } else {
108 // Force the fileSystem API to be loaded in the background page. Using
109 // backgroundPageModuleSystem.require('fileSystem') is insufficient as
110 // requireNative is only allowed while lazily loading an API.
111 backgroundPage.chrome.fileSystem;
112 var bindFileEntryCallback =
113 backgroundPageModuleSystem.require('fileEntryBindingUtil')
114 .getFileBindingsForApi(apiName).bindFileEntryCallback;
115 var entryIdManager = backgroundPageModuleSystem.require('entryIdManager');
116 }
117 return {bindFileEntryCallback: bindFileEntryCallback,
118 entryIdManager: entryIdManager};
119 }
120
121 exports.$set('getFileBindingsForApi', getFileBindingsForApi);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698