|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.bidi; |
| 19 | + |
| 20 | +import java.io.StringReader; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.function.Function; |
| 23 | +import org.openqa.selenium.WebDriver; |
| 24 | +import org.openqa.selenium.bidi.storage.DeleteCookiesParameters; |
| 25 | +import org.openqa.selenium.bidi.storage.GetCookiesParameters; |
| 26 | +import org.openqa.selenium.bidi.storage.GetCookiesResult; |
| 27 | +import org.openqa.selenium.bidi.storage.PartitionKey; |
| 28 | +import org.openqa.selenium.bidi.storage.SetCookieParameters; |
| 29 | +import org.openqa.selenium.internal.Require; |
| 30 | +import org.openqa.selenium.json.Json; |
| 31 | +import org.openqa.selenium.json.JsonInput; |
| 32 | + |
| 33 | +public class Storage { |
| 34 | + private static final Json JSON = new Json(); |
| 35 | + |
| 36 | + private final BiDi bidi; |
| 37 | + |
| 38 | + private final Function<JsonInput, GetCookiesResult> getCookiesResultMapper = |
| 39 | + jsonInput -> jsonInput.read(GetCookiesResult.class); |
| 40 | + |
| 41 | + private final Function<JsonInput, PartitionKey> partitionKeyResultMapper = |
| 42 | + jsonInput -> { |
| 43 | + Map<String, Object> response = jsonInput.read(Map.class); |
| 44 | + try (StringReader reader = new StringReader(JSON.toJson(response.get("partitionKey"))); |
| 45 | + JsonInput input = JSON.newInput(reader)) { |
| 46 | + return input.read(PartitionKey.class); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + public Storage(WebDriver driver) { |
| 51 | + Require.nonNull("WebDriver", driver); |
| 52 | + |
| 53 | + if (!(driver instanceof HasBiDi)) { |
| 54 | + throw new IllegalArgumentException("WebDriver instance must support BiDi protocol"); |
| 55 | + } |
| 56 | + |
| 57 | + this.bidi = ((HasBiDi) driver).getBiDi(); |
| 58 | + } |
| 59 | + |
| 60 | + public GetCookiesResult getCookies(GetCookiesParameters params) { |
| 61 | + return this.bidi.send( |
| 62 | + new Command<>("storage.getCookies", params.toMap(), getCookiesResultMapper)); |
| 63 | + } |
| 64 | + |
| 65 | + public PartitionKey setCookie(SetCookieParameters params) { |
| 66 | + return this.bidi.send( |
| 67 | + new Command<>("storage.setCookie", params.toMap(), partitionKeyResultMapper)); |
| 68 | + } |
| 69 | + |
| 70 | + public PartitionKey deleteCookies(DeleteCookiesParameters params) { |
| 71 | + return this.bidi.send( |
| 72 | + new Command<>("storage.deleteCookies", params.toMap(), partitionKeyResultMapper)); |
| 73 | + } |
| 74 | +} |
0 commit comments