summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/html/forms/external_popup_menu.cc
blob: 91ecc914be412287db584ddec97a9e9a3dc6272d (plain)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright (C) 2010 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "third_party/blink/renderer/core/html/forms/external_popup_menu.h"

#include "build/build_config.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/public/platform/web_coalesced_input_event.h"
#include "third_party/blink/public/platform/web_mouse_event.h"
#include "third_party/blink/public/platform/web_vector.h"
#include "third_party/blink/public/web/web_external_popup_menu.h"
#include "third_party/blink/public/web/web_local_frame_client.h"
#include "third_party/blink/public/web/web_menu_item_info.h"
#include "third_party/blink/public/web/web_popup_menu_info.h"
#include "third_party/blink/public/web/web_view.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h"
#include "third_party/blink/renderer/core/events/current_input_event.h"
#include "third_party/blink/renderer/core/exported/web_view_impl.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
#include "third_party/blink/renderer/core/html/forms/html_option_element.h"
#include "third_party/blink/renderer/core/html/forms/html_select_element.h"
#include "third_party/blink/renderer/core/layout/layout_box.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/geometry/float_quad.h"
#include "third_party/blink/renderer/platform/geometry/int_point.h"
#include "third_party/blink/renderer/platform/text/text_direction.h"
#if defined(OS_MACOSX)
#include "third_party/blink/renderer/core/page/chrome_client.h"
#endif

