~osomon/oxide/i18n

« back to all changes in this revision

Viewing changes to qt/core/glue/private/oxide_qt_web_context_adapter_p.cc

  • Committer: Olivier Tilloy
  • Date: 2014-04-08 10:03:11 UTC
  • mfrom: (312.2.173 oxide)
  • Revision ID: olivier.tilloy@canonical.com-20140408100311-b3zb7q1jfrevbrf1
Merge the latest changes from trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include "net/base/net_errors.h"
26
26
#include "net/url_request/url_request.h"
27
27
 
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"
 
36
 
 
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"
34
40
 
35
41
namespace oxide {
36
42
namespace qt {
37
43
 
38
 
class BrowserContextDelegate : public oxide::BrowserContextDelegate {
39
 
 public:
40
 
  BrowserContextDelegate(WebContextAdapterPrivate* ui_delegate,
41
 
                         WebContextAdapter::IOThreadDelegate* io_delegate) :
42
 
      ui_thread_delegate_(ui_delegate->AsWeakPtr()),
43
 
      io_thread_delegate_(io_delegate) {
44
 
  }
45
 
 
46
 
  virtual ~BrowserContextDelegate() {}
47
 
 
48
 
  WebContextAdapter::IOThreadDelegate* io_thread_delegate() const {
49
 
    return io_thread_delegate_.get();
50
 
  }
51
 
 
52
 
 private:
53
 
  virtual int OnBeforeURLRequest(net::URLRequest* request,
54
 
                                 const net::CompletionCallback& callback,
55
 
                                 GURL* new_url) {
56
 
    if (!io_thread_delegate_) {
57
 
      return net::OK;
58
 
    }
59
 
 
60
 
    bool cancelled = false;
61
 
 
62
 
    OxideQBeforeURLRequestEvent* event =
63
 
        new OxideQBeforeURLRequestEvent(
64
 
          QUrl(QString::fromStdString(request->url().spec())),
65
 
          QString::fromStdString(request->method()));
66
 
 
67
 
    OxideQBeforeURLRequestEventPrivate* eventp =
68
 
        OxideQBeforeURLRequestEventPrivate::get(event);
69
 
    eventp->request_cancelled = &cancelled;
70
 
    eventp->new_url = new_url;
71
 
 
72
 
    io_thread_delegate_->OnBeforeURLRequest(event);
73
 
 
74
 
    return cancelled ? net::ERR_ABORTED : net::OK;
75
 
  }
76
 
 
77
 
  virtual int OnBeforeSendHeaders(net::URLRequest* request,
78
 
                                  const net::CompletionCallback& callback,
79
 
                                  net::HttpRequestHeaders* headers) {
80
 
    if (!io_thread_delegate_) {
81
 
      return net::OK;
82
 
    }
83
 
 
84
 
    bool cancelled = false;
85
 
 
86
 
    OxideQBeforeSendHeadersEvent* event =
87
 
        new OxideQBeforeSendHeadersEvent(
88
 
          QUrl(QString::fromStdString(request->url().spec())),
89
 
          QString::fromStdString(request->method()));
90
 
 
91
 
    OxideQBeforeSendHeadersEventPrivate* eventp =
92
 
        OxideQBeforeSendHeadersEventPrivate::get(event);
93
 
    eventp->request_cancelled = &cancelled;
94
 
    eventp->headers = headers;
95
 
 
96
 
    io_thread_delegate_->OnBeforeSendHeaders(event);
97
 
 
98
 
    return cancelled ? net::ERR_ABORTED : net::OK;
99
 
  }
100
 
 
101
 
