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 | |
21 namespace content { | 22 namespace content { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // A URLRequestJobFactory that will return URLRequestContentJobWithCallbacks | 26 // A URLRequestJobFactory that will return URLRequestContentJobWithCallbacks |
26 // instances for content:// scheme URLs. | 27 // instances for content:// scheme URLs. |
27 class CallbacksJobFactory : public net::URLRequestJobFactory { | 28 class CallbacksJobFactory : public net::URLRequestJobFactory { |
28 public: | 29 public: |
29 class JobObserver { | 30 class JobObserver { |
30 public: | 31 public: |
31 virtual ~JobObserver() {} | 32 virtual ~JobObserver() {} |
32 virtual void OnJobCreated() = 0; | 33 virtual void OnJobCreated() = 0; |
33 }; | 34 }; |
34 | 35 |
35 CallbacksJobFactory(const base::FilePath& path, JobObserver* observer) | 36 CallbacksJobFactory(const base::FilePath& path, JobObserver* observer) |
36 : path_(path), observer_(observer) { | 37 : path_(path), observer_(observer) { |
37 } | 38 } |
38 | 39 |
39 ~CallbacksJobFactory() override {} | 40 ~CallbacksJobFactory() override {} |
40 | 41 |
41 net::URLRequestJob* MaybeCreateJobWithProtocolHandler( | 42 net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
42 const std::string& scheme, | 43 const std::string& scheme, |
43 net::URLRequest* request, | 44 net::URLRequest* request, |
44 net::NetworkDelegate* network_delegate) const override { | 45 net::NetworkDelegate* network_delegate) const override { |
45 URLRequestContentJob* job = | 46 URLRequestContentJob* job = |
46 new URLRequestContentJob( | 47 new URLRequestContentJob( |
47 request, | 48 request, |
48 network_delegate, | 49 network_delegate, |
49 path_, | 50 path_, |
50 const_cast<base::MessageLoop*>(&message_loop_)->task_runner()); | 51 base::ThreadTaskRunnerHandle::Get()); |
51 observer_->OnJobCreated(); | 52 observer_->OnJobCreated(); |
52 return job; | 53 return job; |
53 } | 54 } |
54 | 55 |
55 net::URLRequestJob* MaybeInterceptRedirect( | 56 net::URLRequestJob* MaybeInterceptRedirect( |
56 net::URLRequest* request, | 57 net::URLRequest* request, |
57 net::NetworkDelegate* network_delegate, | 58 net::NetworkDelegate* network_delegate, |
58 const GURL& location) const override { | 59 const GURL& location) const override { |
59 return nullptr; | 60 return nullptr; |
60 } | 61 } |
(...skipping 10 matching lines...) Expand all Loading... |
71 | 72 |
72 bool IsHandledURL(const GURL& url) const override { | 73 bool IsHandledURL(const GURL& url) const override { |
73 return IsHandledProtocol(url.scheme()); | 74 return IsHandledProtocol(url.scheme()); |
74 } | 75 } |
75 | 76 |
76 bool IsSafeRedirectTarget(const GURL& location) const override { | 77 bool IsSafeRedirectTarget(const GURL& location) const override { |
77 return false; | 78 return false; |
78 } | 79 } |
79 | 80 |
80 private: | 81 private: |
81 base::MessageLoop message_loop_; | |
82 base::FilePath path_; | 82 base::FilePath path_; |
83 JobObserver* observer_; | 83 JobObserver* observer_; |
84 }; | 84 }; |
85 | 85 |
86 class JobObserverImpl : public CallbacksJobFactory::JobObserver { | 86 class JobObserverImpl : public CallbacksJobFactory::JobObserver { |
87 public: | 87 public: |
88 JobObserverImpl() : num_jobs_created_(0) {} | 88 JobObserverImpl() : num_jobs_created_(0) {} |
89 ~JobObserverImpl() override {} | 89 ~JobObserverImpl() override {} |
90 | 90 |
91 void OnJobCreated() override { ++num_jobs_created_; } | 91 void OnJobCreated() override { ++num_jobs_created_; } |
(...skipping 22 matching lines...) Expand all Loading... |
114 URLRequestContentJobTest(); | 114 URLRequestContentJobTest(); |
115 | 115 |
116 protected: | 116 protected: |
117 // This inserts an image file into the android MediaStore and retrieves the | 117 // This inserts an image file into the android MediaStore and retrieves the |
118 // content URI. Then creates and runs a URLRequestContentJob to get the | 118 // 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 | 119 // 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 | 120 // appropriate Range http header to the request and verify the bytes |
121 // retrieved. | 121 // retrieved. |
122 void RunRequest(const Range* range); | 122 void RunRequest(const Range* range); |
123 | 123 |
| 124 base::MessageLoop message_loop_; |
124 JobObserverImpl observer_; | 125 JobObserverImpl observer_; |
125 net::TestURLRequestContext context_; | 126 net::TestURLRequestContext context_; |
126 net::TestDelegate delegate_; | 127 net::TestDelegate delegate_; |
127 }; | 128 }; |
128 | 129 |
129 URLRequestContentJobTest::URLRequestContentJobTest() {} | 130 URLRequestContentJobTest::URLRequestContentJobTest() {} |
130 | 131 |
131 void URLRequestContentJobTest::RunRequest(const Range* range) { | 132 void URLRequestContentJobTest::RunRequest(const Range* range) { |
132 base::FilePath test_dir; | 133 base::FilePath test_dir; |
133 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir); | 134 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 } | 193 } |
193 | 194 |
194 TEST_F(URLRequestContentJobTest, ContentURIWithZeroRange) { | 195 TEST_F(URLRequestContentJobTest, ContentURIWithZeroRange) { |
195 Range range(0, 0); | 196 Range range(0, 0); |
196 RunRequest(&range); | 197 RunRequest(&range); |
197 } | 198 } |
198 | 199 |
199 } // namespace | 200 } // namespace |
200 | 201 |
201 } // namespace content | 202 } // namespace content |
OLD | NEW |