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

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

Issue 2229223002: Route IME-related IPCs to the active/focused RenderWidget (Mac) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed erickchen@ comments + Making RWHVMacTest -> InputMethodMacTest. Created 4 years, 4 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 <objc/runtime.h> 7 #import <objc/runtime.h>
8 #include <OpenGL/gl.h> 8 #include <OpenGL/gl.h>
9 #include <QuartzCore/QuartzCore.h> 9 #include <QuartzCore/QuartzCore.h>
10 #include <stdint.h> 10 #include <stdint.h>
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 // We are owned by RenderWidgetHostViewCocoa, so if we go away before the 546 // We are owned by RenderWidgetHostViewCocoa, so if we go away before the
547 // RenderWidgetHost does we need to tell it not to hold a stale pointer to 547 // RenderWidgetHost does we need to tell it not to hold a stale pointer to
548 // us. 548 // us.
549 if (render_widget_host_) { 549 if (render_widget_host_) {
550 // If this is a RenderWidgetHostViewGuest's platform_view_, we're not the 550 // If this is a RenderWidgetHostViewGuest's platform_view_, we're not the
551 // RWH's view, the RenderWidgetHostViewGuest is. So don't reset the RWH's 551 // RWH's view, the RenderWidgetHostViewGuest is. So don't reset the RWH's
552 // view, the RenderWidgetHostViewGuest will do it. 552 // view, the RenderWidgetHostViewGuest will do it.
553 if (!is_guest_view_hack_) 553 if (!is_guest_view_hack_)
554 render_widget_host_->SetView(NULL); 554 render_widget_host_->SetView(NULL);
555 } 555 }
556
557 // In case the view is deleted (by cocoa view) before calling destroy, we need
558 // to remove this view from the observer list of TextInputManager.
559 if (text_input_manager_ && text_input_manager_->HasObserver(this))
560 text_input_manager_->RemoveObserver(this);
556 } 561 }
557 562
558 void RenderWidgetHostViewMac::SetDelegate( 563 void RenderWidgetHostViewMac::SetDelegate(
559 NSObject<RenderWidgetHostViewMacDelegate>* delegate) { 564 NSObject<RenderWidgetHostViewMacDelegate>* delegate) {
560 [cocoa_view_ setResponderDelegate:delegate]; 565 [cocoa_view_ setResponderDelegate:delegate];
561 } 566 }
562 567
563 void RenderWidgetHostViewMac::SetAllowPauseForResizeOrRepaint(bool allow) { 568 void RenderWidgetHostViewMac::SetAllowPauseForResizeOrRepaint(bool allow) {
564 allow_pause_for_resize_or_repaint_ = allow; 569 allow_pause_for_resize_or_repaint_ = allow;
565 } 570 }
566 571
567 cc::SurfaceId RenderWidgetHostViewMac::SurfaceIdForTesting() const { 572 cc::SurfaceId RenderWidgetHostViewMac::SurfaceIdForTesting() const {
568 return browser_compositor_->GetDelegatedFrameHost()->SurfaceIdForTesting(); 573 return browser_compositor_->GetDelegatedFrameHost()->SurfaceIdForTesting();
569 } 574 }
570 575
571 ui::TextInputType RenderWidgetHostViewMac::GetTextInputType() { 576 ui::TextInputType RenderWidgetHostViewMac::GetTextInputType() {
572 if (!GetTextInputManager() || !GetTextInputManager()->GetActiveWidget()) 577 if (!GetTextInputManager() || !GetTextInputManager()->GetActiveWidget())
573 return ui::TEXT_INPUT_TYPE_NONE; 578 return ui::TEXT_INPUT_TYPE_NONE;
574 return GetTextInputManager()->GetTextInputState()->type; 579 return GetTextInputManager()->GetTextInputState()->type;
575 } 580 }
576 581
582 RenderWidgetHostImpl* RenderWidgetHostViewMac::GetActiveWidget() {
583 return GetTextInputManager() ? GetTextInputManager()->GetActiveWidget()
584 : nullptr;
585 }
586
577 /////////////////////////////////////////////////////////////////////////////// 587 ///////////////////////////////////////////////////////////////////////////////
578 // RenderWidgetHostViewMac, RenderWidgetHostView implementation: 588 // RenderWidgetHostViewMac, RenderWidgetHostView implementation:
579 589
580 bool RenderWidgetHostViewMac::OnMessageReceived(const IPC::Message& message) { 590 bool RenderWidgetHostViewMac::OnMessageReceived(const IPC::Message& message) {
581 bool handled = true; 591 bool handled = true;
582 IPC_BEGIN_MESSAGE_MAP(RenderWidgetHostViewMac, message) 592 IPC_BEGIN_MESSAGE_MAP(RenderWidgetHostViewMac, message)
583 IPC_MESSAGE_HANDLER(ViewMsg_GetRenderedTextCompleted, 593 IPC_MESSAGE_HANDLER(ViewMsg_GetRenderedTextCompleted,
584 OnGetRenderedTextCompleted) 594 OnGetRenderedTextCompleted)
585 IPC_MESSAGE_HANDLER(ViewHostMsg_SetNeedsBeginFrames, 595 IPC_MESSAGE_HANDLER(ViewHostMsg_SetNeedsBeginFrames,
586 OnSetNeedsBeginFrames) 596 OnSetNeedsBeginFrames)
(...skipping 2352 matching lines...) Expand 10 before | Expand all | Expand 10 after
2939 // text when it cancels an ongoing composition, i.e. I have never seen an 2949 // text when it cancels an ongoing composition, i.e. I have never seen an
2940 // input method calls this method. 2950 // input method calls this method.
2941 hasMarkedText_ = NO; 2951 hasMarkedText_ = NO;
2942 markedText_.clear(); 2952 markedText_.clear();
2943 markedTextSelectedRange_ = NSMakeRange(NSNotFound, 0); 2953 markedTextSelectedRange_ = NSMakeRange(NSNotFound, 0);
2944 underlines_.clear(); 2954 underlines_.clear();
2945 2955
2946 // If we are handling a key down event, then ConfirmComposition() will be 2956 // If we are handling a key down event, then ConfirmComposition() will be
2947 // called in keyEvent: method. 2957 // called in keyEvent: method.
2948 if (!handlingKeyDown_) { 2958 if (!handlingKeyDown_) {
2949 renderWidgetHostView_->render_widget_host_->ImeConfirmComposition( 2959 if (renderWidgetHostView_->GetActiveWidget()) {
2950 base::string16(), gfx::Range::InvalidRange(), false); 2960 renderWidgetHostView_->GetActiveWidget()->ImeConfirmComposition(
2961 base::string16(), gfx::Range::InvalidRange(), false);
2962 }
2951 } else { 2963 } else {
2952 unmarkTextCalled_ = YES; 2964 unmarkTextCalled_ = YES;
2953 } 2965 }
2954 } 2966 }
2955 2967
2956 - (void)setMarkedText:(id)string selectedRange:(NSRange)newSelRange 2968 - (void)setMarkedText:(id)string selectedRange:(NSRange)newSelRange
2957 replacementRange:(NSRange)replacementRange { 2969 replacementRange:(NSRange)replacementRange {
2958 // An input method updates the composition string. 2970 // An input method updates the composition string.
2959 // We send the given text and range to the renderer so it can update the 2971 // We send the given text and range to the renderer so it can update the
2960 // composition node of WebKit. 2972 // composition node of WebKit.
(...skipping 14 matching lines...) Expand all
2975 } else { 2987 } else {
2976 // Use a thin black underline by default. 2988 // Use a thin black underline by default.
2977 underlines_.push_back(blink::WebCompositionUnderline( 2989 underlines_.push_back(blink::WebCompositionUnderline(
2978 0, length, SK_ColorBLACK, false, SK_ColorTRANSPARENT)); 2990 0, length, SK_ColorBLACK, false, SK_ColorTRANSPARENT));
2979 } 2991 }
2980 2992
2981 // If we are handling a key down event, then SetComposition() will be 2993 // If we are handling a key down event, then SetComposition() will be
2982 // called in keyEvent: method. 2994 // called in keyEvent: method.
2983 // Input methods of Mac use setMarkedText calls with an empty text to cancel 2995 // Input methods of Mac use setMarkedText calls with an empty text to cancel
2984 // an ongoing composition. So, we should check whether or not the given text 2996 // an ongoing composition. So, we should check whether or not the given text
2985 // is empty to update the input method state. (Our input method backend can 2997 // is empty to update the input method state. (Our input method backend
2986 // automatically cancels an ongoing composition when we send an empty text. 2998 // automatically cancels an ongoing composition when we send an empty text.
2987 // So, it is OK to send an empty text to the renderer.) 2999 // So, it is OK to send an empty text to the renderer.)
2988 if (handlingKeyDown_) { 3000 if (handlingKeyDown_) {
2989 setMarkedTextReplacementRange_ = gfx::Range(replacementRange); 3001 setMarkedTextReplacementRange_ = gfx::Range(replacementRange);
2990 } else { 3002 } else {
2991 renderWidgetHostView_->render_widget_host_->ImeSetComposition( 3003 if (renderWidgetHostView_->GetActiveWidget()) {
2992 markedText_, underlines_, gfx::Range(replacementRange), 3004 renderWidgetHostView_->GetActiveWidget()->ImeSetComposition(
2993 newSelRange.location, NSMaxRange(newSelRange)); 3005 markedText_, underlines_, gfx::Range(replacementRange),
3006 newSelRange.location, NSMaxRange(newSelRange));
3007 }
2994 } 3008 }
2995 } 3009 }
2996 3010
2997 - (void)doCommandBySelector:(SEL)selector { 3011 - (void)doCommandBySelector:(SEL)selector {
2998 // An input method calls this function to dispatch an editing command to be 3012 // An input method calls this function to dispatch an editing command to be
2999 // handled by this view. 3013 // handled by this view.
3000 if (selector == @selector(noop:)) 3014 if (selector == @selector(noop:))
3001 return; 3015 return;
3002 3016
3003 std::string command( 3017 std::string command(
(...skipping 14 matching lines...) Expand all
3018 } else { 3032 } else {
3019 RenderWidgetHostImpl* rwh = renderWidgetHostView_->render_widget_host_; 3033 RenderWidgetHostImpl* rwh = renderWidgetHostView_->render_widget_host_;
3020 rwh->Send(new InputMsg_ExecuteEditCommand(rwh->GetRoutingID(), 3034 rwh->Send(new InputMsg_ExecuteEditCommand(rwh->GetRoutingID(),
3021 command, "")); 3035 command, ""));
3022 } 3036 }
3023 } 3037 }
3024 3038
3025 - (void)insertText:(id)string replacementRange:(NSRange)replacementRange { 3039 - (void)insertText:(id)string replacementRange:(NSRange)replacementRange {
3026 // An input method has characters to be inserted. 3040 // An input method has characters to be inserted.
3027 // Same as Linux, Mac calls this method not only: 3041 // Same as Linux, Mac calls this method not only:
3028 // * when an input method finishs composing text, but also; 3042 // * when an input method finishes composing text, but also;
3029 // * when we type an ASCII character (without using input methods). 3043 // * when we type an ASCII character (without using input methods).
3030 // When we aren't using input methods, we should send the given character as 3044 // When we aren't using input methods, we should send the given character as
3031 // a Char event so it is dispatched to an onkeypress() event handler of 3045 // a Char event so it is dispatched to an onkeypress() event handler of
3032 // JavaScript. 3046 // JavaScript.
3033 // On the other hand, when we are using input methods, we should send the 3047 // On the other hand, when we are using input methods, we should send the
3034 // given characters as an input method event and prevent the characters from 3048 // given characters as an input method event and prevent the characters from
3035 // being dispatched to onkeypress() event handlers. 3049 // being dispatched to onkeypress() event handlers.
3036 // Text inserting might be initiated by other source instead of keyboard 3050 // Text inserting might be initiated by other source instead of keyboard
3037 // events, such as the Characters dialog. In this case the text should be 3051 // events, such as the Characters dialog. In this case the text should be
3038 // sent as an input method event as well. 3052 // sent as an input method event as well.
3039 // TODO(suzhe): It's hard for us to support replacementRange without accessing 3053 // TODO(suzhe): It's hard for us to support replacementRange without accessing
3040 // the full web content. 3054 // the full web content.
3041 BOOL isAttributedString = [string isKindOfClass:[NSAttributedString class]]; 3055 BOOL isAttributedString = [string isKindOfClass:[NSAttributedString class]];
3042 NSString* im_text = isAttributedString ? [string string] : string; 3056 NSString* im_text = isAttributedString ? [string string] : string;
3043 if (handlingKeyDown_) { 3057 if (handlingKeyDown_) {
3044 textToBeInserted_.append(base::SysNSStringToUTF16(im_text)); 3058 textToBeInserted_.append(base::SysNSStringToUTF16(im_text));
3045 } else { 3059 } else {
3046 gfx::Range replacement_range(replacementRange); 3060 gfx::Range replacement_range(replacementRange);
3047 renderWidgetHostView_->render_widget_host_->ImeConfirmComposition( 3061 if (renderWidgetHostView_->GetActiveWidget()) {
3048 base::SysNSStringToUTF16(im_text), replacement_range, false); 3062 renderWidgetHostView_->GetActiveWidget()->ImeConfirmComposition(
3063 base::SysNSStringToUTF16(im_text), replacement_range, false);
3064 }
3049 } 3065 }
3050 3066
3051 // Inserting text will delete all marked text automatically. 3067 // Inserting text will delete all marked text automatically.
3052 hasMarkedText_ = NO; 3068 hasMarkedText_ = NO;
3053 } 3069 }
3054 3070
3055 - (void)insertText:(id)string { 3071 - (void)insertText:(id)string {
3056 [self insertText:string replacementRange:NSMakeRange(NSNotFound, 0)]; 3072 [self insertText:string replacementRange:NSMakeRange(NSNotFound, 0)];
3057 } 3073 }
3058 3074
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
3169 3185
3170 hasMarkedText_ = NO; 3186 hasMarkedText_ = NO;
3171 // Should not call [self unmarkText] here, because it'll send unnecessary 3187 // Should not call [self unmarkText] here, because it'll send unnecessary
3172 // cancel composition IPC message to the renderer. 3188 // cancel composition IPC message to the renderer.
3173 } 3189 }
3174 3190
3175 - (void)confirmComposition { 3191 - (void)confirmComposition {
3176 if (!hasMarkedText_) 3192 if (!hasMarkedText_)
3177 return; 3193 return;
3178 3194
3179 if (renderWidgetHostView_->render_widget_host_) 3195 if (renderWidgetHostView_->GetActiveWidget()) {
3180 renderWidgetHostView_->render_widget_host_->ImeConfirmComposition( 3196 renderWidgetHostView_->GetActiveWidget()->ImeConfirmComposition(
3181 base::string16(), gfx::Range::InvalidRange(), false); 3197 base::string16(), gfx::Range::InvalidRange(), false);
3198 }
3182 3199
3183 [self cancelComposition]; 3200 [self cancelComposition];
3184 } 3201 }
3185 3202
3186 // Overriding a NSResponder method to support application services. 3203 // Overriding a NSResponder method to support application services.
3187 3204
3188 - (id)validRequestorForSendType:(NSString*)sendType 3205 - (id)validRequestorForSendType:(NSString*)sendType
3189 returnType:(NSString*)returnType { 3206 returnType:(NSString*)returnType {
3190 id requestor = nil; 3207 id requestor = nil;
3191 BOOL sendTypeIsString = [sendType isEqual:NSStringPboardType]; 3208 BOOL sendTypeIsString = [sendType isEqual:NSStringPboardType];
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
3255 3272
3256 // "-webkit-app-region: drag | no-drag" is implemented on Mac by excluding 3273 // "-webkit-app-region: drag | no-drag" is implemented on Mac by excluding
3257 // regions that are not draggable. (See ControlRegionView in 3274 // regions that are not draggable. (See ControlRegionView in
3258 // native_app_window_cocoa.mm). This requires the render host view to be 3275 // native_app_window_cocoa.mm). This requires the render host view to be
3259 // draggable by default. 3276 // draggable by default.
3260 - (BOOL)mouseDownCanMoveWindow { 3277 - (BOOL)mouseDownCanMoveWindow {
3261 return YES; 3278 return YES;
3262 } 3279 }
3263 3280
3264 @end 3281 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698