| 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_view_impl.h" | 14 #include "content/renderer/render_widget.h" |
| 15 #include "ipc/ipc_message.h" |
| 15 #include "third_party/WebKit/public/platform/WebPoint.h" | 16 #include "third_party/WebKit/public/platform/WebPoint.h" |
| 16 #include "third_party/WebKit/public/platform/WebRect.h" | 17 #include "third_party/WebKit/public/platform/WebRect.h" |
| 17 #include "third_party/WebKit/public/platform/WebString.h" | 18 #include "third_party/WebKit/public/platform/WebString.h" |
| 18 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 19 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 19 #include "third_party/WebKit/public/web/WebView.h" | 20 #include "third_party/WebKit/public/web/WebView.h" |
| 20 #include "third_party/WebKit/public/web/mac/WebSubstringUtil.h" | 21 #include "third_party/WebKit/public/web/mac/WebSubstringUtil.h" |
| 21 #include "ui/gfx/geometry/rect.h" | 22 #include "ui/gfx/geometry/rect.h" |
| 22 | 23 |
| 23 namespace content { | 24 namespace content { |
| 24 | 25 |
| 25 TextInputClientObserver::TextInputClientObserver(RenderViewImpl* render_view) | 26 TextInputClientObserver::TextInputClientObserver( |
| 26 #if defined(ENABLE_PLUGINS) | 27 scoped_refptr<RenderWidget> render_widget, |
| 27 : RenderViewObserver(render_view), render_view_impl_(render_view) { | 28 blink::WebView* web_view) |
| 28 #else | 29 : render_widget_(render_widget), web_view_(web_view) {} |
| 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 DCHECK(render_widget_); |
| 51 return render_widget_->Send(message); |
| 53 } | 52 } |
| 54 | 53 |
| 55 blink::WebView* TextInputClientObserver::webview() { | 54 blink::WebView* TextInputClientObserver::webview() { |
| 56 return render_view()->GetWebView(); | 55 return web_view_; |
| 56 } |
| 57 |
| 58 int32_t TextInputClientObserver::GetRoutingID() { |
| 59 DCHECK(render_widget_); |
| 60 return render_widget_->routing_id(); |
| 57 } | 61 } |
| 58 | 62 |
| 59 void TextInputClientObserver::OnStringAtPoint(gfx::Point point) { | 63 void TextInputClientObserver::OnStringAtPoint(gfx::Point point) { |
| 60 #if defined(OS_MACOSX) | 64 #if defined(OS_MACOSX) |
| 61 blink::WebPoint baselinePoint; | 65 blink::WebPoint baselinePoint; |
| 62 NSAttributedString* string = blink::WebSubstringUtil::attributedWordAtPoint( | 66 NSAttributedString* string = blink::WebSubstringUtil::attributedWordAtPoint( |
| 63 webview(), point, baselinePoint); | 67 webview(), render_widget_->webwidget(), point, baselinePoint); |
| 64 | 68 |
| 65 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( | 69 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( |
| 66 mac::AttributedStringCoder::Encode(string)); | 70 mac::AttributedStringCoder::Encode(string)); |
| 67 Send(new TextInputClientReplyMsg_GotStringAtPoint( | 71 Send(new TextInputClientReplyMsg_GotStringAtPoint( |
| 68 routing_id(), *encoded.get(), baselinePoint)); | 72 GetRoutingID(), *encoded.get(), baselinePoint)); |
| 69 #else | 73 #else |
| 70 NOTIMPLEMENTED(); | 74 NOTIMPLEMENTED(); |
| 71 #endif | 75 #endif |
| 72 } | 76 } |
| 73 | 77 |
| 74 void TextInputClientObserver::OnCharacterIndexForPoint(gfx::Point point) { | 78 void TextInputClientObserver::OnCharacterIndexForPoint(gfx::Point point) { |
| 75 blink::WebPoint web_point(point); | 79 blink::WebPoint web_point(point); |
| 76 uint32_t index = static_cast<uint32_t>( | 80 uint32_t index = static_cast<uint32_t>( |
| 77 webview()->focusedFrame()->characterIndexForPoint(web_point)); | 81 webview()->focusedFrame()->characterIndexForPoint(web_point)); |
| 78 Send(new TextInputClientReplyMsg_GotCharacterIndexForPoint(routing_id(), | 82 Send(new TextInputClientReplyMsg_GotCharacterIndexForPoint(GetRoutingID(), |
| 79 index)); | 83 index)); |
| 80 } | 84 } |
| 81 | 85 |
| 82 void TextInputClientObserver::OnFirstRectForCharacterRange(gfx::Range range) { | 86 void TextInputClientObserver::OnFirstRectForCharacterRange(gfx::Range range) { |
| 83 gfx::Rect rect; | 87 gfx::Rect rect; |
| 84 #if defined(ENABLE_PLUGINS) | 88 #if defined(ENABLE_PLUGINS) |
| 85 if (render_view_impl_->GetFocusedPepperPlugin()) { | 89 if (render_widget_->focused_pepper_plugin()) { |
| 86 rect = render_view_impl_->GetFocusedPepperPlugin()->GetCaretBounds(); | 90 rect = render_widget_->focused_pepper_plugin()->GetCaretBounds(); |
| 87 } else | 91 } else |
| 88 #endif | 92 #endif |
| 89 { | 93 { |
| 90 blink::WebLocalFrame* frame = webview()->focusedFrame(); | 94 blink::WebLocalFrame* frame = webview()->focusedFrame(); |
| 91 // TODO(yabinh): Null check should not be necessary. | 95 // TODO(yabinh): Null check should not be necessary. |
| 92 // See crbug.com/304341 | 96 // See crbug.com/304341 |
| 93 if (frame) { | 97 if (frame) { |
| 94 blink::WebRect web_rect; | 98 blink::WebRect web_rect; |
| 95 frame->firstRectForCharacterRange(range.start(), range.length(), | 99 frame->firstRectForCharacterRange(range.start(), range.length(), |
| 96 web_rect); | 100 web_rect); |
| 97 rect = web_rect; | 101 rect = web_rect; |
| 98 } | 102 } |
| 99 } | 103 } |
| 100 Send(new TextInputClientReplyMsg_GotFirstRectForRange(routing_id(), rect)); | 104 Send(new TextInputClientReplyMsg_GotFirstRectForRange(GetRoutingID(), rect)); |
| 101 } | 105 } |
| 102 | 106 |
| 103 void TextInputClientObserver::OnStringForRange(gfx::Range range) { | 107 void TextInputClientObserver::OnStringForRange(gfx::Range range) { |
| 104 #if defined(OS_MACOSX) | 108 #if defined(OS_MACOSX) |
| 105 blink::WebPoint baselinePoint; | 109 blink::WebPoint baselinePoint; |
| 106 NSAttributedString* string = nil; | 110 NSAttributedString* string = nil; |
| 107 blink::WebLocalFrame* frame = webview()->focusedFrame(); | 111 blink::WebLocalFrame* frame = webview()->focusedFrame(); |
| 108 // TODO(yabinh): Null check should not be necessary. | 112 // TODO(yabinh): Null check should not be necessary. |
| 109 // See crbug.com/304341 | 113 // See crbug.com/304341 |
| 110 if (frame) { | 114 if (frame) { |
| 111 string = blink::WebSubstringUtil::attributedSubstringInRange( | 115 string = blink::WebSubstringUtil::attributedSubstringInRange( |
| 112 frame, range.start(), range.length(), &baselinePoint); | 116 frame, range.start(), range.length(), &baselinePoint); |
| 113 } | 117 } |
| 114 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( | 118 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( |
| 115 mac::AttributedStringCoder::Encode(string)); | 119 mac::AttributedStringCoder::Encode(string)); |
| 116 Send(new TextInputClientReplyMsg_GotStringForRange(routing_id(), | 120 Send(new TextInputClientReplyMsg_GotStringForRange( |
| 117 *encoded.get(), baselinePoint)); | 121 GetRoutingID(), *encoded.get(), baselinePoint)); |
| 118 #else | 122 #else |
| 119 NOTIMPLEMENTED(); | 123 NOTIMPLEMENTED(); |
| 120 #endif | 124 #endif |
| 121 } | 125 } |
| 122 | 126 |
| 123 } // namespace content | 127 } // namespace content |
| OLD | NEW |