Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Side by Side Diff: content/browser/renderer_host/render_widget_host_view_mac.mm

Issue 2164483006: [MacViews] Implemented text context menu (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/renderer_host/render_widget_host_view_mac.h" 5 #include "content/browser/renderer_host/render_widget_host_view_mac.h"
6 6
7 #import <Carbon/Carbon.h> 7 #import <Carbon/Carbon.h>
8 #import <objc/runtime.h> 8 #import <objc/runtime.h>
9 #include <OpenGL/gl.h> 9 #include <OpenGL/gl.h>
10 #include <QuartzCore/QuartzCore.h> 10 #include <QuartzCore/QuartzCore.h>
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 #include "content/public/browser/web_contents.h" 66 #include "content/public/browser/web_contents.h"
67 #include "gpu/ipc/common/gpu_messages.h" 67 #include "gpu/ipc/common/gpu_messages.h"
68 #include "skia/ext/platform_canvas.h" 68 #include "skia/ext/platform_canvas.h"
69 #include "skia/ext/skia_utils_mac.h" 69 #include "skia/ext/skia_utils_mac.h"
70 #include "third_party/WebKit/public/platform/WebInputEvent.h" 70 #include "third_party/WebKit/public/platform/WebInputEvent.h"
71 #import "ui/base/clipboard/clipboard_util_mac.h" 71 #import "ui/base/clipboard/clipboard_util_mac.h"
72 #include "ui/base/cocoa/animation_utils.h" 72 #include "ui/base/cocoa/animation_utils.h"
73 #import "ui/base/cocoa/appkit_utils.h" 73 #import "ui/base/cocoa/appkit_utils.h"
74 #include "ui/base/cocoa/cocoa_base_utils.h" 74 #include "ui/base/cocoa/cocoa_base_utils.h"
75 #import "ui/base/cocoa/fullscreen_window_manager.h" 75 #import "ui/base/cocoa/fullscreen_window_manager.h"
76 #include "ui/base/cocoa/text_services_context_menu.h"
76 #import "ui/base/cocoa/underlay_opengl_hosting_window.h" 77 #import "ui/base/cocoa/underlay_opengl_hosting_window.h"
77 #include "ui/base/layout.h" 78 #include "ui/base/layout.h"
78 #include "ui/compositor/compositor.h" 79 #include "ui/compositor/compositor.h"
79 #include "ui/compositor/layer.h" 80 #include "ui/compositor/layer.h"
80 #include "ui/display/display.h" 81 #include "ui/display/display.h"
81 #include "ui/display/screen.h" 82 #include "ui/display/screen.h"
82 #include "ui/events/base_event_utils.h" 83 #include "ui/events/base_event_utils.h"
83 #include "ui/events/event_utils.h" 84 #include "ui/events/event_utils.h"
84 #include "ui/events/keycodes/keyboard_codes.h" 85 #include "ui/events/keycodes/keyboard_codes.h"
85 #include "ui/gfx/geometry/dip_util.h" 86 #include "ui/gfx/geometry/dip_util.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 return render_widget_host_view; 132 return render_widget_host_view;
132 content::WebContents* guest = 133 content::WebContents* guest =
133 guest_manager->GetFullPageGuest(web_contents); 134 guest_manager->GetFullPageGuest(web_contents);
134 if (!guest) 135 if (!guest)
135 return render_widget_host_view; 136 return render_widget_host_view;
136 return guest->GetRenderWidgetHostView(); 137 return guest->GetRenderWidgetHostView();
137 } 138 }
138 139
139 } // namespace 140 } // namespace
140 141
141 // These are not documented, so use only after checking -respondsToSelector:.
142 @interface NSApplication (UndocumentedSpeechMethods)
143 - (void)speakString:(NSString*)string;
144 - (void)stopSpeaking:(id)sender;
145 - (BOOL)isSpeaking;
146 @end
147
148 // Private methods: 142 // Private methods:
149 @interface RenderWidgetHostViewCocoa () 143 @interface RenderWidgetHostViewCocoa ()
150 @property(nonatomic, assign) NSRange selectedRange; 144 @property(nonatomic, assign) NSRange selectedRange;
151 @property(nonatomic, assign) NSRange markedRange; 145 @property(nonatomic, assign) NSRange markedRange;
152 146
153 + (BOOL)shouldAutohideCursorForEvent:(NSEvent*)event; 147 + (BOOL)shouldAutohideCursorForEvent:(NSEvent*)event;
154 - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)r; 148 - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)r;
155 - (void)processedWheelEvent:(const blink::WebMouseWheelEvent&)event 149 - (void)processedWheelEvent:(const blink::WebMouseWheelEvent&)event
156 consumed:(BOOL)consumed; 150 consumed:(BOOL)consumed;
157 - (void)processedGestureScrollEvent:(const blink::WebGestureEvent&)event 151 - (void)processedGestureScrollEvent:(const blink::WebGestureEvent&)event
(...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 715
722 if (!display_link_->GetVSyncParameters(&vsync_timebase_, &vsync_interval_)) { 716 if (!display_link_->GetVSyncParameters(&vsync_timebase_, &vsync_interval_)) {
723 vsync_timebase_ = base::TimeTicks(); 717 vsync_timebase_ = base::TimeTicks();
724 vsync_interval_ = base::TimeDelta(); 718 vsync_interval_ = base::TimeDelta();
725 return; 719 return;
726 } 720 }
727 721
728 browser_compositor_->UpdateVSyncParameters(vsync_timebase_, vsync_interval_); 722 browser_compositor_->UpdateVSyncParameters(vsync_timebase_, vsync_interval_);
729 } 723 }
730 724
731 void RenderWidgetHostViewMac::SpeakText(const std::string& text) {
732 [NSApp speakString:base::SysUTF8ToNSString(text)];
733 }
734
735 RenderWidgetHostViewBase* 725 RenderWidgetHostViewBase*
736 RenderWidgetHostViewMac::GetFocusedViewForTextSelection() { 726 RenderWidgetHostViewMac::GetFocusedViewForTextSelection() {
737 // We obtain the TextSelection from focused RWH which is obtained from the 727 // We obtain the TextSelection from focused RWH which is obtained from the
738 // frame tree. BrowserPlugin-based guests' RWH is not part of the frame tree 728 // frame tree. BrowserPlugin-based guests' RWH is not part of the frame tree
739 // and the focused RWH will be that of the embedder which is incorrect. In 729 // and the focused RWH will be that of the embedder which is incorrect. In
740 // this case we should use TextSelection for |this| since RWHV for guest 730 // this case we should use TextSelection for |this| since RWHV for guest
741 // forwards text selection information to its platform view. 731 // forwards text selection information to its platform view.
742 return is_guest_view_hack_ ? this : GetFocusedWidget() 732 return is_guest_view_hack_ ? this : GetFocusedWidget()
743 ? GetFocusedWidget()->GetView() 733 ? GetFocusedWidget()->GetView()
744 : nullptr; 734 : nullptr;
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
1024 RenderWidgetHostViewBase* focused_view = GetFocusedViewForTextSelection(); 1014 RenderWidgetHostViewBase* focused_view = GetFocusedViewForTextSelection();
1025 if (!focused_view) 1015 if (!focused_view)
1026 return; 1016 return;
1027 1017
1028 const TextInputManager::TextSelection* selection = 1018 const TextInputManager::TextSelection* selection =
1029 GetTextInputManager()->GetTextSelection(focused_view); 1019 GetTextInputManager()->GetTextSelection(focused_view);
1030 1020
1031 base::string16 text; 1021 base::string16 text;
1032 if (!selection->GetSelectedText(&text)) 1022 if (!selection->GetSelectedText(&text))
1033 return; 1023 return;
1034 selected_text_ = base::UTF16ToUTF8(text); 1024 selected_text_ = text;
1035 1025
1036 [cocoa_view_ setSelectedRange:selection->range.ToNSRange()]; 1026 [cocoa_view_ setSelectedRange:selection->range.ToNSRange()];
1037 // Updates markedRange when there is no marked text so that retrieving 1027 // Updates markedRange when there is no marked text so that retrieving
1038 // markedRange immediately after calling setMarkdText: returns the current 1028 // markedRange immediately after calling setMarkdText: returns the current
1039 // caret position. 1029 // caret position.
1040 if (![cocoa_view_ hasMarkedText]) { 1030 if (![cocoa_view_ hasMarkedText]) {
1041 [cocoa_view_ setMarkedRange:selection->range.ToNSRange()]; 1031 [cocoa_view_ setMarkedRange:selection->range.ToNSRange()];
1042 } 1032 }
1043 1033
1044 // TODO(ekaramad): The following values are tracked by TextInputManager and 1034 // TODO(ekaramad): The following values are tracked by TextInputManager and
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
1112 // set it again every single time the mouse moves. 1102 // set it again every single time the mouse moves.
1113 base::string16 display_text = tooltip_text_; 1103 base::string16 display_text = tooltip_text_;
1114 if (tooltip_text_.length() > kMaxTooltipLength) 1104 if (tooltip_text_.length() > kMaxTooltipLength)
1115 display_text = tooltip_text_.substr(0, kMaxTooltipLength); 1105 display_text = tooltip_text_.substr(0, kMaxTooltipLength);
1116 1106
1117 NSString* tooltip_nsstring = base::SysUTF16ToNSString(display_text); 1107 NSString* tooltip_nsstring = base::SysUTF16ToNSString(display_text);
1118 [cocoa_view_ setToolTipAtMousePoint:tooltip_nsstring]; 1108 [cocoa_view_ setToolTipAtMousePoint:tooltip_nsstring];
1119 } 1109 }
1120 } 1110 }
1121 1111
1122 bool RenderWidgetHostViewMac::SupportsSpeech() const {
1123 return [NSApp respondsToSelector:@selector(speakString:)] &&
1124 [NSApp respondsToSelector:@selector(stopSpeaking:)];
1125 }
1126
1127 void RenderWidgetHostViewMac::SpeakSelection() { 1112 void RenderWidgetHostViewMac::SpeakSelection() {
1128 if (![NSApp respondsToSelector:@selector(speakString:)])
1129 return;
1130
1131 if (selected_text_.empty() && render_widget_host_) { 1113 if (selected_text_.empty() && render_widget_host_) {
1132 // If there's no selection, speak all text. Send an asynchronous IPC 1114 // If there's no selection, speak all text. Send an asynchronous IPC
1133 // request for fetching all the text for a webcontent. 1115 // request for fetching all the text for a webcontent.
1134 // ViewMsg_GetRenderedTextCompleted is sent back to IPC Message receiver. 1116 // ViewMsg_GetRenderedTextCompleted is sent back to IPC Message receiver.
1135 render_widget_host_->Send(new ViewMsg_GetRenderedText( 1117 render_widget_host_->Send(new ViewMsg_GetRenderedText(
1136 render_widget_host_->GetRoutingID())); 1118 render_widget_host_->GetRoutingID()));
1137 return; 1119 return;
1138 } 1120 }
1139 1121
1140 SpeakText(selected_text_); 1122 ui::TextServicesContextMenu::SpeakText(selected_text_);
1141 }
1142
1143 bool RenderWidgetHostViewMac::IsSpeaking() const {
1144 return [NSApp respondsToSelector:@selector(isSpeaking)] &&
1145 [NSApp isSpeaking];
1146 }
1147
1148 void RenderWidgetHostViewMac::StopSpeaking() {
1149 if ([NSApp respondsToSelector:@selector(stopSpeaking:)])
1150 [NSApp stopSpeaking:cocoa_view_];
1151 } 1123 }
1152 1124
1153 // 1125 //
1154 // RenderWidgetHostViewCocoa uses the stored selection text, 1126 // RenderWidgetHostViewCocoa uses the stored selection text,
1155 // which implements NSServicesRequests protocol. 1127 // which implements NSServicesRequests protocol.
1156 // 1128 //
1157 1129
1158 void RenderWidgetHostViewMac::SetShowingContextMenu(bool showing) { 1130 void RenderWidgetHostViewMac::SetShowingContextMenu(bool showing) {
1159 RenderWidgetHostViewBase::SetShowingContextMenu(showing); 1131 RenderWidgetHostViewBase::SetShowingContextMenu(showing);
1160 1132
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after
1691 else 1663 else
1692 DisablePasswordInput(); 1664 DisablePasswordInput();
1693 } else { 1665 } else {
1694 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_PASSWORD) 1666 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_PASSWORD)
1695 DisablePasswordInput(); 1667 DisablePasswordInput();
1696 } 1668 }
1697 } 1669 }
1698 1670
1699 void RenderWidgetHostViewMac::OnGetRenderedTextCompleted( 1671 void RenderWidgetHostViewMac::OnGetRenderedTextCompleted(
1700 const std::string& text) { 1672 const std::string& text) {
1701 SpeakText(text); 1673 ui::TextServicesContextMenu::SpeakText(base::UTF8ToUTF16(text));
1702 } 1674 }
1703 1675
1704 void RenderWidgetHostViewMac::PauseForPendingResizeOrRepaintsAndDraw() { 1676 void RenderWidgetHostViewMac::PauseForPendingResizeOrRepaintsAndDraw() {
1705 if (!render_widget_host_ || render_widget_host_->is_hidden()) 1677 if (!render_widget_host_ || render_widget_host_->is_hidden())
1706 return; 1678 return;
1707 1679
1708 // Pausing for one view prevents others from receiving frames. 1680 // Pausing for one view prevents others from receiving frames.
1709 // This may lead to large delays, causing overlaps. See crbug.com/352020. 1681 // This may lead to large delays, causing overlaps. See crbug.com/352020.
1710 if (!allow_pause_for_resize_or_repaint_) 1682 if (!allow_pause_for_resize_or_repaint_)
1711 return; 1683 return;
(...skipping 648 matching lines...) Expand 10 before | Expand all | Expand 10 after
2360 } 2332 }
2361 2333
2362 - (void)showLookUpDictionaryOverlayInternal:(NSAttributedString*) string 2334 - (void)showLookUpDictionaryOverlayInternal:(NSAttributedString*) string
2363 baselinePoint:(NSPoint) baselinePoint 2335 baselinePoint:(NSPoint) baselinePoint
2364 targetView:(NSView*) view { 2336 targetView:(NSView*) view {
2365 if ([string length] == 0) { 2337 if ([string length] == 0) {
2366 // The PDF plugin does not support getting the attributed string at point. 2338 // The PDF plugin does not support getting the attributed string at point.
2367 // Until it does, use NSPerformService(), which opens Dictionary.app. 2339 // Until it does, use NSPerformService(), which opens Dictionary.app.
2368 // TODO(shuchen): Support GetStringAtPoint() & GetStringFromRange() for PDF. 2340 // TODO(shuchen): Support GetStringAtPoint() & GetStringFromRange() for PDF.
2369 // See crbug.com/152438. 2341 // See crbug.com/152438.
2370 NSString* text = base::SysUTF8ToNSString( 2342 NSString* text =
2371 renderWidgetHostView_->selected_text()); 2343 base::SysUTF16ToNSString(renderWidgetHostView_->selected_text());
2372 if ([text length] == 0) 2344 if ([text length] == 0)
2373 return; 2345 return;
2374 scoped_refptr<ui::UniquePasteboard> pasteboard = new ui::UniquePasteboard; 2346 scoped_refptr<ui::UniquePasteboard> pasteboard = new ui::UniquePasteboard;
2375 NSArray* types = [NSArray arrayWithObject:NSStringPboardType]; 2347 NSArray* types = [NSArray arrayWithObject:NSStringPboardType];
2376 [pasteboard->get() declareTypes:types owner:nil]; 2348 [pasteboard->get() declareTypes:types owner:nil];
2377 if ([pasteboard->get() setString:text forType:NSStringPboardType]) 2349 if ([pasteboard->get() setString:text forType:NSStringPboardType])
2378 NSPerformService(@"Look Up in Dictionary", pasteboard->get()); 2350 NSPerformService(@"Look Up in Dictionary", pasteboard->get());
2379 return; 2351 return;
2380 } 2352 }
2381 dispatch_async(dispatch_get_main_queue(), ^{ 2353 dispatch_async(dispatch_get_main_queue(), ^{
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after
2752 if (known) 2724 if (known)
2753 return valid; 2725 return valid;
2754 } 2726 }
2755 2727
2756 SEL action = [item action]; 2728 SEL action = [item action];
2757 BOOL is_render_view = 2729 BOOL is_render_view =
2758 RenderViewHost::From(renderWidgetHostView_->render_widget_host_) != 2730 RenderViewHost::From(renderWidgetHostView_->render_widget_host_) !=
2759 nullptr; 2731 nullptr;
2760 2732
2761 if (action == @selector(stopSpeaking:)) 2733 if (action == @selector(stopSpeaking:))
2762 return is_render_view && renderWidgetHostView_->IsSpeaking(); 2734 return is_render_view && ui::TextServicesContextMenu::IsSpeaking();
2763 2735
2764 if (action == @selector(startSpeaking:)) 2736 if (action == @selector(startSpeaking:))
2765 return is_render_view && renderWidgetHostView_->SupportsSpeech(); 2737 return is_render_view;
2766 2738
2767 // For now, these actions are always enabled for render view, 2739 // For now, these actions are always enabled for render view,
2768 // this is sub-optimal. 2740 // this is sub-optimal.
2769 // TODO(suzhe): Plumb the "can*" methods up from WebCore. 2741 // TODO(suzhe): Plumb the "can*" methods up from WebCore.
2770 if (action == @selector(undo:) || 2742 if (action == @selector(undo:) ||
2771 action == @selector(redo:) || 2743 action == @selector(redo:) ||
2772 action == @selector(cut:) || 2744 action == @selector(cut:) ||
2773 action == @selector(copy:) || 2745 action == @selector(copy:) ||
2774 action == @selector(copyToFindPboard:) || 2746 action == @selector(copyToFindPboard:) ||
2775 action == @selector(paste:) || 2747 action == @selector(paste:) ||
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after
3349 renderWidgetHostView_->render_widget_host_->delegate(); 3321 renderWidgetHostView_->render_widget_host_->delegate();
3350 if (render_widget_host_delegate) 3322 if (render_widget_host_delegate)
3351 render_widget_host_delegate->SelectAll(); 3323 render_widget_host_delegate->SelectAll();
3352 } 3324 }
3353 3325
3354 - (void)startSpeaking:(id)sender { 3326 - (void)startSpeaking:(id)sender {
3355 GetRenderWidgetHostViewToUse(renderWidgetHostView_.get())->SpeakSelection(); 3327 GetRenderWidgetHostViewToUse(renderWidgetHostView_.get())->SpeakSelection();
3356 } 3328 }
3357 3329
3358 - (void)stopSpeaking:(id)sender { 3330 - (void)stopSpeaking:(id)sender {
3359 GetRenderWidgetHostViewToUse(renderWidgetHostView_.get())->StopSpeaking(); 3331 ui::TextServicesContextMenu::StopSpeaking();
3360 } 3332 }
3361 3333
3362 - (void)cancelComposition { 3334 - (void)cancelComposition {
3363 if (!hasMarkedText_) 3335 if (!hasMarkedText_)
3364 return; 3336 return;
3365 3337
3366 NSTextInputContext* inputContext = [self inputContext]; 3338 NSTextInputContext* inputContext = [self inputContext];
3367 [inputContext discardMarkedText]; 3339 [inputContext discardMarkedText];
3368 3340
3369 hasMarkedText_ = NO; 3341 hasMarkedText_ = NO;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
3420 3392
3421 @end 3393 @end
3422 3394
3423 // 3395 //
3424 // Supporting application services 3396 // Supporting application services
3425 // 3397 //
3426 @implementation RenderWidgetHostViewCocoa(NSServicesRequests) 3398 @implementation RenderWidgetHostViewCocoa(NSServicesRequests)
3427 3399
3428 - (BOOL)writeSelectionToPasteboard:(NSPasteboard*)pboard 3400 - (BOOL)writeSelectionToPasteboard:(NSPasteboard*)pboard
3429 types:(NSArray*)types { 3401 types:(NSArray*)types {
3430 const std::string& str = renderWidgetHostView_->selected_text(); 3402 const base::string16& str = renderWidgetHostView_->selected_text();
3431 if (![types containsObject:NSStringPboardType] || str.empty()) return NO; 3403 if (![types containsObject:NSStringPboardType] || str.empty()) return NO;
3432 3404
3433 base::scoped_nsobject<NSString> text(
3434 [[NSString alloc] initWithUTF8String:str.c_str()]);
3435 NSArray* toDeclare = [NSArray arrayWithObject:NSStringPboardType]; 3405 NSArray* toDeclare = [NSArray arrayWithObject:NSStringPboardType];
3436 [pboard declareTypes:toDeclare owner:nil]; 3406 [pboard declareTypes:toDeclare owner:nil];
3437 return [pboard setString:text forType:NSStringPboardType]; 3407 return [pboard setString:SysUTF16ToNSString(str) forType:NSStringPboardType];
3438 } 3408 }
3439 3409
3440 - (BOOL)readSelectionFromPasteboard:(NSPasteboard*)pboard { 3410 - (BOOL)readSelectionFromPasteboard:(NSPasteboard*)pboard {
3441 NSString *string = [pboard stringForType:NSStringPboardType]; 3411 NSString *string = [pboard stringForType:NSStringPboardType];
3442 if (!string) return NO; 3412 if (!string) return NO;
3443 3413
3444 // If the user is currently using an IME, confirm the IME input, 3414 // If the user is currently using an IME, confirm the IME input,
3445 // and then insert the text from the service, the same as TextEdit and Safari. 3415 // and then insert the text from the service, the same as TextEdit and Safari.
3446 [self finishComposingText]; 3416 [self finishComposingText];
3447 [self insertText:string]; 3417 [self insertText:string];
3448 return YES; 3418 return YES;
3449 } 3419 }
3450 3420
3451 - (BOOL)isOpaque { 3421 - (BOOL)isOpaque {
3452 return opaque_; 3422 return opaque_;
3453 } 3423 }
3454 3424
3455 // "-webkit-app-region: drag | no-drag" is implemented on Mac by excluding 3425 // "-webkit-app-region: drag | no-drag" is implemented on Mac by excluding
3456 // regions that are not draggable. (See ControlRegionView in 3426 // regions that are not draggable. (See ControlRegionView in
3457 // native_app_window_cocoa.mm). This requires the render host view to be 3427 // native_app_window_cocoa.mm). This requires the render host view to be
3458 // draggable by default. 3428 // draggable by default.
3459 - (BOOL)mouseDownCanMoveWindow { 3429 - (BOOL)mouseDownCanMoveWindow {
3460 return YES; 3430 return YES;
3461 } 3431 }
3462 3432
3463 @end 3433 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698