File: offline_page_auto_fetcher_service.h

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 (125 lines) | stat: -rw-r--r-- 5,232 bytes parent folder | download | duplicates (5)
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
// 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.

#ifndef CHROME_BROWSER_OFFLINE_PAGES_ANDROID_OFFLINE_PAGE_AUTO_FETCHER_SERVICE_H_
#define CHROME_BROWSER_OFFLINE_PAGES_ANDROID_OFFLINE_PAGE_AUTO_FETCHER_SERVICE_H_

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/offline_pages/android/auto_fetch_page_load_watcher.h"
#include "chrome/common/offline_page_auto_fetcher.mojom-forward.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/offline_pages/core/background/request_coordinator.h"
#include "components/offline_pages/core/background/request_queue_results.h"
#include "components/offline_pages/core/offline_page_model.h"
#include "url/gurl.h"

namespace offline_pages {
class RequestCoordinator;
class SavePageRequest;

// Implementation notes for the auto-fetch-on-net-error-page feature:
//
// The 'auto-fetch-on-net-error-page' (auto-fetch) feature kicks in when a
// 'dino' (offline error) page is reached. Chrome will schedule a request to
// save the page when the device gains connectivity. Users can cancel or
// explicitly request this behavior through UI on the dino page. Chrome attempts
// to avoid doing a background page save if the user ends up successfully
// navigating to the page. If the page is saved in the background, the a system
// notification is presented.
//
// Background page saves are implemented through |RequestCoordinator|. The
// |OfflinePageClientPolicy| for this feature is configured with the option
// |defer_background_fetch_while_page_is_active|. This instructs
// |RequestCoordinator| to first check if the page to be saved is currently
// active. If it is, the request is deferred. If a request is deferred 5 times,
// it is considered failed and removed. For this feature, we expect this
// condition to be rare because |RequestCoordinator| only processes requests
// when the device is connected, and the dino page automatically reloads when
// the device is connected.
//
// Additionally, save page requests are removed upon successful navigation
// commit. See |AutoFetchPageLoadWatcher|.

// A KeyedService for the auto-fetch feature.
// * Provides an interface to schedule and cancel auto-fetch requests.
// * Listens for complete fetches with RequestCoordinator::Observer, and
//   triggers the system notification.
class OfflinePageAutoFetcherService : public KeyedService,
                                      public RequestCoordinator::Observer {
 public:
  using OfflinePageAutoFetcherScheduleResult =
      chrome::mojom::OfflinePageAutoFetcherScheduleResult;
  using TryScheduleCallback = base::OnceCallback<void(
      chrome::mojom::OfflinePageAutoFetcherScheduleResult)>;

  // Injected interface for testing.
  class Delegate {
   public:
    // Calls |offline_pages::ShowAutoFetchCompleteNotification()|.
    virtual void ShowAutoFetchCompleteNotification(
        const std::u16string& pageTitle,
        const std::string& original_url,
        const std::string& final_url,
        int android_tab_id,
        int64_t offline_id) = 0;
  };

  explicit OfflinePageAutoFetcherService(
      RequestCoordinator* request_coordinator,
      OfflinePageModel* offline_page_model,
      Delegate* delegate);
  ~OfflinePageAutoFetcherService() override;

  AutoFetchPageLoadWatcher* page_load_watcher() { return &page_load_watcher_; }

  // Auto fetching interface. Schedules and cancels fetch requests.
  void TrySchedule(bool user_requested,
                   const GURL& url,
                   int android_tab_id,
                   TryScheduleCallback callback);
  void CancelSchedule(const GURL& url);
  void CancelAll(base::OnceClosure callback);

  // KeyedService implementation.
  void Shutdown() override;

  // Testing methods.
  bool IsTaskQueueEmptyForTesting();

  // RequestCoordinator::Observer implementation.
  void OnAdded(const SavePageRequest& request) override {}
  void OnCompleted(const SavePageRequest& request,
                   RequestNotifier::BackgroundSavePageResult status) override;
  void OnChanged(const SavePageRequest& request) override {}
  void OnNetworkProgress(const SavePageRequest& request,
                         int64_t received_bytes) override {}

 private:
  base::WeakPtr<OfflinePageAutoFetcherService> GetWeakPtr() {
    return weak_ptr_factory_.GetWeakPtr();
  }

  void TryScheduleDone(TryScheduleCallback callback, AddRequestResult result);
  void AutoFetchComplete(const OfflinePageItem* page);
  void CancelAllDone(base::OnceClosure callback,
                     const MultipleItemStatuses& result);

  std::unique_ptr<AutoFetchNotifier> notifier_;
  AutoFetchPageLoadWatcher page_load_watcher_;
  raw_ptr<RequestCoordinator> request_coordinator_;
  raw_ptr<OfflinePageModel> offline_page_model_;
  raw_ptr<Delegate> delegate_;
  base::WeakPtrFactory<OfflinePageAutoFetcherService> weak_ptr_factory_{this};
};

}  // namespace offline_pages

#endif  // CHROME_BROWSER_OFFLINE_PAGES_ANDROID_OFFLINE_PAGE_AUTO_FETCHER_SERVICE_H_