| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/android/url_request_content_job.h" | 5 #include "content/browser/android/url_request_content_job.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 15 #include "base/test/test_file_util.h" | 16 #include "base/test/test_file_util.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
| 17 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 21 |
| 20 | 22 |
| 21 namespace content { | 23 namespace content { |
| 22 | 24 |
| 23 namespace { | 25 namespace { |
| 24 | 26 |
| 25 // A URLRequestJobFactory that will return URLRequestContentJobWithCallbacks | 27 // A URLRequestJobFactory that will return URLRequestContentJobWithCallbacks |
| 26 // instances for content:// scheme URLs. | 28 // instances for content:// scheme URLs. |
| 27 class CallbacksJobFactory : public net::URLRequestJobFactory { | 29 class CallbacksJobFactory : public net::URLRequestJobFactory { |
| 28 public: | 30 public: |
| 29 class JobObserver { | 31 class JobObserver { |
| 30 public: | 32 public: |
| 31 virtual ~JobObserver() {} | 33 virtual ~JobObserver() {} |
| 32 virtual void OnJobCreated() = 0; | 34 virtual void OnJobCreated() = 0; |
| 33 }; | 35 }; |
| 34 | 36 |
| 35 CallbacksJobFactory(const base::FilePath& path, JobObserver* observer) | 37 CallbacksJobFactory(const base::FilePath& path, JobObserver* observer) |
| 36 : path_(path), observer_(observer) { | 38 : path_(path), observer_(observer) { |
| 37 } | 39 } |
| 38 | 40 |
| 39 ~CallbacksJobFactory() override {} | 41 ~CallbacksJobFactory() override {} |
| 40 | 42 |
| 41 net::URLRequestJob* MaybeCreateJobWithProtocolHandler( | 43 net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
| 42 const std::string& scheme, | 44 const std::string& scheme, |
| 43 net::URLRequest* request, | 45 net::URLRequest* request, |
| 44 net::NetworkDelegate* network_delegate) const override { | 46 net::NetworkDelegate* network_delegate) const override { |
| 45 URLRequestContentJob* job = | 47 URLRequestContentJob* job = new URLRequestContentJob( |
| 46 new URLRequestContentJob( | 48 request, network_delegate, path_, base::ThreadTaskRunnerHandle::Get()); |
| 47 request, | |
| 48 network_delegate, | |
| 49 path_, | |
| 50 const_cast<base::MessageLoop*>(&message_loop_)->task_runner()); | |
| 51 observer_->OnJobCreated(); | 49 observer_->OnJobCreated(); |
| 52 return job; | 50 return job; |
| 53 } | 51 } |
| 54 | 52 |
| 55 net::URLRequestJob* MaybeInterceptRedirect( | 53 net::URLRequestJob* MaybeInterceptRedirect( |
| 56 net::URLRequest* request, | 54 net::URLRequest* request, |
| 57 net::NetworkDelegate* network_delegate, | 55 net::NetworkDelegate* network_delegate, |
| 58 const GURL& location) const override { | 56 const GURL& location) const override { |
| 59 return nullptr; | 57 return nullptr; |
| 60 } | 58 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 71 | 69 |
| 72 bool IsHandledURL(const GURL& url) const override { | 70 bool IsHandledURL(const GURL& url) const override { |
| 73 return IsHandledProtocol(url.scheme()); | 71 return IsHandledProtocol(url.scheme()); |
| 74 } | 72 } |
| 75 | 73 |
| 76 bool IsSafeRedirectTarget(const GURL& location) const override { | 74 bool IsSafeRedirectTarget(const GURL& location) const override { |
| 77 return false; | 75 return false; |
| 78 } | 76 } |
| 79 | 77 |
| 80 private: | 78 private: |
| 81 base::MessageLoop message_loop_; | |
| 82 base::FilePath path_; | 79 base::FilePath path_; |
| 83 JobObserver* observer_; | 80 JobObserver* observer_; |
| 84 }; | 81 }; |
| 85 | 82 |
| 86 class JobObserverImpl : public CallbacksJobFactory::JobObserver { | 83 class JobObserverImpl : public CallbacksJobFactory::JobObserver { |
| 87 public: | 84 public: |
| 88 JobObserverImpl() : num_jobs_created_(0) {} | 85 JobObserverImpl() : num_jobs_created_(0) {} |
| 89 ~JobObserverImpl() override {} | 86 ~JobObserverImpl() override {} |
| 90 | 87 |
| 91 void OnJobCreated() override { ++num_jobs_created_; } | 88 void OnJobCreated() override { ++num_jobs_created_; } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 114 URLRequestContentJobTest(); | 111 URLRequestContentJobTest(); |
| 115 | 112 |
| 116 protected: | 113 protected: |
| 117 // This inserts an image file into the android MediaStore and retrieves the | 114 // This inserts an image file into the android MediaStore and retrieves the |
| 118 // content URI. Then creates and runs a URLRequestContentJob to get the | 115 // content URI. Then creates and runs a URLRequestContentJob to get the |
| 119 // contents out of it. If a Range is provided, this function will add the | 116 // contents out of it. If a Range is provided, this function will add the |
| 120 // appropriate Range http header to the request and verify the bytes | 117 // appropriate Range http header to the request and verify the bytes |
| 121 // retrieved. | 118 // retrieved. |
| 122 void RunRequest(const Range* range); | 119 void RunRequest(const Range* range); |
| 123 | 120 |
| 121 base::MessageLoop message_loop_; |
| 124 JobObserverImpl observer_; | 122 JobObserverImpl observer_; |
| 125 net::TestURLRequestContext context_; | 123 net::TestURLRequestContext context_; |
| 126 net::TestDelegate delegate_; | 124 net::TestDelegate delegate_; |
| 127 }; | 125 }; |
| 128 | 126 |
| 129 URLRequestContentJobTest::URLRequestContentJobTest() {} | 127 URLRequestContentJobTest::URLRequestContentJobTest() {} |
| 130 | 128 |
| 131 void URLRequestContentJobTest::RunRequest(const Range* range) { | 129 void URLRequestContentJobTest::RunRequest(const Range* range) { |
| 132 base::FilePath test_dir; | 130 base::FilePath test_dir; |
| 133 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir); | 131 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 } | 190 } |
| 193 | 191 |
| 194 TEST_F(URLRequestContentJobTest, ContentURIWithZeroRange) { | 192 TEST_F(URLRequestContentJobTest, ContentURIWithZeroRange) { |
| 195 Range range(0, 0); | 193 Range range(0, 0); |
| 196 RunRequest(&range); | 194 RunRequest(&range); |
| 197 } | 195 } |
| 198 | 196 |
| 199 } // namespace | 197 } // namespace |
| 200 | 198 |
| 201 } // namespace content | 199 } // namespace content |
| OLD | NEW |