diff --git a/brightray/browser/browser_context.cc b/brightray/browser/browser_context.cc index 7694d6143a4..bbb2a4e7f9b 100644 --- a/brightray/browser/browser_context.cc +++ b/brightray/browser/browser_context.cc @@ -4,6 +4,7 @@ #include "browser/browser_context.h" +#include "browser/media/media_device_id_salt.h" #include "browser/brightray_paths.h" #include "browser/browser_client.h" #include "browser/inspectable_web_contents_impl.h" @@ -56,6 +57,13 @@ class BrowserContext::ResourceContext : public content::ResourceContext { return getter_->GetURLRequestContext(); } + std::string GetMediaDeviceIDSalt() override { + auto media_device_id_salt_ = getter_->GetMediaDeviceIDSalt(); + if (media_device_id_salt_) + return media_device_id_salt_->GetSalt(); + return content::ResourceContext::GetMediaDeviceIDSalt(); + } + URLRequestContextGetter* getter_; }; @@ -114,6 +122,7 @@ void BrowserContext::InitPrefs() { void BrowserContext::RegisterInternalPrefs(PrefRegistrySimple* registry) { InspectableWebContentsImpl::RegisterPrefs(registry); + MediaDeviceIDSalt::RegisterPrefs(registry); } URLRequestContextGetter* BrowserContext::GetRequestContext() { @@ -143,6 +152,14 @@ net::NetworkDelegate* BrowserContext::CreateNetworkDelegate() { return new NetworkDelegate; } +MediaDeviceIDSalt* BrowserContext::GetMediaDeviceIDSalt() { + if (IsOffTheRecord()) + return nullptr; + if (!media_device_id_salt_.get()) + media_device_id_salt_.reset(new MediaDeviceIDSalt(prefs_.get())); + return media_device_id_salt_.get(); +} + base::FilePath BrowserContext::GetPath() const { return path_; } diff --git a/brightray/browser/browser_context.h b/brightray/browser/browser_context.h index 868c253aaed..498fe9940ad 100644 --- a/brightray/browser/browser_context.h +++ b/brightray/browser/browser_context.h @@ -24,6 +24,7 @@ class SpecialStoragePolicy; namespace brightray { +class MediaDeviceIDSalt; class PermissionManager; class BrowserContext : public base::RefCounted, @@ -87,6 +88,7 @@ class BrowserContext : public base::RefCounted, // URLRequestContextGetter::Delegate: net::NetworkDelegate* CreateNetworkDelegate() override; + MediaDeviceIDSalt* GetMediaDeviceIDSalt() override; base::FilePath GetPath() const override; @@ -128,6 +130,7 @@ class BrowserContext : public base::RefCounted, scoped_refptr storage_policy_; std::unique_ptr prefs_; std::unique_ptr permission_manager_; + std::unique_ptr media_device_id_salt_; base::WeakPtrFactory weak_factory_; diff --git a/brightray/browser/media/media_device_id_salt.cc b/brightray/browser/media/media_device_id_salt.cc new file mode 100644 index 00000000000..612449b25de --- /dev/null +++ b/brightray/browser/media/media_device_id_salt.cc @@ -0,0 +1,53 @@ +// Copyright 2013 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 "browser/media/media_device_id_salt.h" + +#include "components/prefs/pref_registry_simple.h" +#include "components/prefs/pref_service.h" +#include "content/public/browser/browser_thread.h" +#include "content/public/browser/resource_context.h" + +using content::BrowserThread; + +namespace brightray { + +namespace { + +const char kMediaDeviceIdSalt[] = "brightray.media.device_id_salt"; + +} // namespace + +MediaDeviceIDSalt::MediaDeviceIDSalt(PrefService* pref_service) { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + + media_device_id_salt_.Init(kMediaDeviceIdSalt, pref_service); + if (media_device_id_salt_.GetValue().empty()) { + media_device_id_salt_.SetValue( + content::ResourceContext::CreateRandomMediaDeviceIDSalt()); + } +} + +MediaDeviceIDSalt::~MediaDeviceIDSalt() { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + media_device_id_salt_.Destroy(); +} + +std::string MediaDeviceIDSalt::GetSalt() { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + return media_device_id_salt_.GetValue(); +} + +// static +void MediaDeviceIDSalt::RegisterPrefs(PrefRegistrySimple* registry) { + registry->RegisterStringPref(kMediaDeviceIdSalt, std::string()); +} + +// static +void MediaDeviceIDSalt::Reset(PrefService* pref_service) { + pref_service->SetString( + kMediaDeviceIdSalt, + content::ResourceContext::CreateRandomMediaDeviceIDSalt()); +} + +} // namespace brightray diff --git a/brightray/browser/media/media_device_id_salt.h b/brightray/browser/media/media_device_id_salt.h new file mode 100644 index 00000000000..7941af8a563 --- /dev/null +++ b/brightray/browser/media/media_device_id_salt.h @@ -0,0 +1,40 @@ +// Copyright 2013 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. + +#ifndef BRIGHTRAY_BROWSER_MEDIA_MEDIA_DEVICE_ID_SALT_H_ +#define BRIGHTRAY_BROWSER_MEDIA_MEDIA_DEVICE_ID_SALT_H_ + +#include + +#include "base/macros.h" +#include "base/memory/ref_counted.h" +#include "components/prefs/pref_member.h" + +class PrefRegistrySimple; +class PrefService; + +namespace brightray { + +// MediaDeviceIDSalt is responsible for creating and retrieving a salt string +// that is used for creating MediaSource IDs that can be cached by a web +// service. If the cache is cleared, the MediaSourceIds are invalidated. +class MediaDeviceIDSalt { + public: + explicit MediaDeviceIDSalt(PrefService* pref_service); + ~MediaDeviceIDSalt(); + + std::string GetSalt(); + + static void RegisterPrefs(PrefRegistrySimple* pref_registry); + static void Reset(PrefService* pref_service); + + private: + StringPrefMember media_device_id_salt_; + + DISALLOW_COPY_AND_ASSIGN(MediaDeviceIDSalt); +}; + +} // namespace brightray + +#endif // BRIGHTRAY_BROWSER_MEDIA_MEDIA_DEVICE_ID_SALT_H_ diff --git a/brightray/browser/url_request_context_getter.h b/brightray/browser/url_request_context_getter.h index 88fdc392b90..c4ff58e2426 100644 --- a/brightray/browser/url_request_context_getter.h +++ b/brightray/browser/url_request_context_getter.h @@ -31,6 +31,7 @@ class URLRequestJobFactory; namespace brightray { class DevToolsNetworkControllerHandle; +class MediaDeviceIDSalt; class NetLog; class URLRequestContextGetter : public net::URLRequestContextGetter { @@ -57,6 +58,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { GetRequireCTDelegate() { return nullptr; } + virtual MediaDeviceIDSalt* GetMediaDeviceIDSalt() { return nullptr; } }; URLRequestContextGetter( @@ -77,6 +79,9 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { net::HostResolver* host_resolver(); net::URLRequestJobFactory* job_factory() const { return job_factory_; } + MediaDeviceIDSalt* GetMediaDeviceIDSalt() const { + return delegate_->GetMediaDeviceIDSalt(); + } private: Delegate* delegate_; diff --git a/brightray/filenames.gypi b/brightray/filenames.gypi index c36bddea5d8..7b2db49869d 100644 --- a/brightray/filenames.gypi +++ b/brightray/filenames.gypi @@ -43,6 +43,8 @@ 'browser/mac/notification_presenter_mac.mm', 'browser/media/media_capture_devices_dispatcher.cc', 'browser/media/media_capture_devices_dispatcher.h', + 'browser/media/media_device_id_salt.cc', + 'browser/media/media_device_id_salt.h', 'browser/media/media_stream_devices_controller.cc', 'browser/media/media_stream_devices_controller.h', 'browser/net/devtools_network_conditions.cc',