File: paint_preview_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 (217 lines) | stat: -rw-r--r-- 7,674 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// 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_PAINT_PREVIEW_SERVICES_PAINT_PREVIEW_TAB_SERVICE_H_
#define CHROME_BROWSER_PAINT_PREVIEW_SERVICES_PAINT_PREVIEW_TAB_SERVICE_H_

#include <memory>
#include <optional>
#include <vector>

#include "base/containers/flat_set.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 "base/sequence_checker.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 "components/paint_preview/common/proto/paint_preview.pb.h"
#include "content/public/browser/global_routing_id.h"

#if BUILDFLAG(IS_ANDROID)
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#endif  // BUILDFLAG(IS_ANDROID)

namespace content {
class WebContents;
}  // namespace content

namespace paint_preview {

// A service for capturing and using Paint Previews per Tab. Captures are stored
// using Tab IDs as the key such that the data can be accessed even if the
// browser is restarted.
class PaintPreviewTabService : public PaintPreviewBaseService {
 public:
  PaintPreviewTabService(std::unique_ptr<PaintPreviewFileMixin> file_mixin,
                         std::unique_ptr<PaintPreviewPolicy> policy,
                         bool is_off_the_record);
  ~PaintPreviewTabService() override;

  enum Status {
    kOk = 0,
    kDirectoryCreationFailed = 1,
    kCaptureFailed = 2,
    kProtoSerializationFailed = 3,
    kWebContentsGone = 4,
    kCaptureInProgress = 5,
    kInvalid = 6,
  };

  using FinishedCallback = base::OnceCallback<void(Status)>;
  using BooleanCallback = base::OnceCallback<void(bool)>;

  bool CacheInitialized() const { return cache_ready_; }

  // Captures a Paint Preview of |contents| which should be associated with
  // |tab_id| for storage. |callback| is invoked on completion to indicate
  // status.
  void CaptureTab(int tab_id,
                  content::WebContents* contents,
                  bool accessibility_enabled,
                  float page_scale_factor,
                  int scroll_offset_x,
                  int scroll_offset_y,
                  FinishedCallback callback);

  // Destroys the Paint Preview associated with |tab_id|. This MUST be called
  // when a tab is closed to ensure the captured contents don't outlive the tab.
  void TabClosed(int tab_id);

  // Checks if there is a capture taken for |tab_id|.
  bool HasCaptureForTab(int tab_id);

  // This should be called on startup with a list of restored tab ids
  // (|active_tab_ids|). This performs an audit over all Paint Previews stored
  // by this service and destroys any that don't correspond to active tabs. This
  // is required as TabClosed could have been interrupted or an accounting error
  // occurred.
  void AuditArtifacts(const std::vector<int>& active_tab_ids);

#if BUILDFLAG(IS_ANDROID)
  // JNI wrapped versions of the above methods
  void CaptureTabAndroid(
      JNIEnv* env,
      jint j_tab_id,
      const base::android::JavaParamRef<jobject>& j_web_contents,
      jboolean j_accessibility_enabled,
      jfloat j_page_scale_factor,
      jint j_x,
      jint j_y,
      const base::android::JavaParamRef<jobject>& j_callback);
  void TabClosedAndroid(JNIEnv* env, jint j_tab_id);
  jboolean HasCaptureForTabAndroid(JNIEnv* env, jint j_tab_id);
  void AuditArtifactsAndroid(
      JNIEnv* env,
      const base::android::JavaParamRef<jintArray>& j_tab_ids);
  jboolean IsCacheInitializedAndroid(JNIEnv* env);
  std::string GetPathAndroid(JNIEnv* env);

  base::android::ScopedJavaGlobalRef<jobject> GetJavaRef() { return java_ref_; }
#endif  // BUILDFLAG(IS_ANDROID)

 private:
  class TabServiceTask {
   public:
    using FinishedCallback = base::OnceCallback<void(Status)>;

