#ifndef IOS_WEB_JS_MESSAGING_WEB_FRAME_IMPL_H_
#define IOS_WEB_JS_MESSAGING_WEB_FRAME_IMPL_H_
#import <map>
#import <string>
#import "base/cancelable_callback.h"
#import "base/memory/raw_ptr.h"
#import "base/memory/weak_ptr.h"
#import "base/values.h"
#import "ios/web/js_messaging/web_frame_internal.h"
#import "ios/web/public/js_messaging/content_world.h"
#import "ios/web/public/js_messaging/web_frame.h"
#import "ios/web/public/web_state.h"
#import "ios/web/public/web_state_observer.h"
#import "url/gurl.h"
#import "url/origin.h"
@class WKFrameInfo;
namespace web {
class JavaScriptContentWorld;
class WebFrameImpl final : public WebFrame,
public WebFrameInternal,
public web::WebStateObserver {
public:
WebFrameImpl(WKFrameInfo* frame_info,
const std::string& frame_id,
bool is_main_frame,
url::Origin security_origin,
web::WebState* web_state,
ContentWorld content_world);
WebFrameImpl(const WebFrameImpl&) = delete;
WebFrameImpl& operator=(const WebFrameImpl&) = delete;
~WebFrameImpl() override;
WebState* GetWebState();
WebFrameInternal* GetWebFrameInternal() override;
std::string GetFrameId() const override;
bool IsMainFrame() const override;
url::Origin GetSecurityOrigin() const override;
GURL GetUrl() const override;
BrowserState* GetBrowserState() override;
bool CallJavaScriptFunction(const std::string& name,
const base::Value::List& parameters) override;
bool CallJavaScriptFunction(
const std::string& name,
const base::Value::List& parameters,
base::OnceCallback<void(const base::Value*)> callback,
base::TimeDelta timeout) override;
bool ExecuteJavaScript(const std::u16string& script) override;
bool ExecuteJavaScript(
const std::u16string& script,
base::OnceCallback<void(const base::Value*)> callback) override;
bool ExecuteJavaScript(const std::u16string& script,
ExecuteJavaScriptCallbackWithError callback) override;
base::WeakPtr<WebFrame> AsWeakPtr() override;
bool CallJavaScriptFunctionInContentWorld(
const std::string& name,
const base::Value::List& parameters,
JavaScriptContentWorld* content_world) override;
bool CallJavaScriptFunctionInContentWorld(
const std::string& name,
const base::Value::List& parameters,
JavaScriptContentWorld* content_world,
base::OnceCallback<void(const base::Value*)> callback,
base::TimeDelta timeout) override;
bool ExecuteJavaScriptInContentWorld(
const std::u16string& script,
JavaScriptContentWorld* content_world,
ExecuteJavaScriptCallbackWithError callback) override;
void WebStateDestroyed(web::WebState* web_state) override;
private:
bool CallJavaScriptFunctionInContentWorld(
const std::string& name,
const base::Value::List& parameters,
JavaScriptContentWorld* content_world,
bool reply_with_result);
void DetachFromWebState();
typedef base::CancelableOnceCallback<void(void)> TimeoutCallback;
struct RequestCallbacks {
RequestCallbacks(base::OnceCallback<void(const base::Value*)> completion,
std::unique_ptr<TimeoutCallback>);
~RequestCallbacks();
base::OnceCallback<void(const base::Value*)> completion;
std::unique_ptr<TimeoutCallback> timeout_callback;
};
bool ExecuteJavaScriptFunction(JavaScriptContentWorld* content_world,
const std::string& name,
const base::Value::List& parameters,
int message_id,
bool reply_with_result);
ExecuteJavaScriptCallbackWithError ExecuteJavaScriptCallbackAdapter(
base::OnceCallback<void(const base::Value*)> callback);
void LogScriptWarning(NSString* script, NSError* error);
void CompleteRequest(int message_id, const base::Value* result);
void CompleteRequest(std::unique_ptr<RequestCallbacks> request_callbacks,
const base::Value* result);
void CancelRequest(int message_id);
void CancelPendingRequests();
std::map<uint32_t, std::unique_ptr<struct RequestCallbacks>>
pending_requests_;
WKFrameInfo* frame_info_;
std::string frame_id_;
int next_message_id_ = 0;
bool is_main_frame_ = false;
url::Origin security_origin_;
raw_ptr<web::WebState> web_state_ = nullptr;
ContentWorld content_world_;
base::WeakPtrFactory<WebFrameImpl> weak_ptr_factory_{this};
};
}
#endif