blob: 2a439f638b030e9942b1d94e2aae75c84cb7bd24 [file] [log] [blame]
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_WEB_JS_FEATURES_CLIPBOARD_CLIPBOARD_JAVA_SCRIPT_FEATURE_H_
#define IOS_WEB_JS_FEATURES_CLIPBOARD_CLIPBOARD_JAVA_SCRIPT_FEATURE_H_
#import <optional>
#import "base/memory/weak_ptr.h"
#import "base/no_destructor.h"
#import "ios/web/public/js_messaging/java_script_feature.h"
namespace web {
class WebFrame;
// A feature to intercept clipboard API calls.
class ClipboardJavaScriptFeature : public JavaScriptFeature {
public:
// This feature holds no state, so only a single static instance is ever
// needed.
static ClipboardJavaScriptFeature* GetInstance();
ClipboardJavaScriptFeature(const ClipboardJavaScriptFeature&) = delete;
ClipboardJavaScriptFeature& operator=(const ClipboardJavaScriptFeature&) =
delete;
private:
friend class base::NoDestructor<ClipboardJavaScriptFeature>;
friend class ClipboardJavaScriptFeatureTest;
ClipboardJavaScriptFeature();
~ClipboardJavaScriptFeature() override;
// JavaScriptFeature overrides.
std::optional<std::string> GetScriptMessageHandlerName() const override;
// Handles messages sent from the injected JavaScript.
// This message is sent whenever a clipboard API is called.
// Expected arguments in `message` body:
// - command: (string) "read" or "write".
// - requestId: (double) The unique ID for this clipboard request.
// - frameId: (string) The ID of the frame sending the request.
void ScriptMessageReceived(WebState* web_state,
const ScriptMessage& message) override;
// Calls the JavaScript function `__gCrWeb.clipboard.resolveRequest` to settle
// the promise for the given `request_id`.
// `web_frame` is the frame where the script should be executed.
// `allowed` indicates whether the clipboard operation was permitted.
void ResolveClipboardRequest(int request_id,
base::WeakPtr<WebFrame> web_frame,
bool allowed);
};
} // namespace web
#endif // IOS_WEB_JS_FEATURES_CLIPBOARD_CLIPBOARD_JAVA_SCRIPT_FEATURE_H_