    TabServiceTask(int tab_id,
                   const DirectoryKey& key,
                   content::FrameTreeNodeId frame_tree_node_id,
                   content::GlobalRenderFrameHostId frame_routing_id,
                   float page_scale_factor,
                   int x,
                   int y,
                   base::ScopedClosureRunner capture_handle);
    ~TabServiceTask();

    TabServiceTask(const TabServiceTask& other) = delete;
    TabServiceTask& operator=(const TabServiceTask& other) = delete;

    int tab_id() const { return tab_id_; }
    const DirectoryKey& key() const { return key_; }
    content::FrameTreeNodeId frame_tree_node_id() const {
      return frame_tree_node_id_;
    }
    content::GlobalRenderFrameHostId frame_routing_id() const {
      return frame_routing_id_;
    }
    float page_scale_factor() const { return page_scale_factor_; }
    int scroll_offset_x() const { return scroll_offset_x_; }
    int scroll_offset_y() const { return scroll_offset_y_; }

    void SetWaitForAccessibility() { wait_for_accessibility_ = true; }

    void SetCallback(FinishedCallback callback) {
      finished_callback_ = std::move(callback);
    }

    void OnAXTreeWritten(bool success) {
      wait_for_accessibility_ = false;
      if (status_ != kInvalid && finished_callback_) {
        std::move(finished_callback_).Run(status_);
      }
    }

    void OnCaptured(Status status) {
      status_ = status;
      if (!wait_for_accessibility_ && finished_callback_) {
        std::move(finished_callback_).Run(status_);
      }
    }

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

    void ReleaseCaptureHandle() { capture_handle_.RunAndReset(); }

   private:
    int tab_id_;
    DirectoryKey key_;
    content::FrameTreeNodeId frame_tree_node_id_;
    content::GlobalRenderFrameHostId frame_routing_id_;
    float page_scale_factor_;
    int scroll_offset_x_;
    int scroll_offset_y_;

    bool wait_for_accessibility_{false};
    Status status_{kInvalid};

    base::ScopedClosureRunner capture_handle_;

    FinishedCallback finished_callback_;
    base::WeakPtrFactory<TabServiceTask> weak_ptr_factory_{this};
  };

  void DeleteTask(int tab_id);

  // Caches current captures in |captured_tab_ids_|. Called as part of
  // initialization.
  void InitializeCache(const base::flat_set<DirectoryKey>& in_use_keys);

  // The FTN ID is to look-up the content::WebContents.
  void CaptureTabInternal(base::WeakPtr<TabServiceTask> task,
                          bool accessibility_enabled,
                          const std::optional<base::FilePath>& file_path);

  void OnAXTreeWritten(base::WeakPtr<TabServiceTask> task, bool result);

  void OnCaptured(base::WeakPtr<TabServiceTask> task,
                  PaintPreviewBaseService::CaptureStatus status,
                  std::unique_ptr<CaptureResult> result);

  void OnFinished(base::WeakPtr<TabServiceTask> task, bool success);

  void CleanupOldestFiles(int tab_id, const std::vector<DirectoryKey>& keys);

  void RunAudit(const std::vector<int>& active_tab_ids,
                const base::flat_set<DirectoryKey>& in_use_keys);

  bool cache_ready_;
  base::flat_set<int> captured_tab_ids_;
  base::flat_map<int, std::unique_ptr<TabServiceTask>> tasks_;
#if BUILDFLAG(IS_ANDROID)
  base::android::ScopedJavaGlobalRef<jobject> java_ref_;
#endif  // BUILDFLAG(IS_ANDROID)
  SEQUENCE_CHECKER(sequence_checker_);
  base::WeakPtrFactory<PaintPreviewTabService> weak_ptr_factory_{this};
};

}  // namespace paint_preview

#endif  // CHROME_BROWSER_PAINT_PREVIEW_SERVICES_PAINT_PREVIEW_TAB_SERVICE_H_