Raymes Khoury | 978652c | 2017-11-27 06:28:37 | [diff] [blame] | 1 | // Copyright 2017 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 "content/browser/plugin_service_impl.h" |
| 6 | |
Gyuyoung Kim | f4e2e652 | 2017-12-06 17:47:08 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
Raymes Khoury | 978652c | 2017-11-27 06:28:37 | [diff] [blame] | 9 | #include "build/build_config.h" |
| 10 | #include "components/ukm/test_ukm_recorder.h" |
| 11 | #include "content/browser/ppapi_plugin_process_host.h" |
Steven Holte | 3804134 | 2018-06-22 22:00:57 | [diff] [blame] | 12 | #include "content/browser/web_contents/web_contents_impl.h" |
Raymes Khoury | 978652c | 2017-11-27 06:28:37 | [diff] [blame] | 13 | #include "content/public/browser/browser_thread.h" |
| 14 | #include "content/public/browser/render_frame_host.h" |
| 15 | #include "content/public/browser/render_process_host.h" |
| 16 | #include "content/public/browser/web_contents.h" |
| 17 | #include "content/public/test/navigation_simulator.h" |
| 18 | #include "content/public/test/test_renderer_host.h" |
| 19 | #include "content/public/test/test_utils.h" |
Charlie Harrison | ef958bf | 2018-08-20 14:49:02 | [diff] [blame] | 20 | #include "services/metrics/public/cpp/ukm_source.h" |
Raymes Khoury | 978652c | 2017-11-27 06:28:37 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
| 23 | namespace content { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | constexpr char kURL1[] = "http://google.com/"; |
| 28 | constexpr char kURL2[] = "http://youtube.com/"; |
| 29 | constexpr char kPepperBrokerEvent[] = "Pepper.Broker"; |
| 30 | |
| 31 | class TestBrokerClient : public PpapiPluginProcessHost::BrokerClient { |
| 32 | public: |
| 33 | void GetPpapiChannelInfo(base::ProcessHandle* renderer_handle, |
| 34 | int* renderer_id) override {} |
| 35 | void OnPpapiChannelOpened(const IPC::ChannelHandle& channel_handle, |
| 36 | base::ProcessId plugin_pid, |
| 37 | int plugin_child_id) override {} |
| 38 | bool Incognito() override { return false; } |
| 39 | }; |
| 40 | |
| 41 | } // anonymous namespace |
| 42 | |
| 43 | class PluginServiceImplTest : public RenderViewHostTestHarness { |
| 44 | public: |
| 45 | PluginServiceImplTest() = default; |
| 46 | ~PluginServiceImplTest() override = default; |
| 47 | |
| 48 | void SetUp() override { |
| 49 | RenderViewHostTestHarness::SetUp(); |
| 50 | |
Gyuyoung Kim | f4e2e652 | 2017-12-06 17:47:08 | [diff] [blame] | 51 | test_ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>(); |
Raymes Khoury | 978652c | 2017-11-27 06:28:37 | [diff] [blame] | 52 | } |
| 53 | |
Steven Holte | 3804134 | 2018-06-22 22:00:57 | [diff] [blame] | 54 | // Check if count events were recorded and if they were all recorded with |
| 55 | // the specified source_id. |
| 56 | bool RecordedBrokerEvents(ukm::SourceId source_id, size_t count) { |
Raymes Khoury | 978652c | 2017-11-27 06:28:37 | [diff] [blame] | 57 | RunAllPendingInMessageLoop(BrowserThread::UI); |
Steven Holte | a027016 | 2017-12-01 03:23:06 | [diff] [blame] | 58 | auto entries = test_ukm_recorder_->GetEntriesByName(kPepperBrokerEvent); |
Steven Holte | 3804134 | 2018-06-22 22:00:57 | [diff] [blame] | 59 | if (entries.size() != count) |
| 60 | return false; |
Steven Holte | a027016 | 2017-12-01 03:23:06 | [diff] [blame] | 61 | for (const auto* const entry : entries) { |
Steven Holte | 3804134 | 2018-06-22 22:00:57 | [diff] [blame] | 62 | if (entry->source_id != source_id) |
| 63 | return false; |
Steven Holte | a027016 | 2017-12-01 03:23:06 | [diff] [blame] | 64 | } |
Steven Holte | 3804134 | 2018-06-22 22:00:57 | [diff] [blame] | 65 | return true; |
Raymes Khoury | 978652c | 2017-11-27 06:28:37 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void ResetUKM() { |
Gyuyoung Kim | f4e2e652 | 2017-12-06 17:47:08 | [diff] [blame] | 69 | test_ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>(); |
Raymes Khoury | 978652c | 2017-11-27 06:28:37 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | private: |
| 73 | std::unique_ptr<ukm::TestUkmRecorder> test_ukm_recorder_; |
| 74 | |
| 75 | DISALLOW_COPY_AND_ASSIGN(PluginServiceImplTest); |
| 76 | }; |
| 77 | |
| 78 | TEST_F(PluginServiceImplTest, RecordBrokerUsage) { |
| 79 | TestBrokerClient client; |
| 80 | |
| 81 | NavigateAndCommit(GURL(kURL1)); |
Steven Holte | 3804134 | 2018-06-22 22:00:57 | [diff] [blame] | 82 | ukm::SourceId source_id = static_cast<WebContentsImpl*>(web_contents()) |
| 83 | ->GetUkmSourceIdForLastCommittedSource(); |
Raymes Khoury | 978652c | 2017-11-27 06:28:37 | [diff] [blame] | 84 | PluginServiceImpl* service = PluginServiceImpl::GetInstance(); |
| 85 | |
| 86 | // Internal usage of the broker should not record metrics. Internal usage will |
| 87 | // not pass a RFH. |
| 88 | service->OpenChannelToPpapiBroker(0, 0, base::FilePath(), &client); |
Steven Holte | 3804134 | 2018-06-22 22:00:57 | [diff] [blame] | 89 | EXPECT_TRUE(RecordedBrokerEvents(source_id, 0)); |
Raymes Khoury | 978652c | 2017-11-27 06:28:37 | [diff] [blame] | 90 | |
| 91 | // Top level frame usage should be recorded. |
| 92 | int render_process_id = main_rfh()->GetProcess()->GetID(); |
| 93 | int render_frame_id = main_rfh()->GetRoutingID(); |
| 94 | service->OpenChannelToPpapiBroker(render_process_id, render_frame_id, |
| 95 | base::FilePath(), &client); |
Steven Holte | 3804134 | 2018-06-22 22:00:57 | [diff] [blame] | 96 | EXPECT_TRUE(RecordedBrokerEvents(source_id, 1)); |
Raymes Khoury | 978652c | 2017-11-27 06:28:37 | [diff] [blame] | 97 | |
| 98 | ResetUKM(); |
| 99 | |
| 100 | // Iframe usage should be recorded under the top level frame origin. |
| 101 | RenderFrameHost* child_frame = |
| 102 | RenderFrameHostTester::For(main_rfh())->AppendChild("child"); |
| 103 | child_frame = NavigationSimulator::NavigateAndCommitFromDocument(GURL(kURL2), |
| 104 | child_frame); |
| 105 | EXPECT_EQ(GURL(kURL2), child_frame->GetLastCommittedURL()); |
| 106 | render_process_id = child_frame->GetProcess()->GetID(); |
| 107 | render_frame_id = child_frame->GetRoutingID(); |
| 108 | service->OpenChannelToPpapiBroker(render_process_id, render_frame_id, |
| 109 | base::FilePath(), &client); |
Steven Holte | 3804134 | 2018-06-22 22:00:57 | [diff] [blame] | 110 | EXPECT_TRUE(RecordedBrokerEvents(source_id, 1)); |
Raymes Khoury | 978652c | 2017-11-27 06:28:37 | [diff] [blame] | 111 | } |
| 112 | |
Steven Holte | a027016 | 2017-12-01 03:23:06 | [diff] [blame] | 113 | } // namespace content |