| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/rand_util.h" |
| 13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 14 #include "components/offline_pages/background/offliner_factory.h" | 15 #include "components/offline_pages/background/offliner_factory.h" |
| 15 #include "components/offline_pages/background/offliner_policy.h" | 16 #include "components/offline_pages/background/offliner_policy.h" |
| 16 #include "components/offline_pages/background/request_picker.h" | 17 #include "components/offline_pages/background/request_picker.h" |
| 17 #include "components/offline_pages/background/save_page_request.h" | 18 #include "components/offline_pages/background/save_page_request.h" |
| 18 #include "components/offline_pages/offline_page_item.h" | 19 #include "components/offline_pages/offline_page_item.h" |
| 19 #include "components/offline_pages/offline_page_model.h" | 20 #include "components/offline_pages/offline_page_model.h" |
| 20 | 21 |
| 21 namespace offline_pages { | 22 namespace offline_pages { |
| 22 | 23 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 37 // macro adapted to allow for a dynamically suffixed histogram name. | 38 // macro adapted to allow for a dynamically suffixed histogram name. |
| 38 // Note: The factory creates and owns the histogram. | 39 // Note: The factory creates and owns the histogram. |
| 39 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( | 40 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( |
| 40 histogram_name, 1, | 41 histogram_name, 1, |
| 41 static_cast<int>(Offliner::RequestStatus::STATUS_COUNT), | 42 static_cast<int>(Offliner::RequestStatus::STATUS_COUNT), |
| 42 static_cast<int>(Offliner::RequestStatus::STATUS_COUNT) + 1, | 43 static_cast<int>(Offliner::RequestStatus::STATUS_COUNT) + 1, |
| 43 base::HistogramBase::kUmaTargetedHistogramFlag); | 44 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 44 histogram->Add(static_cast<int>(request_status)); | 45 histogram->Add(static_cast<int>(request_status)); |
| 45 } | 46 } |
| 46 | 47 |
| 48 // This should use the same algorithm as we use for OfflinePageItem, so the IDs |
| 49 // are similar. |
| 50 int64_t GenerateOfflineId() { |
| 51 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; |
| 52 } |
| 53 |
| 54 |
| 47 } // namespace | 55 } // namespace |
| 48 | 56 |
| 49 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 57 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
| 50 std::unique_ptr<OfflinerFactory> factory, | 58 std::unique_ptr<OfflinerFactory> factory, |
| 51 std::unique_ptr<RequestQueue> queue, | 59 std::unique_ptr<RequestQueue> queue, |
| 52 std::unique_ptr<Scheduler> scheduler) | 60 std::unique_ptr<Scheduler> scheduler) |
| 53 : is_busy_(false), | 61 : is_busy_(false), |
| 54 is_canceled_(false), | 62 is_canceled_(false), |
| 55 offliner_(nullptr), | 63 offliner_(nullptr), |
| 56 policy_(std::move(policy)), | 64 policy_(std::move(policy)), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 70 | 78 |
| 71 bool RequestCoordinator::SavePageLater( | 79 bool RequestCoordinator::SavePageLater( |
| 72 const GURL& url, const ClientId& client_id, bool user_requested) { | 80 const GURL& url, const ClientId& client_id, bool user_requested) { |
| 73 DVLOG(2) << "URL is " << url << " " << __func__; | 81 DVLOG(2) << "URL is " << url << " " << __func__; |
| 74 | 82 |
| 75 if (!OfflinePageModel::CanSaveURL(url)) { | 83 if (!OfflinePageModel::CanSaveURL(url)) { |
| 76 DVLOG(1) << "Not able to save page for requested url: " << url; | 84 DVLOG(1) << "Not able to save page for requested url: " << url; |
| 77 return false; | 85 return false; |
| 78 } | 86 } |
| 79 | 87 |
| 80 // TODO(petewil): We need a robust scheme for allocating new IDs. | 88 int64_t id = GenerateOfflineId(); |
| 81 static int64_t id = 0; | |
| 82 | 89 |
| 83 // Build a SavePageRequest. | 90 // Build a SavePageRequest. |
| 84 offline_pages::SavePageRequest request( | 91 offline_pages::SavePageRequest request( |
| 85 id++, url, client_id, base::Time::Now(), user_requested); | 92 id, url, client_id, base::Time::Now(), user_requested); |
| 86 | 93 |
| 87 // Put the request on the request queue. | 94 // Put the request on the request queue. |
| 88 queue_->AddRequest(request, | 95 queue_->AddRequest(request, |
| 89 base::Bind(&RequestCoordinator::AddRequestResultCallback, | 96 base::Bind(&RequestCoordinator::AddRequestResultCallback, |
| 90 weak_ptr_factory_.GetWeakPtr())); | 97 weak_ptr_factory_.GetWeakPtr())); |
| 91 return true; | 98 return true; |
| 92 } | 99 } |
| 93 void RequestCoordinator::GetAllRequests(const GetRequestsCallback& callback) { | 100 void RequestCoordinator::GetAllRequests(const GetRequestsCallback& callback) { |
| 94 // Get all matching requests from the request queue, send them to our | 101 // Get all matching requests from the request queue, send them to our |
| 95 // callback. We bind the namespace and callback to the front of the callback | 102 // callback. We bind the namespace and callback to the front of the callback |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 return trigger_conditions; | 378 return trigger_conditions; |
| 372 } | 379 } |
| 373 | 380 |
| 374 void RequestCoordinator::GetOffliner() { | 381 void RequestCoordinator::GetOffliner() { |
| 375 if (!offliner_) { | 382 if (!offliner_) { |
| 376 offliner_ = factory_->GetOffliner(policy_.get()); | 383 offliner_ = factory_->GetOffliner(policy_.get()); |
| 377 } | 384 } |
| 378 } | 385 } |
| 379 | 386 |
| 380 } // namespace offline_pages | 387 } // namespace offline_pages |
| OLD | NEW |