25
25
#include "net/base/net_errors.h"
26
26
#include "net/url_request/url_request.h"
28
#include "shared/browser/oxide_browser_context.h"
29
#include "shared/browser/oxide_browser_context_delegate.h"
30
28
#include "qt/core/api/oxideqnetworkcallbackevents.h"
31
29
#include "qt/core/api/oxideqnetworkcallbackevents_p.h"
32
30
#include "qt/core/api/oxideqstoragepermissionrequest.h"
33
31
#include "qt/core/api/oxideqstoragepermissionrequest_p.h"
32
#include "qt/core/browser/oxide_qt_render_widget_host_view_factory.h"
33
#include "shared/browser/oxide_browser_context.h"
34
#include "shared/browser/oxide_browser_context_delegate.h"
35
#include "shared/browser/oxide_user_script_master.h"
37
#include "../oxide_qt_render_widget_host_view_delegate_factory.h"
38
#include "../oxide_qt_user_script_adapter.h"
39
#include "../oxide_qt_user_script_adapter_p.h"
38
class BrowserContextDelegate : public oxide::BrowserContextDelegate {
40
BrowserContextDelegate(WebContextAdapterPrivate* ui_delegate,
41
WebContextAdapter::IOThreadDelegate* io_delegate) :
42
ui_thread_delegate_(ui_delegate->AsWeakPtr()),
43
io_thread_delegate_(io_delegate) {
46
virtual ~BrowserContextDelegate() {}
48
WebContextAdapter::IOThreadDelegate* io_thread_delegate() const {
49
return io_thread_delegate_.get();
53
virtual int OnBeforeURLRequest(net::URLRequest* request,
54
const net::CompletionCallback& callback,
56
if (!io_thread_delegate_) {
60
bool cancelled = false;
62
OxideQBeforeURLRequestEvent* event =
63
new OxideQBeforeURLRequestEvent(
64
QUrl(QString::fromStdString(request->url().spec())),
65
QString::fromStdString(request->method()));
67
OxideQBeforeURLRequestEventPrivate* eventp =
68
OxideQBeforeURLRequestEventPrivate::get(event);
69
eventp->request_cancelled = &cancelled;
70
eventp->new_url = new_url;
72
io_thread_delegate_->OnBeforeURLRequest(event);
74
return cancelled ? net::ERR_ABORTED : net::OK;
77
virtual int OnBeforeSendHeaders(net::URLRequest* request,
78
const net::CompletionCallback& callback,
79
net::HttpRequestHeaders* headers) {
80
if (!io_thread_delegate_) {
84
bool cancelled = false;
86
OxideQBeforeSendHeadersEvent* event =
87
new OxideQBeforeSendHeadersEvent(
88
QUrl(QString::fromStdString(request->url().spec())),
89
QString::fromStdString(request->method()));
91
OxideQBeforeSendHeadersEventPrivate* eventp =
92
OxideQBeforeSendHeadersEventPrivate::get(event);
93
eventp->request_cancelled = &cancelled;
94
eventp->headers = headers;
96
io_thread_delegate_->OnBeforeSendHeaders(event);
98
return cancelled ? net::ERR_ABORTED : net::OK;
101
virtual oxide::StoragePermission CanAccessStorage(
103
const GURL& first_party_url,
105
oxide::StorageType type) {
106
oxide::StoragePermission result = oxide::STORAGE_PERMISSION_UNDEFINED;
108
if (!io_thread_delegate_) {
112
OxideQStoragePermissionRequest* req =
113
new OxideQStoragePermissionRequest(
114
QUrl(QString::fromStdString(url.spec())),
115
QUrl(QString::fromStdString(first_party_url.spec())),
117
static_cast<OxideQStoragePermissionRequest::Type>(type));
119
OxideQStoragePermissionRequestPrivate::get(req)->permission = &result;
121
io_thread_delegate_->HandleStoragePermissionRequest(req);
44
WebContextAdapterPrivate::ConstructProperties::ConstructProperties() :
45
cookie_policy(net::StaticCookiePolicy::ALLOW_ALL_COOKIES),
46
session_cookie_mode(content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES),
47
popup_blocker_enabled(true) {}
50
WebContextAdapterPrivate* WebContextAdapterPrivate::Create(
51
WebContextAdapter* adapter,
52
WebContextAdapter::IOThreadDelegate* io_delegate,
53
RenderWidgetHostViewDelegateFactory* view_factory) {
54
return new WebContextAdapterPrivate(adapter, io_delegate, view_factory);
57
WebContextAdapterPrivate::WebContextAdapterPrivate(
58
WebContextAdapter* adapter,
59
WebContextAdapter::IOThreadDelegate* io_delegate,
60
RenderWidgetHostViewDelegateFactory* view_factory) :
62
io_thread_delegate_(io_delegate),
63
view_factory_(view_factory),
64
construct_props_(new ConstructProperties()) {}
66
void WebContextAdapterPrivate::Destroy() {
68
context_->SetDelegate(NULL);
73
void WebContextAdapterPrivate::UpdateUserScripts() {
78
std::vector<oxide::UserScript *> scripts;
80
for (int i = 0; i < user_scripts_.size(); ++i) {
81
UserScriptAdapterPrivate* script =
82
UserScriptAdapterPrivate::get(user_scripts_.at(i));
83
if (script->state == UserScriptAdapterPrivate::Loading ||
84
script->state == UserScriptAdapterPrivate::Constructing) {
86
} else if (script->state == UserScriptAdapterPrivate::Loaded) {
87
scripts.push_back(&script->user_script);
91
context_->UserScriptManager().SerializeUserScriptsAndSendUpdates(scripts);
94
int WebContextAdapterPrivate::OnBeforeURLRequest(
95
net::URLRequest* request,
96
const net::CompletionCallback& callback,
98
if (!io_thread_delegate_) {
102
bool cancelled = false;
104
OxideQBeforeURLRequestEvent* event =
105
new OxideQBeforeURLRequestEvent(
106
QUrl(QString::fromStdString(request->url().spec())),
107
QString::fromStdString(request->method()));
109
OxideQBeforeURLRequestEventPrivate* eventp =
110
OxideQBeforeURLRequestEventPrivate::get(event);
111
eventp->request_cancelled = &cancelled;
112
eventp->new_url = new_url;
114
io_thread_delegate_->OnBeforeURLRequest(event);
116
return cancelled ? net::ERR_ABORTED : net::OK;
119
int WebContextAdapterPrivate::OnBeforeSendHeaders(
120
net::URLRequest* request,
121
const net::CompletionCallback& callback,
122
net::HttpRequestHeaders* headers) {
123
if (!io_thread_delegate_) {
127
bool cancelled = false;
129
OxideQBeforeSendHeadersEvent* event =
130
new OxideQBeforeSendHeadersEvent(
131
QUrl(QString::fromStdString(request->url().spec())),
132
QString::fromStdString(request->method()));
134
OxideQBeforeSendHeadersEventPrivate* eventp =
135
OxideQBeforeSendHeadersEventPrivate::get(event);
136
eventp->request_cancelled = &cancelled;
137
eventp->headers = headers;
139
io_thread_delegate_->OnBeforeSendHeaders(event);
141
return cancelled ? net::ERR_ABORTED : net::OK;
144
oxide::StoragePermission WebContextAdapterPrivate::CanAccessStorage(
146
const GURL& first_party_url,
148
oxide::StorageType type) {
149
oxide::StoragePermission result = oxide::STORAGE_PERMISSION_UNDEFINED;
151
if (!io_thread_delegate_) {
126
virtual bool GetUserAgentOverride(const GURL& url,
127
std::string* user_agent) {
128
if (!io_thread_delegate_) {
132
QString new_user_agent;
133
bool overridden = io_thread_delegate_->GetUserAgentOverride(
134
QUrl(QString::fromStdString(url.spec())), &new_user_agent);
136
*user_agent = new_user_agent.toStdString();
140
base::WeakPtr<WebContextAdapterPrivate> ui_thread_delegate_;
141
scoped_ptr<WebContextAdapter::IOThreadDelegate> io_thread_delegate_;
144
WebContextAdapterPrivate::WebContextAdapterPrivate(
145
WebContextAdapter* adapter,
146
WebContextAdapter::IOThreadDelegate* io_delegate) :
148
construct_props_(new ConstructProperties()),
149
context_delegate_(new BrowserContextDelegate(this, io_delegate)) {}
151
void WebContextAdapterPrivate::Init() {
155
OxideQStoragePermissionRequest* req =
156
new OxideQStoragePermissionRequest(
157
QUrl(QString::fromStdString(url.spec())),
158
QUrl(QString::fromStdString(first_party_url.spec())),
160
static_cast<OxideQStoragePermissionRequest::Type>(type));
162
OxideQStoragePermissionRequestPrivate::get(req)->permission = &result;
164
io_thread_delegate_->HandleStoragePermissionRequest(req);
169
bool WebContextAdapterPrivate::GetUserAgentOverride(const GURL& url,
170
std::string* user_agent) {
171
if (!io_thread_delegate_) {
175
QString new_user_agent;
176
bool overridden = io_thread_delegate_->GetUserAgentOverride(
177
QUrl(QString::fromStdString(url.spec())), &new_user_agent);
179
*user_agent = new_user_agent.toStdString();
183
WebContextAdapterPrivate::~WebContextAdapterPrivate() {}
186
WebContextAdapterPrivate* WebContextAdapterPrivate::get(
187
WebContextAdapter* adapter) {
188
return adapter->priv;
192
WebContextAdapterPrivate* WebContextAdapterPrivate::FromBrowserContext(
193
oxide::BrowserContext* context) {
194
return static_cast<WebContextAdapterPrivate *>(context->GetDelegate());
197
oxide::BrowserContext* WebContextAdapterPrivate::GetContext() {
202
DCHECK(construct_props_);
154
204
oxide::BrowserContext::Params params(
155
205
construct_props_->data_path,
156
construct_props_->cache_path);
206
construct_props_->cache_path,
207
construct_props_->session_cookie_mode);
157
208
context_ = oxide::BrowserContext::Create(params);
159
210
if (!construct_props_->product.empty()) {
160
context()->SetProduct(construct_props_->product);
211
context_->SetProduct(construct_props_->product);
162
213
if (!construct_props_->user_agent.empty()) {
163
context()->SetUserAgent(construct_props_->user_agent);
214
context_->SetUserAgent(construct_props_->user_agent);
165
216
if (!construct_props_->accept_langs.empty()) {
166
context()->SetAcceptLangs(construct_props_->accept_langs);
217
context_->SetAcceptLangs(construct_props_->accept_langs);
168
context()->SetDelegate(context_delegate_);
219
context_->SetCookiePolicy(construct_props_->cookie_policy);
220
context_->SetIsPopupBlockerEnabled(construct_props_->popup_blocker_enabled);
222
context_->SetDelegate(this);
170
224
construct_props_.reset();
173
WebContextAdapterPrivate::~WebContextAdapterPrivate() {}
176
WebContextAdapterPrivate* WebContextAdapterPrivate::get(
177
WebContextAdapter* adapter) {
178
return adapter->priv.data();
181
WebContextAdapter::IOThreadDelegate*
182
WebContextAdapterPrivate::GetIOThreadDelegate() const {
183
return context_delegate_->io_thread_delegate();
226
// BrowserContext takes ownership of this
227
new RenderWidgetHostViewFactory(context_.get(), view_factory_.release());
186
234
} // namespace qt