blob: 6156e81e9440680f8e7c8ecbf3a2b87b05bae388 [file] [log] [blame]
Raymes Khoury978652c2017-11-27 06:28:371// 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 Kimf4e2e6522017-12-06 17:47:087#include <memory>
8
Raymes Khoury978652c2017-11-27 06:28:379#include "build/build_config.h"
10#include "components/ukm/test_ukm_recorder.h"
11#include "content/browser/ppapi_plugin_process_host.h"
Steven Holte38041342018-06-22 22:00:5712#include "content/browser/web_contents/web_contents_impl.h"
Raymes Khoury978652c2017-11-27 06:28:3713#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 Harrisonef958bf2018-08-20 14:49:0220#include "services/metrics/public/cpp/ukm_source.h"
Raymes Khoury978652c2017-11-27 06:28:3721#include "testing/gtest/include/gtest/gtest.h"
22
23namespace content {
24
25namespace {
26
27constexpr char kURL1[] = "http://google.com/";
28constexpr char kURL2[] = "http://youtube.com/";
29constexpr char kPepperBrokerEvent[] = "Pepper.Broker";
30
31class 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
43class PluginServiceImplTest : public RenderViewHostTestHarness {
44 public:
45 PluginServiceImplTest() = default;
46 ~PluginServiceImplTest() override = default;
47
48 void SetUp() override {
49 RenderViewHostTestHarness::SetUp();
50
Gyuyoung Kimf4e2e6522017-12-06 17:47:0851 test_ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();
Raymes Khoury978652c2017-11-27 06:28:3752 }
53
Steven Holte38041342018-06-22 22:00:5754 // 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 Khoury978652c2017-11-27 06:28:3757 RunAllPendingInMessageLoop(BrowserThread::UI);
Steven Holtea0270162017-12-01 03:23:0658 auto entries = test_ukm_recorder_->GetEntriesByName(kPepperBrokerEvent);
Steven Holte38041342018-06-22 22:00:5759 if (entries.size() != count)
60 return false;
Steven Holtea0270162017-12-01 03:23:0661 for (const auto* const entry : entries) {
Steven Holte38041342018-06-22 22:00:5762 if (entry->source_id != source_id)
63 return false;
Steven Holtea0270162017-12-01 03:23:0664 }
Steven Holte38041342018-06-22 22:00:5765 return true;
Raymes Khoury978652c2017-11-27 06:28:3766 }
67
68 void ResetUKM() {
Gyuyoung Kimf4e2e6522017-12-06 17:47:0869 test_ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();
Raymes Khoury978652c2017-11-27 06:28:3770 }
71
72 private:
73 std::unique_ptr<ukm::TestUkmRecorder> test_ukm_recorder_;
74
75 DISALLOW_COPY_AND_ASSIGN(PluginServiceImplTest);
76};
77
78TEST_F(PluginServiceImplTest, RecordBrokerUsage) {
79 TestBrokerClient client;
80
81 NavigateAndCommit(GURL(kURL1));
Steven Holte38041342018-06-22 22:00:5782 ukm::SourceId source_id = static_cast<WebContentsImpl*>(web_contents())
83 ->GetUkmSourceIdForLastCommittedSource();
Raymes Khoury978652c2017-11-27 06:28:3784 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 Holte38041342018-06-22 22:00:5789 EXPECT_TRUE(RecordedBrokerEvents(source_id, 0));
Raymes Khoury978652c2017-11-27 06:28:3790
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 Holte38041342018-06-22 22:00:5796 EXPECT_TRUE(RecordedBrokerEvents(source_id, 1));
Raymes Khoury978652c2017-11-27 06:28:3797
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 Holte38041342018-06-22 22:00:57110 EXPECT_TRUE(RecordedBrokerEvents(source_id, 1));
Raymes Khoury978652c2017-11-27 06:28:37111}
112
Steven Holtea0270162017-12-01 03:23:06113} // namespace content