Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / glue / resource_fetcher.cc
blob8729aa74369316e95213b791dae7ae4f5e909f7b
1 // Copyright (c) 2012 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.
5 #include "webkit/glue/resource_fetcher.h"
7 #include "base/logging.h"
8 #include "third_party/WebKit/Source/Platform/chromium/public/Platform.h"
9 #include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h"
10 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLError.h"
11 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLLoader.h"
12 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
17 using base::TimeDelta;
18 using WebKit::WebFrame;
19 using WebKit::WebURLError;
20 using WebKit::WebURLLoader;
21 using WebKit::WebURLRequest;
22 using WebKit::WebURLResponse;
24 namespace webkit_glue {
26 ResourceFetcher::ResourceFetcher(const GURL& url, WebFrame* frame,
27 WebURLRequest::TargetType target_type,
28 const Callback& callback)
29 : url_(url),
30 target_type_(target_type),
31 completed_(false),
32 callback_(callback) {
33 // Can't do anything without a frame. However, delegate can be NULL (so we
34 // can do a http request and ignore the results).
35 DCHECK(frame);
36 Start(frame);
39 ResourceFetcher::~ResourceFetcher() {
40 if (!completed_ && loader_.get())
41 loader_->cancel();
44 void ResourceFetcher::Cancel() {
45 if (!completed_) {
46 loader_->cancel();
47 completed_ = true;
51 void ResourceFetcher::Start(WebFrame* frame) {
52 WebURLRequest request(url_);
53 request.setTargetType(target_type_);
54 if (target_type_ == WebURLRequest::TargetIsFavicon) {
55 // Disable cookies to avoid side effects when fetching favicon.
56 request.setAllowCookies(false);
58 request.setFirstPartyForCookies(frame->document().firstPartyForCookies());
59 frame->dispatchWillSendRequest(request);
61 loader_.reset(WebKit::Platform::current()->createURLLoader());
62 loader_->loadAsynchronously(request, this);
65 void ResourceFetcher::RunCallback(const WebURLResponse& response,
66 const std::string& data) {
67 if (callback_.is_null())
68 return;
70 // Take a reference to the callback as running the callback may lead to our
71 // destruction.
72 Callback callback = callback_;
73 callback.Run(response, data);
76 /////////////////////////////////////////////////////////////////////////////
77 // WebURLLoaderClient methods
79 void ResourceFetcher::willSendRequest(
80 WebURLLoader* loader, WebURLRequest& new_request,
81 const WebURLResponse& redirect_response) {
84 void ResourceFetcher::didSendData(
85 WebURLLoader* loader, unsigned long long bytes_sent,
86 unsigned long long total_bytes_to_be_sent) {
89 void ResourceFetcher::didReceiveResponse(
90 WebURLLoader* loader, const WebURLResponse& response) {
91 DCHECK(!completed_);
92 response_ = response;
95 void ResourceFetcher::didReceiveData(
96 WebURLLoader* loader, const char* data, int data_length,
97 int encoded_data_length) {
98 DCHECK(!completed_);
99 DCHECK(data_length > 0);
101 data_.append(data, data_length);
104 void ResourceFetcher::didReceiveCachedMetadata(
105 WebURLLoader* loader, const char* data, int data_length) {
106 DCHECK(!completed_);
107 DCHECK(data_length > 0);
109 metadata_.assign(data, data_length);
112 void ResourceFetcher::didFinishLoading(
113 WebURLLoader* loader, double finishTime) {
114 DCHECK(!completed_);
115 completed_ = true;
117 RunCallback(response_, data_);
120 void ResourceFetcher::didFail(WebURLLoader* loader, const WebURLError& error) {
121 DCHECK(!completed_);
122 completed_ = true;
124 // Go ahead and tell our delegate that we're done.
125 RunCallback(WebURLResponse(), std::string());
128 /////////////////////////////////////////////////////////////////////////////
129 // A resource fetcher with a timeout
131 ResourceFetcherWithTimeout::ResourceFetcherWithTimeout(
132 const GURL& url, WebFrame* frame, WebURLRequest::TargetType target_type,
133 int timeout_secs, const Callback& callback)
134 : ResourceFetcher(url, frame, target_type, callback) {
135 timeout_timer_.Start(FROM_HERE, TimeDelta::FromSeconds(timeout_secs), this,
136 &ResourceFetcherWithTimeout::TimeoutFired);
139 ResourceFetcherWithTimeout::~ResourceFetcherWithTimeout() {
142 void ResourceFetcherWithTimeout::TimeoutFired() {
143 if (!completed_) {
144 loader_->cancel();
145 didFail(NULL, WebURLError());
149 } // namespace webkit_glue