[email protected] | b25b3ee | 2012-01-13 05:19:54 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 86a7d3c | 2011-09-12 16:45:32 | [diff] [blame] | 5 | #include "content/renderer/text_input_client_observer.h" |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 6 | |
| 7 | #include "base/memory/scoped_ptr.h" |
[email protected] | 86a7d3c | 2011-09-12 16:45:32 | [diff] [blame] | 8 | #include "content/common/text_input_client_messages.h" |
[email protected] | e16c7a1 | 2013-12-05 16:30:08 | [diff] [blame] | 9 | #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
[email protected] | 310ebd630 | 2011-10-10 19:06:28 | [diff] [blame] | 10 | #include "content/renderer/render_view_impl.h" |
[email protected] | 5c30b5e0 | 2013-05-30 03:46:08 | [diff] [blame] | 11 | #include "third_party/WebKit/public/platform/WebPoint.h" |
| 12 | #include "third_party/WebKit/public/platform/WebRect.h" |
| 13 | #include "third_party/WebKit/public/platform/WebString.h" |
[email protected] | c258f1b | 2014-04-12 09:25:37 | [diff] [blame] | 14 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 15 | #include "third_party/WebKit/public/web/WebView.h" |
[email protected] | c258f1b | 2014-04-12 09:25:37 | [diff] [blame] | 16 | #include "third_party/WebKit/public/web/mac/WebSubstringUtil.h" |
tfarina | 3b0452d | 2014-12-31 15:20:09 | [diff] [blame] | 17 | #include "ui/gfx/geometry/rect.h" |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 18 | |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 19 | namespace content { |
| 20 | |
[email protected] | 310ebd630 | 2011-10-10 19:06:28 | [diff] [blame] | 21 | TextInputClientObserver::TextInputClientObserver(RenderViewImpl* render_view) |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 22 | : RenderViewObserver(render_view), |
[email protected] | b25b3ee | 2012-01-13 05:19:54 | [diff] [blame] | 23 | render_view_impl_(render_view) { |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | TextInputClientObserver::~TextInputClientObserver() { |
| 27 | } |
| 28 | |
| 29 | bool TextInputClientObserver::OnMessageReceived(const IPC::Message& message) { |
| 30 | bool handled = true; |
| 31 | IPC_BEGIN_MESSAGE_MAP(TextInputClientObserver, message) |
[email protected] | 6623d87 | 2014-03-18 17:43:13 | [diff] [blame] | 32 | IPC_MESSAGE_HANDLER(TextInputClientMsg_StringAtPoint, |
| 33 | OnStringAtPoint) |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 34 | IPC_MESSAGE_HANDLER(TextInputClientMsg_CharacterIndexForPoint, |
| 35 | OnCharacterIndexForPoint) |
| 36 | IPC_MESSAGE_HANDLER(TextInputClientMsg_FirstRectForCharacterRange, |
| 37 | OnFirstRectForCharacterRange) |
| 38 | IPC_MESSAGE_HANDLER(TextInputClientMsg_StringForRange, OnStringForRange) |
| 39 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 40 | IPC_END_MESSAGE_MAP() |
| 41 | return handled; |
| 42 | } |
| 43 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 44 | blink::WebView* TextInputClientObserver::webview() { |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 45 | return render_view()->GetWebView(); |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | 6623d87 | 2014-03-18 17:43:13 | [diff] [blame] | 48 | void TextInputClientObserver::OnStringAtPoint(gfx::Point point) { |
| 49 | #if defined(OS_MACOSX) |
| 50 | blink::WebPoint baselinePoint; |
| 51 | NSAttributedString* string = blink::WebSubstringUtil::attributedWordAtPoint( |
| 52 | webview(), point, baselinePoint); |
| 53 | |
| 54 | scoped_ptr<const mac::AttributedStringCoder::EncodedString> encoded( |
| 55 | mac::AttributedStringCoder::Encode(string)); |
| 56 | Send(new TextInputClientReplyMsg_GotStringAtPoint( |
| 57 | routing_id(), *encoded.get(), baselinePoint)); |
| 58 | #else |
| 59 | NOTIMPLEMENTED(); |
| 60 | #endif |
| 61 | } |
| 62 | |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 63 | void TextInputClientObserver::OnCharacterIndexForPoint(gfx::Point point) { |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 64 | blink::WebPoint web_point(point); |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 65 | size_t index = webview()->focusedFrame()->characterIndexForPoint(web_point); |
| 66 | Send(new TextInputClientReplyMsg_GotCharacterIndexForPoint(routing_id(), |
| 67 | index)); |
| 68 | } |
| 69 | |
[email protected] | db4fc1e | 2013-09-06 20:01:51 | [diff] [blame] | 70 | void TextInputClientObserver::OnFirstRectForCharacterRange(gfx::Range range) { |
[email protected] | b25b3ee | 2012-01-13 05:19:54 | [diff] [blame] | 71 | gfx::Rect rect; |
[email protected] | ea2fb97 | 2013-08-07 05:44:26 | [diff] [blame] | 72 | #if defined(ENABLE_PLUGINS) |
[email protected] | e16c7a1 | 2013-12-05 16:30:08 | [diff] [blame] | 73 | if (render_view_impl_->focused_pepper_plugin()) { |
| 74 | rect = render_view_impl_->focused_pepper_plugin()->GetCaretBounds(); |
| 75 | } else |
[email protected] | ea2fb97 | 2013-08-07 05:44:26 | [diff] [blame] | 76 | #endif |
| 77 | { |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 78 | blink::WebFrame* frame = webview()->focusedFrame(); |
[email protected] | 1b12cf3 | 2013-08-07 23:35:16 | [diff] [blame] | 79 | if (frame) { |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 80 | blink::WebRect web_rect; |
[email protected] | 1b12cf3 | 2013-08-07 23:35:16 | [diff] [blame] | 81 | frame->firstRectForCharacterRange(range.start(), range.length(), |
| 82 | web_rect); |
| 83 | rect = web_rect; |
| 84 | } |
[email protected] | b25b3ee | 2012-01-13 05:19:54 | [diff] [blame] | 85 | } |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 86 | Send(new TextInputClientReplyMsg_GotFirstRectForRange(routing_id(), rect)); |
| 87 | } |
| 88 | |
[email protected] | db4fc1e | 2013-09-06 20:01:51 | [diff] [blame] | 89 | void TextInputClientObserver::OnStringForRange(gfx::Range range) { |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 90 | #if defined(OS_MACOSX) |
shuchen | 3146b3a | 2015-09-22 16:08:24 | [diff] [blame] | 91 | blink::WebPoint baselinePoint; |
[email protected] | 1b12cf3 | 2013-08-07 23:35:16 | [diff] [blame] | 92 | NSAttributedString* string = nil; |
[email protected] | c258f1b | 2014-04-12 09:25:37 | [diff] [blame] | 93 | blink::WebLocalFrame* frame = webview()->focusedFrame()->toWebLocalFrame(); |
[email protected] | 1b12cf3 | 2013-08-07 23:35:16 | [diff] [blame] | 94 | if (frame) { |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 95 | string = blink::WebSubstringUtil::attributedSubstringInRange( |
shuchen | 3146b3a | 2015-09-22 16:08:24 | [diff] [blame] | 96 | frame, range.start(), range.length(), &baselinePoint); |
[email protected] | 1b12cf3 | 2013-08-07 23:35:16 | [diff] [blame] | 97 | } |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 98 | scoped_ptr<const mac::AttributedStringCoder::EncodedString> encoded( |
| 99 | mac::AttributedStringCoder::Encode(string)); |
| 100 | Send(new TextInputClientReplyMsg_GotStringForRange(routing_id(), |
shuchen | 3146b3a | 2015-09-22 16:08:24 | [diff] [blame] | 101 | *encoded.get(), baselinePoint)); |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 102 | #else |
| 103 | NOTIMPLEMENTED(); |
| 104 | #endif |
| 105 | } |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 106 | |
| 107 | } // namespace content |