[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / browser_plugin / browser_plugin_embedder.cc
blob4c6fecd362a7f31e303c9c22a7d9db955919fff1
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/browser_plugin/browser_plugin_embedder.h"
7 #include "content/browser/browser_plugin/browser_plugin_guest.h"
8 #include "content/browser/renderer_host/render_view_host_impl.h"
9 #include "content/browser/web_contents/web_contents_impl.h"
10 #include "content/common/browser_plugin/browser_plugin_messages.h"
11 #include "content/common/drag_messages.h"
12 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/browser_plugin_guest_manager.h"
14 #include "content/public/browser/native_web_keyboard_event.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "third_party/WebKit/public/web/WebFindOptions.h"
17 #include "ui/events/keycodes/keyboard_codes.h"
19 namespace content {
21 BrowserPluginEmbedder::BrowserPluginEmbedder(WebContentsImpl* web_contents)
22 : WebContentsObserver(web_contents),
23 guest_drag_ending_(false),
24 weak_ptr_factory_(this) {
27 BrowserPluginEmbedder::~BrowserPluginEmbedder() {
30 // static
31 BrowserPluginEmbedder* BrowserPluginEmbedder::Create(
32 WebContentsImpl* web_contents) {
33 return new BrowserPluginEmbedder(web_contents);
36 bool BrowserPluginEmbedder::DragEnteredGuest(BrowserPluginGuest* guest) {
37 guest_dragging_over_ = guest->AsWeakPtr();
38 return guest_started_drag_.get() == guest;
41 void BrowserPluginEmbedder::DragLeftGuest(BrowserPluginGuest* guest) {
42 // Avoid race conditions in switching between guests being hovered over by
43 // only un-setting if the caller is marked as the guest being dragged over.
44 if (guest_dragging_over_.get() == guest) {
45 guest_dragging_over_.reset();
49 // static
50 bool BrowserPluginEmbedder::NotifyScreenInfoChanged(
51 WebContents* guest_web_contents) {
52 if (guest_web_contents->GetRenderViewHost()) {
53 auto render_widget_host =
54 RenderWidgetHostImpl::From(guest_web_contents->GetRenderViewHost());
55 render_widget_host->NotifyScreenInfoChanged();
58 // Returns false to iterate over all guests.
59 return false;
62 void BrowserPluginEmbedder::ScreenInfoChanged() {
63 GetBrowserPluginGuestManager()->ForEachGuest(web_contents(), base::Bind(
64 &BrowserPluginEmbedder::NotifyScreenInfoChanged));
67 // static
68 bool BrowserPluginEmbedder::CancelDialogs(WebContents* guest_web_contents) {
69 static_cast<WebContentsImpl*>(guest_web_contents)
70 ->CancelActiveAndPendingDialogs();
72 // Returns false to iterate over all guests.
73 return false;
76 void BrowserPluginEmbedder::CancelGuestDialogs() {
77 GetBrowserPluginGuestManager()->ForEachGuest(
78 web_contents(), base::Bind(&BrowserPluginEmbedder::CancelDialogs));
81 void BrowserPluginEmbedder::StartDrag(BrowserPluginGuest* guest) {
82 guest_started_drag_ = guest->AsWeakPtr();
83 guest_drag_ending_ = false;
86 BrowserPluginGuestManager*
87 BrowserPluginEmbedder::GetBrowserPluginGuestManager() const {
88 return web_contents()->GetBrowserContext()->GetGuestManager();
91 void BrowserPluginEmbedder::ClearGuestDragStateIfApplicable() {
92 // The order at which we observe SystemDragEnded() and DragSourceEndedAt() is
93 // platform dependent.
94 // In OSX, we see SystemDragEnded() first, where in aura, we see
95 // DragSourceEndedAt() first. For this reason, we check if both methods were
96 // called before resetting |guest_started_drag_|.
97 if (guest_drag_ending_) {
98 if (guest_started_drag_)
99 guest_started_drag_.reset();
100 } else {
101 guest_drag_ending_ = true;
105 // static
106 bool BrowserPluginEmbedder::DidSendScreenRectsCallback(
107 WebContents* guest_web_contents) {
108 static_cast<RenderViewHostImpl*>(
109 guest_web_contents->GetRenderViewHost())->SendScreenRects();
110 // Not handled => Iterate over all guests.
111 return false;
114 void BrowserPluginEmbedder::DidSendScreenRects() {
115 GetBrowserPluginGuestManager()->ForEachGuest(
116 web_contents(),
117 base::Bind(&BrowserPluginEmbedder::DidSendScreenRectsCallback));
120 bool BrowserPluginEmbedder::OnMessageReceived(const IPC::Message& message) {
121 return OnMessageReceived(message, nullptr);
124 bool BrowserPluginEmbedder::OnMessageReceived(
125 const IPC::Message& message,
126 RenderFrameHost* render_frame_host) {
127 bool handled = true;
128 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(BrowserPluginEmbedder, message,
129 render_frame_host)
130 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_Attach, OnAttach)
131 IPC_MESSAGE_HANDLER_GENERIC(DragHostMsg_UpdateDragCursor,
132 OnUpdateDragCursor(&handled));
133 IPC_MESSAGE_UNHANDLED(handled = false)
134 IPC_END_MESSAGE_MAP()
135 return handled;
138 void BrowserPluginEmbedder::DragSourceEndedAt(int client_x, int client_y,
139 int screen_x, int screen_y, blink::WebDragOperation operation) {
140 if (guest_started_drag_) {
141 gfx::Point guest_offset =
142 guest_started_drag_->GetScreenCoordinates(gfx::Point());
143 guest_started_drag_->DragSourceEndedAt(client_x - guest_offset.x(),
144 client_y - guest_offset.y(), screen_x, screen_y, operation);
146 ClearGuestDragStateIfApplicable();
149 void BrowserPluginEmbedder::SystemDragEnded() {
150 // When the embedder's drag/drop operation ends, we need to pass the message
151 // to the guest that initiated the drag/drop operation. This will ensure that
152 // the guest's RVH state is reset properly.
153 if (guest_started_drag_)
154 guest_started_drag_->EmbedderSystemDragEnded();
156 guest_dragging_over_.reset();
157 ClearGuestDragStateIfApplicable();
160 void BrowserPluginEmbedder::OnUpdateDragCursor(bool* handled) {
161 *handled = !!guest_dragging_over_;
164 void BrowserPluginEmbedder::OnAttach(
165 RenderFrameHost* render_frame_host,
166 int browser_plugin_instance_id,
167 const BrowserPluginHostMsg_Attach_Params& params) {
168 WebContents* guest_web_contents =
169 GetBrowserPluginGuestManager()->GetGuestByInstanceID(
170 render_frame_host->GetProcess()->GetID(),
171 browser_plugin_instance_id);
172 if (!guest_web_contents)
173 return;
174 BrowserPluginGuest* guest = static_cast<WebContentsImpl*>(guest_web_contents)
175 ->GetBrowserPluginGuest();
176 guest->Attach(browser_plugin_instance_id,
177 static_cast<WebContentsImpl*>(web_contents()),
178 params);
181 bool BrowserPluginEmbedder::HandleKeyboardEvent(
182 const NativeWebKeyboardEvent& event) {
183 if ((event.windowsKeyCode != ui::VKEY_ESCAPE) ||
184 (event.modifiers & blink::WebInputEvent::InputModifiers)) {
185 return false;
188 bool event_consumed = false;
189 GetBrowserPluginGuestManager()->ForEachGuest(
190 web_contents(),
191 base::Bind(&BrowserPluginEmbedder::UnlockMouseIfNecessaryCallback,
192 &event_consumed));
194 return event_consumed;
197 bool BrowserPluginEmbedder::Find(int request_id,
198 const base::string16& search_text,
199 const blink::WebFindOptions& options) {
200 return GetBrowserPluginGuestManager()->ForEachGuest(
201 web_contents(),
202 base::Bind(&BrowserPluginEmbedder::FindInGuest,
203 request_id,
204 search_text,
205 options));
208 bool BrowserPluginEmbedder::StopFinding(StopFindAction action) {
209 return GetBrowserPluginGuestManager()->ForEachGuest(
210 web_contents(),
211 base::Bind(&BrowserPluginEmbedder::StopFindingInGuest, action));
214 BrowserPluginGuest* BrowserPluginEmbedder::GetFullPageGuest() {
215 WebContentsImpl* guest_contents = static_cast<WebContentsImpl*>(
216 GetBrowserPluginGuestManager()->GetFullPageGuest(web_contents()));
217 if (!guest_contents)
218 return nullptr;
219 return guest_contents->GetBrowserPluginGuest();
222 // static
223 bool BrowserPluginEmbedder::UnlockMouseIfNecessaryCallback(bool* mouse_unlocked,
224 WebContents* guest) {
225 *mouse_unlocked |= static_cast<WebContentsImpl*>(guest)
226 ->GetBrowserPluginGuest()
227 ->mouse_locked();
228 guest->GotResponseToLockMouseRequest(false);
230 // Returns false to iterate over all guests.
231 return false;
234 // static
235 bool BrowserPluginEmbedder::FindInGuest(int request_id,
236 const base::string16& search_text,
237 const blink::WebFindOptions& options,
238 WebContents* guest) {
239 if (static_cast<WebContentsImpl*>(guest)->GetBrowserPluginGuest()->Find(
240 request_id, search_text, options)) {
241 // There can only ever currently be one browser plugin that handles find so
242 // we can break the iteration at this point.
243 return true;
245 return false;
248 // static
249 bool BrowserPluginEmbedder::StopFindingInGuest(StopFindAction action,
250 WebContents* guest) {
251 if (static_cast<WebContentsImpl*>(guest)->GetBrowserPluginGuest()
252 ->StopFinding(action)) {
253 // There can only ever currently be one browser plugin that handles find so
254 // we can break the iteration at this point.
255 return true;
257 return false;
260 } // namespace content