OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Custom binding for the chrome.app.runtime API. | 5 // Custom binding for the chrome.app.runtime API. |
6 | 6 |
7 var binding = require('binding').Binding.create('app.runtime'); | 7 var binding = require('binding').Binding.create('app.runtime'); |
8 | 8 |
9 var eventBindings = require('event_bindings'); | 9 var eventBindings = require('event_bindings'); |
10 var fileSystemHelpers = requireNative('file_system_natives'); | 10 var fileSystemHelpers = requireNative('file_system_natives'); |
11 var GetIsolatedFileSystem = fileSystemHelpers.GetIsolatedFileSystem; | 11 var GetIsolatedFileSystem = fileSystemHelpers.GetIsolatedFileSystem; |
12 var appNatives = requireNative('app_runtime'); | 12 var appNatives = requireNative('app_runtime'); |
13 var DeserializeString = appNatives.DeserializeString; | 13 var DeserializeString = appNatives.DeserializeString; |
14 var SerializeToString = appNatives.SerializeToString; | 14 var SerializeToString = appNatives.SerializeToString; |
15 var CreateBlob = appNatives.CreateBlob; | 15 var CreateBlob = appNatives.CreateBlob; |
16 var entryIdManager = require('entryIdManager'); | 16 var entryIdManager = require('entryIdManager'); |
17 | 17 |
18 eventBindings.registerArgumentMassager('app.runtime.onLaunched', | 18 eventBindings.registerArgumentMassager('app.runtime.onLaunched', |
19 function(args, dispatch) { | 19 function(args, dispatch) { |
20 var launchData = args[0]; | 20 var launchData = args[0]; |
21 | 21 |
22 if (launchData && typeof launchData.id !== 'undefined') { | 22 if (!launchData) { |
23 // new-style dispatch. | 23 // An onLaunched corresponding to launching directly or from |
24 // the app launcher or browser. | |
25 dispatch([]); | |
26 return; | |
27 } | |
28 | |
29 if (launchData.items) { | |
30 // An onLaunched corresponding to file_handlers in the app's manifest. | |
31 // NOTE: Using the new-style dispatch. | |
benwells
2013/09/04 23:54:20
To answer the earlier comment: I have no idea what
sergeygs
2013/09/05 09:19:03
Done.
| |
24 var items = [] | 32 var items = [] |
25 var numItems = launchData.items.length; | 33 var numItems = launchData.items.length; |
26 var itemLoaded = function(err, item) { | 34 var itemLoaded = function(err, item) { |
27 if (err) { | 35 if (err) { |
28 console.error('Error getting fileEntry, code: ' + err.code); | 36 console.error('Error getting fileEntry, code: ' + err.code); |
29 } else { | 37 } else { |
30 $Array.push(items, item); | 38 $Array.push(items, item); |
31 } | 39 } |
32 if (--numItems === 0) { | 40 if (--numItems === 0) { |
33 if (items.length === 0) { | 41 if (items.length === 0) { |
34 dispatch([]); | 42 dispatch([]); |
35 } else { | 43 } else { |
36 var data = { id: launchData.id, items: items }; | 44 var data = { id: launchData.id, items: items }; |
37 dispatch([data]); | 45 dispatch([data]); |
38 } | 46 } |
39 } | 47 } |
40 }; | 48 }; |
41 $Array.forEach(launchData.items, function(item) { | 49 $Array.forEach(launchData.items, function(item) { |
42 var fs = GetIsolatedFileSystem(item.fileSystemId); | 50 var fs = GetIsolatedFileSystem(item.fileSystemId); |
43 fs.root.getFile(item.baseName, {}, function(fileEntry) { | 51 fs.root.getFile(item.baseName, {}, function(fileEntry) { |
44 entryIdManager.registerEntry(item.entryId, fileEntry); | 52 entryIdManager.registerEntry(item.entryId, fileEntry); |
45 itemLoaded(null, { entry: fileEntry, type: item.mimeType }); | 53 itemLoaded(null, { entry: fileEntry, type: item.mimeType }); |
46 }, function(fileError) { | 54 }, function(fileError) { |
47 itemLoaded(fileError); | 55 itemLoaded(fileError); |
48 }); | 56 }); |
49 }); | 57 }); |
50 } else if (launchData) { | 58 } else { |
59 // Default case. This currently covers an onLaunched corresponding to | |
60 // url_handlers in the app's manifest. | |
51 dispatch([launchData]); | 61 dispatch([launchData]); |
52 } else { | |
53 dispatch([]); | |
54 } | 62 } |
55 }); | 63 }); |
56 | 64 |
57 exports.binding = binding.generate(); | 65 exports.binding = binding.generate(); |
OLD | NEW |