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