summaryrefslogtreecommitdiffstats
path: root/chromium/content/browser/plugin_service_impl_unittest.cc
blob: 6fa3ef1528e740e2f34ec2993188b7a6b8a0a8f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/plugin_service_impl.h"

#include <memory>

#include "build/build_config.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/browser/ppapi_plugin_process_host.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_utils.h"
#include "services/metrics/public/cpp/ukm_source.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {

namespace {

constexpr char kURL1[] = "http://google.com/";
constexpr char kURL2[] = "http://youtube.com/";
constexpr char kPepperBrokerEvent[] = "Pepper.Broker";

class TestBrokerClient : public PpapiPluginProcessHost::BrokerClient {
 public:
  void GetPpapiChannelInfo(base::ProcessHandle* renderer_handle,
                           int* renderer_id) override {}
  void OnPpapiChannelOpened(const IPC::ChannelHandle& channel_handle,
                            base::ProcessId plugin_pid,
                            int plugin_child_id) override {}
  bool Incognito() override { return false; }
};

}  // anonymous namespace

class PluginServiceImplTest : public RenderViewHostTestHarness {
 public:
  PluginServiceImplTest() = default;
  ~PluginServiceImplTest() override = default;

  void SetUp() override {
    RenderViewHostTestHarness::SetUp();

    test_ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();
  }

  // Check if count events were recorded and if they were all recorded with
  // the specified source_id.
  bool RecordedBrokerEvents(ukm::SourceId source_id, size_t count) {
    RunAllPendingInMessageLoop(BrowserThread::UI);
    auto entries = test_ukm_recorder_->GetEntriesByName(kPepperBrokerEvent);
    if (entries.size() != count)
      return false;
    for (const auto* const entry : entries) {
      if (entry->source_id != source_id)
        return false;
    }
    return true;
  }

  void ResetUKM() {
    test_ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();
  }

 private:
  std::unique_ptr<ukm::TestUkmRecorder> test_ukm_recorder_;

  DISALLOW_COPY_AND_ASSIGN(PluginServiceImplTest);
};

TEST_F(PluginServiceImplTest, RecordBrokerUsage) {
  TestBrokerClient client;

  NavigateAndCommit(GURL(kURL1));
  ukm::SourceId source_id = static_cast<WebContentsImpl*>(web_contents())
                                ->GetMainFrame()
                                ->GetPageUkmSourceId();
  PluginServiceImpl* service = PluginServiceImpl::GetInstance();

  // Internal usage of the broker should not record metrics. Internal usage will
  // not pass a RFH.
  service->OpenChannelToPpapiBroker(0, 0, base::FilePath(), &client);
  EXPECT_TRUE(RecordedBrokerEvents(source_id, 0));

  // Top level frame usage should be recorded.
  int render_process_id = main_rfh()->GetProcess()->GetID();
  int render_frame_id = main_rfh()->GetRoutingID();
  service->OpenChannelToPpapiBroker(render_process_id, render_frame_id,
                                    base::FilePath(), &client);
  EXPECT_TRUE(RecordedBrokerEvents(source_id, 1));

  ResetUKM();

  // Iframe usage should be recorded under the top level frame origin.
  RenderFrameHost* child_frame =
      RenderFrameHostTester::For(main_rfh())->AppendChild("child");
  child_frame = NavigationSimulator::NavigateAndCommitFromDocument(GURL(kURL2),
                                                                   child_frame);
  EXPECT_EQ(GURL(kURL2), child_frame->GetLastCommittedURL());
  render_process_id = child_frame->GetProcess()->GetID();
  render_frame_id = child_frame->GetRoutingID();
  service->OpenChannelToPpapiBroker(render_process_id, render_frame_id,
                                    base::FilePath(), &client);
  EXPECT_TRUE(RecordedBrokerEvents(source_id, 1));
}

}  // namespace content