OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/browser/bluetooth/bluetooth_dispatcher_host.h" |
| 6 |
| 7 #include "base/run_loop.h" |
| 8 #include "content/common/bluetooth/bluetooth_messages.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/content_browser_client.h" |
| 11 #include "content/public/test/mock_render_process_host.h" |
| 12 #include "content/public/test/test_renderer_host.h" |
| 13 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 14 |
| 15 namespace content { |
| 16 namespace { |
| 17 |
| 18 class BluetoothDispatcherHostTest : public RenderViewHostTestHarness { |
| 19 protected: |
| 20 void TearDown() override { |
| 21 if (old_browser_client_) { |
| 22 SetBrowserClientForTesting(old_browser_client_); |
| 23 } |
| 24 RenderViewHostTestHarness::TearDown(); |
| 25 } |
| 26 |
| 27 void SetBrowserClient(ContentBrowserClient* new_browser_client) { |
| 28 EXPECT_FALSE(old_browser_client_); |
| 29 old_browser_client_ = SetBrowserClientForTesting(new_browser_client); |
| 30 } |
| 31 |
| 32 ContentBrowserClient* old_browser_client_ = nullptr; |
| 33 }; |
| 34 |
| 35 class WaitForRequestDeviceMessage : public IPC::Listener { |
| 36 public: |
| 37 explicit WaitForRequestDeviceMessage(base::RunLoop* loop) : loop_(loop) {} |
| 38 |
| 39 bool OnMessageReceived(const IPC::Message& msg) override { |
| 40 if (msg.type() == BluetoothMsg_RequestDeviceError::ID || |
| 41 msg.type() == BluetoothMsg_RequestDeviceSuccess::ID) { |
| 42 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 43 loop_->QuitClosure()); |
| 44 } |
| 45 return false; // Store the message in the sink. |
| 46 } |
| 47 |
| 48 private: |
| 49 base::RunLoop* loop_; |
| 50 }; |
| 51 |
| 52 class DisallowBluetoothContentBrowserClient : public ContentBrowserClient { |
| 53 bool AllowWebBluetooth() override { return false; } |
| 54 }; |
| 55 |
| 56 // PolicyTest.BlockWebBluetooth tests that policy affects the |
| 57 // ContentBrowserClient. |
| 58 TEST_F(BluetoothDispatcherHostTest, ContentClientCanDisableAPI) { |
| 59 NavigateAndCommit(GURL("https://example.com/")); |
| 60 ASSERT_EQ("https://example.com", |
| 61 main_rfh()->GetLastCommittedOrigin().Serialize()); |
| 62 |
| 63 scoped_refptr<content::BluetoothDispatcherHost> bluetooth = |
| 64 new content::BluetoothDispatcherHost(process()->GetID()); |
| 65 process()->AddFilter(bluetooth.get()); |
| 66 |
| 67 DisallowBluetoothContentBrowserClient disallow_bluetooth_client; |
| 68 SetBrowserClient(&disallow_bluetooth_client); |
| 69 |
| 70 // Make the adapter say it's present. |
| 71 scoped_refptr<device::MockBluetoothAdapter> adapter = |
| 72 new testing::NiceMock<device::MockBluetoothAdapter>; |
| 73 EXPECT_CALL(*adapter, IsPresent()).WillRepeatedly(testing::Return(true)); |
| 74 bluetooth->SetBluetoothAdapterForTesting(adapter); |
| 75 |
| 76 // Build a filter for the requestDevice() call. |
| 77 std::vector<content::BluetoothScanFilter> scan_filters; |
| 78 content::BluetoothScanFilter scan_filter; |
| 79 scan_filter.name = "Hello"; |
| 80 scan_filters.push_back(scan_filter); |
| 81 |
| 82 base::RunLoop request_device_loop; |
| 83 WaitForRequestDeviceMessage waiter(&request_device_loop); |
| 84 process()->sink().AddFilter(&waiter); |
| 85 |
| 86 bluetooth->OnMessageReceived(BluetoothHostMsg_RequestDevice( |
| 87 1, 1, main_rfh()->GetRoutingID(), scan_filters, |
| 88 std::vector<device::BluetoothUUID>())); |
| 89 |
| 90 request_device_loop.Run(); |
| 91 |
| 92 // Check that we reject with the right message. |
| 93 const IPC::Message* response_msg = process()->sink().GetUniqueMessageMatching( |
| 94 BluetoothMsg_RequestDeviceError::ID); |
| 95 ASSERT_TRUE(response_msg); |
| 96 BluetoothMsg_RequestDeviceError::Param response; |
| 97 BluetoothMsg_RequestDeviceError::Read(response_msg, &response); |
| 98 EXPECT_EQ(blink::WebBluetoothError::ChooserDisabled, base::get<2>(response)); |
| 99 |
| 100 // Avoid leaking the MockBluetoothAdapter. |
| 101 bluetooth->SetBluetoothAdapterForTesting(nullptr); |
| 102 |
| 103 // Make sure no more messages go through the BluetoothDispatcherHost after |
| 104 // it's destroyed. |
| 105 DeleteContents(); |
| 106 } |
| 107 |
| 108 } // namespace |
| 109 } // namespace content |
OLD | NEW |