namespace blink {

ExternalPopupMenu::ExternalPopupMenu(LocalFrame& frame,
                                     HTMLSelectElement& owner_element,
                                     WebView& web_view)
    : owner_element_(owner_element),
      local_frame_(frame),
      web_view_(web_view),
      dispatch_event_timer_(frame.GetTaskRunner(TaskType::kInternalDefault),
                            this,
                            &ExternalPopupMenu::DispatchEvent),
      web_external_popup_menu_(nullptr) {}

ExternalPopupMenu::~ExternalPopupMenu() = default;

void ExternalPopupMenu::Trace(Visitor* visitor) {
  visitor->Trace(owner_element_);
  visitor->Trace(local_frame_);
  PopupMenu::Trace(visitor);
}

bool ExternalPopupMenu::ShowInternal() {
  // Blink core reuses the PopupMenu of an element.  For simplicity, we do
  // recreate the actual external popup everytime.
  if (web_external_popup_menu_) {
    web_external_popup_menu_->Close();
    web_external_popup_menu_ = nullptr;
  }

  WebPopupMenuInfo info;
  GetPopupMenuInfo(info, *owner_element_);
  if (info.items.empty())
    return false;
  WebLocalFrameImpl* webframe =
      WebLocalFrameImpl::FromFrame(local_frame_.Get());
  web_external_popup_menu_ =
      webframe->Client()->CreateExternalPopupMenu(info, this);
  if (web_external_popup_menu_) {
    LayoutObject* layout_object = owner_element_->GetLayoutObject();
    if (!layout_object || !layout_object->IsBox())
      return false;
    IntRect rect = EnclosingIntRect(
        ToLayoutBox(layout_object)
            ->LocalToAbsoluteRect(
                ToLayoutBox(layout_object)->PhysicalBorderBoxRect()));
    IntRect rect_in_viewport = local_frame_->View()->FrameToViewport(rect);
    web_external_popup_menu_->Show(rect_in_viewport);
    return true;
  }

  // The client might refuse to create a popup (when there is already one
  // pending to be shown for example).
  DidCancel();
  return false;
}

void ExternalPopupMenu::Show() {
  if (!ShowInternal())
    return;
#if defined(OS_MACOSX)
  const WebInputEvent* current_event = CurrentInputEvent::Get();
  if (current_event && current_event->GetType() == WebInputEvent::kMouseDown) {
    synthetic_event_ = std::make_unique<WebMouseEvent>();
    *synthetic_event_ = *static_cast<const WebMouseEvent*>(current_event);
    synthetic_event_->SetType(WebInputEvent::kMouseUp);
    dispatch_event_timer_.StartOneShot(TimeDelta(), FROM_HERE);
    // FIXME: show() is asynchronous. If preparing a popup is slow and a
    // user released the mouse button before showing the popup, mouseup and
    // click events are correctly dispatched. Dispatching the synthetic
    // mouseup event is redundant in this case.
  }
#endif
}

void ExternalPopupMenu::DispatchEvent(TimerBase*) {
  web_view_.MainFrameWidget()->HandleInputEvent(
      blink::WebCoalescedInputEvent(*synthetic_event_));
  synthetic_event_.reset();
}

void ExternalPopupMenu::Hide() {
  if (owner_element_)
    owner_element_->PopupDidHide();
  if (!web_external_popup_menu_)
    return;
  web_external_popup_menu_->Close();
  web_external_popup_menu_ = nullptr;
}

void ExternalPopupMenu::UpdateFromElement(UpdateReason reason) {
  switch (reason) {
    case kBySelectionChange:
    case kByDOMChange:
      if (needs_update_)
        return;
      needs_update_ = true;
      owner_element_->GetDocument()
          .GetTaskRunner(TaskType::kUserInteraction)
          ->PostTask(FROM_HERE, WTF::Bind(&ExternalPopupMenu::Update,
                                          WrapPersistent(this)));
      break;

    case kByStyleChange:
      // TODO(tkent): We should update the popup location/content in some
      // cases.  e.g. Updating ComputedStyle of the SELECT element affects
      // popup position and OPTION style.
      break;
  }
}

void ExternalPopupMenu::Update() {
  if (!web_external_popup_menu_ || !owner_element_)
    return;
  owner_element_->GetDocument().UpdateStyleAndLayoutTree();
  // disconnectClient() might have been called.
  if (!owner_element_)
    return;
  needs_update_ = false;

  if (ShowInternal())
    return;
  // We failed to show a popup.  Notify it to the owner.
  Hide();
}

void ExternalPopupMenu::DisconnectClient() {
  Hide();
  owner_element_ = nullptr;
}

void ExternalPopupMenu::DidChangeSelection(int index) {}

void ExternalPopupMenu::DidAcceptIndex(int index) {
  // Calling methods on the HTMLSelectElement might lead to this object being
  // derefed. This ensures it does not get deleted while we are running this
  // method.
  int popup_menu_item_index = ToPopupMenuItemIndex(index, *owner_element_);

  if (owner_element_) {
    owner_element_->PopupDidHide();
    owner_element_->SelectOptionByPopup(popup_menu_item_index);
  }
  web_external_popup_menu_ = nullptr;
}

// Android uses this function even for single SELECT.
void ExternalPopupMenu::DidAcceptIndices(const WebVector<int>& indices) {
  if (!owner_element_) {
    web_external_popup_menu_ = nullptr;
    return;
  }

  HTMLSelectElement* owner_element = owner_element_;
  owner_element->PopupDidHide();

  if (indices.empty()) {
    owner_element->SelectOptionByPopup(-1);
  } else if (!owner_element->IsMultiple()) {
    owner_element->SelectOptionByPopup(
        ToPopupMenuItemIndex(indices[indices.size() - 1], *owner_element));
  } else {
    Vector<int> list_indices;
    wtf_size_t list_count = SafeCast<wtf_size_t>(indices.size());
    list_indices.ReserveCapacity(list_count);
    for (wtf_size_t i = 0; i < list_count; ++i)
      list_indices.push_back(ToPopupMenuItemIndex(indices[i], *owner_element));
    owner_element->SelectMultipleOptionsByPopup(list_indices);
  }

  web_external_popup_menu_ = nullptr;
}

void ExternalPopupMenu::DidCancel() {
  if (owner_element_)
    owner_element_->PopupDidHide();
  web_external_popup_menu_ = nullptr;
}

void ExternalPopupMenu::GetPopupMenuInfo(WebPopupMenuInfo& info,
                                         HTMLSelectElement& owner_element) {
  const HeapVector<Member<HTMLElement>>& list_items =
      owner_element.GetListItems();
  wtf_size_t item_count = list_items.size();
  wtf_size_t count = 0;
  Vector<WebMenuItemInfo> items(item_count);
  for (wtf_size_t i = 0; i < item_count; ++i) {
    if (owner_element.ItemIsDisplayNone(*list_items[i]))
      continue;

    Element& item_element = *list_items[i];
    WebMenuItemInfo& popup_item = items[count++];
    popup_item.label = owner_element.ItemText(item_element);
    popup_item.tool_tip = item_element.title();
    popup_item.checked = false;
    if (IsHTMLHRElement(item_element)) {
      popup_item.type = WebMenuItemInfo::kSeparator;
    } else if (IsHTMLOptGroupElement(item_element)) {
      popup_item.type = WebMenuItemInfo::kGroup;
    } else {
      popup_item.type = WebMenuItemInfo::kOption;
      popup_item.checked = ToHTMLOptionElement(item_element).Selected();
    }
    popup_item.enabled = !item_element.IsDisabledFormControl();
    const ComputedStyle& style = *owner_element.ItemComputedStyle(item_element);
    popup_item.text_direction = ToWebTextDirection(style.Direction());
    popup_item.has_text_direction_override = IsOverride(style.GetUnicodeBidi());
  }

  const ComputedStyle& menu_style = owner_element.GetComputedStyle()
                                        ? *owner_element.GetComputedStyle()
                                        : *owner_element.EnsureComputedStyle();
  const SimpleFontData* font_data = menu_style.GetFont().PrimaryFont();
  DCHECK(font_data);
  info.item_height = font_data ? font_data->GetFontMetrics().Height() : 0;
  info.item_font_size = static_cast<int>(
      menu_style.GetFont().GetFontDescription().ComputedSize());
  info.selected_index = ToExternalPopupMenuItemIndex(
      owner_element.SelectedListIndex(), owner_element);
  info.right_aligned = menu_style.Direction() == TextDirection::kRtl;
  info.allow_multiple_selection = owner_element.IsMultiple();
  if (count < item_count)
    items.Shrink(count);
  info.items = items;
}

int ExternalPopupMenu::ToPopupMenuItemIndex(int external_popup_menu_item_index,
                                            HTMLSelectElement& owner_element) {
  if (external_popup_menu_item_index < 0)
    return external_popup_menu_item_index;

  int index_tracker = 0;
  const HeapVector<Member<HTMLElement>>& items = owner_element.GetListItems();
  for (wtf_size_t i = 0; i < items.size(); ++i) {
    if (owner_element.ItemIsDisplayNone(*items[i]))
      continue;
    if (index_tracker++ == external_popup_menu_item_index)
      return i;
  }
  return -1;
}

int ExternalPopupMenu::ToExternalPopupMenuItemIndex(
    int popup_menu_item_index,
    HTMLSelectElement& owner_element) {
  if (popup_menu_item_index < 0)
    return popup_menu_item_index;

  int index_tracker = 0;
  const HeapVector<Member<HTMLElement>>& items = owner_element.GetListItems();
  for (wtf_size_t i = 0; i < items.size(); ++i) {
    if (owner_element.ItemIsDisplayNone(*items[i]))
      continue;
    if (popup_menu_item_index == static_cast<int>(i))
      return index_tracker;
    ++index_tracker;
  }
  return -1;
}

}  // namespace blink