Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 1 | // Copyright (c) 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/net/storage_test_utils.h" |
| 6 | |
| 7 | #include "content/public/test/browser_test_utils.h" |
| 8 | |
| 9 | namespace storage { |
| 10 | namespace test { |
| 11 | |
Asami Doi | bcbe9af | 2021-03-24 04:50:52 | [diff] [blame] | 12 | const std::vector<std::string> kStorageTypesForFrame{ |
| 13 | "Cookie", "LocalStorage", "FileSystem", "FileSystemAccess", |
| 14 | "SessionStorage", "IndexedDb", "WebSql", "CacheStorage", |
| 15 | "ServiceWorker", "CookieStore", "StorageFoundation"}; |
| 16 | |
| 17 | const std::vector<std::string> kStorageTypesForWorker{ |
| 18 | "WorkerFileSystemAccess", "WorkerCacheStorage", "WorkerIndexedDb", |
| 19 | "WorkerStorageFoundation"}; |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 20 | |
| 21 | const std::vector<std::string> kCrossTabCommunicationTypes{ |
| 22 | "SharedWorker", |
| 23 | "WebLock", |
| 24 | }; |
| 25 | |
| 26 | constexpr char kRequestStorageAccess[] = |
| 27 | "document.requestStorageAccess().then(" |
| 28 | " () => { window.domAutomationController.send(true); }," |
| 29 | " () => { window.domAutomationController.send(false); }," |
| 30 | ");"; |
| 31 | |
| 32 | constexpr char kHasStorageAccess[] = |
| 33 | "document.hasStorageAccess().then(" |
| 34 | " (result) => { window.domAutomationController.send(result); }," |
| 35 | " () => { window.domAutomationController.send(false); }," |
| 36 | ");"; |
| 37 | |
Chris Fredrickson | f39f87b | 2022-07-11 11:34:04 | [diff] [blame^] | 38 | std::string GetFrameContent(content::RenderFrameHost* frame) { |
| 39 | return content::EvalJs(frame, "document.body.textContent").ExtractString(); |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void SetStorageForFrame(content::RenderFrameHost* frame) { |
Asami Doi | bcbe9af | 2021-03-24 04:50:52 | [diff] [blame] | 43 | for (const auto& data_type : kStorageTypesForFrame) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 44 | bool data = content::EvalJs(frame, "set" + data_type + "()", |
| 45 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 46 | .ExtractBool(); |
Ari Chivukula | 8e111da | 2021-10-08 23:57:00 | [diff] [blame] | 47 | if (frame->GetLastCommittedOrigin() != |
| 48 | frame->GetMainFrame()->GetLastCommittedOrigin() && |
| 49 | data_type == "WebSql") { |
| 50 | // Third-party context WebSQL is disabled as of M97. |
| 51 | EXPECT_FALSE(data) << "SetStorageForFrame for " << data_type; |
| 52 | } else { |
| 53 | EXPECT_TRUE(data) << "SetStorageForFrame for " << data_type; |
| 54 | } |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Asami Doi | bcbe9af | 2021-03-24 04:50:52 | [diff] [blame] | 58 | void SetStorageForWorker(content::RenderFrameHost* frame) { |
| 59 | for (const auto& data_type : kStorageTypesForWorker) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 60 | bool data = content::EvalJs(frame, "set" + data_type + "()", |
| 61 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 62 | .ExtractBool(); |
Asami Doi | bcbe9af | 2021-03-24 04:50:52 | [diff] [blame] | 63 | EXPECT_TRUE(data) << "SetStorageForWorker for " << data_type; |
| 64 | } |
| 65 | } |
| 66 | |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 67 | void ExpectStorageForFrame(content::RenderFrameHost* frame, bool expected) { |
Asami Doi | bcbe9af | 2021-03-24 04:50:52 | [diff] [blame] | 68 | for (const auto& data_type : kStorageTypesForFrame) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 69 | bool data = content::EvalJs(frame, "has" + data_type + "();", |
| 70 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 71 | .ExtractBool(); |
Ari Chivukula | 8e111da | 2021-10-08 23:57:00 | [diff] [blame] | 72 | if (frame->GetLastCommittedOrigin() != |
| 73 | frame->GetMainFrame()->GetLastCommittedOrigin() && |
| 74 | data_type == "WebSql") { |
| 75 | // Third-party context WebSQL is disabled as of M97. |
| 76 | EXPECT_FALSE(data) << "ExpectStorageForFrame for " << data_type; |
| 77 | } else { |
| 78 | EXPECT_EQ(expected, data) << "ExpectStorageForFrame for " << data_type; |
| 79 | } |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Asami Doi | bcbe9af | 2021-03-24 04:50:52 | [diff] [blame] | 83 | void ExpectStorageForWorker(content::RenderFrameHost* frame, bool expected) { |
| 84 | for (const auto& data_type : kStorageTypesForWorker) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 85 | bool data = content::EvalJs(frame, "has" + data_type + "();", |
| 86 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 87 | .ExtractBool(); |
Asami Doi | bcbe9af | 2021-03-24 04:50:52 | [diff] [blame] | 88 | EXPECT_EQ(expected, data) << "ExpectStorageForWorker for " << data_type; |
| 89 | } |
| 90 | } |
| 91 | |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 92 | void SetCrossTabInfoForFrame(content::RenderFrameHost* frame) { |
| 93 | for (const auto& data_type : kCrossTabCommunicationTypes) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 94 | bool data = content::EvalJs(frame, "set" + data_type + "()", |
| 95 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 96 | .ExtractBool(); |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 97 | EXPECT_TRUE(data) << data_type; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void ExpectCrossTabInfoForFrame(content::RenderFrameHost* frame, |
| 102 | bool expected) { |
| 103 | for (const auto& data_type : kCrossTabCommunicationTypes) { |
Avi Drissman | 0f0e26e5 | 2021-04-29 03:13:34 | [diff] [blame] | 104 | bool data = content::EvalJs(frame, "has" + data_type + "();", |
| 105 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 106 | .ExtractBool(); |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 107 | EXPECT_EQ(expected, data) << data_type; |
| 108 | } |
| 109 | } |
| 110 | |
Chris Fredrickson | f39f87b | 2022-07-11 11:34:04 | [diff] [blame^] | 111 | bool RequestStorageAccessForFrame(content::RenderFrameHost* frame) { |
| 112 | return content::EvalJs(frame, kRequestStorageAccess, |
| 113 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 114 | .ExtractBool(); |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 115 | } |
| 116 | |
Chris Fredrickson | f39f87b | 2022-07-11 11:34:04 | [diff] [blame^] | 117 | bool HasStorageAccessForFrame(content::RenderFrameHost* frame) { |
| 118 | return content::EvalJs(frame, kHasStorageAccess, |
| 119 | content::EXECUTE_SCRIPT_USE_MANUAL_REPLY) |
| 120 | .ExtractBool(); |
Brandon Maslen | 6134c85 | 2020-05-18 21:45:21 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | } // namespace test |
| 124 | } // namespace storage |