| 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 CreateBlob = appNatives.CreateBlob; | 13 var CreateBlob = appNatives.CreateBlob; |
| 13 | 14 |
| 14 chromeHidden.Event.registerArgumentMassager('app.runtime.onLaunched', | 15 chromeHidden.Event.registerArgumentMassager('app.runtime.onLaunched', |
| 15 function(args, dispatch) { | 16 function(args, dispatch) { |
| 16 var launchData = args[0]; | 17 var launchData = args[0]; |
| 17 var intentData = args[1]; | 18 var intentData = args[1]; |
| 19 var intentId = args[2]; |
| 20 |
| 21 if (launchData) { |
| 22 if (intentId) { |
| 23 var fn = function(success, data) { |
| 24 chrome.app.runtime.postIntentResponse({ |
| 25 'intentId': intentId, |
| 26 'success': success, |
| 27 'data': SerializeToString(data) |
| 28 }); |
| 29 }; |
| 30 launchData.intent.postResult = fn.bind(undefined, true); |
| 31 launchData.intent.postFailure = fn.bind(undefined, false); |
| 32 } else { |
| 33 launchData.intent.postResult = function() {}; |
| 34 launchData.intent.postFailure = function() {}; |
| 35 } |
| 36 } |
| 18 | 37 |
| 19 if (launchData && intentData) { | 38 if (launchData && intentData) { |
| 20 switch(intentData.format) { | 39 switch(intentData.format) { |
| 21 case('fileEntry'): | 40 case('fileEntry'): |
| 22 var fs = GetIsolatedFileSystem(intentData.fileSystemId); | 41 var fs = GetIsolatedFileSystem(intentData.fileSystemId); |
| 23 try { | 42 try { |
| 24 fs.root.getFile(intentData.baseName, {}, function(fileEntry) { | 43 fs.root.getFile(intentData.baseName, {}, function(fileEntry) { |
| 25 launchData.intent.data = fileEntry; | 44 launchData.intent.data = fileEntry; |
| 26 launchData.intent.postResult = function() {}; | |
| 27 launchData.intent.postFailure = function() {}; | |
| 28 dispatch([launchData]); | 45 dispatch([launchData]); |
| 29 }, function(fileError) { | 46 }, function(fileError) { |
| 30 console.error('Error getting fileEntry, code: ' + fileError.code); | 47 console.error('Error getting fileEntry, code: ' + fileError.code); |
| 31 dispatch([]); | 48 dispatch([]); |
| 32 }); | 49 }); |
| 33 } catch (e) { | 50 } catch (e) { |
| 34 console.error('Error in event handler for onLaunched: ' + e.stack); | 51 console.error('Error in event handler for onLaunched: ' + e.stack); |
| 35 dispatch([]); | 52 dispatch([]); |
| 36 } | 53 } |
| 37 break; | 54 break; |
| 38 case('filesystem'): | 55 case('filesystem'): |
| 39 launchData.intent.data = GetIsolatedFileSystem(intentData.fileSystemId, | 56 launchData.intent.data = GetIsolatedFileSystem(intentData.fileSystemId, |
| 40 intentData.baseName); | 57 intentData.baseName); |
| 41 launchData.intent.postResult = function() {}; | 58 launchData.intent.postResult = function() {}; |
| 42 launchData.intent.postFailure = function() {}; | 59 launchData.intent.postFailure = function() {}; |
| 43 dispatch([launchData]); | 60 dispatch([launchData]); |
| 44 break; | 61 break; |
| 45 case('serialized'): | 62 case('serialized'): |
| 46 var deserializedData = DeserializeString(intentData.data); | 63 var deserializedData = DeserializeString(intentData.data); |
| 47 launchData.intent.data = deserializedData; | 64 launchData.intent.data = deserializedData; |
| 48 launchData.intent.postResult = function() {}; | |
| 49 launchData.intent.postFailure = function() {}; | |
| 50 dispatch([launchData]); | 65 dispatch([launchData]); |
| 51 break; | 66 break; |
| 52 case('blob'): | 67 case('blob'): |
| 53 var blobData = CreateBlob(intentData.blobFilePath, | 68 var blobData = CreateBlob(intentData.blobFilePath, |
| 54 intentData.blobLength); | 69 intentData.blobLength); |
| 55 launchData.intent.data = blobData; | 70 launchData.intent.data = blobData; |
| 56 launchData.intent.postResult = function() {}; | |
| 57 launchData.intent.postFailure = function() {}; | |
| 58 dispatch([launchData]); | 71 dispatch([launchData]); |
| 59 break; | 72 break; |
| 60 default: | 73 default: |
| 61 console.error('Unexpected launch data format'); | 74 console.error('Unexpected launch data format'); |
| 62 dispatch([]); | 75 dispatch([]); |
| 63 } | 76 } |
| 64 } else if (launchData) { | 77 } else if (launchData) { |
| 65 dispatch([launchData]); | 78 dispatch([launchData]); |
| 66 } else { | 79 } else { |
| 67 dispatch([]); | 80 dispatch([]); |
| 68 } | 81 } |
| 69 }); | 82 }); |
| OLD | NEW |