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 bindings for the chrome.app.runtime API. | 5 // Custom bindings for the chrome.app.runtime API. |
6 | 6 |
7 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 7 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
8 var fileSystemHelpers = requireNative('file_system_natives'); | 8 var fileSystemHelpers = requireNative('file_system_natives'); |
9 var GetIsolatedFileSystem = fileSystemHelpers.GetIsolatedFileSystem; | 9 var GetIsolatedFileSystem = fileSystemHelpers.GetIsolatedFileSystem; |
10 var appNatives = requireNative('app_runtime'); | 10 var appNatives = requireNative('app_runtime'); |
11 var DeserializeString = appNatives.DeserializeString; | 11 var DeserializeString = appNatives.DeserializeString; |
12 var SerializeToString = appNatives.SerializeToString; | 12 var SerializeToString = appNatives.SerializeToString; |
13 var CreateBlob = appNatives.CreateBlob; | 13 var CreateBlob = appNatives.CreateBlob; |
14 | 14 |
15 chromeHidden.Event.registerArgumentMassager('app.runtime.onLaunched', | 15 chromeHidden.Event.registerArgumentMassager('app.runtime.onLaunched', |
16 function(args, dispatch) { | 16 function(args, dispatch) { |
17 var launchData = args[0]; | 17 var launchData = args[0]; |
18 var intentData = args[1]; | |
19 var intentId = args[2]; | |
20 | 18 |
21 if (launchData && typeof launchData.id !== 'undefined') { | 19 if (launchData && typeof launchData.id !== 'undefined') { |
22 // new-style dispatch. | 20 // new-style dispatch. |
23 var items = [] | 21 var items = [] |
24 var numItems = launchData.items.length; | 22 var numItems = launchData.items.length; |
25 var itemLoaded = function(err, item) { | 23 var itemLoaded = function(err, item) { |
26 if (err) { | 24 if (err) { |
27 console.error('Error getting fileEntry, code: ' + err.code); | 25 console.error('Error getting fileEntry, code: ' + err.code); |
28 } else { | 26 } else { |
29 items.push(item); | 27 items.push(item); |
30 } | 28 } |
31 if (--numItems === 0) { | 29 if (--numItems === 0) { |
32 if (items.length === 0) { | 30 if (items.length === 0) { |
33 dispatch([]); | 31 dispatch([]); |
34 } else { | 32 } else { |
35 var data = { id: launchData.id, items: items }; | 33 var data = { id: launchData.id, items: items }; |
36 // TODO(benwells): remove once we no longer support intents. | |
37 data.intent = { | |
38 action: "http://webintents.org/view", | |
39 type: "chrome-extension://fileentry", | |
40 data: items[0].entry, | |
41 postResult: function() {}, | |
42 postFailure: function() {} | |
43 }; | |
44 dispatch([data]); | 34 dispatch([data]); |
45 } | 35 } |
46 } | 36 } |
47 }; | 37 }; |
48 launchData.items.forEach(function(item) { | 38 launchData.items.forEach(function(item) { |
49 var fs = GetIsolatedFileSystem(item.fileSystemId); | 39 var fs = GetIsolatedFileSystem(item.fileSystemId); |
50 fs.root.getFile(item.baseName, {}, function(fileEntry) { | 40 fs.root.getFile(item.baseName, {}, function(fileEntry) { |
51 itemLoaded(null, { entry: fileEntry, type: item.mimeType }); | 41 itemLoaded(null, { entry: fileEntry, type: item.mimeType }); |
52 }, function(fileError) { | 42 }, function(fileError) { |
53 itemLoaded(fileError); | 43 itemLoaded(fileError); |
54 }); | 44 }); |
55 }); | 45 }); |
| 46 } else if (launchData) { |
| 47 dispatch([launchData]); |
56 } else { | 48 } else { |
57 if (launchData) { | 49 dispatch([]); |
58 if (intentId) { | |
59 var fn = function(success, data) { | |
60 chrome.app.runtime.postIntentResponse({ | |
61 'intentId': intentId, | |
62 'success': success, | |
63 'data': SerializeToString(data) | |
64 }); | |
65 }; | |
66 launchData.intent.postResult = fn.bind(undefined, true); | |
67 launchData.intent.postFailure = fn.bind(undefined, false); | |
68 } else { | |
69 launchData.intent.postResult = function() {}; | |
70 launchData.intent.postFailure = function() {}; | |
71 } | |
72 } | |
73 | |
74 if (launchData && intentData) { | |
75 switch(intentData.format) { | |
76 case('fileEntry'): | |
77 var fs = GetIsolatedFileSystem(intentData.fileSystemId); | |
78 try { | |
79 fs.root.getFile(intentData.baseName, {}, function(fileEntry) { | |
80 launchData.intent.data = fileEntry; | |
81 dispatch([launchData]); | |
82 }, function(fileError) { | |
83 console.error('Error getting fileEntry, code: ' + fileError.code); | |
84 dispatch([]); | |
85 }); | |
86 } catch (e) { | |
87 console.error('Error in event handler for onLaunched: ' + e.stack); | |
88 dispatch([]); | |
89 } | |
90 break; | |
91 case('filesystem'): | |
92 launchData.intent.data = GetIsolatedFileSystem( | |
93 intentData.fileSystemId, intentData.baseName); | |
94 launchData.intent.postResult = function() {}; | |
95 launchData.intent.postFailure = function() {}; | |
96 dispatch([launchData]); | |
97 break; | |
98 case('serialized'): | |
99 var deserializedData = DeserializeString(intentData.data); | |
100 launchData.intent.data = deserializedData; | |
101 dispatch([launchData]); | |
102 break; | |
103 case('blob'): | |
104 var blobData = CreateBlob(intentData.blobFilePath, | |
105 intentData.blobLength); | |
106 launchData.intent.data = blobData; | |
107 dispatch([launchData]); | |
108 break; | |
109 default: | |
110 console.error('Unexpected launch data format'); | |
111 dispatch([]); | |
112 } | |
113 } else if (launchData) { | |
114 dispatch([launchData]); | |
115 } else { | |
116 dispatch([]); | |
117 } | |
118 } | 50 } |
119 }); | 51 }); |
OLD | NEW |