| @@ -0,0 +1,308 @@ |
| 1 | +//! `setSharedItem`, `getSharedItem` and `removeSharedItem` on Android. |
| 2 | +//! |
| 3 | +//! The iOS side of this is a keychain access group, which is why the page's |
| 4 | +//! globals are called `_craftSharedKeychain*`. Android has no such thing, so |
| 5 | +//! the shim uses a `SharedPreferences` file per group — plain, not encrypted, |
| 6 | +//! which is worth knowing given the name the page sees. That is the shim's |
| 7 | +//! choice and is ported rather than changed here. |
| 8 | +//! |
| 9 | +//! ## The group is a filename |
| 10 | +//! |
| 11 | +//! `"craft_shared_" + group` becomes the preferences file's name, and `group` |
| 12 | +//! is whatever the page passed. A group containing a path separator does not |
| 13 | +//! land where a reader would guess, and `getSharedPreferences` is what decides |
| 14 | +//! what happens then. Reproduced exactly: the naming is the only part of this |
| 15 | +//! that can silently disagree between the two implementations, and it is |
| 16 | +//! decided before any JNI call. |
| 17 | +//! |
| 18 | +//! ## Every reply carries the key back |
| 19 | +//! |
| 20 | +//! Both the resolve shapes name the key, so a page can tell which of several |
| 21 | +//! outstanding calls settled — which matters here more than elsewhere, since |
| 22 | +//! all three actions share one pair of globals and a second call overwrites |
| 23 | +//! the first's resolve function. |
| 24 | + |
| 25 | +const std = ; |
| 26 | +const jni = ; |
| 27 | +const bridge_error = ; |
| 28 | + |
| 29 | +const Jni = jni.Jni; |
| 30 | +const jobject = jni.jobject; |
| 31 | + |
| 32 | +pub const A = struct { |
| 33 | + pub const set_shared_item = "setSharedItem"; |
| 34 | + pub const get_shared_item = "getSharedItem"; |
| 35 | + pub const remove_shared_item = "removeSharedItem"; |
| 36 | +}; |
| 37 | + |
| 38 | +pub const resolve_global = "_craftSharedKeychainResolve"; |
| 39 | +pub const reject_global = "_craftSharedKeychainReject"; |
| 40 | + |
| 41 | +/// `Context.MODE_PRIVATE`, a compile-time constant the shim's DEX holds as 0. |
| 42 | +const mode_private: i32 = 0; |
| 43 | + |
| 44 | +const prefix = "craft_shared"; |
| 45 | + |
| 46 | +/// `if (group.isNotEmpty()) "craft_shared_$group" else "craft_shared"`. |
| 47 | +pub fn prefsName(allocator: std.mem.Allocator, group: []const u8) ![:0]u8 { |
| 48 | + if (group.len == 0) { |
| 49 | + const name = try allocator.allocSentinel(u8, prefix.len, 0); |
| 50 | + @memcpy(name, prefix); |
| 51 | + return name; |
| 52 | + } |
| 53 | + |
| 54 | + const name = try allocator.allocSentinel(u8, prefix.len + 1 + group.len, 0); |
| 55 | + @memcpy(name[0..prefix.len], prefix); |
| 56 | + name[prefix.len] = '_'; |
| 57 | + @memcpy(name[prefix.len + 1 ..], group); |
| 58 | + return name; |
| 59 | +} |
| 60 | + |
| 61 | +/// `{success: true, key: "<key>"}` — what `setSharedItem` and |
| 62 | +/// `removeSharedItem` resolve with. |
| 63 | +pub fn successPayload(allocator: std.mem.Allocator, key: []const u8) ![]u8 { |
| 64 | + var out: std.ArrayListUnmanaged(u8) = .empty; |
| 65 | + errdefer out.deinit(allocator); |
| 66 | + |
| 67 | + try out.appendSlice(allocator, "{\"success\":true,\"key\":\""); |
| 68 | + try bridge_error.appendJsonEscaped(allocator, &out, key); |
| 69 | + try out.appendSlice(allocator, "\"}"); |
| 70 | + return out.toOwnedSlice(allocator); |
| 71 | +} |
| 72 | + |
| 73 | +/// `{value: <value>, key: "<key>"}`, where a missing value is a JSON null. |
| 74 | +/// |
| 75 | +/// The shim writes the two branches as two separate `evaluateJavascript` |
| 76 | +/// calls; the only difference between them is this argument, so they are one |
| 77 | +/// function here and the null is a value rather than a branch. |
| 78 | +pub fn valuePayload(allocator: std.mem.Allocator, key: []const u8, value: ?[]const u8) ![]u8 { |
| 79 | + var out: std.ArrayListUnmanaged(u8) = .empty; |
| 80 | + errdefer out.deinit(allocator); |
| 81 | + |
| 82 | + try out.appendSlice(allocator, "{\"value\":"); |
| 83 | + if (value) |text| { |
| 84 | + try out.append(allocator, '"'); |
| 85 | + try bridge_error.appendJsonEscaped(allocator, &out, text); |
| 86 | + try out.append(allocator, '"'); |
| 87 | + } else { |
| 88 | + try out.appendSlice(allocator, "null"); |
| 89 | + } |
| 90 | + try out.appendSlice(allocator, ",\"key\":\""); |
| 91 | + try bridge_error.appendJsonEscaped(allocator, &out, key); |
| 92 | + try out.appendSlice(allocator, "\"}"); |
| 93 | + return out.toOwnedSlice(allocator); |
| 94 | +} |
| 95 | + |
| 96 | +/// `activity.getSharedPreferences(prefsName(group), MODE_PRIVATE)`. |
| 97 | +fn openPrefs(j: Jni, allocator: std.mem.Allocator, activity: jobject, group: []const u8) !jobject { |
| 98 | + const name = try prefsName(allocator, group); |
| 99 | + defer allocator.free(name); |
| 100 | + |
| 101 | + const activity_cls = try j.objectClass(activity); |
| 102 | + return j.callObjectMethodA( |
| 103 | + activity, |
| 104 | + try j.methodId( |
| 105 | + activity_cls, |
| 106 | + "getSharedPreferences", |
| 107 | + "(Ljava/lang/String;I)Landroid/content/SharedPreferences;", |
| 108 | + ), |
| 109 | + &.{ .{ .l = try j.newStringUtf(name.ptr) }, .{ .i = mode_private } }, |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +/// `prefs.edit().putString(key, value).apply()`. |
| 114 | +pub fn set( |
| 115 | + j: Jni, |
| 116 | + allocator: std.mem.Allocator, |
| 117 | + activity: jobject, |
| 118 | + group: []const u8, |
| 119 | + key: []const u8, |
| 120 | + value: []const u8, |
| 121 | +) !void { |
| 122 | + try j.pushLocalFrame(16); |
| 123 | + defer _ = j.popLocalFrame(null); |
| 124 | + |
| 125 | + const prefs = try openPrefs(j, allocator, activity, group); |
| 126 | + const editor = try editorFor(j, prefs); |
| 127 | + const editor_cls = try j.objectClass(editor); |
| 128 | + |
| 129 | + _ = try j.callObjectMethodA( |
| 130 | + editor, |
| 131 | + try j.methodId( |
| 132 | + editor_cls, |
| 133 | + "putString", |
| 134 | + "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/SharedPreferences$Editor;", |
| 135 | + ), |
| 136 | + &.{ |
| 137 | + .{ .l = try javaString(j, allocator, key) }, |
| 138 | + .{ .l = try javaString(j, allocator, value) }, |
| 139 | + }, |
| 140 | + ); |
| 141 | + |
| 142 | + // `apply` rather than `commit`: it writes on a background thread and |
| 143 | + // returns void, so a failure is not observable here — which is exactly |
| 144 | + // what the shim's resolve already promises regardless. |
| 145 | + try j.callVoidMethodA(editor, try j.methodId(editor_cls, "apply", "()V"), &.{}); |
| 146 | +} |
| 147 | + |
| 148 | +/// `prefs.edit().remove(key).apply()`. |
| 149 | +pub fn remove( |
| 150 | + j: Jni, |
| 151 | + allocator: std.mem.Allocator, |
| 152 | + activity: jobject, |
| 153 | + group: []const u8, |
| 154 | + key: []const u8, |
| 155 | +) !void { |
| 156 | + try j.pushLocalFrame(16); |
| 157 | + defer _ = j.popLocalFrame(null); |
| 158 | + |
| 159 | + const prefs = try openPrefs(j, allocator, activity, group); |
| 160 | + const editor = try editorFor(j, prefs); |
| 161 | + const editor_cls = try j.objectClass(editor); |
| 162 | + |
| 163 | + _ = try j.callObjectMethodA( |
| 164 | + editor, |
| 165 | + try j.methodId( |
| 166 | + editor_cls, |
| 167 | + "remove", |
| 168 | + "(Ljava/lang/String;)Landroid/content/SharedPreferences$Editor;", |
| 169 | + ), |
| 170 | + &.{.{ .l = try javaString(j, allocator, key) }}, |
| 171 | + ); |
| 172 | + try j.callVoidMethodA(editor, try j.methodId(editor_cls, "apply", "()V"), &.{}); |
| 173 | +} |
| 174 | + |
| 175 | +/// `prefs.getString(key, null)`, null included. |
| 176 | +/// |
| 177 | +/// The caller owns the returned bytes; null means the key is absent, which is |
| 178 | +/// the answer the page gets as `{value: null}` rather than as a rejection. |
| 179 | +pub fn get( |
| 180 | + j: Jni, |
| 181 | + allocator: std.mem.Allocator, |
| 182 | + activity: jobject, |
| 183 | + group: []const u8, |
| 184 | + key: []const u8, |
| 185 | +) !?[]u8 { |
| 186 | + try j.pushLocalFrame(16); |
| 187 | + defer _ = j.popLocalFrame(null); |
| 188 | + |
| 189 | + const prefs = try openPrefs(j, allocator, activity, group); |
| 190 | + const value = try j.callObjectMethodA( |
| 191 | + prefs, |
| 192 | + try j.methodId( |
| 193 | + try j.objectClass(prefs), |
| 194 | + "getString", |
| 195 | + "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", |
| 196 | + ), |
| 197 | + &.{ .{ .l = try javaString(j, allocator, key) }, .{ .l = null } }, |
| 198 | + ); |
| 199 | + |
| 200 | + if (value == null) return null; |
| 201 | + return try j.stringToUtf8(allocator, value); |
| 202 | +} |
| 203 | + |
| 204 | +fn editorFor(j: Jni, prefs: jobject) !jobject { |
| 205 | + return j.callObjectMethod( |
| 206 | + prefs, |
| 207 | + try j.methodId( |
| 208 | + try j.objectClass(prefs), |
| 209 | + "edit", |
| 210 | + "()Landroid/content/SharedPreferences$Editor;", |
| 211 | + ), |
| 212 | + ); |
| 213 | +} |
| 214 | + |
| 215 | +fn javaString(j: Jni, allocator: std.mem.Allocator, text: []const u8) !jni.jstring { |
| 216 | + const terminated = try allocator.allocSentinel(u8, text.len, 0); |
| 217 | + defer allocator.free(terminated); |
| 218 | + @memcpy(terminated, text); |
| 219 | + return j.newStringUtf(terminated.ptr); |
| 220 | +} |
| 221 | + |
| 222 | +// ============================================================================= |
| 223 | +// Tests |
| 224 | +// ============================================================================= |
| 225 | + |
| 226 | +const testing = std.testing; |
| 227 | + |
| 228 | +test "the preferences file is named the way the shim names it" { |
| 229 | + const plain = try prefsName(testing.allocator, ""); |
| 230 | + defer testing.allocator.free(plain); |
| 231 | + try testing.expectEqualStrings("craft_shared", plain); |
| 232 | + |
| 233 | + const grouped = try prefsName(testing.allocator, "team"); |
| 234 | + defer testing.allocator.free(grouped); |
| 235 | + try testing.expectEqualStrings("craft_shared_team", grouped); |
| 236 | + |
| 237 | + // `isNotEmpty()` is a length check, not a blank check — a group of one |
| 238 | + // space is a group, and its file is named with the space in it. |
| 239 | + const spaced = try prefsName(testing.allocator, " "); |
| 240 | + defer testing.allocator.free(spaced); |
| 241 | + try testing.expectEqualStrings("craft_shared_ ", spaced); |
| 242 | +} |
| 243 | + |
| 244 | +test "the name is NUL-terminated, because FindClass's neighbour needs it" { |
| 245 | + // `NewStringUTF` takes a C string. A slice that merely looks right here |
| 246 | + // would read past the end at the call site instead. |
| 247 | + const name = try prefsName(testing.allocator, "team"); |
| 248 | + defer testing.allocator.free(name); |
| 249 | + try testing.expectEqual(@as(u8, 0), name.ptr[name.len]); |
| 250 | +} |
| 251 | + |
| 252 | +test "a set or remove resolves with the key it was given" { |
| 253 | + const payload = try successPayload(testing.allocator, "token"); |
| 254 | + defer testing.allocator.free(payload); |
| 255 | + try testing.expectEqualStrings("{\"success\":true,\"key\":\"token\"}", payload); |
| 256 | +} |
| 257 | + |
| 258 | +test "a get resolves with the value, or with a JSON null" { |
| 259 | + const found = try valuePayload(testing.allocator, "token", "abc"); |
| 260 | + defer testing.allocator.free(found); |
| 261 | + try testing.expectEqualStrings("{\"value\":\"abc\",\"key\":\"token\"}", found); |
| 262 | + |
| 263 | + // The absent case is a resolve, not a rejection: a page reading a key it |
| 264 | + // never wrote has done nothing wrong. |
| 265 | + const missing = try valuePayload(testing.allocator, "token", null); |
| 266 | + defer testing.allocator.free(missing); |
| 267 | + try testing.expectEqualStrings("{\"value\":null,\"key\":\"token\"}", missing); |
| 268 | + |
| 269 | + // And an empty string is not the absent case. |
| 270 | + const empty = try valuePayload(testing.allocator, "token", ""); |
| 271 | + defer testing.allocator.free(empty); |
| 272 | + try testing.expectEqualStrings("{\"value\":\"\",\"key\":\"token\"}", empty); |
| 273 | +} |
| 274 | + |
| 275 | +test "a key or value carrying a quote survives as JSON" { |
| 276 | + // Both are page text, and both go through the reply channel — the same |
| 277 | + // shape that hung the promise before #154 was fixed. |
| 278 | + const payload = try valuePayload(testing.allocator, "it's", "a \"quoted\" value\nwith a newline"); |
| 279 | + defer testing.allocator.free(payload); |
| 280 | + |
| 281 | + var parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, payload, .{}); |
| 282 | + defer parsed.deinit(); |
| 283 | + try testing.expectEqualStrings("it's", parsed.value.object.get("key").?.string); |
| 284 | + try testing.expectEqualStrings( |
| 285 | + "a \"quoted\" value\nwith a newline", |
| 286 | + parsed.value.object.get("value").?.string, |
| 287 | + ); |
| 288 | + |
| 289 | + const success = try successPayload(testing.allocator, "it's"); |
| 290 | + defer testing.allocator.free(success); |
| 291 | + var parsed_success = try std.json.parseFromSlice(std.json.Value, testing.allocator, success, .{}); |
| 292 | + defer parsed_success.deinit(); |
| 293 | + try testing.expectEqualStrings("it's", parsed_success.value.object.get("key").?.string); |
| 294 | + try testing.expect(parsed_success.value.object.get("success").?.bool); |
| 295 | +} |
| 296 | + |
| 297 | +test "MODE_PRIVATE is zero" { |
| 298 | + // A compile-time constant in Java, so the shim's DEX holds the literal. |
| 299 | + try testing.expectEqual(@as(i32, 0), mode_private); |
| 300 | +} |
| 301 | + |
| 302 | +test "the actions and globals match the shim exactly" { |
| 303 | + try testing.expectEqualStrings("setSharedItem", A.set_shared_item); |
| 304 | + try testing.expectEqualStrings("getSharedItem", A.get_shared_item); |
| 305 | + try testing.expectEqualStrings("removeSharedItem", A.remove_shared_item); |
| 306 | + try testing.expectEqualStrings("_craftSharedKeychainResolve", resolve_global); |
| 307 | + try testing.expectEqualStrings("_craftSharedKeychainReject", reject_global); |
| 308 | +} |