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/callback.h" | 7 #include "base/callback.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 // Start a repeating timer waiting for the download to complete. The | 39 // Start a repeating timer waiting for the download to complete. The |
40 // callback has to be a static function, so we hold on to our instance. | 40 // callback has to be a static function, so we hold on to our instance. |
41 FetcherDelegate::instance_ = this; | 41 FetcherDelegate::instance_ = this; |
42 CreateTimer(kWaitIntervalMs); | 42 CreateTimer(kWaitIntervalMs); |
43 } | 43 } |
44 | 44 |
45 ResourceFetcher::Callback* NewCallback() { | 45 ResourceFetcher::Callback* NewCallback() { |
46 return ::NewCallback(this, &FetcherDelegate::OnURLFetchComplete); | 46 return ::NewCallback(this, &FetcherDelegate::OnURLFetchComplete); |
47 } | 47 } |
48 | 48 |
49 void OnURLFetchComplete(const WebURLResponse& response, | 49 virtual void OnURLFetchComplete(const WebURLResponse& response, |
50 const std::string& data) { | 50 const std::string& data) { |
51 response_ = response; | 51 response_ = response; |
52 data_ = data; | 52 data_ = data; |
53 completed_ = true; | 53 completed_ = true; |
54 DestroyTimer(); | 54 DestroyTimer(); |
55 MessageLoop::current()->Quit(); | 55 MessageLoop::current()->Quit(); |
56 } | 56 } |
57 | 57 |
58 bool completed() const { return completed_; } | 58 bool completed() const { return completed_; } |
59 bool timed_out() const { return time_elapsed_ms_ > kMaxWaitTimeMs; } | 59 bool timed_out() const { return time_elapsed_ms_ > kMaxWaitTimeMs; } |
60 | 60 |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 delegate->WaitForResponse(); | 216 delegate->WaitForResponse(); |
217 | 217 |
218 // When we timeout, we still call the Delegate callback but we pass in empty | 218 // When we timeout, we still call the Delegate callback but we pass in empty |
219 // values. | 219 // values. |
220 EXPECT_TRUE(delegate->completed()); | 220 EXPECT_TRUE(delegate->completed()); |
221 EXPECT_TRUE(delegate->response().isNull()); | 221 EXPECT_TRUE(delegate->response().isNull()); |
222 EXPECT_EQ(delegate->data(), std::string()); | 222 EXPECT_EQ(delegate->data(), std::string()); |
223 EXPECT_TRUE(delegate->time_elapsed_ms() < kMaxWaitTimeMs); | 223 EXPECT_TRUE(delegate->time_elapsed_ms() < kMaxWaitTimeMs); |
224 } | 224 } |
225 | 225 |
| 226 class EvilFetcherDelegate : public FetcherDelegate { |
| 227 public: |
| 228 void SetFetcher(ResourceFetcher* fetcher) { |
| 229 fetcher_.reset(fetcher); |
| 230 } |
| 231 |
| 232 void OnURLFetchComplete(const WebURLResponse& response, |
| 233 const std::string& data) { |
| 234 // Destroy the ResourceFetcher here. We are testing that upon returning |
| 235 // to the ResourceFetcher that it does not crash. |
| 236 fetcher_.reset(); |
| 237 FetcherDelegate::OnURLFetchComplete(response, data); |
| 238 } |
| 239 |
| 240 private: |
| 241 scoped_ptr<ResourceFetcher> fetcher_; |
| 242 }; |
| 243 |
| 244 TEST_F(ResourceFetcherTests, ResourceFetcherDeletedInCallback) { |
| 245 ASSERT_TRUE(test_server_.Start()); |
| 246 |
| 247 WebFrame* frame = test_shell_->webView()->mainFrame(); |
| 248 |
| 249 // Grab a page that takes at least 1 sec to respond, but set the fetcher to |
| 250 // timeout in 0 sec. |
| 251 GURL url(test_server_.GetURL("slow?1")); |
| 252 scoped_ptr<EvilFetcherDelegate> delegate(new EvilFetcherDelegate); |
| 253 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcherWithTimeout( |
| 254 url, frame, 0, delegate->NewCallback())); |
| 255 delegate->SetFetcher(fetcher.release()); |
| 256 |
| 257 delegate->WaitForResponse(); |
| 258 } |
| 259 |
226 } // namespace | 260 } // namespace |
OLD | NEW |