Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Ken Rockot | 27a62d5 | 2019-10-30 01:57:01 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Lukasz Anforowicz | 84dbd4d4 | 2021-09-09 19:58:21 | [diff] [blame] | 5 | #include <limits> |
| 6 | |
| 7 | #include "base/base_paths.h" |
| 8 | #include "base/files/file_path.h" |
| 9 | #include "base/files/file_util.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 10 | #include "base/functional/callback_helpers.h" |
Lukasz Anforowicz | 84dbd4d4 | 2021-09-09 19:58:21 | [diff] [blame] | 11 | #include "base/path_service.h" |
Sean Maher | 52fa5a7 | 2022-11-14 15:53:25 | [diff] [blame] | 12 | #include "base/task/sequenced_task_runner.h" |
Lukasz Anforowicz | 84dbd4d4 | 2021-09-09 19:58:21 | [diff] [blame] | 13 | #include "base/test/bind.h" |
| 14 | #include "base/test/metrics/histogram_tester.h" |
| 15 | #include "base/threading/thread_restrictions.h" |
Gabriel Charette | d87f10f | 2022-03-31 00:44:22 | [diff] [blame] | 16 | #include "base/time/time.h" |
Ken Rockot | 27a62d5 | 2019-10-30 01:57:01 | [diff] [blame] | 17 | #include "content/public/browser/service_process_host.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 18 | #include "content/public/test/browser_test.h" |
Lukasz Anforowicz | 84dbd4d4 | 2021-09-09 19:58:21 | [diff] [blame] | 19 | #include "content/public/test/browser_test_utils.h" |
Ken Rockot | 27a62d5 | 2019-10-30 01:57:01 | [diff] [blame] | 20 | #include "content/public/test/content_browser_test.h" |
| 21 | #include "mojo/public/cpp/bindings/remote.h" |
| 22 | #include "services/data_decoder/public/cpp/data_decoder.h" |
Lukasz Anforowicz | 84dbd4d4 | 2021-09-09 19:58:21 | [diff] [blame] | 23 | #include "services/data_decoder/public/cpp/decode_image.h" |
Ken Rockot | 27a62d5 | 2019-10-30 01:57:01 | [diff] [blame] | 24 | #include "services/data_decoder/public/mojom/data_decoder_service.mojom.h" |
Lukasz Anforowicz | 84dbd4d4 | 2021-09-09 19:58:21 | [diff] [blame] | 25 | #include "services/data_decoder/public/mojom/image_decoder.mojom.h" |
Lukasz Anforowicz | 84dbd4d4 | 2021-09-09 19:58:21 | [diff] [blame] | 26 | #include "testing/gmock/include/gmock/gmock.h" |
| 27 | |
| 28 | using ::testing::Pair; |
| 29 | using ::testing::UnorderedElementsAre; |
Ken Rockot | 27a62d5 | 2019-10-30 01:57:01 | [diff] [blame] | 30 | |
| 31 | namespace content { |
| 32 | |
Lukasz Anforowicz | 84dbd4d4 | 2021-09-09 19:58:21 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | // Populates `output` and returns true on success (i.e. if `relative_path` |
| 36 | // exists and can be read into `output`). Otherwise returns false. |
| 37 | bool ReadTestFile(const base::FilePath& relative_path, |
| 38 | std::vector<uint8_t>& output) { |
| 39 | base::FilePath source_root_dir; |
Ho Cheung | a18707a | 2023-10-18 15:30:40 | [diff] [blame] | 40 | if (!base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &source_root_dir)) { |
Lukasz Anforowicz | 84dbd4d4 | 2021-09-09 19:58:21 | [diff] [blame] | 41 | return false; |
Ho Cheung | a18707a | 2023-10-18 15:30:40 | [diff] [blame] | 42 | } |
Lukasz Anforowicz | 84dbd4d4 | 2021-09-09 19:58:21 | [diff] [blame] | 43 | |
| 44 | std::string file_contents_as_string; |
| 45 | { |
| 46 | base::ScopedAllowBlockingForTesting allow_file_io_for_testing; |
| 47 | base::FilePath absolute_path = source_root_dir.Append(relative_path); |
| 48 | if (!base::ReadFileToString(absolute_path, &file_contents_as_string)) |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | // Convert chars to uint8_ts. |
| 53 | for (const char& c : file_contents_as_string) |
| 54 | output.push_back(c); |
| 55 | |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | // Populates `out_measurement_value` and returns true on success (i.e. if the |
| 60 | // `metric_name` has a single measurement in `histograms`). Otherwise returns |
| 61 | // false. |
| 62 | bool GetSingleMeasurement(const base::HistogramTester& histograms, |
| 63 | const char* metric_name, |
| 64 | base::TimeDelta& out_measurement_value) { |
| 65 | DCHECK(metric_name); |
| 66 | |
| 67 | std::vector<base::Bucket> buckets = histograms.GetAllSamples(metric_name); |
| 68 | if (buckets.size() != 1u) |
| 69 | return false; |
| 70 | |
| 71 | EXPECT_EQ(1u, buckets.size()); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 72 | out_measurement_value = base::Milliseconds(buckets.front().min); |
Lukasz Anforowicz | 84dbd4d4 | 2021-09-09 19:58:21 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | |
| 76 | } // namespace |
| 77 | |
Ken Rockot | 27a62d5 | 2019-10-30 01:57:01 | [diff] [blame] | 78 | using DataDecoderBrowserTest = ContentBrowserTest; |
| 79 | |
| 80 | class ServiceProcessObserver : public ServiceProcessHost::Observer { |
| 81 | public: |
| 82 | ServiceProcessObserver() { ServiceProcessHost::AddObserver(this); } |
| 83 | |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 84 | ServiceProcessObserver(const ServiceProcessObserver&) = delete; |
| 85 | ServiceProcessObserver& operator=(const ServiceProcessObserver&) = delete; |
| 86 | |
Ken Rockot | 27a62d5 | 2019-10-30 01:57:01 | [diff] [blame] | 87 | ~ServiceProcessObserver() override { |
| 88 | ServiceProcessHost::RemoveObserver(this); |
| 89 | } |
| 90 | |
| 91 | int instances_started() const { return instances_started_; } |
| 92 | |
| 93 | void WaitForNextLaunch() { |
| 94 | launch_wait_loop_.emplace(); |
| 95 | launch_wait_loop_->Run(); |
| 96 | } |
| 97 | |
| 98 | void OnServiceProcessLaunched(const ServiceProcessInfo& info) override { |
| 99 | if (info.IsService<data_decoder::mojom::DataDecoderService>()) { |
| 100 | ++instances_started_; |
| 101 | if (launch_wait_loop_) |
| 102 | launch_wait_loop_->Quit(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private: |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 107 | std::optional<base::RunLoop> launch_wait_loop_; |
Ken Rockot | 27a62d5 | 2019-10-30 01:57:01 | [diff] [blame] | 108 | int instances_started_ = 0; |
Ken Rockot | 27a62d5 | 2019-10-30 01:57:01 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | IN_PROC_BROWSER_TEST_F(DataDecoderBrowserTest, Launch) { |
| 112 | ServiceProcessObserver observer; |
| 113 | |
| 114 | // Verifies that the DataDecoder client object launches a service process as |
| 115 | // needed. |
| 116 | data_decoder::DataDecoder decoder; |
| 117 | |
| 118 | // |GetService()| must always ensure a connection to the service on all |
| 119 | // platforms, so we use it instead of a more specific API whose behavior may |
| 120 | // vary across platforms. |
| 121 | decoder.GetService(); |
| 122 | |
| 123 | observer.WaitForNextLaunch(); |
| 124 | EXPECT_EQ(1, observer.instances_started()); |
| 125 | } |
| 126 | |
| 127 | IN_PROC_BROWSER_TEST_F(DataDecoderBrowserTest, LaunchIsolated) { |
| 128 | ServiceProcessObserver observer; |
| 129 | |
| 130 | // Verifies that separate DataDecoder client objects will launch separate |
Daniel Cheng | bc846a3b | 2025-05-08 22:31:36 | [diff] [blame] | 131 | // service processes. We also bind an ImageDecoder interface to ensure that |
| 132 | // the instances don't go idle. |
Ken Rockot | 27a62d5 | 2019-10-30 01:57:01 | [diff] [blame] | 133 | data_decoder::DataDecoder decoder1; |
Daniel Cheng | bc846a3b | 2025-05-08 22:31:36 | [diff] [blame] | 134 | mojo::Remote<data_decoder::mojom::ImageDecoder> image_decoder1; |
| 135 | decoder1.GetService()->BindImageDecoder( |
| 136 | image_decoder1.BindNewPipeAndPassReceiver()); |
Ken Rockot | 27a62d5 | 2019-10-30 01:57:01 | [diff] [blame] | 137 | observer.WaitForNextLaunch(); |
| 138 | EXPECT_EQ(1, observer.instances_started()); |
| 139 | |
| 140 | data_decoder::DataDecoder decoder2; |
Daniel Cheng | bc846a3b | 2025-05-08 22:31:36 | [diff] [blame] | 141 | mojo::Remote<data_decoder::mojom::ImageDecoder> image_decoder2; |
| 142 | decoder2.GetService()->BindImageDecoder( |
| 143 | image_decoder2.BindNewPipeAndPassReceiver()); |
Ken Rockot | 27a62d5 | 2019-10-30 01:57:01 | [diff] [blame] | 144 | observer.WaitForNextLaunch(); |
| 145 | EXPECT_EQ(2, observer.instances_started()); |
| 146 | |
| 147 | // Both interfaces should be connected end-to-end. |
Daniel Cheng | bc846a3b | 2025-05-08 22:31:36 | [diff] [blame] | 148 | image_decoder1.FlushForTesting(); |
| 149 | image_decoder2.FlushForTesting(); |
| 150 | EXPECT_TRUE(image_decoder1.is_connected()); |
| 151 | EXPECT_TRUE(image_decoder2.is_connected()); |
Ken Rockot | 27a62d5 | 2019-10-30 01:57:01 | [diff] [blame] | 152 | } |
| 153 | |
Lukasz Anforowicz | 84dbd4d4 | 2021-09-09 19:58:21 | [diff] [blame] | 154 | IN_PROC_BROWSER_TEST_F(DataDecoderBrowserTest, DecodeImageIsolated) { |
| 155 | std::vector<uint8_t> file_contents; |
| 156 | base::FilePath content_test_data_path = GetTestDataFilePath(); |
| 157 | base::FilePath png_path = |
| 158 | content_test_data_path.AppendASCII("site_isolation/png-corp.png"); |
| 159 | ASSERT_TRUE(ReadTestFile(png_path, file_contents)); |
| 160 | |
| 161 | base::HistogramTester histograms; |
| 162 | { |
| 163 | base::RunLoop run_loop; |
| 164 | data_decoder::DecodeImageCallback callback = |
| 165 | base::BindLambdaForTesting([&](const SkBitmap& decoded_bitmap) { |
| 166 | EXPECT_EQ(100, decoded_bitmap.width()); |
| 167 | EXPECT_EQ(100, decoded_bitmap.height()); |
| 168 | run_loop.Quit(); |
| 169 | }); |
| 170 | data_decoder::DecodeImageIsolated( |
| 171 | file_contents, data_decoder::mojom::ImageCodec::kDefault, |
| 172 | false, // shrink_to_fit |
| 173 | std::numeric_limits<uint32_t>::max(), // max_size_in_bytes |
| 174 | gfx::Size(), // desired_image_frame_size |
| 175 | std::move(callback)); |
| 176 | run_loop.Run(); |
| 177 | } |
| 178 | |
| 179 | FetchHistogramsFromChildProcesses(); |
| 180 | EXPECT_THAT( |
| 181 | histograms.GetTotalCountsForPrefix("Security.DataDecoder"), |
| 182 | UnorderedElementsAre( |
| 183 | Pair("Security.DataDecoder.Image.Isolated.EndToEndTime", 1), |
| 184 | Pair("Security.DataDecoder.Image.Isolated.ProcessOverhead", 1), |
| 185 | Pair("Security.DataDecoder.Image.DecodingTime", 1))); |
| 186 | |
| 187 | base::TimeDelta end_to_end_duration_estimate; |
| 188 | EXPECT_TRUE(GetSingleMeasurement( |
| 189 | histograms, "Security.DataDecoder.Image.Isolated.EndToEndTime", |
| 190 | end_to_end_duration_estimate)); |
| 191 | |
| 192 | base::TimeDelta overhead_estimate; |
| 193 | EXPECT_TRUE(GetSingleMeasurement( |
| 194 | histograms, "Security.DataDecoder.Image.Isolated.ProcessOverhead", |
| 195 | overhead_estimate)); |
| 196 | |
| 197 | base::TimeDelta decoding_duration_estimate; |
| 198 | EXPECT_TRUE(GetSingleMeasurement(histograms, |
| 199 | "Security.DataDecoder.Image.DecodingTime", |
| 200 | decoding_duration_estimate)); |
| 201 | |
| 202 | EXPECT_LE(decoding_duration_estimate, end_to_end_duration_estimate); |
| 203 | EXPECT_LE(overhead_estimate, end_to_end_duration_estimate); |
| 204 | } |
| 205 | |
| 206 | IN_PROC_BROWSER_TEST_F(DataDecoderBrowserTest, DecodeImage) { |
| 207 | std::vector<uint8_t> file_contents; |
| 208 | base::FilePath content_test_data_path = GetTestDataFilePath(); |
| 209 | base::FilePath png_path = |
| 210 | content_test_data_path.AppendASCII("site_isolation/png-corp.png"); |
| 211 | ASSERT_TRUE(ReadTestFile(png_path, file_contents)); |
| 212 | |
| 213 | base::HistogramTester histograms; |
| 214 | { |
| 215 | base::RunLoop run_loop; |
| 216 | data_decoder::DecodeImageCallback callback = |
| 217 | base::BindLambdaForTesting([&](const SkBitmap& decoded_bitmap) { |
| 218 | EXPECT_EQ(100, decoded_bitmap.width()); |
| 219 | EXPECT_EQ(100, decoded_bitmap.height()); |
| 220 | run_loop.Quit(); |
| 221 | }); |
| 222 | |
| 223 | data_decoder::DataDecoder decoder; |
| 224 | data_decoder::DecodeImage( |
| 225 | &decoder, file_contents, data_decoder::mojom::ImageCodec::kDefault, |
| 226 | false, // shrink_to_fit |
| 227 | std::numeric_limits<uint32_t>::max(), // max_size_in_bytes |
| 228 | gfx::Size(), // desired_image_frame_size |
| 229 | std::move(callback)); |
| 230 | run_loop.Run(); |
| 231 | } |
| 232 | |
| 233 | FetchHistogramsFromChildProcesses(); |
| 234 | EXPECT_THAT( |
| 235 | histograms.GetTotalCountsForPrefix("Security.DataDecoder"), |
| 236 | UnorderedElementsAre( |
| 237 | Pair("Security.DataDecoder.Image.Reusable.EndToEndTime", 1), |
| 238 | Pair("Security.DataDecoder.Image.Reusable.ProcessOverhead", 1), |
| 239 | Pair("Security.DataDecoder.Image.DecodingTime", 1))); |
| 240 | |
| 241 | base::TimeDelta end_to_end_duration_estimate; |
| 242 | EXPECT_TRUE(GetSingleMeasurement( |
| 243 | histograms, "Security.DataDecoder.Image.Reusable.EndToEndTime", |
| 244 | end_to_end_duration_estimate)); |
| 245 | |
| 246 | base::TimeDelta overhead_estimate; |
| 247 | EXPECT_TRUE(GetSingleMeasurement( |
| 248 | histograms, "Security.DataDecoder.Image.Reusable.ProcessOverhead", |
| 249 | overhead_estimate)); |
| 250 | |
| 251 | base::TimeDelta decoding_duration_estimate; |
| 252 | EXPECT_TRUE(GetSingleMeasurement(histograms, |
| 253 | "Security.DataDecoder.Image.DecodingTime", |
| 254 | decoding_duration_estimate)); |
| 255 | |
| 256 | EXPECT_LE(decoding_duration_estimate, end_to_end_duration_estimate); |
| 257 | EXPECT_LE(overhead_estimate, end_to_end_duration_estimate); |
| 258 | } |
| 259 | |
Robert Sesek | 31db673 | 2022-05-26 20:43:26 | [diff] [blame] | 260 | IN_PROC_BROWSER_TEST_F(DataDecoderBrowserTest, |
| 261 | NoCallbackAfterDestruction_Json) { |
| 262 | base::RunLoop run_loop; |
| 263 | |
| 264 | auto decoder = std::make_unique<data_decoder::DataDecoder>(); |
| 265 | auto* raw_decoder = decoder.get(); |
| 266 | |
| 267 | // Android's in-process parser can complete synchronously, so queue the |
| 268 | // delete task first unlike in the other tests. |
Sean Maher | 52fa5a7 | 2022-11-14 15:53:25 | [diff] [blame] | 269 | base::SequencedTaskRunner::GetCurrentDefault()->DeleteSoon( |
| 270 | FROM_HERE, std::move(decoder)); |
Robert Sesek | 31db673 | 2022-05-26 20:43:26 | [diff] [blame] | 271 | |
| 272 | bool got_callback = false; |
| 273 | raw_decoder->ParseJson( |
| 274 | "[1, 2, 3]", |
| 275 | base::BindOnce( |
| 276 | [](bool* got_callback, base::ScopedClosureRunner quit_closure_runner, |
| 277 | data_decoder::DataDecoder::ValueOrError result) { |
| 278 | *got_callback = true; |
| 279 | }, |
| 280 | // Pass the quit closure as a ScopedClosureRunner, so that the loop |
| 281 | // is quit if the callback is destroyed un-run or after it runs. |
| 282 | &got_callback, base::ScopedClosureRunner(run_loop.QuitClosure()))); |
| 283 | |
| 284 | run_loop.Run(); |
| 285 | |
| 286 | EXPECT_FALSE(got_callback); |
| 287 | } |
| 288 | |
| 289 | IN_PROC_BROWSER_TEST_F(DataDecoderBrowserTest, NoCallbackAfterDestruction_Xml) { |
| 290 | base::RunLoop run_loop; |
| 291 | |
| 292 | auto decoder = std::make_unique<data_decoder::DataDecoder>(); |
| 293 | bool got_callback = false; |
| 294 | decoder->ParseXml( |
| 295 | "<marquee>hello world</marquee>", |
| 296 | data_decoder::mojom::XmlParser::WhitespaceBehavior::kIgnore, |
| 297 | base::BindOnce( |
| 298 | [](bool* got_callback, base::ScopedClosureRunner quit_closure_runner, |
| 299 | data_decoder::DataDecoder::ValueOrError result) { |
| 300 | *got_callback = true; |
| 301 | }, |
| 302 | // Pass the quit closure as a ScopedClosureRunner, so that the loop |
| 303 | // is quit if the callback is destroyed un-run or after it runs. |
| 304 | &got_callback, base::ScopedClosureRunner(run_loop.QuitClosure()))); |
| 305 | |
Sean Maher | 52fa5a7 | 2022-11-14 15:53:25 | [diff] [blame] | 306 | base::SequencedTaskRunner::GetCurrentDefault()->DeleteSoon( |
| 307 | FROM_HERE, std::move(decoder)); |
Robert Sesek | 31db673 | 2022-05-26 20:43:26 | [diff] [blame] | 308 | run_loop.Run(); |
| 309 | |
| 310 | EXPECT_FALSE(got_callback); |
| 311 | } |
| 312 | |
| 313 | IN_PROC_BROWSER_TEST_F(DataDecoderBrowserTest, |
| 314 | NoCallbackAfterDestruction_Gzip) { |
| 315 | base::RunLoop run_loop; |
| 316 | |
| 317 | auto decoder = std::make_unique<data_decoder::DataDecoder>(); |
| 318 | bool got_callback = false; |
| 319 | decoder->GzipCompress( |
| 320 | {{0x1, 0x1, 0x1, 0x1, 0x1, 0x1}}, |
| 321 | base::BindOnce( |
| 322 | [](bool* got_callback, base::ScopedClosureRunner quit_closure_runner, |
Claudio DeSouza | d2afab4 | 2022-07-14 20:40:51 | [diff] [blame] | 323 | base::expected<mojo_base::BigBuffer, std::string> result) { |
| 324 | *got_callback = true; |
| 325 | }, |
Robert Sesek | 31db673 | 2022-05-26 20:43:26 | [diff] [blame] | 326 | // Pass the quit closure as a ScopedClosureRunner, so that the loop |
| 327 | // is quit if the callback is destroyed un-run or after it runs. |
| 328 | &got_callback, base::ScopedClosureRunner(run_loop.QuitClosure()))); |
| 329 | |
Sean Maher | 52fa5a7 | 2022-11-14 15:53:25 | [diff] [blame] | 330 | base::SequencedTaskRunner::GetCurrentDefault()->DeleteSoon( |
| 331 | FROM_HERE, std::move(decoder)); |
Robert Sesek | 31db673 | 2022-05-26 20:43:26 | [diff] [blame] | 332 | run_loop.Run(); |
| 333 | |
| 334 | EXPECT_FALSE(got_callback); |
| 335 | } |
| 336 | |
Ken Rockot | 27a62d5 | 2019-10-30 01:57:01 | [diff] [blame] | 337 | } // namespace content |