OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/glue/resource_fetcher.h" | 5 #include "webkit/glue/resource_fetcher.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" | 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKitClient.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKitClient.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 } | 46 } |
47 | 47 |
48 void ResourceFetcher::Start(WebFrame* frame) { | 48 void ResourceFetcher::Start(WebFrame* frame) { |
49 WebURLRequest request(url_); | 49 WebURLRequest request(url_); |
50 frame->dispatchWillSendRequest(request); | 50 frame->dispatchWillSendRequest(request); |
51 | 51 |
52 loader_.reset(WebKit::webKitClient()->createURLLoader()); | 52 loader_.reset(WebKit::webKitClient()->createURLLoader()); |
53 loader_->loadAsynchronously(request, this); | 53 loader_->loadAsynchronously(request, this); |
54 } | 54 } |
55 | 55 |
| 56 void ResourceFetcher::RunCallback(const WebURLResponse& response, |
| 57 const std::string& data) { |
| 58 if (!callback_.get()) |
| 59 return; |
| 60 |
| 61 // Take care to clear callback_ before running the callback as it may lead to |
| 62 // our destruction. |
| 63 scoped_ptr<Callback> callback; |
| 64 callback.swap(callback_); |
| 65 callback->Run(response, data); |
| 66 } |
| 67 |
56 ///////////////////////////////////////////////////////////////////////////// | 68 ///////////////////////////////////////////////////////////////////////////// |
57 // WebURLLoaderClient methods | 69 // WebURLLoaderClient methods |
58 | 70 |
59 void ResourceFetcher::willSendRequest( | 71 void ResourceFetcher::willSendRequest( |
60 WebURLLoader* loader, WebURLRequest& new_request, | 72 WebURLLoader* loader, WebURLRequest& new_request, |
61 const WebURLResponse& redirect_response) { | 73 const WebURLResponse& redirect_response) { |
62 } | 74 } |
63 | 75 |
64 void ResourceFetcher::didSendData( | 76 void ResourceFetcher::didSendData( |
65 WebURLLoader* loader, unsigned long long bytes_sent, | 77 WebURLLoader* loader, unsigned long long bytes_sent, |
(...skipping 20 matching lines...) Expand all Loading... |
86 DCHECK(data_length > 0); | 98 DCHECK(data_length > 0); |
87 | 99 |
88 metadata_.assign(data, data_length); | 100 metadata_.assign(data, data_length); |
89 } | 101 } |
90 | 102 |
91 void ResourceFetcher::didFinishLoading( | 103 void ResourceFetcher::didFinishLoading( |
92 WebURLLoader* loader, double finishTime) { | 104 WebURLLoader* loader, double finishTime) { |
93 DCHECK(!completed_); | 105 DCHECK(!completed_); |
94 completed_ = true; | 106 completed_ = true; |
95 | 107 |
96 if (callback_.get()) { | 108 RunCallback(response_, data_); |
97 callback_->Run(response_, data_); | |
98 callback_.reset(); | |
99 } | |
100 } | 109 } |
101 | 110 |
102 void ResourceFetcher::didFail(WebURLLoader* loader, const WebURLError& error) { | 111 void ResourceFetcher::didFail(WebURLLoader* loader, const WebURLError& error) { |
103 DCHECK(!completed_); | 112 DCHECK(!completed_); |
104 completed_ = true; | 113 completed_ = true; |
105 | 114 |
106 // Go ahead and tell our delegate that we're done. | 115 // Go ahead and tell our delegate that we're done. |
107 if (callback_.get()) { | 116 RunCallback(WebURLResponse(), std::string()); |
108 callback_->Run(WebURLResponse(), std::string()); | |
109 callback_.reset(); | |
110 } | |
111 } | 117 } |
112 | 118 |
113 ///////////////////////////////////////////////////////////////////////////// | 119 ///////////////////////////////////////////////////////////////////////////// |
114 // A resource fetcher with a timeout | 120 // A resource fetcher with a timeout |
115 | 121 |
116 ResourceFetcherWithTimeout::ResourceFetcherWithTimeout( | 122 ResourceFetcherWithTimeout::ResourceFetcherWithTimeout( |
117 const GURL& url, WebFrame* frame, int timeout_secs, Callback* c) | 123 const GURL& url, WebFrame* frame, int timeout_secs, Callback* c) |
118 : ResourceFetcher(url, frame, c) { | 124 : ResourceFetcher(url, frame, c) { |
119 timeout_timer_.Start(TimeDelta::FromSeconds(timeout_secs), this, | 125 timeout_timer_.Start(TimeDelta::FromSeconds(timeout_secs), this, |
120 &ResourceFetcherWithTimeout::TimeoutFired); | 126 &ResourceFetcherWithTimeout::TimeoutFired); |
121 } | 127 } |
122 | 128 |
123 ResourceFetcherWithTimeout::~ResourceFetcherWithTimeout() { | 129 ResourceFetcherWithTimeout::~ResourceFetcherWithTimeout() { |
124 } | 130 } |
125 | 131 |
126 void ResourceFetcherWithTimeout::TimeoutFired() { | 132 void ResourceFetcherWithTimeout::TimeoutFired() { |
127 if (!completed_) { | 133 if (!completed_) { |
128 loader_->cancel(); | 134 loader_->cancel(); |
129 didFail(NULL, WebURLError()); | 135 didFail(NULL, WebURLError()); |
130 } | 136 } |
131 } | 137 } |
132 | 138 |
133 } // namespace webkit_glue | 139 } // namespace webkit_glue |
OLD | NEW |