Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: components/offline_pages/background/request_coordinator.cc

Issue 2234873004: Have the RequestCoordinator generate the offline_id (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge, and CR fixes per Dimich Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
47 } // namespace 54 } // namespace
48 55
49 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, 56 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy,
50 std::unique_ptr<OfflinerFactory> factory, 57 std::unique_ptr<OfflinerFactory> factory,
51 std::unique_ptr<RequestQueue> queue, 58 std::unique_ptr<RequestQueue> queue,
52 std::unique_ptr<Scheduler> scheduler) 59 std::unique_ptr<Scheduler> scheduler)
53 : is_busy_(false), 60 : is_busy_(false),
54 is_canceled_(false), 61 is_canceled_(false),
55 offliner_(nullptr), 62 offliner_(nullptr),
56 policy_(std::move(policy)), 63 policy_(std::move(policy)),
(...skipping 13 matching lines...) Expand all
70 77
71 bool RequestCoordinator::SavePageLater( 78 bool RequestCoordinator::SavePageLater(
72 const GURL& url, const ClientId& client_id, bool user_requested) { 79 const GURL& url, const ClientId& client_id, bool user_requested) {
73 DVLOG(2) << "URL is " << url << " " << __func__; 80 DVLOG(2) << "URL is " << url << " " << __func__;
74 81
75 if (!OfflinePageModel::CanSaveURL(url)) { 82 if (!OfflinePageModel::CanSaveURL(url)) {
76 DVLOG(1) << "Not able to save page for requested url: " << url; 83 DVLOG(1) << "Not able to save page for requested url: " << url;
77 return false; 84 return false;
78 } 85 }
79 86
80 // TODO(petewil): Use an int64_t random number for this, pass it to the 87 int64_t id = GenerateOfflineId();
81 // offliner, and eventually use it as the offline_id - that way the client
82 // uses offline_id everywhere.
83 static int64_t id = 0;
84 88
85 // Build a SavePageRequest. 89 // Build a SavePageRequest.
86 offline_pages::SavePageRequest request( 90 offline_pages::SavePageRequest request(id, url, client_id, base::Time::Now(),
87 id++, url, client_id, base::Time::Now(), user_requested); 91 user_requested);
88 92
89 // Put the request on the request queue. 93 // Put the request on the request queue.
90 queue_->AddRequest(request, 94 queue_->AddRequest(request,
91 base::Bind(&RequestCoordinator::AddRequestResultCallback, 95 base::Bind(&RequestCoordinator::AddRequestResultCallback,
92 weak_ptr_factory_.GetWeakPtr())); 96 weak_ptr_factory_.GetWeakPtr()));
93 NotifyAdded(request); 97 NotifyAdded(request);
94 return true; 98 return true;
95 } 99 }
96 void RequestCoordinator::GetAllRequests(const GetRequestsCallback& callback) { 100 void RequestCoordinator::GetAllRequests(const GetRequestsCallback& callback) {
97 // 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
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 OnRemoved(request, SavePageStatus::REMOVED_BY_CLIENT)); 414 OnRemoved(request, SavePageStatus::REMOVED_BY_CLIENT));
411 } 415 }
412 416
413 void RequestCoordinator::GetOffliner() { 417 void RequestCoordinator::GetOffliner() {
414 if (!offliner_) { 418 if (!offliner_) {
415 offliner_ = factory_->GetOffliner(policy_.get()); 419 offliner_ = factory_->GetOffliner(policy_.get());
416 } 420 }
417 } 421 }
418 422
419 } // namespace offline_pages 423 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698