File: offline_page_auto_fetcher_service.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (182 lines) | stat: -rw-r--r-- 6,899 bytes parent folder | download | duplicates (6)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/offline_pages/android/offline_page_auto_fetcher_service.h"

#include <optional>
#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/offline_pages/android/auto_fetch_notifier.h"
#include "chrome/browser/offline_pages/request_coordinator_factory.h"
#include "chrome/common/offline_page_auto_fetcher.mojom.h"
#include "components/offline_pages/core/auto_fetch.h"
#include "components/offline_pages/core/background/request_coordinator.h"
#include "components/offline_pages/core/background/save_page_request.h"
#include "components/offline_pages/core/client_id.h"
#include "components/offline_pages/core/client_namespace_constants.h"
#include "components/offline_pages/core/offline_page_item_utils.h"
#include "components/offline_pages/core/offline_page_model.h"
#include "components/offline_pages/core/offline_store_utils.h"
#include "url/gurl.h"

namespace offline_pages {

namespace {
constexpr int kMaximumInFlight = 3;

class AutoFetchNotifierImpl : public AutoFetchNotifier {
 public:
  ~AutoFetchNotifierImpl() override = default;
  // Ensures that the in-progress notification is showing with the appropriate
  // request count.
  void NotifyInProgress(int in_flight_count) override {
    ShowAutoFetchInProgressNotification(in_flight_count);
  }
  // Update the request count if the in-progress notification is already
  // showing. This won't trigger showing the notification if it's not already
  // shown. If |in_flight_count| is 0, the notification will be hidden.
  void InProgressCountChanged(int in_flight_count) override {
    UpdateAutoFetchInProgressNotificationCountIfShowing(in_flight_count);
  }
};

}  // namespace

OfflinePageAutoFetcherService::OfflinePageAutoFetcherService(
    RequestCoordinator* request_coordinator,
    OfflinePageModel* offline_page_model,
    Delegate* delegate)
    : notifier_(std::make_unique<AutoFetchNotifierImpl>()),
      page_load_watcher_(
          notifier_.get(),
          request_coordinator,
          std::make_unique<AutoFetchPageLoadWatcher::AndroidTabFinder>()),
      request_coordinator_(request_coordinator),
      offline_page_model_(offline_page_model),
      delegate_(delegate) {
  request_coordinator_->AddObserver(this);
  if (AutoFetchInProgressNotificationCanceled()) {
    CancelAll(base::BindOnce(&AutoFetchCancellationComplete));
  }
}

OfflinePageAutoFetcherService::~OfflinePageAutoFetcherService() = default;

void OfflinePageAutoFetcherService::Shutdown() {
  request_coordinator_->RemoveObserver(this);
}

void OfflinePageAutoFetcherService::TrySchedule(bool user_requested,
                                                const GURL& url,
                                                int android_tab_id,
                                                TryScheduleCallback callback) {
  // Return an early failure if the URL is not suitable.
  if (!OfflinePageModel::CanSaveURL(url)) {
    std::move(callback).Run(OfflinePageAutoFetcherScheduleResult::kOtherError);
    return;
  }

  // Attempt to schedule a new request.
  RequestCoordinator::SavePageLaterParams params;
  params.url = url;
  auto_fetch::ClientIdMetadata metadata(android_tab_id);
  params.client_id = auto_fetch::MakeClientId(metadata);
  params.user_requested = false;
  params.availability =
      RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER;

  params.add_options.disallow_duplicate_requests = true;
  if (!user_requested) {
    params.add_options.maximum_in_flight_requests_for_namespace =
        kMaximumInFlight;
  }
  request_coordinator_->SavePageLater(
      params, base::BindOnce(&OfflinePageAutoFetcherService::TryScheduleDone,
                             GetWeakPtr(), std::move(callback)));
}

void OfflinePageAutoFetcherService::CancelAll(base::OnceClosure callback) {
  auto condition = base::BindRepeating([](const SavePageRequest& request) {
    return request.client_id().name_space == kAutoAsyncNamespace;
  });

  request_coordinator_->RemoveRequestsIf(
      condition, base::BindOnce(&OfflinePageAutoFetcherService::CancelAllDone,
                                GetWeakPtr(), std::move(callback)));
}

void OfflinePageAutoFetcherService::CancelSchedule(const GURL& url) {
  auto predicate = base::BindRepeating(
      [](const GURL& url, const SavePageRequest& request) {
        return request.client_id().name_space == kAutoAsyncNamespace &&
               EqualsIgnoringFragment(request.url(), url);
      },
      url);
  request_coordinator_->RemoveRequestsIf(predicate,
                                         /*done_callback=*/base::DoNothing());
}

void OfflinePageAutoFetcherService::CancelAllDone(
    base::OnceClosure callback,
    const MultipleItemStatuses& result) {
  std::move(callback).Run();
}

void OfflinePageAutoFetcherService::TryScheduleDone(
    TryScheduleCallback callback,
    AddRequestResult result) {
  // Translate the result and forward to the mojo caller.
  OfflinePageAutoFetcherScheduleResult callback_result;
  switch (result) {
    case AddRequestResult::REQUEST_QUOTA_HIT:
      callback_result = OfflinePageAutoFetcherScheduleResult::kNotEnoughQuota;
      break;
    case AddRequestResult::DUPLICATE_URL:
    case AddRequestResult::ALREADY_EXISTS:
      callback_result = OfflinePageAutoFetcherScheduleResult::kAlreadyScheduled;
      break;
    case AddRequestResult::SUCCESS:
      callback_result = OfflinePageAutoFetcherScheduleResult::kScheduled;
      break;
    case AddRequestResult::STORE_FAILURE:
    case AddRequestResult::URL_ERROR:
      callback_result = OfflinePageAutoFetcherScheduleResult::kOtherError;
      break;
  }
  std::move(callback).Run(callback_result);
}

void OfflinePageAutoFetcherService::OnCompleted(
    const SavePageRequest& request,
    RequestNotifier::BackgroundSavePageResult status) {
  if (request.client_id().name_space != kAutoAsyncNamespace ||
      status != RequestNotifier::BackgroundSavePageResult::SUCCESS)
    return;

  offline_page_model_->GetPageByOfflineId(
      request.request_id(),
      base::BindOnce(&OfflinePageAutoFetcherService::AutoFetchComplete,
                     weak_ptr_factory_.GetWeakPtr()));
}

void OfflinePageAutoFetcherService::AutoFetchComplete(
    const OfflinePageItem* page) {
  if (!page)
    return;
  std::optional<auto_fetch::ClientIdMetadata> metadata =
      auto_fetch::ExtractMetadata(page->client_id);
  if (!metadata)
    return;

  delegate_->ShowAutoFetchCompleteNotification(
      page->title, page->GetOriginalUrl().spec(), page->url.spec(),
      metadata.value().android_tab_id, page->offline_id);
}

}  // namespace offline_pages