File: long_screenshots_tab_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 (133 lines) | stat: -rw-r--r-- 4,990 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
// Copyright 2020 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_LONG_SCREENSHOTS_LONG_SCREENSHOTS_TAB_SERVICE_H_
#define CHROME_BROWSER_LONG_SCREENSHOTS_LONG_SCREENSHOTS_TAB_SERVICE_H_

#include <memory>
#include <vector>

#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "build/build_config.h"
#include "components/paint_preview/browser/paint_preview_base_service.h"
#include "components/paint_preview/browser/paint_preview_policy.h"
#include "content/public/browser/frame_tree_node_id.h"
#include "third_party/re2/src/re2/re2.h"

namespace content {
class WebContents;
}  // namespace content

namespace long_screenshots {

// A service for capturing Long Screenshots using PaintPreview. Writes the
// retrieved bitmap to file.
// TODO(tgupta): Handle the deletion of old files when the long screenshots
// feature ends or when Chrome starts up (to handle when Chrome is killed in the
// background and there was no opportunity to clean the files).
class LongScreenshotsTabService
    : public paint_preview::PaintPreviewBaseService {
 public:
  LongScreenshotsTabService(
      std::unique_ptr<paint_preview::PaintPreviewFileMixin> file_mixin,
      std::unique_ptr<paint_preview::PaintPreviewPolicy> policy,
      bool is_off_the_record);
  ~LongScreenshotsTabService() override;

  // Define a list of statuses to describe the calling of paint preview and
  // generation of the bitmap.
  //
  // A Java counterpart will be generated for this enum.
  // GENERATED_JAVA_ENUM_PACKAGE: (
  // org.chromium.chrome.browser.share.long_screenshots.bitmap_generation)
  enum Status {
    kUnknown = 0,
    kOk = 1,
    kDirectoryCreationFailed = 2,
    kCaptureFailed = 3,
    kProtoSerializationFailed = 4,
    kWebContentsGone = 5,
    kNativeServiceUninitialized = 6,
    kLowMemoryDetected = 7,
    kProtoDeserializationFailed = 8,
    kNativeServiceNotInitialized = 9,
  };

  // Captures a Paint Preview of |contents| which should be associated with
  // |tab_id| for storage. |callback| is invoked on completion to indicate
  // status.
  // Clip args specify the bounds of the capture:
  // clip_x: Where to start the capture on the X axis.
  // clip_y: Where to start the capture on the Y axis.
  // clip_width: How wide of a capture relative to clip_x.
  // clip_height: How wide of a capture relative to clip_y.
  // in_memory: Use in memory capture mode.
  void CaptureTab(int tab_id,
                  const GURL& url,
                  content::WebContents* contents,
                  int clip_x,
                  int clip_y,
                  int clip_width,
                  int clip_height,
                  bool in_memory);

  // Delete all old long screenshot files.
  void DeleteAllLongScreenshotFiles();

  // JNI wrapped versions of the above methods
  void CaptureTabAndroid(
      JNIEnv* env,
      jint j_tab_id,
      const base::android::JavaParamRef<jobject>& j_gurl,
      const base::android::JavaParamRef<jobject>& j_web_contents,
      jint clip_x,
      jint clip_y,
      jint clip_width,
      jint clip_height,
      jboolean in_memory);
  void LongScreenshotsClosedAndroid(JNIEnv* env);

  base::android::ScopedJavaGlobalRef<jobject> GetJavaRef() { return java_ref_; }

 private:
  friend class LongScreenshotsTabServiceTest;

  // Retrieves the content::WebContents from the |frame_tree_node_id|
  // (confirming that the contents are alive using the |frame_routing_id|).
  // Calls PaintPreviewBaseService to retrieve the bitmap and write it to file.
  void CaptureTabInternal(int tab_id,
                          content::FrameTreeNodeId frame_tree_node_id,
                          content::GlobalRenderFrameHostId frame_routing_id,
                          int clip_x,
                          int clip_y,
                          int clip_width,
                          int clip_height,
                          bool in_memory,
                          const std::optional<base::FilePath>& file_path);

  void OnCaptured(paint_preview::PaintPreviewBaseService::CaptureStatus status,
                  std::unique_ptr<paint_preview::CaptureResult> result);

  content::RenderFrameHost* GetRootRenderFrameHost(
      content::RenderFrameHost* main_frame,
      const GURL& url);
  bool IsAmpUrl(const GURL& url);

  const re2::RE2 google_amp_cache_path_regex_;
  const re2::RE2 google_amp_viewer_path_regex_;
  const re2::RE2 google_news_path_regex_;

  base::ScopedClosureRunner capture_handle_;
  base::android::ScopedJavaGlobalRef<jobject> java_ref_;
  base::WeakPtrFactory<LongScreenshotsTabService> weak_ptr_factory_{this};
};

}  // namespace long_screenshots

#endif  // CHROME_BROWSER_LONG_SCREENSHOTS_LONG_SCREENSHOTS_TAB_SERVICE_H_