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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/renderer_host/chrome_render_widget_host_view_mac_delegate.h"
#include <cmath>
#include "base/auto_reset.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/devtools/devtools_window.h"
#include "chrome/browser/profiles/profile.h"
#import "chrome/browser/renderer_host/chrome_render_widget_host_view_mac_history_swiper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/inactive_window_mouse_event_controller.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/webui/top_chrome/webui_url_utils.h"
#include "chrome/common/url_constants.h"
#include "components/prefs/pref_service.h"
#include "components/spellcheck/browser/pref_names.h"
#include "components/spellcheck/browser/spellcheck_platform.h"
#include "components/spellcheck/common/spellcheck_panel.mojom.h"
#include "components/tabs/public/tab_interface.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "content/public/browser/preloading.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#if BUILDFLAG(ENABLE_GLIC)
#include "chrome/browser/glic/glic_enabling.h"
#include "chrome/browser/glic/glic_keyed_service.h"
#endif
@interface ChromeRenderWidgetHostViewMacDelegate () <HistorySwiperDelegate>
@property(readonly) content::WebContents* webContents;
@property(readonly) NSView* nsView;
@property(readonly) PrefService* prefService;
@end
@implementation ChromeRenderWidgetHostViewMacDelegate {
// The widget host (process + routing IDs) that this delegate is managing.
int32_t _widgetProcessId;
int32_t _widgetRoutingId;
// Responsible for 2-finger swipes history navigation.
HistorySwiper* __strong _historySwiper;
// A boolean set to true while resigning first responder status, to avoid
// infinite recursion in the case of reentrance.
BOOL _resigningFirstResponder;
}
- (instancetype)initWithRenderWidgetHost:
(content::RenderWidgetHost*)renderWidgetHost {
self = [super init];
if (self) {
_widgetProcessId = renderWidgetHost->GetProcess()->GetDeprecatedID();
_widgetRoutingId = renderWidgetHost->GetRoutingID();
_historySwiper = [[HistorySwiper alloc] initWithDelegate:self];
}
return self;
}
- (void)dealloc {
[_historySwiper setDelegate:nil];
}
- (content::WebContents*)webContents {
content::RenderWidgetHost* renderWidgetHost =
content::RenderWidgetHost::FromID(_widgetProcessId, _widgetRoutingId);
if (!renderWidgetHost) {
return nullptr;
}
content::RenderViewHost* renderViewHost =
content::RenderViewHost::From(renderWidgetHost);
if (!renderViewHost) {
return nullptr;
}
return content::WebContents::FromRenderViewHost(renderViewHost);
}
- (NSView*)nsView {
content::RenderWidgetHost* renderWidgetHost =
content::RenderWidgetHost::FromID(_widgetProcessId, _widgetRoutingId);
if (!renderWidgetHost) {
return nil;
}
content::RenderWidgetHostView* renderWidgetHostView =
renderWidgetHost->GetView();
if (!renderWidgetHostView) {
return nil;
}
return renderWidgetHostView->GetNativeView().GetNativeNSView();
}
- (PrefService*)prefService {
content::RenderWidgetHost* renderWidgetHost =
content::RenderWidgetHost::FromID(_widgetProcessId, _widgetRoutingId);
if (!renderWidgetHost) {
return nullptr;
}
return Profile::FromBrowserContext(
renderWidgetHost->GetProcess()->GetBrowserContext())
->GetPrefs();
}
// Handle an event. All incoming key and mouse events flow through this
// delegate method if implemented. Return YES if the event is fully handled, or
// NO if normal processing should take place.
- (BOOL)handleEvent:(NSEvent*)event {
return [_historySwiper handleEvent:event];
}
// NSWindow events.
// This is a low level API which provides touches associated with an event.
// It is used in conjunction with gestures to determine finger placement
// on the trackpad.
- (void)touchesMovedWithEvent:(NSEvent*)event {
[_historySwiper touchesMovedWithEvent:event];
}
- (void)touchesBeganWithEvent:(NSEvent*)event {
[_historySwiper touchesBeganWithEvent:event];
}
- (void)touchesCancelledWithEvent:(NSEvent*)event {
[_historySwiper touchesCancelledWithEvent:event];
}
- (void)touchesEndedWithEvent:(NSEvent*)event {
[_historySwiper touchesEndedWithEvent:event];
}
// HistorySwiperDelegate methods
- (BOOL)shouldAllowHistorySwiping {
content::WebContents* webContents = self.webContents;
if (!webContents) {
return NO;
}
return !DevToolsWindow::IsDevToolsWindow(webContents);
}
- (NSView*)viewThatWantsHistoryOverlay {
return self.nsView;
}
- (BOOL)canNavigateInDirection:(history_swiper::NavigationDirection)direction
onWindow:(NSWindow*)window {
content::WebContents* webContents = self.webContents;
if (!webContents) {
return NO;
}
if (direction == history_swiper::kForwards) {
return chrome::CanGoForward(webContents);
} else {
return chrome::CanGoBack(webContents);
}
}
- (void)navigateInDirection:(history_swiper::NavigationDirection)direction
onWindow:(NSWindow*)window {
content::WebContents* webContents = self.webContents;
if (!webContents) {
return;
}
if (direction == history_swiper::kForwards) {
chrome::GoForward(webContents);
} else {
chrome::GoBack(webContents);
}
}
- (void)backwardsSwipeNavigationLikely {
content::WebContents* webContents = self.webContents;
if (!webContents) {
return;
}
webContents->BackNavigationLikely(
content::preloading_predictor::kBackGestureNavigation,
WindowOpenDisposition::CURRENT_TAB);
}
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item
isValidItem:(BOOL*)valid {
PrefService* pref = self.prefService;
if (!pref) {
return NO;
}
const PrefService::Preference* spellCheckEnablePreference =
pref->FindPreference(spellcheck::prefs::kSpellCheckEnable);
DCHECK(spellCheckEnablePreference);
const bool spellCheckUserModifiable =
spellCheckEnablePreference->IsUserModifiable();
SEL action = item.action;
// For now, this action is always enabled for render view;
// this is sub-optimal.
// TODO(suzhe): Plumb the "can*" methods up from WebCore.
if (action == @selector(checkSpelling:)) {
*valid = spellCheckUserModifiable;
return YES;
}
// TODO(groby): Clarify who sends this and if toggleContinuousSpellChecking:
// is still necessary.
if (action == @selector(toggleContinuousSpellChecking:)) {
if ([(id)item respondsToSelector:@selector(setState:)]) {
NSControlStateValue checkedState =
pref->GetBoolean(spellcheck::prefs::kSpellCheckEnable)
? NSControlStateValueOn
: NSControlStateValueOff;
[(id)item setState:checkedState];
}
*valid = spellCheckUserModifiable;
return YES;
}
if (action == @selector(showGuessPanel:) ||
action == @selector(toggleGrammarChecking:)) {
*valid = spellCheckUserModifiable;
return YES;
}
return NO;
}
- (void)rendererHandledGestureScrollEvent:(const blink::WebGestureEvent&)event
consumed:(BOOL)consumed {
[_historySwiper rendererHandledGestureScrollEvent:event consumed:consumed];
}
- (void)rendererHandledOverscrollEvent:(const ui::DidOverscrollParams&)params {
[_historySwiper onOverscrolled:params];
}
// Spellchecking methods
// The next five methods are implemented here since this class is the first
// responder for anything in the browser.
// This message is sent whenever the user specifies that a word should be
// changed from the spellChecker.
- (void)changeSpelling:(id)sender {
content::WebContents* webContents = self.webContents;
if (!webContents) {
return;
}
// Grab the currently selected word from the spell panel, as this is the word
// that we want to replace the selected word in the text with.
NSString* newWord = [[sender selectedCell] stringValue];
if (newWord != nil) {
webContents->ReplaceMisspelling(base::SysNSStringToUTF16(newWord));
}
}
// This message is sent by NSSpellChecker whenever the next word should be
// advanced to, either after a correction or clicking the "Find Next" button.
// This isn't documented anywhere useful, like in NSSpellProtocol.h with the
// other spelling panel methods. This is probably because Apple assumes that the
// the spelling panel will be used with an NSText, which will automatically
// catch this and advance to the next word for you. Thanks Apple.
// This is also called from the Edit -> Spelling -> Check Spelling menu item.
- (void)checkSpelling:(id)sender {
content::WebContents* webContents = self.webContents;
if (!webContents) {
return;
}
if (content::RenderFrameHost* frame = webContents->GetFocusedFrame()) {
mojo::Remote<spellcheck::mojom::SpellCheckPanel>
focused_spell_check_panel_client;
frame->GetRemoteInterfaces()->GetInterface(
focused_spell_check_panel_client.BindNewPipeAndPassReceiver());
focused_spell_check_panel_client->AdvanceToNextMisspelling();
}
}
// This message is sent by the spelling panel whenever a word is ignored.
- (void)ignoreSpelling:(id)sender {
// Ideally, we would ask the current RenderView for its tag, but that would
// mean making a blocking IPC call from the browser. Instead,
// spellcheck_platform::CheckSpelling remembers the last tag and
// spellcheck_platform::IgnoreWord assumes that is the correct tag.
NSString* wordToIgnore = [sender stringValue];
if (wordToIgnore != nil)
spellcheck_platform::IgnoreWord(nullptr,
base::SysNSStringToUTF16(wordToIgnore));
}
- (void)showGuessPanel:(id)sender {
content::WebContents* webContents = self.webContents;
if (!webContents) {
return;
}
const bool visible = spellcheck_platform::SpellingPanelVisible();
if (content::RenderFrameHost* frame = webContents->GetFocusedFrame()) {
mojo::Remote<spellcheck::mojom::SpellCheckPanel>
focused_spell_check_panel_client;
frame->GetRemoteInterfaces()->GetInterface(
focused_spell_check_panel_client.BindNewPipeAndPassReceiver());
focused_spell_check_panel_client->ToggleSpellPanel(visible);
}
}
- (void)toggleContinuousSpellChecking:(id)sender {
PrefService* pref = self.prefService;
if (!pref) {
return;
}
pref->SetBoolean(spellcheck::prefs::kSpellCheckEnable,
!pref->GetBoolean(spellcheck::prefs::kSpellCheckEnable));
}
// END Spellchecking methods
// If a dialog is visible, make its window key. See becomeFirstResponder.
- (void)makeAnyDialogKey {
content::WebContents* webContents = self.webContents;
if (!webContents) {
return;
}
web_modal::WebContentsModalDialogManager* manager =
web_modal::WebContentsModalDialogManager::FromWebContents(webContents);
if (!manager) {
return;
}
if (manager->IsDialogActive()) {
manager->FocusTopmostDialog();
}
}
// If the RenderWidgetHostView becomes first responder while it has a dialog
// (say, if the user was interacting with the omnibox and then tabs back into
// the web contents), then make the dialog window key.
- (void)becomeFirstResponder {
[self makeAnyDialogKey];
}
// If the RenderWidgetHostView is asked to resign first responder while a child
// window is key, then the user performed some action which targets the browser
// window, like clicking the omnibox or typing cmd+L. In that case, the browser
// window should become key.
- (void)resignFirstResponder {
NSWindow* browserWindow = self.nsView.window;
DCHECK(browserWindow);
// If the browser window is already key, there's nothing to do.
if (browserWindow.isKeyWindow) {
return;
}
// Otherwise, look for it in the key window's chain of parents.
NSWindow* keyWindowOrParent = NSApp.keyWindow;
while (keyWindowOrParent && keyWindowOrParent != browserWindow) {
keyWindowOrParent = keyWindowOrParent.parentWindow;
}
// If the browser window isn't among the parents, there's nothing to do.
if (keyWindowOrParent != browserWindow) {
return;
}
// Otherwise, temporarily set an ivar so that -windowDidBecomeKey, below,
// doesn't immediately make the dialog key.
base::AutoReset<BOOL> scoped(&_resigningFirstResponder, YES);
// …then make the browser window key.
[browserWindow makeKeyWindow];
}
// If the browser window becomes key while the RenderWidgetHostView is first
// responder, make the dialog key (if there is one).
- (void)windowDidBecomeKey {
if (_resigningFirstResponder) {
return;
}
NSView* view = self.nsView;
if (view.window.firstResponder == view) {
[self makeAnyDialogKey];
}
}
- (AcceptMouseEvents)acceptsMouseEventsOption {
content::WebContents* webContents = self.webContents;
if (!webContents) {
return AcceptMouseEvents::kWhenInActiveWindow;
}
// If this web contents is in a tab, and the tab wants to accept mouse events
// while the window is inactive.
if (tabs::TabInterface* tab =
tabs::TabInterface::MaybeGetFromContents(webContents)) {
if (tabs::TabFeatures* features = tab->GetTabFeatures()) {
if (tabs::InactiveWindowMouseEventController* inactive_event_controller =
features->inactive_window_mouse_event_controller()) {
if (inactive_event_controller
->ShouldAcceptMouseEventsWhileWindowInactive()) {
return AcceptMouseEvents::kWhenInActiveApp;
}
}
}
}
// For Top Chrome WebUIs, allows inactive windows to accept
// mouse events as long as the application is active. This
// mimics the behavior of views UI.
if (IsTopChromeWebUIURL(webContents->GetVisibleURL()) ||
IsTopChromeUntrustedWebUIURL(webContents->GetVisibleURL())) {
return AcceptMouseEvents::kWhenInActiveApp;
}
#if BUILDFLAG(ENABLE_GLIC)
// WebContents managed by glic should be allowed to accept mouse events while
// inactive, aligning with the expected behavior of native chrome dialogs.
// TODO(crbug.com/399119513): Consider making this a single WebContents
// scoped setting, allowing this behavior to be configured by feature code.
glic::GlicKeyedService* glic_service = glic::GlicKeyedService::Get(
Profile::FromBrowserContext(webContents->GetBrowserContext()));
if (glic_service && glic_service->IsActiveWebContents(webContents)) {
return AcceptMouseEvents::kWhenInActiveApp;
}
#endif
return AcceptMouseEvents::kWhenInActiveWindow;
}
@end
|