2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2012 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.
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
#include "libcef/browser/browser_context_impl.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "libcef/browser/browser_host_impl.h"
|
2012-06-25 20:22:50 +02:00
|
|
|
#include "libcef/browser/context.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "libcef/browser/download_manager_delegate.h"
|
|
|
|
#include "libcef/browser/thread_util.h"
|
|
|
|
#include "libcef/browser/url_request_context_getter.h"
|
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/threading/thread.h"
|
2012-04-04 20:18:09 +02:00
|
|
|
#include "content/public/browser/download_manager.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "content/public/browser/geolocation_permission_context.h"
|
2013-02-23 01:43:28 +01:00
|
|
|
#include "content/public/browser/resource_context.h"
|
|
|
|
#include "content/public/browser/storage_partition.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CefGeolocationPermissionContext
|
|
|
|
: public content::GeolocationPermissionContext {
|
|
|
|
public:
|
|
|
|
// CefGeolocationCallback implementation.
|
|
|
|
class CallbackImpl : public CefGeolocationCallback {
|
|
|
|
public:
|
|
|
|
typedef base::Callback<void(bool)> // NOLINT(readability/function)
|
|
|
|
CallbackType;
|
|
|
|
|
|
|
|
explicit CallbackImpl(
|
|
|
|
CefGeolocationPermissionContext* context,
|
|
|
|
int bridge_id,
|
|
|
|
const CallbackType& callback)
|
|
|
|
: context_(context),
|
|
|
|
bridge_id_(bridge_id),
|
|
|
|
callback_(callback) {}
|
|
|
|
|
|
|
|
virtual void Continue(bool allow) OVERRIDE {
|
|
|
|
if (CEF_CURRENTLY_ON_IOT()) {
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
// Callback must be executed on the UI thread.
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&CallbackImpl::Run, callback_, allow));
|
|
|
|
context_->RemoveCallback(bridge_id_);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_IOT,
|
|
|
|
base::Bind(&CallbackImpl::Continue, this, allow));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Disconnect() {
|
|
|
|
callback_.Reset();
|
|
|
|
context_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void Run(const CallbackType& callback, bool allow) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
callback.Run(allow);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefGeolocationPermissionContext* context_;
|
|
|
|
int bridge_id_;
|
|
|
|
CallbackType callback_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CallbackImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
CefGeolocationPermissionContext() {}
|
|
|
|
|
|
|
|
virtual void RequestGeolocationPermission(
|
|
|
|
int render_process_id,
|
|
|
|
int render_view_id,
|
|
|
|
int bridge_id,
|
|
|
|
const GURL& requesting_frame,
|
|
|
|
base::Callback<void(bool)> callback) // NOLINT(readability/function)
|
|
|
|
OVERRIDE {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
CefRefPtr<CefBrowserHostImpl> browser =
|
|
|
|
CefBrowserHostImpl::GetBrowserByRoutingID(render_process_id,
|
|
|
|
render_view_id);
|
|
|
|
if (browser.get()) {
|
|
|
|
CefRefPtr<CefClient> client = browser->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
CefRefPtr<CefGeolocationHandler> handler =
|
|
|
|
client->GetGeolocationHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
CefRefPtr<CallbackImpl> callbackPtr(
|
|
|
|
new CallbackImpl(this, bridge_id, callback));
|
|
|
|
|
|
|
|
// Add the callback reference to the map.
|
|
|
|
callback_map_.insert(std::make_pair(bridge_id, callbackPtr));
|
|
|
|
|
|
|
|
// Notify the handler.
|
|
|
|
handler->OnRequestGeolocationPermission(browser.get(),
|
|
|
|
requesting_frame.spec(), bridge_id, callbackPtr.get());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disallow geolocation access by default.
|
2013-01-18 18:09:48 +01:00
|
|
|
CEF_POST_TASK(CEF_UIT, base::Bind(callback, false));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void CancelGeolocationPermissionRequest(
|
|
|
|
int render_process_id,
|
|
|
|
int render_view_id,
|
|
|
|
int bridge_id,
|
|
|
|
const GURL& requesting_frame) OVERRIDE {
|
|
|
|
RemoveCallback(bridge_id);
|
|
|
|
|
|
|
|
CefRefPtr<CefBrowserHostImpl> browser =
|
|
|
|
CefBrowserHostImpl::GetBrowserByRoutingID(render_process_id,
|
|
|
|
render_view_id);
|
|
|
|
if (browser.get()) {
|
|
|
|
CefRefPtr<CefClient> client = browser->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
CefRefPtr<CefGeolocationHandler> handler =
|
|
|
|
client->GetGeolocationHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
// Notify the handler.
|
|
|
|
handler->OnCancelGeolocationPermission(browser.get(),
|
|
|
|
requesting_frame.spec(), bridge_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveCallback(int bridge_id) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
// Disconnect the callback and remove the reference from the map.
|
|
|
|
CallbackMap::iterator it = callback_map_.find(bridge_id);
|
|
|
|
if (it != callback_map_.end()) {
|
|
|
|
it->second->Disconnect();
|
|
|
|
callback_map_.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Map of bridge ids to callback references.
|
|
|
|
typedef std::map<int, CefRefPtr<CallbackImpl> > CallbackMap;
|
|
|
|
CallbackMap callback_map_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefGeolocationPermissionContext);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
class CefBrowserContextImpl::CefResourceContext : public content::ResourceContext {
|
2013-02-23 01:43:28 +01:00
|
|
|
public:
|
|
|
|
CefResourceContext() : getter_(NULL) {}
|
|
|
|
virtual ~CefResourceContext() {}
|
|
|
|
|
|
|
|
// ResourceContext implementation:
|
|
|
|
virtual net::HostResolver* GetHostResolver() OVERRIDE {
|
|
|
|
CHECK(getter_);
|
|
|
|
return getter_->host_resolver();
|
|
|
|
}
|
|
|
|
virtual net::URLRequestContext* GetRequestContext() OVERRIDE {
|
|
|
|
CHECK(getter_);
|
|
|
|
return getter_->GetURLRequestContext();
|
|
|
|
}
|
2013-08-15 21:38:55 +02:00
|
|
|
virtual bool AllowMicAccess(const GURL& origin) OVERRIDE {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE {
|
|
|
|
return true;
|
|
|
|
}
|
2013-02-23 01:43:28 +01:00
|
|
|
|
|
|
|
void set_url_request_context_getter(CefURLRequestContextGetter* getter) {
|
|
|
|
getter_ = getter;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CefURLRequestContextGetter* getter_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefResourceContext);
|
|
|
|
};
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
CefBrowserContextImpl::CefBrowserContextImpl()
|
2013-08-28 17:56:18 +02:00
|
|
|
: resource_context_(new CefResourceContext) {
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
CefBrowserContextImpl::~CefBrowserContextImpl() {
|
2012-11-30 20:08:20 +01:00
|
|
|
// Delete the download manager delegate here because otherwise we'll crash
|
2012-06-28 19:21:18 +02:00
|
|
|
// when it's accessed from the content::BrowserContext destructor.
|
|
|
|
if (download_manager_delegate_.get())
|
2012-11-30 20:08:20 +01:00
|
|
|
download_manager_delegate_.reset(NULL);
|
2012-06-11 19:35:23 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
if (resource_context_.get()) {
|
|
|
|
BrowserThread::DeleteSoon(
|
2012-06-28 19:21:18 +02:00
|
|
|
BrowserThread::IO, FROM_HERE, resource_context_.release());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
base::FilePath CefBrowserContextImpl::GetPath() const {
|
2013-09-03 20:06:25 +02:00
|
|
|
return CefContext::Get()->cache_path();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
bool CefBrowserContextImpl::IsOffTheRecord() const {
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-22 00:50:34 +02:00
|
|
|
content::DownloadManagerDelegate*
|
2013-09-03 18:43:31 +02:00
|
|
|
CefBrowserContextImpl::GetDownloadManagerDelegate() {
|
2012-06-28 19:21:18 +02:00
|
|
|
DCHECK(!download_manager_delegate_.get());
|
|
|
|
|
2012-11-30 20:08:20 +01:00
|
|
|
content::DownloadManager* manager = BrowserContext::GetDownloadManager(this);
|
|
|
|
download_manager_delegate_.reset(new CefDownloadManagerDelegate(manager));
|
2012-06-22 00:50:34 +02:00
|
|
|
return download_manager_delegate_.get();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
net::URLRequestContextGetter* CefBrowserContextImpl::GetRequestContext() {
|
2013-02-23 01:43:28 +01:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
return GetDefaultStoragePartition(this)->GetURLRequestContext();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
net::URLRequestContextGetter*
|
2013-09-03 18:43:31 +02:00
|
|
|
CefBrowserContextImpl::GetRequestContextForRenderProcess(
|
2012-04-03 03:34:16 +02:00
|
|
|
int renderer_child_id) {
|
|
|
|
return GetRequestContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
net::URLRequestContextGetter*
|
2013-09-03 18:43:31 +02:00
|
|
|
CefBrowserContextImpl::GetMediaRequestContext() {
|
2012-08-29 00:26:35 +02:00
|
|
|
return GetRequestContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
net::URLRequestContextGetter*
|
2013-09-03 18:43:31 +02:00
|
|
|
CefBrowserContextImpl::GetMediaRequestContextForRenderProcess(
|
2012-08-29 00:26:35 +02:00
|
|
|
int renderer_child_id) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return GetRequestContext();
|
|
|
|
}
|
|
|
|
|
2012-10-03 21:52:50 +02:00
|
|
|
net::URLRequestContextGetter*
|
2013-09-03 18:43:31 +02:00
|
|
|
CefBrowserContextImpl::GetMediaRequestContextForStoragePartition(
|
2013-02-23 01:43:28 +01:00
|
|
|
const base::FilePath& partition_path,
|
2012-11-05 21:18:20 +01:00
|
|
|
bool in_memory) {
|
2012-10-03 21:52:50 +02:00
|
|
|
return GetRequestContext();
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
void CefBrowserContextImpl::RequestMIDISysExPermission(
|
2013-07-24 22:15:18 +02:00
|
|
|
int render_process_id,
|
|
|
|
int render_view_id,
|
|
|
|
const GURL& requesting_frame,
|
|
|
|
const MIDISysExPermissionCallback& callback) {
|
|
|
|
// TODO(CEF): Implement Web MIDI API permission handling.
|
|
|
|
callback.Run(false);
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
content::ResourceContext* CefBrowserContextImpl::GetResourceContext() {
|
2012-04-03 03:34:16 +02:00
|
|
|
return resource_context_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
content::GeolocationPermissionContext*
|
2013-09-03 18:43:31 +02:00
|
|
|
CefBrowserContextImpl::GetGeolocationPermissionContext() {
|
2012-04-03 03:34:16 +02:00
|
|
|
if (!geolocation_permission_context_) {
|
|
|
|
geolocation_permission_context_ =
|
|
|
|
new CefGeolocationPermissionContext();
|
|
|
|
}
|
|
|
|
return geolocation_permission_context_;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
quota::SpecialStoragePolicy* CefBrowserContextImpl::GetSpecialStoragePolicy() {
|
2012-04-03 03:34:16 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-21 01:40:15 +01:00
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
net::URLRequestContextGetter* CefBrowserContextImpl::CreateRequestContext(
|
2013-03-12 21:23:24 +01:00
|
|
|
content::ProtocolHandlerMap* protocol_handlers) {
|
2013-02-23 01:43:28 +01:00
|
|
|
DCHECK(!url_request_getter_);
|
|
|
|
url_request_getter_ = new CefURLRequestContextGetter(
|
|
|
|
BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
|
|
|
|
BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
|
2013-03-12 21:23:24 +01:00
|
|
|
protocol_handlers);
|
2013-02-23 01:43:28 +01:00
|
|
|
resource_context_->set_url_request_context_getter(url_request_getter_.get());
|
|
|
|
return url_request_getter_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
net::URLRequestContextGetter*
|
2013-09-03 18:43:31 +02:00
|
|
|
CefBrowserContextImpl::CreateRequestContextForStoragePartition(
|
2013-02-23 01:43:28 +01:00
|
|
|
const base::FilePath& partition_path,
|
|
|
|
bool in_memory,
|
2013-03-12 21:23:24 +01:00
|
|
|
content::ProtocolHandlerMap* protocol_handlers) {
|
2013-02-23 01:43:28 +01:00
|
|
|
return NULL;
|
|
|
|
}
|