  virtual oxide::StoragePermission CanAccessStorage(
102
 
      const GURL& url,
103
 
      const GURL& first_party_url,
104
 
      bool write,
105
 
      oxide::StorageType type) {
106
 
    oxide::StoragePermission result = oxide::STORAGE_PERMISSION_UNDEFINED;
107
 
 
108
 
    if (!io_thread_delegate_) {
109
 
      return result;
110
 
    }
111
 
 
112
 
    OxideQStoragePermissionRequest* req =
113
 
        new OxideQStoragePermissionRequest(
114
 
          QUrl(QString::fromStdString(url.spec())),
115
 
          QUrl(QString::fromStdString(first_party_url.spec())),
116
 
          write,
117
 
          static_cast<OxideQStoragePermissionRequest::Type>(type));
118
 
 
119
 
    OxideQStoragePermissionRequestPrivate::get(req)->permission = &result;
120
 
 
121
 
    io_thread_delegate_->HandleStoragePermissionRequest(req);
122
 
 
 
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) {}
 
48
 
 
49
// static
 
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);
 
55
}
 
56
 
 
57
WebContextAdapterPrivate::WebContextAdapterPrivate(
 
58
    WebContextAdapter* adapter,
 
59
    WebContextAdapter::IOThreadDelegate* io_delegate,
 
60
    RenderWidgetHostViewDelegateFactory* view_factory) :
 
61
    adapter_(adapter),
 
62
    io_thread_delegate_(io_delegate),
 
63
    view_factory_(view_factory),
 
64
    construct_props_(new ConstructProperties()) {}
 
65
 
 
66
void WebContextAdapterPrivate::Destroy() {
 
67
  if (context_) {
 
68
    context_->SetDelegate(NULL);
 
69
  }
 
70
  adapter_ = NULL;
 
71
}
 
72
 
 
73
void WebContextAdapterPrivate::UpdateUserScripts() {
 
74
  if (!context_) {
 
75
    return;
 
76
  }
 
77
 
 
78
  std::vector<oxide::UserScript *> scripts;
 
79
 
 
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) {
 
85
      return;
 
86
    } else if (script->state == UserScriptAdapterPrivate::Loaded) {
 
87
      scripts.push_back(&script->user_script);
 
88
    }
 
89
  }
 
90
 
 
91
  context_->UserScriptManager().SerializeUserScriptsAndSendUpdates(scripts);
 
92
}
 
93
 
 
94
int WebContextAdapterPrivate::OnBeforeURLRequest(
 
95
    net::URLRequest* request,
 
96
    const net::CompletionCallback& callback,
 
97
    GURL* new_url) {
 
98
  if (!io_thread_delegate_) {
 
99
    return net::OK;
 
100
  }
 
101
 
 
102
  bool cancelled = false;
 
103
 
 
104
  OxideQBeforeURLRequestEvent* event =
 
105
      new OxideQBeforeURLRequestEvent(
 
106
        QUrl(QString::fromStdString(request->url().spec())),
 
107
        QString::fromStdString(request->method()));
 
108
 
 
109
  OxideQBeforeURLRequestEventPrivate* eventp =
 
110
      OxideQBeforeURLRequestEventPrivate::get(event);
 
111
  eventp->request_cancelled = &cancelled;
 
112
  eventp->new_url = new_url;
 
113
 
 
114
  io_thread_delegate_->OnBeforeURLRequest(event);
 
115
 
 
116
  return cancelled ? net::ERR_ABORTED : net::OK;
 
117
}
 
118
 
 
119
int WebContextAdapterPrivate::OnBeforeSendHeaders(
 
120
    net::URLRequest* request,
 
121
    const net::CompletionCallback& callback,
 
122
    net::HttpRequestHeaders* headers) {
 
123
  if (!io_thread_delegate_) {
 
124
    return net::OK;
 
125
  }
 
126
 
 
127
  bool cancelled = false;
 
128
 
 
129
  OxideQBeforeSendHeadersEvent* event =
 
130
      new OxideQBeforeSendHeadersEvent(
 
131
        QUrl(QString::fromStdString(request->url().spec())),
 
132
        QString::fromStdString(request->method()));
 
133
 
 
134
  OxideQBeforeSendHeadersEventPrivate* eventp =
 
135
      OxideQBeforeSendHeadersEventPrivate::get(event);
 
136
  eventp->request_cancelled = &cancelled;
 
137
  eventp->headers = headers;
 
138
 
 
139
  io_thread_delegate_->OnBeforeSendHeaders(event);
 
140
 
 
141
  return cancelled ? net::ERR_ABORTED : net::OK;
 
142
}
 
143
 
 
144
oxide::StoragePermission WebContextAdapterPrivate::CanAccessStorage(
 
145
    const GURL& url,
 
146
    const GURL& first_party_url,
 
147
    bool write,
 
148
    oxide::StorageType type) {
 
149
  oxide::StoragePermission result = oxide::STORAGE_PERMISSION_UNDEFINED;
 
150
 
 
151
  if (!io_thread_delegate_) {
123
152
    return result;
124
153
  }
125
154
 
126
 
  virtual bool GetUserAgentOverride(const GURL& url,
127
 
                                    std::string* user_agent) {
128
 
    if (!io_thread_delegate_) {
129
 
      return false;
130
 
    }
131
 
 
132
 
    QString new_user_agent;
133
 
    bool overridden = io_thread_delegate_->GetUserAgentOverride(
134
 
        QUrl(QString::fromStdString(url.spec())), &new_user_agent);
135
 
 
136
 
    *user_agent = new_user_agent.toStdString();
137
 
    return overridden;
138
 
  }
139
 
 
140
 
  base::WeakPtr<WebContextAdapterPrivate> ui_thread_delegate_;
141
 
  scoped_ptr<WebContextAdapter::IOThreadDelegate> io_thread_delegate_;
142
 
};
143
 
 
144
 
