REVIEW: ContentBrowserClient now provides geolocation api key and request context
This commit is contained in:
parent
f9de29ed8c
commit
97eb7f2c98
2 changed files with 84 additions and 5 deletions
|
@ -22,6 +22,7 @@
|
|||
#include "atom/browser/web_contents_permission_helper.h"
|
||||
#include "atom/browser/web_contents_preferences.h"
|
||||
#include "atom/browser/window_list.h"
|
||||
#include "atom/common/google_api_key.h"
|
||||
#include "atom/common/options_switches.h"
|
||||
#include "base/command_line.h"
|
||||
#include "base/files/file_util.h"
|
||||
|
@ -46,10 +47,15 @@
|
|||
#include "content/public/common/url_constants.h"
|
||||
#include "content/public/common/web_preferences.h"
|
||||
#include "net/ssl/ssl_cert_request_info.h"
|
||||
#include "net/url_request/url_request_context.h"
|
||||
#include "net/url_request/url_request_context_builder.h"
|
||||
#include "net/url_request/url_request_context_getter.h"
|
||||
#include "ppapi/host/ppapi_host.h"
|
||||
#include "ui/base/l10n/l10n_util.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
using content::BrowserThread;
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace {
|
||||
|
@ -63,6 +69,45 @@ std::string g_custom_service_worker_schemes = "";
|
|||
void Noop(scoped_refptr<content::SiteInstance>) {
|
||||
}
|
||||
|
||||
class GeoURLRequestContextGetter : public net::URLRequestContextGetter {
|
||||
public:
|
||||
GeoURLRequestContextGetter() = default;
|
||||
~GeoURLRequestContextGetter() override {
|
||||
if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
|
||||
BrowserThread::PostTask(
|
||||
BrowserThread::IO, FROM_HERE,
|
||||
base::Bind(&GeoURLRequestContextGetter::NotifyContextShutdownOnIO,
|
||||
this));
|
||||
}
|
||||
}
|
||||
|
||||
void NotifyContextShutdownOnIO() {
|
||||
url_request_context_.reset();
|
||||
net::URLRequestContextGetter::NotifyContextShuttingDown();
|
||||
}
|
||||
|
||||
net::URLRequestContext* GetURLRequestContext() override {
|
||||
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
||||
if (!url_request_context_.get()) {
|
||||
net::URLRequestContextBuilder builder;
|
||||
builder.set_proxy_config_service(
|
||||
net::ProxyService::CreateSystemProxyConfigService(
|
||||
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)));
|
||||
url_request_context_ = builder.Build();
|
||||
}
|
||||
return url_request_context_.get();
|
||||
}
|
||||
|
||||
scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
|
||||
const override {
|
||||
return BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<net::URLRequestContext> url_request_context_;
|
||||
DISALLOW_COPY_AND_ASSIGN(GeoURLRequestContextGetter);
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
// static
|
||||
|
@ -75,8 +120,9 @@ void AtomBrowserClient::SetCustomServiceWorkerSchemes(
|
|||
g_custom_service_worker_schemes = base::JoinString(schemes, ",");
|
||||
}
|
||||
|
||||
AtomBrowserClient::AtomBrowserClient() : delegate_(nullptr) {
|
||||
}
|
||||
AtomBrowserClient::AtomBrowserClient()
|
||||
: delegate_(nullptr),
|
||||
geo_request_context_getter_(new GeoURLRequestContextGetter) {}
|
||||
|
||||
AtomBrowserClient::~AtomBrowserClient() {
|
||||
}
|
||||
|
@ -262,8 +308,8 @@ void AtomBrowserClient::OverrideSiteInstanceForNavigation(
|
|||
// when this function returns.
|
||||
// FIXME(zcbenz): We should adjust
|
||||
// OverrideSiteInstanceForNavigation's interface to solve this.
|
||||
content::BrowserThread::PostTask(
|
||||
content::BrowserThread::UI, FROM_HERE,
|
||||
BrowserThread::PostTask(
|
||||
BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(&Noop, base::RetainedRef(site_instance)));
|
||||
|
||||
// Remember the original web contents for the pending renderer process.
|
||||
|
@ -338,6 +384,30 @@ void AtomBrowserClient::DidCreatePpapiPlugin(
|
|||
base::WrapUnique(new chrome::ChromeBrowserPepperHostFactory(host)));
|
||||
}
|
||||
|
||||
scoped_refptr<net::URLRequestContextGetter>
|
||||
AtomBrowserClient::GetGeoRequestContextGetterFromUIThread() {
|
||||
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
||||
return geo_request_context_getter_;
|
||||
}
|
||||
|
||||
void AtomBrowserClient::GetGeolocationRequestContext(
|
||||
base::OnceCallback<void(scoped_refptr<net::URLRequestContextGetter>)>
|
||||
callback) {
|
||||
BrowserThread::PostTaskAndReplyWithResult(
|
||||
BrowserThread::UI, FROM_HERE,
|
||||
base::BindOnce(&AtomBrowserClient::GetGeoRequestContextGetterFromUIThread,
|
||||
base::Unretained(this)),
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
std::string AtomBrowserClient::GetGeolocationApiKey() {
|
||||
std::unique_ptr<base::Environment> env(base::Environment::Create());
|
||||
std::string api_key;
|
||||
if (!env->GetVar("GOOGLE_API_KEY", &api_key))
|
||||
api_key = GOOGLEAPIS_API_KEY;
|
||||
return api_key;
|
||||
}
|
||||
|
||||
content::QuotaPermissionContext*
|
||||
AtomBrowserClient::CreateQuotaPermissionContext() {
|
||||
return new AtomQuotaPermissionContext;
|
||||
|
@ -396,7 +466,7 @@ bool AtomBrowserClient::CanCreateWindow(
|
|||
bool user_gesture,
|
||||
bool opener_suppressed,
|
||||
bool* no_javascript_access) {
|
||||
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
||||
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
||||
|
||||
int opener_render_process_id = opener->GetProcess()->GetID();
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ class ClientCertificateDelegate;
|
|||
|
||||
namespace net {
|
||||
class SSLCertRequestInfo;
|
||||
class URLRequestContextGetter;
|
||||
}
|
||||
|
||||
namespace atom {
|
||||
|
@ -62,6 +63,10 @@ class AtomBrowserClient : public brightray::BrowserClient,
|
|||
void AppendExtraCommandLineSwitches(base::CommandLine* command_line,
|
||||
int child_process_id) override;
|
||||
void DidCreatePpapiPlugin(content::BrowserPpapiHost* browser_host) override;
|
||||
void GetGeolocationRequestContext(
|
||||
base::OnceCallback<void(scoped_refptr<net::URLRequestContextGetter>)>
|
||||
callback) override;
|
||||
std::string GetGeolocationApiKey() override;
|
||||
content::QuotaPermissionContext* CreateQuotaPermissionContext() override;
|
||||
void AllowCertificateError(
|
||||
content::WebContents* web_contents,
|
||||
|
@ -129,6 +134,8 @@ class AtomBrowserClient : public brightray::BrowserClient,
|
|||
bool IsRendererSandboxed(int process_id);
|
||||
bool RendererUsesNativeWindowOpen(int process_id);
|
||||
bool RendererDisablesPopups(int process_id);
|
||||
scoped_refptr<net::URLRequestContextGetter>
|
||||
GetGeoRequestContextGetterFromUIThread();
|
||||
|
||||
// pending_render_process => web contents.
|
||||
std::map<int, content::WebContents*> pending_processes_;
|
||||
|
@ -144,6 +151,8 @@ class AtomBrowserClient : public brightray::BrowserClient,
|
|||
|
||||
Delegate* delegate_;
|
||||
|
||||
scoped_refptr<net::URLRequestContextGetter> geo_request_context_getter_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(AtomBrowserClient);
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue