simple-module.js (4975B)
1 // Copyright 2022 The Chromium Authors 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 globalVar = 0; 6 7 async function busyWaitMs(time_to_wait) { 8 const startTime = Date.now(); 9 while (Date.now() - startTime < time_to_wait) { 10 11 } 12 } 13 14 class TestURLSelectionOperation { 15 async run(urls, data) { 16 if (data && data.hasOwnProperty('setKey') && data.hasOwnProperty('setValue')) { 17 await sharedStorage.set(data['setKey'], data['setValue']); 18 } 19 20 if (data && data.hasOwnProperty('mockResult')) { 21 return data['mockResult']; 22 } 23 24 return -1; 25 } 26 } 27 28 class TestURLSelectionOperationTwo { 29 async run(urls, data) { 30 if (data && data.hasOwnProperty('mockResult')) { 31 return data['mockResult']; 32 } 33 34 return -1; 35 } 36 } 37 38 class TestSlowURLSelectionOperation { 39 async run(urls, data) { 40 await busyWaitMs(100); 41 if (data && data.hasOwnProperty('mockResult')) { 42 return data['mockResult']; 43 } 44 45 return -1; 46 } 47 } 48 49 class IncrementGlobalVariableAndReturnOriginalValueOperation { 50 async run(urls, data) { 51 return globalVar++; 52 } 53 } 54 55 class VerifyKeyValue { 56 async run(urls, data) { 57 if (data && data.hasOwnProperty('expectedKey') && 58 data.hasOwnProperty('expectedValue')) { 59 const expectedValue = data['expectedValue']; 60 const value = await sharedStorage.get(data['expectedKey']); 61 if (value === expectedValue) { 62 return 1; 63 } 64 } 65 return -1; 66 } 67 } 68 69 class VerifyKeyNotFound { 70 async run(urls, data) { 71 if (data && data.hasOwnProperty('expectedKey')) { 72 const value = await sharedStorage.get(data['expectedKey']); 73 if (typeof value === 'undefined') { 74 return 1; 75 } 76 } 77 return -1; 78 } 79 } 80 81 class VerifyInterestGroups { 82 async run(urls, data) { 83 if (data && 84 data.hasOwnProperty('expectedOwner') && 85 data.hasOwnProperty('expectedName')) { 86 87 const groups = await interestGroups(); 88 89 if (groups.length !== 1) { 90 return -1; 91 } 92 93 if (groups[0]["owner"] !== data['expectedOwner']) { 94 return -1; 95 } 96 97 if (groups[0]["name"] !== data['expectedName']) { 98 return -1; 99 } 100 101 return 1; 102 } 103 return -1; 104 } 105 } 106 107 class GetWaitIncrementWithinLockOperation { 108 async run(urls, data) { 109 if (data && data.hasOwnProperty('key')) { 110 await navigator.locks.request("lock0", async (lock) => { 111 let value_read = await sharedStorage.get(data['key']); 112 value_read = value_read ? Number(value_read) : 0; 113 114 await busyWaitMs(100); 115 116 await sharedStorage.set(data['key'], value_read + 1); 117 }); 118 119 return 1; 120 } 121 return -1; 122 } 123 } 124 125 class GetWaitSetWithinLockOperation { 126 async run(urls, data) { 127 if (data && data.hasOwnProperty('key') && data.hasOwnProperty('lock_name') 128 && data.hasOwnProperty('append_letter')) { 129 await navigator.locks.request(data['lock_name'], async (lock) => { 130 let value_read = await sharedStorage.get(data['key']); 131 132 if (value_read === undefined) { 133 value_read = ""; 134 } 135 136 await busyWaitMs(500); 137 138 await sharedStorage.set(data['key'], value_read + data['append_letter']); 139 }); 140 141 return 1; 142 } 143 return -1; 144 } 145 } 146 147 class AppendWithLockOptionOperation { 148 async run(urls, data) { 149 if (data && data.hasOwnProperty('key') && data.hasOwnProperty('lock_name') 150 && data.hasOwnProperty('append_letter')) { 151 sharedStorage.append(data['key'], data['append_letter'], {withLock: data['lock_name']}); 152 return 1; 153 } 154 return -1; 155 } 156 } 157 158 class BatchUpdateWithTwoAppendMethodsWithBatchLockOptionOperation { 159 async run(urls, data) { 160 if (data && data.hasOwnProperty('key') && data.hasOwnProperty('lock_name') 161 && data.hasOwnProperty('append_letter')) { 162 sharedStorage.batchUpdate([ 163 new SharedStorageAppendMethod(data['key'], data['append_letter']), 164 new SharedStorageAppendMethod(data['key'], data['append_letter']) 165 ], {withLock: data['lock_name']}); 166 return 1; 167 } 168 return -1; 169 } 170 } 171 172 register('test-url-selection-operation', TestURLSelectionOperation); 173 register('test-url-selection-operation-2', TestURLSelectionOperationTwo); 174 register('test-slow-url-selection-operation', TestSlowURLSelectionOperation); 175 register('increment-global-variable-and-return-original-value-operation', 176 IncrementGlobalVariableAndReturnOriginalValueOperation); 177 register('verify-key-value', VerifyKeyValue); 178 register('verify-key-not-found', VerifyKeyNotFound); 179 register('verify-interest-groups', VerifyInterestGroups); 180 register('get-wait-increment-within-lock', GetWaitIncrementWithinLockOperation); 181 register('get-wait-set-within-lock', GetWaitSetWithinLockOperation); 182 register('append-with-lock-option', AppendWithLockOptionOperation); 183 register('batch-update-with-two-append-methods-with-batch-lock-option', BatchUpdateWithTwoAppendMethodsWithBatchLockOptionOperation);