1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_PAGE_CHROME_CLIENT_IMPL_H_
33 #define THIRD_PARTY_BLINK_RENDERER_CORE_PAGE_CHROME_CLIENT_IMPL_H_
34 
35 #include <memory>
36 
37 #include "cc/input/overscroll_behavior.h"
38 #include "third_party/blink/public/mojom/input/focus_type.mojom-blink-forward.h"
39 #include "third_party/blink/public/web/web_navigation_policy.h"
40 #include "third_party/blink/public/web/web_widget_client.h"
41 #include "third_party/blink/public/web/web_window_features.h"
42 #include "third_party/blink/renderer/core/core_export.h"
43 #include "third_party/blink/renderer/core/page/chrome_client.h"
44 #include "third_party/blink/renderer/platform/graphics/touch_action.h"
45 #include "ui/base/cursor/cursor.h"
46 
47 namespace ui {
48 class Cursor;
49 }
50 
51 namespace blink {
52 
53 class PagePopup;
54 class PagePopupClient;
55 class WebAutofillClient;
56 class WebViewImpl;
57 struct WebRect;
58 
59 // Handles window-level notifications from core on behalf of a WebView.
60 class CORE_EXPORT ChromeClientImpl final : public ChromeClient {
61  public:
62   explicit ChromeClientImpl(WebViewImpl*);
63   ~ChromeClientImpl() override;
64   void Trace(Visitor* visitor) override;
65 
66   // ChromeClient methods:
67   WebViewImpl* GetWebView() const override;
68   void ChromeDestroyed() override;
69   void SetWindowRect(const IntRect&, LocalFrame&) override;
70   IntRect RootWindowRect(LocalFrame&) override;
71   void Focus(LocalFrame*) override;
72   bool CanTakeFocus(mojom::blink::FocusType) override;
73   void TakeFocus(mojom::blink::FocusType) override;
74   void SetKeyboardFocusURL(Element* new_focus_element) override;
75   void BeginLifecycleUpdates(LocalFrame& main_frame) override;
76   void StartDeferringCommits(LocalFrame& main_frame,
77                              base::TimeDelta timeout) override;
78   void StopDeferringCommits(LocalFrame& main_frame,
79                             cc::PaintHoldingCommitTrigger) override;
80   void StartDragging(LocalFrame*,
81                      const WebDragData&,
82                      WebDragOperationsMask,
83                      const SkBitmap& drag_image,
84                      const gfx::Point& drag_image_offset) override;
85   bool AcceptsLoadDrops() const override;
86   Page* CreateWindowDelegate(LocalFrame*,
87                              const FrameLoadRequest&,
88                              const AtomicString& name,
89                              const WebWindowFeatures&,
90                              mojom::blink::WebSandboxFlags,
91                              const FeaturePolicy::FeatureState&,
92                              const SessionStorageNamespaceId&) override;
93   void Show(NavigationPolicy) override;
94   void DidOverscroll(const gfx::Vector2dF& overscroll_delta,
95                      const gfx::Vector2dF& accumulated_overscroll,
96                      const gfx::PointF& position_in_viewport,
97                      const gfx::Vector2dF& velocity_in_viewport) override;
98   void SetOverscrollBehavior(LocalFrame& main_frame,
99                              const cc::OverscrollBehavior&) override;
100   void InjectGestureScrollEvent(LocalFrame& local_frame,
101                                 WebGestureDevice device,
102                                 const gfx::Vector2dF& delta,
103                                 ScrollGranularity granularity,
104                                 CompositorElementId scrollable_area_element_id,
105                                 WebInputEvent::Type injected_type) override;
106   bool ShouldReportDetailedMessageForSource(LocalFrame&,
107                                             const String&) override;
108   void AddMessageToConsole(LocalFrame*,
109                            mojom::ConsoleMessageSource,
110                            mojom::ConsoleMessageLevel,
111                            const String& message,
112                            unsigned line_number,
113                            const String& source_id,
114                            const String& stack_trace) override;
115   bool CanOpenBeforeUnloadConfirmPanel() override;
116   bool OpenBeforeUnloadConfirmPanelDelegate(LocalFrame*,
117                                             bool is_reload) override;
118   void CloseWindowSoon() override;
119   bool OpenJavaScriptAlertDelegate(LocalFrame*, const String&) override;
120   bool OpenJavaScriptConfirmDelegate(LocalFrame*, const String&) override;
121   bool OpenJavaScriptPromptDelegate(LocalFrame*,
122                                     const String& message,
123                                     const String& default_value,
124                                     String& result) override;
125   bool TabsToLinks() override;
126   void InvalidateRect(const IntRect&) override;
127   void ScheduleAnimation(const LocalFrameView*,
128                          base::TimeDelta = base::TimeDelta()) override;
129   IntRect ViewportToScreen(const IntRect&,
130                            const LocalFrameView*) const override;
131   float WindowToViewportScalar(LocalFrame*, const float) const override;
132   void WindowToViewportRect(LocalFrame& frame,
133                             WebFloatRect* viewport_rect) const override;
134   WebScreenInfo GetScreenInfo(LocalFrame&) const override;
135   void OverrideVisibleRectForMainFrame(LocalFrame& frame,
136                                        IntRect* paint_rect) const override;
137   float InputEventsScaleForEmulation() const override;
138   void ContentsSizeChanged(LocalFrame*, const IntSize&) const override;
139   bool DoubleTapToZoomEnabled() const override;
140   void EnablePreferredSizeChangedMode() override;
141   void ZoomToFindInPageRect(const WebRect& rect_in_root_frame) override;
142   void PageScaleFactorChanged() const override;
143   float ClampPageScaleFactorToLimits(float scale) const override;
144   void MainFrameScrollOffsetChanged(LocalFrame& main_frame) const override;
145   void ResizeAfterLayout() const override;
146   void MainFrameLayoutUpdated() const override;
147   void ShowMouseOverURL(const HitTestResult&) override;
148   void SetToolTip(LocalFrame&, const String&, TextDirection) override;
149   void DispatchViewportPropertiesDidChange(
150       const ViewportDescription&) const override;
151   void PrintDelegate(LocalFrame*) override;
152   ColorChooser* OpenColorChooser(LocalFrame*,
153                                  ColorChooserClient*,
154                                  const Color&) override;
155   DateTimeChooser* OpenDateTimeChooser(
156       LocalFrame* frame,
157       DateTimeChooserClient*,
158       const DateTimeChooserParameters&) override;
159   ExternalDateTimeChooser* GetExternalDateTimeChooserForTesting() override;
160   void OpenFileChooser(LocalFrame*, scoped_refptr<FileChooser>) override;
161   void SetCursor(const ui::Cursor&, LocalFrame*) override;
162   void SetCursorOverridden(bool) override;
163   ui::Cursor LastSetCursorForTesting() const override;
164   void SetEventListenerProperties(LocalFrame*,
165                                   cc::EventListenerClass,
166                                   cc::EventListenerProperties) override;
167   cc::EventListenerProperties EventListenerProperties(
168       LocalFrame*,
169       cc::EventListenerClass) const override;
170   // Informs client about the existence of handlers for scroll events so
171   // appropriate scroll optimizations can be chosen.
172   void SetHasScrollEventHandlers(LocalFrame*, bool has_event_handlers) override;
173   void SetNeedsLowLatencyInput(LocalFrame*, bool needs_low_latency) override;
174   void SetNeedsUnbufferedInputForDebugger(LocalFrame*, bool immediate) override;
175   void RequestUnbufferedInputEvents(LocalFrame*) override;
176   void SetTouchAction(LocalFrame*, TouchAction) override;
177 
178   void AttachRootLayer(scoped_refptr<cc::Layer>,
179                        LocalFrame* local_root) override;
180 
181   void AttachCompositorAnimationTimeline(CompositorAnimationTimeline*,
182                                          LocalFrame*) override;
183   void DetachCompositorAnimationTimeline(CompositorAnimationTimeline*,
184                                          LocalFrame*) override;
185 
186   void EnterFullscreen(LocalFrame&,
187                        const FullscreenOptions*,
188                        bool for_cross_process_descendant) override;
189   void ExitFullscreen(LocalFrame&) override;
190   void FullscreenElementChanged(Element* old_element,
191                                 Element* new_element) override;
192 
193   void AnimateDoubleTapZoom(const gfx::Point& point,
194                             const gfx::Rect& rect) override;
195 
196   void ClearLayerSelection(LocalFrame*) override;
197   void UpdateLayerSelection(LocalFrame*, const cc::LayerSelection&) override;
198 
199   // ChromeClient methods:
200   String AcceptLanguages() override;
201   void SetCursorForPlugin(const ui::Cursor&, LocalFrame*) override;
202 
203   // ChromeClientImpl:
204   void SetNewWindowNavigationPolicy(WebNavigationPolicy);
205 
206   // FileChooser calls this function to kick pending file chooser
207   // requests.
208   void DidCompleteFileChooser(FileChooser& file_chooser);
209 
210   void AutoscrollStart(const gfx::PointF& viewport_point, LocalFrame*) override;
211   void AutoscrollFling(const gfx::Vector2dF& velocity, LocalFrame*) override;
212   void AutoscrollEnd(LocalFrame*) override;
213 
214   bool HasOpenedPopup() const override;
215   PopupMenu* OpenPopupMenu(LocalFrame&, HTMLSelectElement&) override;
216   PagePopup* OpenPagePopup(PagePopupClient*) override;
217   void ClosePagePopup(PagePopup*) override;
218   DOMWindow* PagePopupWindowForTesting() const override;
219 
220   void SetBrowserControlsState(float top_height,
221                                float bottom_height,
222                                bool shrinks_layout) override;
223   void SetBrowserControlsShownRatio(float top_ratio,
224                                     float bottom_ratio) override;
225 
226   bool ShouldOpenUIElementDuringPageDismissal(
227       LocalFrame&,
228       UIElementType,
229       const String& dialog_message,
230       Document::PageDismissalType) const override;
231 
232   bool RequestPointerLock(LocalFrame*,
233                           WebWidgetClient::PointerLockCallback,
234                           bool) override;
235   bool RequestPointerLockChange(LocalFrame*,
236                                 WebWidgetClient::PointerLockCallback,
237                                 bool) override;
238   void RequestPointerUnlock(LocalFrame*) override;
239 
240   // AutofillClient pass throughs:
241   void DidAssociateFormControlsAfterLoad(LocalFrame*) override;
242   void HandleKeyboardEventOnTextField(HTMLInputElement&,
243                                       KeyboardEvent&) override;
244   void DidChangeValueInTextField(HTMLFormControlElement&) override;
245   void DidEndEditingOnTextField(HTMLInputElement&) override;
246   void OpenTextDataListChooser(HTMLInputElement&) override;
247   void TextFieldDataListChanged(HTMLInputElement&) override;
248   void DidChangeSelectionInSelectControl(HTMLFormControlElement&) override;
249   void SelectFieldOptionsChanged(HTMLFormControlElement&) override;
250   void AjaxSucceeded(LocalFrame*) override;
251 
252   void ShowVirtualKeyboardOnElementFocus(LocalFrame&) override;
253 
254   TransformationMatrix GetDeviceEmulationTransform() const override;
255 
256   void OnMouseDown(Node&) override;
257   void DidUpdateBrowserControls() const override;
258 
259   FloatSize ElasticOverscroll() const override;
260 
261   void RegisterPopupOpeningObserver(PopupOpeningObserver*) override;
262   void UnregisterPopupOpeningObserver(PopupOpeningObserver*) override;
263   void NotifyPopupOpeningObservers() const override;
264 
265   viz::FrameSinkId GetFrameSinkId(LocalFrame*) override;
266 
267   void RequestDecode(LocalFrame*,
268                      const cc::PaintImage&,
269                      base::OnceCallback<void(bool)>) override;
270 
271   void NotifySwapTime(LocalFrame& frame, ReportTimeCallback callback) override;
272 
273   void FallbackCursorModeLockCursor(LocalFrame* frame,
274                                     bool left,
275                                     bool right,
276                                     bool up,
277                                     bool down) override;
278 
279   void FallbackCursorModeSetCursorVisibility(LocalFrame* frame,
280                                              bool visible) override;
281 
282   void RequestBeginMainFrameNotExpected(LocalFrame& frame,
283                                         bool request) override;
284 
285   int GetLayerTreeId(LocalFrame& frame) override;
286 
287   void DidUpdateTextAutosizerPageInfo(const WebTextAutosizerPageInfo&) override;
288 
289   void DocumentDetached(Document&) override;
290 
291  private:
IsChromeClientImpl()292   bool IsChromeClientImpl() const override { return true; }
293 
294   void SetCursorInternal(const ui::Cursor&, LocalFrame*);
295 
296   // Returns WebAutofillClient associated with the WebLocalFrame. This takes and
297   // returns nullable.
298   WebAutofillClient* AutofillClientFromFrame(LocalFrame*);
299 
300   WebViewImpl* web_view_;  // Weak pointer.
301   HeapHashSet<WeakMember<PopupOpeningObserver>> popup_opening_observers_;
302   Vector<scoped_refptr<FileChooser>> file_chooser_queue_;
303   ui::Cursor last_set_mouse_cursor_for_testing_;
304   bool cursor_overridden_;
305   Member<ExternalDateTimeChooser> external_date_time_chooser_;
306   bool did_request_non_empty_tool_tip_;
307 
308   FRIEND_TEST_ALL_PREFIXES(FileChooserQueueTest, DerefQueuedChooser);
309 };
310 
311 template <>
312 struct DowncastTraits<ChromeClientImpl> {
313   static bool AllowFrom(const ChromeClient& client) {
314     return client.IsChromeClientImpl();
315   }
316 };
317 
318 }  // namespace blink
319 
320 #endif
321