Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: content/renderer/fetchers/associated_resource_fetcher_impl.cc

Issue 2399463007: AssociatedURLLoader shouldn't derive from WebURLLoader (Closed)
Patch Set: Rebase Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/renderer/fetchers/associated_resource_fetcher_impl.h"
6
7 #include <stdint.h>
8
9 #include "base/logging.h"
10 #include "base/macros.h"
11 #include "base/strings/string_util.h"
12 #include "base/time/time.h"
13 #include "third_party/WebKit/public/platform/Platform.h"
14 #include "third_party/WebKit/public/platform/WebHTTPBody.h"
15 #include "third_party/WebKit/public/platform/WebString.h"
16 #include "third_party/WebKit/public/platform/WebURL.h"
17 #include "third_party/WebKit/public/platform/WebURLError.h"
18 #include "third_party/WebKit/public/platform/WebURLRequest.h"
19 #include "third_party/WebKit/public/platform/WebURLResponse.h"
20 #include "third_party/WebKit/public/web/WebAssociatedURLLoader.h"
21 #include "third_party/WebKit/public/web/WebAssociatedURLLoaderClient.h"
22 #include "third_party/WebKit/public/web/WebDocument.h"
23 #include "third_party/WebKit/public/web/WebFrame.h"
24 #include "third_party/WebKit/public/web/WebKit.h"
25 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
26
27 namespace content {
28
29 // static
30 AssociatedResourceFetcher* AssociatedResourceFetcher::Create(const GURL& url) {
31 return new AssociatedResourceFetcherImpl(url);
32 }
33
34 class AssociatedResourceFetcherImpl::ClientImpl
35 : public blink::WebAssociatedURLLoaderClient {
36 public:
37 explicit ClientImpl(const Callback& callback)
38 : completed_(false), status_(LOADING), callback_(callback) {}
39
40 ~ClientImpl() override {}
41
42 virtual void Cancel() { OnLoadCompleteInternal(LOAD_FAILED); }
43
44 bool completed() const { return completed_; }
45
46 private:
47 enum LoadStatus {
48 LOADING,
49 LOAD_FAILED,
50 LOAD_SUCCEEDED,
51 };
52
53 void OnLoadCompleteInternal(LoadStatus status) {
54 DCHECK(!completed_);
55 DCHECK_EQ(status_, LOADING);
56
57 completed_ = true;
58 status_ = status;
59
60 if (callback_.is_null())
61 return;
62
63 // Take a reference to the callback as running the callback may lead to our
64 // destruction.
65 Callback callback = callback_;
66 callback.Run(status_ == LOAD_FAILED ? blink::WebURLResponse() : response_,
67 status_ == LOAD_FAILED ? std::string() : data_);
68 }
69
70 // WebAssociatedURLLoaderClient methods:
71 void didReceiveResponse(const blink::WebURLResponse& response) override {
72 DCHECK(!completed_);
73 response_ = response;
74 }
75 void didReceiveCachedMetadata(const char* data, int data_length) override {
76 DCHECK(!completed_);
77 DCHECK_GT(data_length, 0);
78 }
79 void didReceiveData(const char* data, int data_length) override {
80 // The WebAssociatedURLLoader will continue after a load failure.
81 // For example, for an Access Control error.
82 if (completed_)
83 return;
84 DCHECK_GT(data_length, 0);
85
86 data_.append(data, data_length);
87 }
88 void didFinishLoading(double finishTime) override {
89 // The WebAssociatedURLLoader will continue after a load failure.
90 // For example, for an Access Control error.
91 if (completed_)
92 return;
93 OnLoadCompleteInternal(LOAD_SUCCEEDED);
94 }
95 void didFail(const blink::WebURLError& error) override {
96 OnLoadCompleteInternal(LOAD_FAILED);
97 }
98
99 private:
100 // Set to true once the request is complete.
101 bool completed_;
102
103 // Buffer to hold the content from the server.
104 std::string data_;
105
106 // A copy of the original resource response.
107 blink::WebURLResponse response_;
108
109 LoadStatus status_;
110
111 // Callback when we're done.
112 Callback callback_;
113
114 DISALLOW_COPY_AND_ASSIGN(ClientImpl);
115 };
116
117 AssociatedResourceFetcherImpl::AssociatedResourceFetcherImpl(const GURL& url)
118 : request_(url) {}
119
120 AssociatedResourceFetcherImpl::~AssociatedResourceFetcherImpl() {
121 if (!loader_)
122 return;
123
124 DCHECK(client_);
125
126 if (!client_->completed())
127 loader_->cancel();
128 }
129
130 void AssociatedResourceFetcherImpl::SetSkipServiceWorker(
131 blink::WebURLRequest::SkipServiceWorker skip_service_worker) {
132 DCHECK(!request_.isNull());
133 DCHECK(!loader_);
134
135 request_.setSkipServiceWorker(skip_service_worker);
136 }
137
138 void AssociatedResourceFetcherImpl::SetCachePolicy(
139 blink::WebCachePolicy policy) {
140 DCHECK(!request_.isNull());
141 DCHECK(!loader_);
142
143 request_.setCachePolicy(policy);
144 }
145
146 void AssociatedResourceFetcherImpl::SetLoaderOptions(
147 const blink::WebAssociatedURLLoaderOptions& options) {
148 DCHECK(!request_.isNull());
149 DCHECK(!loader_);
150
151 options_ = options;
152 }
153
154 void AssociatedResourceFetcherImpl::Start(
155 blink::WebFrame* frame,
156 blink::WebURLRequest::RequestContext request_context,
157 blink::WebURLRequest::FrameType frame_type,
158 const Callback& callback) {
159 DCHECK(!loader_);
160 DCHECK(!client_);
161 DCHECK(!request_.isNull());
162 if (!request_.httpBody().isNull())
163 DCHECK_NE("GET", request_.httpMethod().utf8()) << "GETs can't have bodies.";
164
165 request_.setRequestContext(request_context);
166 request_.setFrameType(frame_type);
167 request_.setFirstPartyForCookies(frame->document().firstPartyForCookies());
168 frame->dispatchWillSendRequest(request_);
169
170 client_.reset(new ClientImpl(callback));
171
172 loader_.reset(frame->createAssociatedURLLoader(options_));
173 loader_->loadAsynchronously(request_, client_.get());
174
175 // No need to hold on to the request; reset it now.
176 request_ = blink::WebURLRequest();
177 }
178
179 void AssociatedResourceFetcherImpl::Cancel() {
180 loader_->cancel();
181 client_->Cancel();
182 }
183
184 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/fetchers/associated_resource_fetcher_impl.h ('k') | content/renderer/fetchers/manifest_fetcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698