OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "content/shell/browser/layout_test/layout_test_bluetooth_chooser_factor y.h" | |
6 | |
7 #include "base/strings/utf_string_conversions.h" | |
8 #include "url/gurl.h" | |
9 | |
10 namespace content { | |
11 | |
12 class WebContents; | |
13 | |
14 // Implements a Bluetooth chooser that records events it's sent, instead of | |
15 // showing a dialog, selects the | |
ortuno
2015/09/09 22:29:10
Line wrapping.
| |
16 // first added device. This is used as a default chooser implementation for | |
17 // platforms without a full UI. | |
18 class LayoutTestBluetoothChooserFactory::Chooser : public BluetoothChooser { | |
19 public: | |
20 Chooser(const base::WeakPtr<LayoutTestBluetoothChooserFactory>& factory, | |
21 const EventHandler& event_handler) | |
22 : event_handler(event_handler), factory_(factory) { | |
23 DCHECK(factory); | |
24 factory->choosers_.insert(this); | |
25 } | |
26 | |
27 ~Chooser() override { | |
28 if (factory_) { | |
29 factory_->choosers_.erase(this); | |
30 } | |
31 } | |
32 | |
33 // BluetoothChooser: | |
34 void SetAdapterPresence(AdapterPresence presence) override { | |
35 if (!factory_) { | |
36 return; | |
37 } | |
38 switch (presence) { | |
39 case AdapterPresence::ABSENT: | |
40 factory_->events_.push_back("adapter-removed"); | |
41 break; | |
42 case AdapterPresence::POWERED_OFF: | |
43 factory_->events_.push_back("adapter-disabled"); | |
44 break; | |
45 case AdapterPresence::POWERED_ON: | |
46 factory_->events_.push_back("adapter-enabled"); | |
47 break; | |
48 } | |
49 } | |
50 | |
51 void ShowDiscoveryState(DiscoveryState state) override { | |
52 if (!factory_) { | |
53 return; | |
54 } | |
55 switch (state) { | |
56 case DiscoveryState::FAILED_TO_START: | |
57 factory_->events_.push_back("discovery-failed-to-start"); | |
58 break; | |
59 case DiscoveryState::DISCOVERING: | |
60 factory_->events_.push_back("discovering"); | |
61 break; | |
62 case DiscoveryState::IDLE: | |
63 factory_->events_.push_back("discovery-idle"); | |
64 break; | |
65 } | |
66 } | |
67 | |
68 void AddDevice(const std::string& device_id, | |
69 const base::string16& device_name) override { | |
70 if (!factory_) { | |
71 return; | |
72 } | |
73 std::string event = "add-device("; | |
74 event += UTF16ToUTF8(device_name); | |
75 event += ")="; | |
76 event += device_id; | |
77 factory_->events_.push_back(event); | |
78 } | |
79 | |
80 void RemoveDevice(const std::string& device_id) override { | |
81 if (!factory_) { | |
82 return; | |
83 } | |
84 std::string event = "remove-device("; | |
85 event += device_id; | |
86 event += ")"; | |
87 factory_->events_.push_back(event); | |
88 } | |
89 | |
90 EventHandler event_handler; | |
91 | |
92 private: | |
93 base::WeakPtr<LayoutTestBluetoothChooserFactory> factory_; | |
94 | |
95 DISALLOW_COPY_AND_ASSIGN(Chooser); | |
96 }; | |
97 | |
98 LayoutTestBluetoothChooserFactory::LayoutTestBluetoothChooserFactory() | |
99 : weak_this_(this) {} | |
100 | |
101 LayoutTestBluetoothChooserFactory::~LayoutTestBluetoothChooserFactory() { | |
102 // Copy |choosers_| to make sure event handler executions that modify | |
103 // |choosers_| don't invalidate iterators. | |
104 std::vector<Chooser*> choosers_copy(choosers_.begin(), choosers_.end()); | |
105 for (Chooser* chooser : choosers_copy) { | |
106 chooser->event_handler.Run(BluetoothChooser::Event::CANCELLED, ""); | |
107 } | |
108 } | |
109 | |
110 scoped_ptr<BluetoothChooser> | |
111 LayoutTestBluetoothChooserFactory::RunBluetoothChooser( | |
112 WebContents* web_contents, | |
113 const BluetoothChooser::EventHandler& event_handler, | |
114 const GURL& origin) { | |
115 std::string event = "chooser-opened("; | |
116 event += origin.spec(); | |
117 event += ")"; | |
118 events_.push_back(event); | |
119 return make_scoped_ptr(new Chooser(weak_this_.GetWeakPtr(), event_handler)); | |
120 } | |
121 | |
122 std::vector<std::string> | |
123 LayoutTestBluetoothChooserFactory::GetAndResetEvents() { | |
124 std::vector<std::string> result; | |
125 result.swap(events_); | |
126 return result; | |
127 } | |
128 | |
129 void LayoutTestBluetoothChooserFactory::SendEvent( | |
130 BluetoothChooser::Event event, | |
131 const std::string& device_id) { | |
132 // Copy |choosers_| to make sure event handler executions that modify | |
133 // |choosers_| don't invalidate iterators. | |
134 std::vector<Chooser*> choosers_copy(choosers_.begin(), choosers_.end()); | |
135 for (Chooser* chooser : choosers_copy) { | |
136 chooser->event_handler.Run(event, device_id); | |
137 } | |
138 } | |
139 | |
140 } // namespace content | |
OLD | NEW |