OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/offline_pages/background/request_coordinator.h" | 5 #include "components/offline_pages/background/request_coordinator.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 base::Bind(callback, request, Offliner::RequestStatus::SAVED)); | 77 base::Bind(callback, request, Offliner::RequestStatus::SAVED)); |
78 } | 78 } |
79 return true; | 79 return true; |
80 } | 80 } |
81 | 81 |
82 // Clears the currently processing request, if any. Must have called | 82 // Clears the currently processing request, if any. Must have called |
83 // LoadAndSave first to set the callback and request. | 83 // LoadAndSave first to set the callback and request. |
84 // Clears the currently processing request, if any. | 84 // Clears the currently processing request, if any. |
85 void Cancel() override { | 85 void Cancel() override { |
86 base::ThreadTaskRunnerHandle::Get()->PostTask( | 86 base::ThreadTaskRunnerHandle::Get()->PostTask( |
87 FROM_HERE, | 87 FROM_HERE, base::Bind(callback_, request_, |
88 base::Bind(callback_, request_, Offliner::RequestStatus::CANCELED)); | 88 Offliner::RequestStatus::LOAD_CANCELED)); |
89 } | 89 } |
90 | 90 |
91 void enable_callback(bool enable) { | 91 void enable_callback(bool enable) { |
92 enable_callback_ = enable; | 92 enable_callback_ = enable; |
93 } | 93 } |
94 | 94 |
95 private: | 95 private: |
96 CompletionCallback callback_; | 96 CompletionCallback callback_; |
97 SavePageRequest request_; | 97 SavePageRequest request_; |
98 bool enable_callback_; | 98 bool enable_callback_; |
99 }; | 99 }; |
100 | 100 |
101 class OfflinerFactoryStub : public OfflinerFactory { | 101 class OfflinerFactoryStub : public OfflinerFactory { |
102 public: | 102 public: |
103 | |
104 OfflinerFactoryStub() : offliner_(nullptr) {} | 103 OfflinerFactoryStub() : offliner_(nullptr) {} |
105 | 104 |
106 Offliner* GetOffliner(const OfflinerPolicy* policy) override { | 105 Offliner* GetOffliner(const OfflinerPolicy* policy) override { |
107 if (offliner_.get() == nullptr) { | 106 if (offliner_.get() == nullptr) { |
108 offliner_.reset(new OfflinerStub()); | 107 offliner_.reset(new OfflinerStub()); |
109 } | 108 } |
110 return offliner_.get(); | 109 return offliner_.get(); |
111 } | 110 } |
112 | 111 |
113 private: | 112 private: |
(...skipping 11 matching lines...) Expand all Loading... |
125 void PumpLoop(); | 124 void PumpLoop(); |
126 | 125 |
127 RequestCoordinator* coordinator() { | 126 RequestCoordinator* coordinator() { |
128 return coordinator_.get(); | 127 return coordinator_.get(); |
129 } | 128 } |
130 | 129 |
131 bool is_busy() { | 130 bool is_busy() { |
132 return coordinator_->is_busy(); | 131 return coordinator_->is_busy(); |
133 } | 132 } |
134 | 133 |
135 void SendRequestToOffliner(SavePageRequest& request) { | 134 void SendRequestToOffliner(const SavePageRequest& request) { |
136 coordinator_->SendRequestToOffliner(request); | 135 coordinator_->SendRequestToOffliner(request); |
137 } | 136 } |
138 | 137 |
139 // Empty callback function | 138 // Empty callback function |
140 void EmptyCallbackFunction(bool result) { | 139 void EmptyCallbackFunction(bool result) { |
141 } | 140 } |
142 | 141 |
143 // Callback for Add requests | 142 // Callback for Add requests |
144 void AddRequestDone(RequestQueue::AddRequestResult result, | 143 void AddRequestDone(RequestQueue::AddRequestResult result, |
145 const SavePageRequest& request); | 144 const SavePageRequest& request); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 // We need to give a callback to the request. | 327 // We need to give a callback to the request. |
329 base::Callback<void(bool)> callback = | 328 base::Callback<void(bool)> callback = |
330 base::Bind( | 329 base::Bind( |
331 &RequestCoordinatorTest::EmptyCallbackFunction, | 330 &RequestCoordinatorTest::EmptyCallbackFunction, |
332 base::Unretained(this)); | 331 base::Unretained(this)); |
333 coordinator()->SetProcessingCallbackForTest(callback); | 332 coordinator()->SetProcessingCallbackForTest(callback); |
334 | 333 |
335 // Call the OfflinerDoneCallback to simulate the request failed, wait | 334 // Call the OfflinerDoneCallback to simulate the request failed, wait |
336 // for callbacks. | 335 // for callbacks. |
337 EnableOfflinerCallback(true); | 336 EnableOfflinerCallback(true); |
338 SendOfflinerDoneCallback(request, Offliner::RequestStatus::FAILED); | 337 SendOfflinerDoneCallback(request, Offliner::RequestStatus::LOAD_FAILED); |
339 PumpLoop(); | 338 PumpLoop(); |
340 | 339 |
341 // Verify the request is not removed from the queue, and wait for callbacks. | 340 // Verify the request is not removed from the queue, and wait for callbacks. |
342 coordinator()->queue()->GetRequests( | 341 coordinator()->queue()->GetRequests( |
343 base::Bind(&RequestCoordinatorTest::GetRequestsDone, | 342 base::Bind(&RequestCoordinatorTest::GetRequestsDone, |
344 base::Unretained(this))); | 343 base::Unretained(this))); |
345 PumpLoop(); | 344 PumpLoop(); |
346 | 345 |
347 // Still one request in the queue. | 346 // Still one request in the queue. |
348 EXPECT_EQ(1UL, last_requests().size()); | 347 EXPECT_EQ(1UL, last_requests().size()); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 PumpLoop(); | 405 PumpLoop(); |
407 | 406 |
408 // Now we cancel it while the prerenderer is busy. | 407 // Now we cancel it while the prerenderer is busy. |
409 coordinator()->StopProcessing(); | 408 coordinator()->StopProcessing(); |
410 | 409 |
411 // Let the async callbacks in the cancel run. | 410 // Let the async callbacks in the cancel run. |
412 PumpLoop(); | 411 PumpLoop(); |
413 | 412 |
414 // OfflinerDoneCallback will not end up getting called with status SAVED, | 413 // OfflinerDoneCallback will not end up getting called with status SAVED, |
415 // Since we cancelled the event before it called offliner_->LoadAndSave(). | 414 // Since we cancelled the event before it called offliner_->LoadAndSave(). |
416 EXPECT_EQ(Offliner::RequestStatus::CANCELED, last_offlining_status()); | 415 EXPECT_EQ(Offliner::RequestStatus::LOAD_CANCELED, last_offlining_status()); |
417 } | 416 } |
418 | 417 |
419 } // namespace offline_pages | 418 } // namespace offline_pages |
OLD | NEW |