File: prefetched_pages_tracker_impl.cc

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (131 lines) | stat: -rw-r--r-- 4,334 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/ntp_snippets/remote/prefetched_pages_tracker_impl.h"

#include "base/bind.h"
#include "components/offline_pages/core/client_namespace_constants.h"

using offline_pages::OfflinePageItem;
using offline_pages::OfflinePageModel;

namespace ntp_snippets {

namespace {

bool IsOfflineItemPrefetchedPage(const OfflinePageItem& offline_page_item) {
  return offline_page_item.client_id.name_space ==
         offline_pages::kSuggestedArticlesNamespace;
}

const GURL& GetOfflinePageUrl(const OfflinePageItem& offline_page_item) {
  return offline_page_item.original_url != GURL()
             ? offline_page_item.original_url
             : offline_page_item.url;
}

}  // namespace

PrefetchedPagesTrackerImpl::PrefetchedPagesTrackerImpl(
    OfflinePageModel* offline_page_model)
    : initialized_(false),
      offline_page_model_(offline_page_model),
      weak_ptr_factory_(this) {
  DCHECK(offline_page_model_);
}

PrefetchedPagesTrackerImpl::~PrefetchedPagesTrackerImpl() {
  offline_page_model_->RemoveObserver(this);
}

bool PrefetchedPagesTrackerImpl::IsInitialized() const {
  return initialized_;
}

void PrefetchedPagesTrackerImpl::Initialize(
    base::OnceCallback<void()> callback) {
  if (IsInitialized()) {
    std::move(callback).Run();
  } else {
    initialization_completed_callbacks_.push_back(std::move(callback));
    // The call to get pages might be already in flight, started by previous
    // calls to this method. In this case, there is at least one callback
    // already waiting.
    if (initialization_completed_callbacks_.size() == 1) {
      offline_page_model_->GetPagesByNamespace(
          offline_pages::kSuggestedArticlesNamespace,
          base::BindOnce(&PrefetchedPagesTrackerImpl::OfflinePagesLoaded,
                         weak_ptr_factory_.GetWeakPtr()));
    }
  }
}

bool PrefetchedPagesTrackerImpl::PrefetchedOfflinePageExists(
    const GURL& url) const {
  DCHECK(initialized_);
  DCHECK(prefetched_url_counts_.count(url) == 0 ||
         prefetched_url_counts_.find(url)->second > 0);
  // It is enough to check existence of an item (instead of its count), because
  // the mapping does not contain zero counts.
  return prefetched_url_counts_.count(url) == 1;
}

void PrefetchedPagesTrackerImpl::OfflinePageModelLoaded(
    OfflinePageModel* model) {
  // Ignored. Offline Page model delayes our requests until it is loaded.
}

void PrefetchedPagesTrackerImpl::OfflinePageAdded(
    OfflinePageModel* model,
    const OfflinePageItem& added_page) {
  if (IsOfflineItemPrefetchedPage(added_page)) {
    AddOfflinePage(added_page);
  }
}

void PrefetchedPagesTrackerImpl::OfflinePageDeleted(
    const offline_pages::OfflinePageModel::DeletedPageInfo& page_info) {
  std::map<int64_t, GURL>::iterator offline_id_it =
      offline_id_to_url_mapping_.find(page_info.offline_id);

  if (offline_id_it == offline_id_to_url_mapping_.end()) {
    // We did not know about this page, thus, nothing to delete.
    return;
  }

  std::map<GURL, int>::iterator url_it =
      prefetched_url_counts_.find(offline_id_it->second);
  DCHECK(url_it != prefetched_url_counts_.end());
  --url_it->second;
  if (url_it->second == 0) {
    prefetched_url_counts_.erase(url_it);
  }
  offline_id_to_url_mapping_.erase(offline_id_it);
}

void PrefetchedPagesTrackerImpl::OfflinePagesLoaded(
    const std::vector<OfflinePageItem>& all_prefetched_offline_pages) {
  for (const OfflinePageItem& item : all_prefetched_offline_pages) {
    DCHECK(IsOfflineItemPrefetchedPage(item));
    AddOfflinePage(item);
  }

  initialized_ = true;
  offline_page_model_->AddObserver(this);
  for (auto& callback : initialization_completed_callbacks_) {
    std::move(callback).Run();
  }
  initialization_completed_callbacks_.clear();
}

void PrefetchedPagesTrackerImpl::AddOfflinePage(
    const OfflinePageItem& offline_page_item) {
  const GURL& url = GetOfflinePageUrl(offline_page_item);
  DCHECK(prefetched_url_counts_.count(url) == 0 ||
         prefetched_url_counts_.find(url)->second > 0);
  ++prefetched_url_counts_[url];
  offline_id_to_url_mapping_[offline_page_item.offline_id] = url;
}

}  // namespace ntp_snippets