Commit efc822a3 authored by Ovidio Henriquez's avatar Ovidio Henriquez Committed by Commit Bot

bluetooth: Chooser Context Implementation

This change implements the methods in BluetoothChooserContext and
creates a unit test for the class.

Design Doc:
https://docs.google.com/document/d/1h3uAVXJARHrNWaNACUPiQhLt7XI-fFFQoARSs1WgMDM/edit?usp=sharing

Bug: 589228
Change-Id: I1c564177063f9459081c45ddb4c0bb757fabd667
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1978819
Commit-Queue: Ovidio de Jesús Ruiz-Henríquez <odejesush@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarBalazs Engedy <engedy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#731730}
parent 6a796556
...@@ -169,6 +169,10 @@ jumbo_static_library("browser") { ...@@ -169,6 +169,10 @@ jumbo_static_library("browser") {
"bitmap_fetcher/bitmap_fetcher_service.h", "bitmap_fetcher/bitmap_fetcher_service.h",
"bitmap_fetcher/bitmap_fetcher_service_factory.cc", "bitmap_fetcher/bitmap_fetcher_service_factory.cc",
"bitmap_fetcher/bitmap_fetcher_service_factory.h", "bitmap_fetcher/bitmap_fetcher_service_factory.h",
"bluetooth/bluetooth_chooser_context.cc",
"bluetooth/bluetooth_chooser_context.h",
"bluetooth/bluetooth_chooser_context_factory.cc",
"bluetooth/bluetooth_chooser_context_factory.h",
"bookmarks/bookmark_model_factory.cc", "bookmarks/bookmark_model_factory.cc",
"bookmarks/bookmark_model_factory.h", "bookmarks/bookmark_model_factory.h",
"bookmarks/chrome_bookmark_client.cc", "bookmarks/chrome_bookmark_client.cc",
...@@ -3056,10 +3060,6 @@ jumbo_static_library("browser") { ...@@ -3056,10 +3060,6 @@ jumbo_static_library("browser") {
"badging/badge_manager_factory.h", "badging/badge_manager_factory.h",
"banners/app_banner_manager_desktop.cc", "banners/app_banner_manager_desktop.cc",
"banners/app_banner_manager_desktop.h", "banners/app_banner_manager_desktop.h",
"bluetooth/bluetooth_chooser_context.cc",
"bluetooth/bluetooth_chooser_context.h",
"bluetooth/bluetooth_chooser_context_factory.cc",
"bluetooth/bluetooth_chooser_context_factory.h",
"bookmarks/bookmark_html_writer.cc", "bookmarks/bookmark_html_writer.cc",
"bookmarks/bookmark_html_writer.h", "bookmarks/bookmark_html_writer.h",
"certificate_viewer.h", "certificate_viewer.h",
......
...@@ -5,12 +5,16 @@ ...@@ -5,12 +5,16 @@
#ifndef CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_ #ifndef CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_
#define CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_ #define CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_
#include <map>
#include <string> #include <string>
#include "base/containers/flat_set.h" #include "base/containers/flat_set.h"
#include "chrome/browser/permissions/chooser_context_base.h" #include "chrome/browser/permissions/chooser_context_base.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h" #include "device/bluetooth/public/cpp/bluetooth_uuid.h"
#include "third_party/blink/public/common/bluetooth/web_bluetooth_device_id.h" #include "third_party/blink/public/common/bluetooth/web_bluetooth_device_id.h"
#include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-forward.h"
namespace base { namespace base {
class Value; class Value;
...@@ -37,22 +41,27 @@ class BluetoothChooserContext : public ChooserContextBase { ...@@ -37,22 +41,27 @@ class BluetoothChooserContext : public ChooserContextBase {
// Helper methods for converting between a WebBluetoothDeviceId and a // Helper methods for converting between a WebBluetoothDeviceId and a
// Bluetooth device address string for a given origin pair. // Bluetooth device address string for a given origin pair.
const blink::WebBluetoothDeviceId GetWebBluetoothDeviceId( blink::WebBluetoothDeviceId GetWebBluetoothDeviceId(
const url::Origin& requesting_origin, const url::Origin& requesting_origin,
const url::Origin& embedding_origin, const url::Origin& embedding_origin,
const std::string& device_address); const std::string& device_address);
const std::string GetDeviceAddress( std::string GetDeviceAddress(const url::Origin& requesting_origin,
const url::Origin& embedding_origin,
const blink::WebBluetoothDeviceId& device_id);
// Bluetooth scanning specific interface for generating WebBluetoothDeviceIds
// for scanned devices.
blink::WebBluetoothDeviceId AddScannedDevice(
const url::Origin& requesting_origin, const url::Origin& requesting_origin,
const url::Origin& embedding_origin, const url::Origin& embedding_origin,
const blink::WebBluetoothDeviceId& device_id); const std::string& device_address);
// Bluetooth-specific interface for granting and checking permissions. // Bluetooth-specific interface for granting and checking permissions.
const blink::WebBluetoothDeviceId GrantDevicePermission( blink::WebBluetoothDeviceId GrantServiceAccessPermission(
const url::Origin& requesting_origin, const url::Origin& requesting_origin,
const url::Origin& embedding_origin, const url::Origin& embedding_origin,
const std::string& device_address, const device::BluetoothDevice* device,
base::flat_set<device::BluetoothUUID, device::BluetoothUUIDHash>& const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options);
services);
bool HasDevicePermission(const url::Origin& requesting_origin, bool HasDevicePermission(const url::Origin& requesting_origin,
const url::Origin& embedding_origin, const url::Origin& embedding_origin,
const blink::WebBluetoothDeviceId& device_id); const blink::WebBluetoothDeviceId& device_id);
...@@ -68,13 +77,15 @@ class BluetoothChooserContext : public ChooserContextBase { ...@@ -68,13 +77,15 @@ class BluetoothChooserContext : public ChooserContextBase {
protected: protected:
// ChooserContextBase implementation; // ChooserContextBase implementation;
bool IsValidObject(const base::Value& object) override; bool IsValidObject(const base::Value& object) override;
std::vector<std::unique_ptr<Object>> GetGrantedObjects(
const url::Origin& requesting_origin, private:
const url::Origin& embedding_origin) override; // This map records the generated Web Bluetooth IDs for devices discovered via
std::vector<std::unique_ptr<Object>> GetAllGrantedObjects() override; // the Scanning API. Each requesting/embedding origin pair has its own version
void RevokeObjectPermission(const url::Origin& requesting_origin, // of this map so that IDs cannot be correlated between cross-origin sites.
const url::Origin& embedding_origin, using DeviceAddressToIdMap =
const base::Value& object) override; std::map<std::string, blink::WebBluetoothDeviceId>;
std::map<std::pair<url::Origin, url::Origin>, DeviceAddressToIdMap>
scanned_devices_;
}; };
#endif // CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_ #endif // CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_
This diff is collapsed.
...@@ -153,6 +153,27 @@ void ChooserContextBase::GrantObjectPermission( ...@@ -153,6 +153,27 @@ void ChooserContextBase::GrantObjectPermission(
NotifyPermissionChanged(); NotifyPermissionChanged();
} }
void ChooserContextBase::UpdateObjectPermission(
const url::Origin& requesting_origin,
const url::Origin& embedding_origin,
base::Value& old_object,
base::Value new_object) {
base::Value setting =
GetWebsiteSetting(requesting_origin, embedding_origin, /*info=*/nullptr);
base::Value* objects = setting.FindListKey(kObjectListKey);
if (!objects)
return;
base::Value::ListView object_list = objects->GetList();
auto it = std::find(object_list.begin(), object_list.end(), old_object);
if (it == object_list.end())
return;
*it = std::move(new_object);
SetWebsiteSetting(requesting_origin, embedding_origin, std::move(setting));
NotifyPermissionChanged();
}
void ChooserContextBase::RevokeObjectPermission( void ChooserContextBase::RevokeObjectPermission(
const url::Origin& requesting_origin, const url::Origin& requesting_origin,
const url::Origin& embedding_origin, const url::Origin& embedding_origin,
......
...@@ -98,6 +98,14 @@ class ChooserContextBase : public KeyedService { ...@@ -98,6 +98,14 @@ class ChooserContextBase : public KeyedService {
const url::Origin& embedding_origin, const url::Origin& embedding_origin,
base::Value object); base::Value object);
// Updates |old_object| with |new_object| for |requesting_origin| when
// embedded within |embedding_origin|, and writes the value into
// |host_content_settings_map_|.
void UpdateObjectPermission(const url::Origin& requesting_origin,
const url::Origin& embedding_origin,
base::Value& old_object,
base::Value new_object);
// Revokes |requesting_origin|'s permission to access |object| when embedded // Revokes |requesting_origin|'s permission to access |object| when embedded
// within |embedding_origin|. // within |embedding_origin|.
// //
......
...@@ -141,6 +141,58 @@ TEST_F(ChooserContextBaseTest, GrantObjectPermissionEmbedded) { ...@@ -141,6 +141,58 @@ TEST_F(ChooserContextBaseTest, GrantObjectPermissionEmbedded) {
EXPECT_EQ(0u, objects.size()); EXPECT_EQ(0u, objects.size());
} }
TEST_F(ChooserContextBaseTest, GrantAndUpdateObjectPermission) {
TestChooserContext context(profile());
MockPermissionObserver mock_observer;
context.AddObserver(&mock_observer);
EXPECT_CALL(mock_observer, OnChooserObjectPermissionChanged(_, _));
context.GrantObjectPermission(origin1_, origin1_, object1_.Clone());
std::vector<std::unique_ptr<ChooserContextBase::Object>> objects =
context.GetGrantedObjects(origin1_, origin1_);
EXPECT_EQ(1u, objects.size());
EXPECT_EQ(object1_, objects[0]->value);
testing::Mock::VerifyAndClearExpectations(&mock_observer);
EXPECT_CALL(mock_observer, OnChooserObjectPermissionChanged(_, _));
context.UpdateObjectPermission(origin1_, origin1_, objects[0]->value,
object2_.Clone());
objects = context.GetGrantedObjects(origin1_, origin1_);
EXPECT_EQ(1u, objects.size());
EXPECT_EQ(object2_, objects[0]->value);
}
// UpdateObjectPermission() should not grant new permissions.
TEST_F(ChooserContextBaseTest,
UpdateObjectPermissionWithNonExistentPermission) {
TestChooserContext context(profile());
MockPermissionObserver mock_observer;
context.AddObserver(&mock_observer);
// Attempt to update permission for non-existent |object1_| permission.
EXPECT_CALL(mock_observer, OnChooserObjectPermissionChanged(_, _)).Times(0);
context.UpdateObjectPermission(origin1_, origin1_, object1_,
object2_.Clone());
testing::Mock::VerifyAndClearExpectations(&mock_observer);
std::vector<std::unique_ptr<ChooserContextBase::Object>> objects =
context.GetGrantedObjects(origin1_, origin1_);
EXPECT_TRUE(objects.empty());
// Grant permission for |object2_| but attempt to update permission for
// non-existent |object1_| permission again.
EXPECT_CALL(mock_observer, OnChooserObjectPermissionChanged(_, _));
context.GrantObjectPermission(origin1_, origin1_, object2_.Clone());
testing::Mock::VerifyAndClearExpectations(&mock_observer);
EXPECT_CALL(mock_observer, OnChooserObjectPermissionChanged(_, _)).Times(0);
context.UpdateObjectPermission(origin1_, origin1_, object1_,
object2_.Clone());
testing::Mock::VerifyAndClearExpectations(&mock_observer);
}
TEST_F(ChooserContextBaseTest, GetAllGrantedObjects) { TEST_F(ChooserContextBaseTest, GetAllGrantedObjects) {
TestChooserContext context(profile()); TestChooserContext context(profile());
MockPermissionObserver mock_observer; MockPermissionObserver mock_observer;
......
...@@ -3025,6 +3025,7 @@ test("unit_tests") { ...@@ -3025,6 +3025,7 @@ test("unit_tests") {
"../browser/background_sync/periodic_background_sync_permission_context_unittest.cc", "../browser/background_sync/periodic_background_sync_permission_context_unittest.cc",
"../browser/banners/app_banner_settings_helper_unittest.cc", "../browser/banners/app_banner_settings_helper_unittest.cc",
"../browser/bitmap_fetcher/bitmap_fetcher_service_unittest.cc", "../browser/bitmap_fetcher/bitmap_fetcher_service_unittest.cc",
"../browser/bluetooth/bluetooth_chooser_context_unittest.cc",
"../browser/bookmarks/managed_bookmark_service_unittest.cc", "../browser/bookmarks/managed_bookmark_service_unittest.cc",
"../browser/browser_about_handler_unittest.cc", "../browser/browser_about_handler_unittest.cc",
"../browser/browsing_data/browsing_data_appcache_helper_unittest.cc", "../browser/browsing_data/browsing_data_appcache_helper_unittest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment