· 6 years ago · Jan 29, 2020, 12:12 AM
1function testApk(metadata, file, filename, cleanupPrevApp) {
2 // TODO(stange): Modify this function to check the crx key of the
3 // new APK with the crx key of the previously installed APK. If they
4 // are the same, we can remove the contents of the previously installed APK
5 // and insert the contents of the new APK in the directory without prompting
6 // the user. Otherwise, we will have to use the old method of prompting the
7 // user to remove the previously installed APK before installing the new one.
8 var export_path;
9 // If there is no cleanup to do, default to an resolved Promise.
10 var cleanup = Promise.resolve();
11 if (_main.last_launched_app) {
12 // Try to use the previous export if metadata and apk did not change.
13 if (cleanupPrevApp && JSON.stringify(_main.last_launched_metadata) ==
14 JSON.stringify(metadata) && _main.last_launched_filename == filename) {
15 chrome.management.launchApp(_main.last_launched_app);
16 return Promise.resolve();
17 }
18 cleanup = cleanupLastExport();
19 }
20 return cleanup.then(function() {
21 return saveApk(file, filename);
22 })
23 .then(function(saved_apk) {
24 return runApkToCrx(metadata, saved_apk, ['--single-locale',
25 '--badging-check=suppress'
26 ]);
27 })
28 .then(function(apk_info) {
29 export_path = apk_info.export_path;
30 return fsUtil.getDirEntryInPersistentFS(apk_info.export_path);
31 })
32 .then(function(export_path_entry) {
33 // TODO: change to PromiseWrap.copyTo
34 return fsUtil.copyTree(export_path_entry, _main.external_directory);
35 })
36 .then(function() {
37 return fsUtil.getDirEntry(_main.external_directory,
38 export_path.split('/')
39 .slice(-1),
40 false);
41 })
42 .then(function(exportentry) {
43 _main.last_export_entry = exportentry;
44 _main.last_launched_metadata = metadata;
45 return new Promise(function(resolve, reject) {
46 var launched = false;
47 // TODO: have one onInstalled listener and start/stop listening instead
48 // of installing a new listener every time.
49 chrome.management.onInstalled.addListener(function(extInfo) {
50 if (!launched) {
51 chrome.management.launchApp(extInfo.id, function() {});
52 launched = true;
53 _main.last_launched_app = extInfo.id;
54 _main.last_launched_filename = filename;
55 resolve();
56 }
57 });
58 // loadDirectory does not wait until the extension is loaded to callback,
59 // so it is not very useful. Instead, we're using the management API
60 // above to check for new installs.
61 chrome.developerPrivate.loadDirectory(exportentry, function() {
62 if (chrome.runtime.lastError) {
63 console.error(chrome.runtime.lastError);
64 }
65 });
66 });
67 })
68 .then(function() {
69 var id = chrome.fileSystem.retainEntry(_main.last_export_entry);
70 var storage_data = {
71 'lastLaunchedExportEntry': id,
72 'lastLaunchedAppId': _main.last_launched_app
73 };
74 return PromiseWrap.setLocalStorageValue(storage_data);
75 });
76}
77/**
78 * @public
79 *
80 * Saves given APK file to the appropriate location.
81 * If the file has been previously launched and we still
82 * have the save location, then we erase the directory and
83 * re-save the new APK.
84 *
85 * @param {blob} file APK file to save.
86 * @param {string} filename Name of the APK file.
87 * @return {object} .export_dir is the directory the apk was saved to
88 * and .apk_path is the path to where the apk was saved.
89 */
90function saveApk(file, filename) {
91 // TODO(stange): Modify this to save into a previously used
92 // directory if the crx_key hasn't changed.
93 // Where apk_to_crx.py outputs the unpacked directory.
94 var export_dir;
95 export_dir = _ROOT_DIR + '/' + fsUtil.basename(filename) +
96 '_export_' + makeId();
97 // Save the APK to the output location. This is an optimization to avoid
98 // having python read and write the file in HTML storage which takes ~1
99 // second.
100 var apk_save_path = export_dir + '/vendor/chromium/crx/';
101 return fsUtil.saveFileTo(apk_save_path, file, filename)
102 .then(
103 function(apk_path) {
104 _main.current_save_path = apk_path;
105 return {
106 'export_dir': export_dir,
107 'apk_path': apk_path
108 };
109 });
110}