File: offline_page_utils.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 (184 lines) | stat: -rw-r--r-- 7,733 bytes parent folder | download | duplicates (7)
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
183
184
// Copyright 2015 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_OFFLINE_PAGE_UTILS_H_
#define CHROME_BROWSER_OFFLINE_PAGES_OFFLINE_PAGE_UTILS_H_

#include <stdint.h>
#include <string>
#include <vector>

#include "base/files/file_util.h"
#include "base/functional/callback.h"
#include "components/offline_pages/core/offline_page_model.h"
#include "components/offline_pages/core/offline_page_types.h"
#include "url/gurl.h"

class SimpleFactoryKey;

namespace base {
class Time;
}

namespace content {
class BrowserContext;
class NavigationEntry;
class WebContents;
}

namespace offline_pages {
struct OfflinePageHeader;
struct OfflinePageItem;
struct PageCriteria;

class OfflinePageUtils {
 public:
  // The result of checking duplicate page or request.
  enum class DuplicateCheckResult {
    // Page with same URL is found.
    DUPLICATE_PAGE_FOUND,
    // Request with same URL is found.
    DUPLICATE_REQUEST_FOUND,
    // No page or request with same URL is found.
    NOT_FOUND
  };

  // Controls the UI action that could be triggered during download.
  // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.offlinepages
  // GENERATED_JAVA_CLASS_NAME_OVERRIDE: DownloadUiActionFlags
  enum class DownloadUIActionFlags {
    NONE = 0x0,
    // Shows an infobar to prompt the user for re-download when the duplicate
    // page or request is found.
    PROMPT_DUPLICATE = 0x1,
    // Shows a toast when the new download starts.
    SHOW_TOAST_ON_NEW_DOWNLOAD = 0x2,
    // All actions.
    ALL = 0xFFFF
  };

  static const base::FilePath::CharType kMHTMLExtension[];

  // Callback to inform the duplicate checking result.
  using DuplicateCheckCallback = base::OnceCallback<void(DuplicateCheckResult)>;

  // Returns via callback all offline pages related to |url|. The provided URL
  // is matched both against the original and the actual URL fields (they
  // sometimes differ because of possible redirects). If |tab_id| is provided
  // with a valid ID, offline pages bound to that tab will also be included in
  // the search. The returned list is sorted by descending creation date so that
  // the most recent offline page will be the first element of the list.
  static void SelectPagesForURL(
      SimpleFactoryKey* key,
      const GURL& url,
      int tab_id,
      base::OnceCallback<void(const std::vector<OfflinePageItem>&)> callback);

  static void SelectPagesWithCriteria(
      SimpleFactoryKey* key,
      const PageCriteria& criteria,
      base::OnceCallback<void(const std::vector<OfflinePageItem>&)> callback);

  // Gets the offline page corresponding to the given web contents.  The
  // returned pointer is owned by the web_contents and may be deleted by user
  // navigation, so it is unsafe to store a copy of the returned pointer.
  static const OfflinePageItem* GetOfflinePageFromWebContents(
      content::WebContents* web_contents);

  // Gets the offline header provided when loading the offline page for the
  // given web contents.
  static const OfflinePageHeader* GetOfflineHeaderFromWebContents(
      content::WebContents* web_contents);

  // Returns true if the offline page is shown for previewing purpose.
  static bool IsShowingOfflinePreview(content::WebContents* web_contents);

  // Returns true if download button is shown in the error page.
  static bool IsShowingDownloadButtonInErrorPage(
      content::WebContents* web_contents);

  // Gets an Android Tab ID from a tab containing |web_contents|. Returns false,
  // when tab is not available. Returns true otherwise and sets |tab_id| to the
  // ID of the tab.
  static bool GetTabId(content::WebContents* web_contents, int* tab_id);

  // Returns true if the |web_contents| is currently being presented inside a
  // custom tab.
  static bool CurrentlyShownInCustomTab(content::WebContents* web_contents);

  // Returns original URL of the given web contents. Empty URL is returned if
  // no redirect occurred.
  static GURL GetOriginalURLFromWebContents(content::WebContents* web_contents);

  // Checks for duplicates in all finished or ongoing downloads. Only pages and
  // requests that could result in showing in Download UI are searched for
  // |url| match. Result is returned in |callback|. See DuplicateCheckCallback
  // for more details.
  static void CheckDuplicateDownloads(content::BrowserContext* browser_context,
                                      const GURL& url,
                                      DuplicateCheckCallback callback);

  // Shows appropriate UI to indicate to the user that the |url| is either
  // already downloaded or is already scheduled to be downloaded soon (as
  // indicated by |exists_duplicate_request|). The user either decides to
  // continue with creating a duplicate - which is indicated by invoking the
  // |confirm_continuation|, or cancels the whole operation which does not
  // invoke continuation then.
  static void ShowDuplicatePrompt(base::OnceClosure confirm_continuation,
                                  const GURL& url,
                                  bool exists_duplicate_request,
                                  content::WebContents* web_contents);

  // Shows temporary UI indicating that download was accepted and has started.
  static void ShowDownloadingToast();

  // Schedules to download a page from |url| and categorize under |name_space|.
  // The duplicate pages or requests will be checked. Note that |url| can be
  // different from the one rendered in |web_contents|.
  static void ScheduleDownload(content::WebContents* web_contents,
                               const std::string& name_space,
                               const GURL& url,
                               DownloadUIActionFlags ui_action,
                               const std::string& request_origin);

  static void ScheduleDownload(content::WebContents* web_contents,
                               const std::string& name_space,
                               const GURL& url,
                               DownloadUIActionFlags ui_action);

  // Determines if offline page download should be triggered based on MIME type
  // of download resource.
  static bool CanDownloadAsOfflinePage(const GURL& url,
                                       const std::string& contents_mime_type);

  // Get total size of cache offline pages for a given time range. Returns false
  // when an OfflinePageModel cannot be acquired using the |browser_context|, or
  // the time range is invalid (|begin_time| > |end_time|). Also returning false
  // means no callback should be expected.
  static bool GetCachedOfflinePageSizeBetween(
      content::BrowserContext* browser_context,
      SizeInBytesCallback callback,
      const base::Time& begin_time,
      const base::Time& end_time);

  // Extracts and returns the value of the custom offline header from a
  // navigation entry. Empty string is returned if it is not found.
  // Note that the offline header is assumed to be the onlt extra header if it
  // exists.
  static std::string ExtractOfflineHeaderValueFromNavigationEntry(
      content::NavigationEntry* entry);

  // Returns true if |web_contents| is showing a trusted offline page.
  static bool IsShowingTrustedOfflinePage(content::WebContents* web_contents);

  // Tries to acquires the file access permission. |callback| will be called
  // to inform if the file access permission is granted.
  static void AcquireFileAccessPermission(
      content::WebContents* web_contents,
      base::OnceCallback<void(bool)> callback);
};

}  // namespace offline_pages

#endif  // CHROME_BROWSER_OFFLINE_PAGES_OFFLINE_PAGE_UTILS_H_