| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/timer.h" | 9 #include "base/timer.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 public: | 33 public: |
| 34 FetcherDelegate() | 34 FetcherDelegate() |
| 35 : completed_(false), | 35 : completed_(false), |
| 36 timed_out_(false) { | 36 timed_out_(false) { |
| 37 // Start a repeating timer waiting for the download to complete. The | 37 // Start a repeating timer waiting for the download to complete. The |
| 38 // callback has to be a static function, so we hold on to our instance. | 38 // callback has to be a static function, so we hold on to our instance. |
| 39 FetcherDelegate::instance_ = this; | 39 FetcherDelegate::instance_ = this; |
| 40 StartTimer(); | 40 StartTimer(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 virtual ~FetcherDelegate() {} |
| 44 |
| 43 ResourceFetcher::Callback* NewCallback() { | 45 ResourceFetcher::Callback* NewCallback() { |
| 44 return ::NewCallback(this, &FetcherDelegate::OnURLFetchComplete); | 46 return ::NewCallback(this, &FetcherDelegate::OnURLFetchComplete); |
| 45 } | 47 } |
| 46 | 48 |
| 47 virtual void OnURLFetchComplete(const WebURLResponse& response, | 49 virtual void OnURLFetchComplete(const WebURLResponse& response, |
| 48 const std::string& data) { | 50 const std::string& data) { |
| 49 response_ = response; | 51 response_ = response; |
| 50 data_ = data; | 52 data_ = data; |
| 51 completed_ = true; | 53 completed_ = true; |
| 52 timer_.Stop(); | 54 timer_.Stop(); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // When we timeout, we still call the Delegate callback but we pass in empty | 168 // When we timeout, we still call the Delegate callback but we pass in empty |
| 167 // values. | 169 // values. |
| 168 EXPECT_TRUE(delegate->completed()); | 170 EXPECT_TRUE(delegate->completed()); |
| 169 EXPECT_TRUE(delegate->response().isNull()); | 171 EXPECT_TRUE(delegate->response().isNull()); |
| 170 EXPECT_EQ(delegate->data(), std::string()); | 172 EXPECT_EQ(delegate->data(), std::string()); |
| 171 EXPECT_FALSE(delegate->timed_out()); | 173 EXPECT_FALSE(delegate->timed_out()); |
| 172 } | 174 } |
| 173 | 175 |
| 174 class EvilFetcherDelegate : public FetcherDelegate { | 176 class EvilFetcherDelegate : public FetcherDelegate { |
| 175 public: | 177 public: |
| 178 virtual ~EvilFetcherDelegate() {} |
| 179 |
| 176 void SetFetcher(ResourceFetcher* fetcher) { | 180 void SetFetcher(ResourceFetcher* fetcher) { |
| 177 fetcher_.reset(fetcher); | 181 fetcher_.reset(fetcher); |
| 178 } | 182 } |
| 179 | 183 |
| 180 void OnURLFetchComplete(const WebURLResponse& response, | 184 virtual void OnURLFetchComplete(const WebURLResponse& response, |
| 181 const std::string& data) { | 185 const std::string& data) { |
| 182 // Destroy the ResourceFetcher here. We are testing that upon returning | 186 // Destroy the ResourceFetcher here. We are testing that upon returning |
| 183 // to the ResourceFetcher that it does not crash. | 187 // to the ResourceFetcher that it does not crash. |
| 184 fetcher_.reset(); | 188 fetcher_.reset(); |
| 185 FetcherDelegate::OnURLFetchComplete(response, data); | 189 FetcherDelegate::OnURLFetchComplete(response, data); |
| 186 } | 190 } |
| 187 | 191 |
| 188 private: | 192 private: |
| 189 scoped_ptr<ResourceFetcher> fetcher_; | 193 scoped_ptr<ResourceFetcher> fetcher_; |
| 190 }; | 194 }; |
| 191 | 195 |
| 192 TEST_F(ResourceFetcherTests, ResourceFetcherDeletedInCallback) { | 196 TEST_F(ResourceFetcherTests, ResourceFetcherDeletedInCallback) { |
| 193 ASSERT_TRUE(test_server_.Start()); | 197 ASSERT_TRUE(test_server_.Start()); |
| 194 | 198 |
| 195 WebFrame* frame = test_shell_->webView()->mainFrame(); | 199 WebFrame* frame = test_shell_->webView()->mainFrame(); |
| 196 | 200 |
| 197 // Grab a page that takes at least 1 sec to respond, but set the fetcher to | 201 // Grab a page that takes at least 1 sec to respond, but set the fetcher to |
| 198 // timeout in 0 sec. | 202 // timeout in 0 sec. |
| 199 GURL url(test_server_.GetURL("slow?1")); | 203 GURL url(test_server_.GetURL("slow?1")); |
| 200 scoped_ptr<EvilFetcherDelegate> delegate(new EvilFetcherDelegate); | 204 scoped_ptr<EvilFetcherDelegate> delegate(new EvilFetcherDelegate); |
| 201 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcherWithTimeout( | 205 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcherWithTimeout( |
| 202 url, frame, WebURLRequest::TargetIsMainFrame, | 206 url, frame, WebURLRequest::TargetIsMainFrame, |
| 203 0, delegate->NewCallback())); | 207 0, delegate->NewCallback())); |
| 204 delegate->SetFetcher(fetcher.release()); | 208 delegate->SetFetcher(fetcher.release()); |
| 205 | 209 |
| 206 delegate->WaitForResponse(); | 210 delegate->WaitForResponse(); |
| 207 EXPECT_FALSE(delegate->timed_out()); | 211 EXPECT_FALSE(delegate->timed_out()); |
| 208 } | 212 } |
| 209 | 213 |
| 210 } // namespace | 214 } // namespace |
| OLD | NEW |