Chromium Code Reviews| Index: content/shell/browser/layout_test/layout_test_bluetooth_chooser_factory.cc |
| diff --git a/content/shell/browser/layout_test/layout_test_bluetooth_chooser_factory.cc b/content/shell/browser/layout_test/layout_test_bluetooth_chooser_factory.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ec5c7ac188634341c64bab612006981943154e18 |
| --- /dev/null |
| +++ b/content/shell/browser/layout_test/layout_test_bluetooth_chooser_factory.cc |
| @@ -0,0 +1,147 @@ |
| +// Copyright 2015 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/shell/browser/layout_test/layout_test_bluetooth_chooser_factory.h" |
| + |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "url/gurl.h" |
| + |
| +namespace content { |
| + |
| +class WebContents; |
| + |
| +// Implements a Bluetooth chooser that records events it's sent, instead of |
| +// showing a dialog, selects the |
|
ortuno
2015/09/02 16:14:25
Line wrapping.
Jeffrey Yasskin
2015/09/09 21:26:08
Actually, the content's all wrong too. Fixed.
|
| +// first added device. This is used as a default chooser implementation for |
| +// platforms without a full UI. |
| +class LayoutTestBluetoothChooserFactory::Chooser : public BluetoothChooser { |
| + public: |
| + Chooser(const base::WeakPtr<LayoutTestBluetoothChooserFactory>& factory, |
| + const EventHandler& event_handler) |
| + : event_handler(event_handler), factory_(factory) { |
| + DCHECK(factory); |
| + factory->choosers_.insert(this); |
| + } |
| + |
| + ~Chooser() override { |
| + if (factory_) { |
| + factory_->choosers_.erase(this); |
| + } |
| + } |
| + |
| + // BluetoothChooser: |
| + void SetAdapterPresence(AdapterPresence presence) override { |
| + if (!factory_) { |
|
ortuno
2015/09/02 16:14:25
Q: When would the factory be destroyed before the
Jeffrey Yasskin
2015/09/09 21:26:08
Good point: the factory does cancel all the choose
|
| + return; |
| + } |
| + std::string event = "SetAdapterPresence("; |
| + switch (presence) { |
| + case AdapterPresence::ABSENT: |
| + event += "ABSENT"; |
| + break; |
| + case AdapterPresence::POWERED_OFF: |
| + event += "POWERED_OFF"; |
| + break; |
| + case AdapterPresence::POWERED_ON: |
| + event += "POWERED_ON"; |
| + break; |
| + } |
| + event += ")"; |
| + factory_->events_.push_back(event); |
| + } |
| + |
| + void ShowDiscoveryState(DiscoveryState state) override { |
| + if (!factory_) { |
| + return; |
| + } |
| + std::string event = "ShowDiscoveryState("; |
| + switch (state) { |
| + case DiscoveryState::FAILED_TO_START: |
| + event += "FAILED_TO_START"; |
| + break; |
| + case DiscoveryState::DISCOVERING: |
| + event += "DISCOVERING"; |
| + break; |
| + case DiscoveryState::IDLE: |
| + event += "IDLE"; |
| + break; |
| + } |
| + event += ")"; |
| + factory_->events_.push_back(event); |
| + } |
| + |
| + void AddDevice(const std::string& device_id, |
| + const base::string16& device_name) override { |
| + if (!factory_) { |
| + return; |
| + } |
| + std::string event = "AddDevice("; |
| + event += device_id; |
| + event += ", "; |
| + event += UTF16ToUTF8(device_name); |
| + event += ")"; |
| + factory_->events_.push_back(event); |
| + } |
| + |
| + void RemoveDevice(const std::string& device_id) override { |
| + if (!factory_) { |
| + return; |
| + } |
| + std::string event = "RemoveDevice("; |
| + event += device_id; |
| + event += ")"; |
| + factory_->events_.push_back(event); |
| + } |
| + |
| + EventHandler event_handler; |
| + |
| + private: |
| + base::WeakPtr<LayoutTestBluetoothChooserFactory> factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Chooser); |
| +}; |
| + |
| +LayoutTestBluetoothChooserFactory::LayoutTestBluetoothChooserFactory() |
| + : weak_this_(this) {} |
| + |
| +LayoutTestBluetoothChooserFactory::~LayoutTestBluetoothChooserFactory() { |
| + // Copy |choosers_| to make sure event handler executions that modify |
| + // |choosers_| don't invalidate iterators. |
| + std::vector<Chooser*> choosers_copy(choosers_.begin(), choosers_.end()); |
|
ortuno
2015/09/02 16:14:25
This code is the same as SendEvent. Are calls to o
Jeffrey Yasskin
2015/09/09 21:26:08
Calls to non-virtual functions are fine in destruc
|
| + for (Chooser* chooser : choosers_copy) { |
| + chooser->event_handler.Run(BluetoothChooser::Event::CANCELLED, ""); |
| + } |
| +} |
| + |
| +scoped_ptr<BluetoothChooser> |
| +LayoutTestBluetoothChooserFactory::RunBluetoothChooser( |
| + WebContents* web_contents, |
| + const BluetoothChooser::EventHandler& event_handler, |
| + const GURL& origin) { |
| + std::string event = "RunBluetoothChooser("; |
| + event += origin.spec(); |
| + event += ")"; |
| + events_.push_back(event); |
| + return make_scoped_ptr(new Chooser(weak_this_.GetWeakPtr(), event_handler)); |
| +} |
| + |
| +std::vector<std::string> |
| +LayoutTestBluetoothChooserFactory::GetAndResetEvents() { |
| + std::vector<std::string> result; |
| + result.swap(events_); |
| + return result; |
| +} |
| + |
| +void LayoutTestBluetoothChooserFactory::SendEvent( |
| + BluetoothChooser::Event event, |
| + const std::string& device_id) { |
| + // Copy |choosers_| to make sure event handler executions that modify |
| + // |choosers_| don't invalidate iterators. |
| + std::vector<Chooser*> choosers_copy(choosers_.begin(), choosers_.end()); |
| + for (Chooser* chooser : choosers_copy) { |
| + chooser->event_handler.Run(event, device_id); |
| + } |
| +} |
| + |
| +} // namespace content |