| OLD | NEW |
| 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/renderer/text_input_client_observer.h" | 5 #include "content/renderer/text_input_client_observer.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "content/common/text_input_client_messages.h" | 12 #include "content/common/text_input_client_messages.h" |
| 13 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" | 13 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
| 14 #include "content/renderer/render_frame_impl.h" |
| 14 #include "content/renderer/render_view_impl.h" | 15 #include "content/renderer/render_view_impl.h" |
| 16 #include "content/renderer/render_widget.h" |
| 17 #include "ipc/ipc_message.h" |
| 15 #include "third_party/WebKit/public/platform/WebPoint.h" | 18 #include "third_party/WebKit/public/platform/WebPoint.h" |
| 16 #include "third_party/WebKit/public/platform/WebRect.h" | 19 #include "third_party/WebKit/public/platform/WebRect.h" |
| 17 #include "third_party/WebKit/public/platform/WebString.h" | 20 #include "third_party/WebKit/public/platform/WebString.h" |
| 18 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 21 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 19 #include "third_party/WebKit/public/web/WebView.h" | 22 #include "third_party/WebKit/public/web/WebView.h" |
| 20 #include "third_party/WebKit/public/web/mac/WebSubstringUtil.h" | 23 #include "third_party/WebKit/public/web/mac/WebSubstringUtil.h" |
| 21 #include "ui/gfx/geometry/rect.h" | 24 #include "ui/gfx/geometry/rect.h" |
| 22 | 25 |
| 23 namespace content { | 26 namespace content { |
| 24 | 27 |
| 25 TextInputClientObserver::TextInputClientObserver(RenderViewImpl* render_view) | 28 TextInputClientObserver::TextInputClientObserver(RenderFrameImpl* render_frame) |
| 26 #if defined(ENABLE_PLUGINS) | 29 : render_frame_(render_frame) {} |
| 27 : RenderViewObserver(render_view), render_view_impl_(render_view) { | |
| 28 #else | |
| 29 : RenderViewObserver(render_view) { | |
| 30 #endif | |
| 31 } | |
| 32 | 30 |
| 33 TextInputClientObserver::~TextInputClientObserver() { | 31 TextInputClientObserver::~TextInputClientObserver() { |
| 34 } | 32 } |
| 35 | 33 |
| 36 bool TextInputClientObserver::OnMessageReceived(const IPC::Message& message) { | 34 bool TextInputClientObserver::OnMessageReceived(const IPC::Message& message) { |
| 37 bool handled = true; | 35 bool handled = true; |
| 38 IPC_BEGIN_MESSAGE_MAP(TextInputClientObserver, message) | 36 IPC_BEGIN_MESSAGE_MAP(TextInputClientObserver, message) |
| 39 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringAtPoint, | 37 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringAtPoint, |
| 40 OnStringAtPoint) | 38 OnStringAtPoint) |
| 41 IPC_MESSAGE_HANDLER(TextInputClientMsg_CharacterIndexForPoint, | 39 IPC_MESSAGE_HANDLER(TextInputClientMsg_CharacterIndexForPoint, |
| 42 OnCharacterIndexForPoint) | 40 OnCharacterIndexForPoint) |
| 43 IPC_MESSAGE_HANDLER(TextInputClientMsg_FirstRectForCharacterRange, | 41 IPC_MESSAGE_HANDLER(TextInputClientMsg_FirstRectForCharacterRange, |
| 44 OnFirstRectForCharacterRange) | 42 OnFirstRectForCharacterRange) |
| 45 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringForRange, OnStringForRange) | 43 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringForRange, OnStringForRange) |
| 46 IPC_MESSAGE_UNHANDLED(handled = false) | 44 IPC_MESSAGE_UNHANDLED(handled = false) |
| 47 IPC_END_MESSAGE_MAP() | 45 IPC_END_MESSAGE_MAP() |
| 48 return handled; | 46 return handled; |
| 49 } | 47 } |
| 50 | 48 |
| 51 void TextInputClientObserver::OnDestruct() { | 49 bool TextInputClientObserver::Send(IPC::Message* message) { |
| 52 delete this; | 50 return GetRenderWidget()->Send(message); |
| 53 } | 51 } |
| 54 | 52 |
| 55 blink::WebView* TextInputClientObserver::webview() { | 53 RenderWidget* TextInputClientObserver::GetRenderWidget() { |
| 56 return render_view()->GetWebView(); | 54 return render_frame_->GetRenderWidget(); |
| 55 } |
| 56 |
| 57 blink::WebView* TextInputClientObserver::GetWebView() { |
| 58 return render_frame_->render_view()->webview(); |
| 59 } |
| 60 |
| 61 int32_t TextInputClientObserver::GetRoutingID() { |
| 62 return GetRenderWidget() ? GetRenderWidget()->routing_id() : MSG_ROUTING_NONE; |
| 57 } | 63 } |
| 58 | 64 |
| 59 void TextInputClientObserver::OnStringAtPoint(gfx::Point point) { | 65 void TextInputClientObserver::OnStringAtPoint(gfx::Point point) { |
| 60 #if defined(OS_MACOSX) | 66 #if defined(OS_MACOSX) |
| 61 blink::WebPoint baselinePoint; | 67 blink::WebPoint baselinePoint; |
| 62 NSAttributedString* string = blink::WebSubstringUtil::attributedWordAtPoint( | 68 NSAttributedString* string = blink::WebSubstringUtil::attributedWordAtPoint( |
| 63 webview(), point, baselinePoint); | 69 GetWebView(), GetRenderWidget()->webwidget(), point, baselinePoint); |
| 64 | 70 |
| 65 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( | 71 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( |
| 66 mac::AttributedStringCoder::Encode(string)); | 72 mac::AttributedStringCoder::Encode(string)); |
| 67 Send(new TextInputClientReplyMsg_GotStringAtPoint( | 73 Send(new TextInputClientReplyMsg_GotStringAtPoint( |
| 68 routing_id(), *encoded.get(), baselinePoint)); | 74 GetRoutingID(), *encoded.get(), baselinePoint)); |
| 69 #else | 75 #else |
| 70 NOTIMPLEMENTED(); | 76 NOTIMPLEMENTED(); |
| 71 #endif | 77 #endif |
| 72 } | 78 } |
| 73 | 79 |
| 74 void TextInputClientObserver::OnCharacterIndexForPoint(gfx::Point point) { | 80 void TextInputClientObserver::OnCharacterIndexForPoint(gfx::Point point) { |
| 75 blink::WebPoint web_point(point); | 81 blink::WebPoint web_point(point); |
| 76 uint32_t index = static_cast<uint32_t>( | 82 uint32_t index = static_cast<uint32_t>( |
| 77 webview()->focusedFrame()->characterIndexForPoint(web_point)); | 83 GetWebView()->focusedFrame()->characterIndexForPoint(web_point)); |
| 78 Send(new TextInputClientReplyMsg_GotCharacterIndexForPoint(routing_id(), | 84 Send(new TextInputClientReplyMsg_GotCharacterIndexForPoint(GetRoutingID(), |
| 79 index)); | 85 index)); |
| 80 } | 86 } |
| 81 | 87 |
| 82 void TextInputClientObserver::OnFirstRectForCharacterRange(gfx::Range range) { | 88 void TextInputClientObserver::OnFirstRectForCharacterRange(gfx::Range range) { |
| 83 gfx::Rect rect; | 89 gfx::Rect rect; |
| 84 #if defined(ENABLE_PLUGINS) | 90 #if defined(ENABLE_PLUGINS) |
| 85 if (render_view_impl_->GetFocusedPepperPlugin()) { | 91 PepperPluginInstanceImpl* focused_plugin = |
| 86 rect = render_view_impl_->GetFocusedPepperPlugin()->GetCaretBounds(); | 92 render_frame_->render_view()->GetFocusedPepperPlugin(); |
| 93 if (focused_plugin) { |
| 94 rect = focused_plugin->GetCaretBounds(); |
| 87 } else | 95 } else |
| 88 #endif | 96 #endif |
| 89 { | 97 { |
| 90 blink::WebLocalFrame* frame = webview()->focusedFrame(); | 98 blink::WebLocalFrame* frame = GetWebView()->focusedFrame(); |
| 91 // TODO(yabinh): Null check should not be necessary. | 99 // TODO(yabinh): Null check should not be necessary. |
| 92 // See crbug.com/304341 | 100 // See crbug.com/304341 |
| 93 if (frame) { | 101 if (frame) { |
| 94 blink::WebRect web_rect; | 102 blink::WebRect web_rect; |
| 95 frame->firstRectForCharacterRange(range.start(), range.length(), | 103 frame->firstRectForCharacterRange(range.start(), range.length(), |
| 96 web_rect); | 104 web_rect); |
| 97 rect = web_rect; | 105 rect = web_rect; |
| 98 } | 106 } |
| 99 } | 107 } |
| 100 Send(new TextInputClientReplyMsg_GotFirstRectForRange(routing_id(), rect)); | 108 Send(new TextInputClientReplyMsg_GotFirstRectForRange(GetRoutingID(), rect)); |
| 101 } | 109 } |
| 102 | 110 |
| 103 void TextInputClientObserver::OnStringForRange(gfx::Range range) { | 111 void TextInputClientObserver::OnStringForRange(gfx::Range range) { |
| 104 #if defined(OS_MACOSX) | 112 #if defined(OS_MACOSX) |
| 105 blink::WebPoint baselinePoint; | 113 blink::WebPoint baselinePoint; |
| 106 NSAttributedString* string = nil; | 114 NSAttributedString* string = nil; |
| 107 blink::WebLocalFrame* frame = webview()->focusedFrame(); | 115 blink::WebLocalFrame* frame = GetWebView()->focusedFrame(); |
| 108 // TODO(yabinh): Null check should not be necessary. | 116 // TODO(yabinh): Null check should not be necessary. |
| 109 // See crbug.com/304341 | 117 // See crbug.com/304341 |
| 110 if (frame) { | 118 if (frame) { |
| 111 string = blink::WebSubstringUtil::attributedSubstringInRange( | 119 string = blink::WebSubstringUtil::attributedSubstringInRange( |
| 112 frame, range.start(), range.length(), &baselinePoint); | 120 frame, range.start(), range.length(), &baselinePoint); |
| 113 } | 121 } |
| 114 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( | 122 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( |
| 115 mac::AttributedStringCoder::Encode(string)); | 123 mac::AttributedStringCoder::Encode(string)); |
| 116 Send(new TextInputClientReplyMsg_GotStringForRange(routing_id(), | 124 Send(new TextInputClientReplyMsg_GotStringForRange( |
| 117 *encoded.get(), baselinePoint)); | 125 GetRoutingID(), *encoded.get(), baselinePoint)); |
| 118 #else | 126 #else |
| 119 NOTIMPLEMENTED(); | 127 NOTIMPLEMENTED(); |
| 120 #endif | 128 #endif |
| 121 } | 129 } |
| 122 | 130 |
| 123 } // namespace content | 131 } // namespace content |
| OLD | NEW |