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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/popup_menu_helper_ios.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_view_ios.h"
#include "content/browser/renderer_host/web_menu_runner_ios.h"
#include "content/common/content_export.h"
namespace content {
namespace {
bool g_allow_showing_popup_menus = true;
} // namespace
PopupMenuHelper::PopupMenuHelper(
Delegate* delegate,
RenderFrameHost* render_frame_host,
mojo::PendingRemote<blink::mojom::PopupMenuClient> popup_client)
: delegate_(delegate),
render_frame_host_(
static_cast<RenderFrameHostImpl*>(render_frame_host)->GetWeakPtr()),
popup_client_(std::move(popup_client)) {
RenderWidgetHost* widget_host =
render_frame_host->GetRenderViewHost()->GetWidget();
observation_.Observe(widget_host);
popup_client_.set_disconnect_handler(base::BindOnce(
&PopupMenuHelper::CloseMenu, weak_ptr_factory_.GetWeakPtr()));
}
PopupMenuHelper::~PopupMenuHelper() {
CloseMenu();
}
void PopupMenuHelper::ShowPopupMenu(
const gfx::Rect& bounds,
double item_font_size,
int selected_item,
std::vector<blink::mojom::MenuItemPtr> items,
bool right_aligned,
bool allow_multiple_selection) {
if (!g_allow_showing_popup_menus) {
return;
}
menu_runner_ =
[[WebMenuRunner alloc] initWithDelegate:weak_ptr_factory_.GetWeakPtr()
items:items
initialIndex:selected_item
fontSize:item_font_size
rightAligned:right_aligned];
[menu_runner_ showMenuInView:GetRenderWidgetHostView()->GetNativeView().Get()
withBounds:bounds.ToCGRect()];
}
void PopupMenuHelper::OnMenuItemSelected(int idx) {
popup_client_->DidAcceptIndices({idx});
delegate_->OnMenuClosed();
}
void PopupMenuHelper::OnMenuCanceled() {
popup_client_->DidCancel();
delegate_->OnMenuClosed();
}
void PopupMenuHelper::CloseMenu() {
menu_runner_ = nil;
popup_client_.reset();
}
RenderWidgetHostViewIOS* PopupMenuHelper::GetRenderWidgetHostView() const {
return static_cast<RenderWidgetHostViewIOS*>(
render_frame_host_->GetOutermostMainFrameOrEmbedder()->GetView());
}
void PopupMenuHelper::RenderWidgetHostVisibilityChanged(
RenderWidgetHost* widget_host,
bool became_visible) {
if (!became_visible) {
CloseMenu();
}
}
void PopupMenuHelper::RenderWidgetHostDestroyed(RenderWidgetHost* widget_host) {
CHECK(observation_.IsObservingSource(widget_host));
observation_.Reset();
}
// As declared in //content/public/browser/popup_menu.h.
CONTENT_EXPORT void DontShowPopupMenus() {
g_allow_showing_popup_menus = false;
}
} // namespace content
|