blob: 72022dd14365bc1e4ad845f3a6b94eb1509003f4 [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"
Steven Holtea0270162017-12-01 03:23:0611#include "components/ukm/ukm_source.h"
Raymes Khoury978652c2017-11-27 06:28:3712#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
22namespace content {
23
24namespace {
25
26constexpr char kURL1[] = "http://google.com/";
27constexpr char kURL2[] = "http://youtube.com/";
28constexpr char kPepperBrokerEvent[] = "Pepper.Broker";
29
30class 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
42class PluginServiceImplTest : public RenderViewHostTestHarness {
43 public:
44 PluginServiceImplTest() = default;
45 ~PluginServiceImplTest() override = default;
46
47 void SetUp() override {
48 RenderViewHostTestHarness::SetUp();
49
Gyuyoung Kimf4e2e6522017-12-06 17:47:0850 test_ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();
Raymes Khoury978652c2017-11-27 06:28:3751 }
52
53 bool RecordedBrokerEvent(const GURL& url) {
54 RunAllPendingInMessageLoop(BrowserThread::UI);
Steven Holtea0270162017-12-01 03:23:0655 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 Khoury978652c2017-11-27 06:28:3763 }
64
65 void ResetUKM() {
Gyuyoung Kimf4e2e6522017-12-06 17:47:0866 test_ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();
Raymes Khoury978652c2017-11-27 06:28:3767 }
68
69 private:
70 std::unique_ptr<ukm::TestUkmRecorder> test_ukm_recorder_;
71
72 DISALLOW_COPY_AND_ASSIGN(PluginServiceImplTest);
73};
74
75TEST_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 Holtea0270162017-12-01 03:23:06110} // namespace content