WebContextAdapterPrivate::WebContextAdapterPrivate(
145
 
    WebContextAdapter* adapter,
146
 
    WebContextAdapter::IOThreadDelegate* io_delegate) :
147
 
    adapter(adapter),
148
 
    construct_props_(new ConstructProperties()),
149
 
    context_delegate_(new BrowserContextDelegate(this, io_delegate)) {}
150
 
 
151
 
void WebContextAdapterPrivate::Init() {
152
 
  DCHECK(!context_);
 
155
  OxideQStoragePermissionRequest* req =
 
156
      new OxideQStoragePermissionRequest(
 
157
        QUrl(QString::fromStdString(url.spec())),
 
158
        QUrl(QString::fromStdString(first_party_url.spec())),
 
159
        write,
 
160
        static_cast<OxideQStoragePermissionRequest::Type>(type));
 
161
 
 
162
  OxideQStoragePermissionRequestPrivate::get(req)->permission = &result;
 
163
 
 
164
  io_thread_delegate_->HandleStoragePermissionRequest(req);
 
165
 
 
166
  return result;
 
167
}
 
168
 
 
169
bool WebContextAdapterPrivate::GetUserAgentOverride(const GURL& url,
 
170
                                                    std::string* user_agent) {
 
171
  if (!io_thread_delegate_) {
 
172
    return false;
 
173
  }
 
174
 
 
175
  QString new_user_agent;
 
176
  bool overridden = io_thread_delegate_->GetUserAgentOverride(
 
177
      QUrl(QString::fromStdString(url.spec())), &new_user_agent);
 
178
 
 
179
  *user_agent = new_user_agent.toStdString();
 
180
  return overridden;
 
181
}
 
182
 
 
183
WebContextAdapterPrivate::~WebContextAdapterPrivate() {}
 
184
 
 
185
// static
 
186
WebContextAdapterPrivate* WebContextAdapterPrivate::get(
 
187
    WebContextAdapter* adapter) {
 
188
  return adapter->priv;
 
189
}
 
190
 
 
191
// static
 
192
WebContextAdapterPrivate* WebContextAdapterPrivate::FromBrowserContext(
 
193
    oxide::BrowserContext* context) {
 
194
  return static_cast<WebContextAdapterPrivate *>(context->GetDelegate());
 
195
}
 
196
 
 
197
oxide::BrowserContext* WebContextAdapterPrivate::GetContext() {
 
198
  if (context_) {
 
199
    return context_;
 
200
  }
 
201
 
 
202
  DCHECK(construct_props_);
153
203
 
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);
158
209
 
159
210
  if (!construct_props_->product.empty()) {
160
 
    context()->SetProduct(construct_props_->product);
 
211
    context_->SetProduct(construct_props_->product);
161
212
  }
162
213
  if (!construct_props_->user_agent.empty()) {
163
 
    context()->SetUserAgent(construct_props_->user_agent);
 
214
    context_->SetUserAgent(construct_props_->user_agent);
164
215
  }
165
216
  if (!construct_props_->accept_langs.empty()) {
166
 
    context()->SetAcceptLangs(construct_props_->accept_langs);
 
217
    context_->SetAcceptLangs(construct_props_->accept_langs);
167
218
  }
168
 
  context()->SetDelegate(context_delegate_);
 
219
  context_->SetCookiePolicy(construct_props_->cookie_policy);
 
220
  context_->SetIsPopupBlockerEnabled(construct_props_->popup_blocker_enabled);
 
221
 
 
222
  context_->SetDelegate(this);
169
223
 
170
224
  construct_props_.reset();
171
 
}
172
 
 
173
 
WebContextAdapterPrivate::~WebContextAdapterPrivate() {}
174
 
 
175
 
// static
176
 
WebContextAdapterPrivate* WebContextAdapterPrivate::get(
177
 
    WebContextAdapter* adapter) {
178
 
  return adapter->priv.data();
179
 
}
180
 
 
181
 
WebContextAdapter::IOThreadDelegate*
182
 
WebContextAdapterPrivate::GetIOThreadDelegate() const {
183
 
  return context_delegate_->io_thread_delegate();
 
225
 
 
226
  // BrowserContext takes ownership of this
 
227
  new RenderWidgetHostViewFactory(context_.get(), view_factory_.release());
 
228
 
 
229
  UpdateUserScripts();
 
230
 
 
231
  return context_;
184
232
}
185
233
 
186
234
} // namespace qt