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_
|