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..c3bf190e50f4617c3556071c12b6c298a205b71b |
| --- /dev/null |
| +++ b/content/shell/browser/layout_test/layout_test_bluetooth_chooser_factory.cc |
| @@ -0,0 +1,140 @@ |
| +// 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/09 22:29:10
Line wrapping.
|
| +// 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_) { |
| + return; |
| + } |
| + switch (presence) { |
| + case AdapterPresence::ABSENT: |
| + factory_->events_.push_back("adapter-removed"); |
| + break; |
| + case AdapterPresence::POWERED_OFF: |
| + factory_->events_.push_back("adapter-disabled"); |
| + break; |
| + case AdapterPresence::POWERED_ON: |
| + factory_->events_.push_back("adapter-enabled"); |
| + break; |
| + } |
| + } |
| + |
| + void ShowDiscoveryState(DiscoveryState state) override { |
| + if (!factory_) { |
| + return; |
| + } |
| + switch (state) { |
| + case DiscoveryState::FAILED_TO_START: |
| + factory_->events_.push_back("discovery-failed-to-start"); |
| + break; |
| + case DiscoveryState::DISCOVERING: |
| + factory_->events_.push_back("discovering"); |
| + break; |
| + case DiscoveryState::IDLE: |
| + factory_->events_.push_back("discovery-idle"); |
| + break; |
| + } |
| + } |
| + |
| + void AddDevice(const std::string& device_id, |
| + const base::string16& device_name) override { |
| + if (!factory_) { |
| + return; |
| + } |
| + std::string event = "add-device("; |
| + event += UTF16ToUTF8(device_name); |
| + event += ")="; |
| + event += device_id; |
| + factory_->events_.push_back(event); |
| + } |
| + |
| + void RemoveDevice(const std::string& device_id) override { |
| + if (!factory_) { |
| + return; |
| + } |
| + std::string event = "remove-device("; |
| + 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()); |
| + 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 = "chooser-opened("; |
| + 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 |