Roll src/third_party/skia 2440fcd:4de8c3a
[chromium-blink-merge.git] / content / shell / browser / layout_test / layout_test_bluetooth_chooser_factory.cc
blobb266330230618acf7fe3fd49fd34044e0e79c341
1 // Copyright 2015 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.
5 #include "content/shell/browser/layout_test/layout_test_bluetooth_chooser_factory.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "url/gurl.h"
10 namespace content {
12 class WebContents;
14 // Implements a Bluetooth chooser that records events it's sent, instead of
15 // showing a dialog. It allows tests to control how the chooser responds.
16 class LayoutTestBluetoothChooserFactory::Chooser : public BluetoothChooser {
17 public:
18 Chooser(const base::WeakPtr<LayoutTestBluetoothChooserFactory>& factory,
19 const EventHandler& event_handler)
20 : event_handler(event_handler), factory_(factory) {
21 DCHECK(factory);
22 factory->choosers_.insert(this);
25 ~Chooser() override {
26 CheckFactory();
27 factory_->choosers_.erase(this);
30 // BluetoothChooser:
31 void SetAdapterPresence(AdapterPresence presence) override {
32 CheckFactory();
33 switch (presence) {
34 case AdapterPresence::ABSENT:
35 factory_->events_.push_back("adapter-removed");
36 break;
37 case AdapterPresence::POWERED_OFF:
38 factory_->events_.push_back("adapter-disabled");
39 break;
40 case AdapterPresence::POWERED_ON:
41 factory_->events_.push_back("adapter-enabled");
42 break;
46 void ShowDiscoveryState(DiscoveryState state) override {
47 CheckFactory();
48 switch (state) {
49 case DiscoveryState::FAILED_TO_START:
50 factory_->events_.push_back("discovery-failed-to-start");
51 break;
52 case DiscoveryState::DISCOVERING:
53 factory_->events_.push_back("discovering");
54 break;
55 case DiscoveryState::IDLE:
56 factory_->events_.push_back("discovery-idle");
57 break;
61 void AddDevice(const std::string& device_id,
62 const base::string16& device_name) override {
63 CheckFactory();
64 std::string event = "add-device(";
65 event += base::UTF16ToUTF8(device_name);
66 event += ")=";
67 event += device_id;
68 factory_->events_.push_back(event);
71 void RemoveDevice(const std::string& device_id) override {
72 CheckFactory();
73 std::string event = "remove-device(";
74 event += device_id;
75 event += ")";
76 factory_->events_.push_back(event);
79 EventHandler event_handler;
81 private:
82 void CheckFactory() const {
83 CHECK(factory_) << "The factory should cancel all choosers in its "
84 "destructor, and choosers should be destroyed "
85 "synchronously when canceled.";
88 base::WeakPtr<LayoutTestBluetoothChooserFactory> factory_;
90 DISALLOW_COPY_AND_ASSIGN(Chooser);
93 LayoutTestBluetoothChooserFactory::LayoutTestBluetoothChooserFactory()
94 : weak_this_(this) {}
96 LayoutTestBluetoothChooserFactory::~LayoutTestBluetoothChooserFactory() {
97 SendEvent(BluetoothChooser::Event::CANCELLED, "");
100 scoped_ptr<BluetoothChooser>
101 LayoutTestBluetoothChooserFactory::RunBluetoothChooser(
102 WebContents* web_contents,
103 const BluetoothChooser::EventHandler& event_handler,
104 const GURL& origin) {
105 std::string event = "chooser-opened(";
106 event += origin.spec();
107 event += ")";
108 events_.push_back(event);
109 return make_scoped_ptr(new Chooser(weak_this_.GetWeakPtr(), event_handler));
112 std::vector<std::string>
113 LayoutTestBluetoothChooserFactory::GetAndResetEvents() {
114 std::vector<std::string> result;
115 result.swap(events_);
116 return result;
119 void LayoutTestBluetoothChooserFactory::SendEvent(
120 BluetoothChooser::Event event,
121 const std::string& device_id) {
122 // Copy |choosers_| to make sure event handler executions that modify
123 // |choosers_| don't invalidate iterators.
124 std::vector<Chooser*> choosers_copy(choosers_.begin(), choosers_.end());
125 for (Chooser* chooser : choosers_copy) {
126 chooser->event_handler.Run(event, device_id);
130 } // namespace content