[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] | 310ebd630 | 2011-10-10 19:06:28 | [diff] [blame] | 9 | #include "content/renderer/render_view_impl.h" |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 10 | #include "third_party/WebKit/Source/WebKit/chromium/public/mac/WebSubstringUtil.h" |
| 11 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
[email protected] | e6e90dc | 2011-12-03 00:01:37 | [diff] [blame] | 12 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPoint.h" |
| 13 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" |
| 14 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 15 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 16 | #include "ui/gfx/rect.h" |
| 17 | |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 18 | namespace content { |
| 19 | |
[email protected] | 310ebd630 | 2011-10-10 19:06:28 | [diff] [blame] | 20 | TextInputClientObserver::TextInputClientObserver(RenderViewImpl* render_view) |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 21 | : RenderViewObserver(render_view), |
[email protected] | b25b3ee | 2012-01-13 05:19:54 | [diff] [blame] | 22 | render_view_impl_(render_view) { |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | TextInputClientObserver::~TextInputClientObserver() { |
| 26 | } |
| 27 | |
| 28 | bool TextInputClientObserver::OnMessageReceived(const IPC::Message& message) { |
| 29 | bool handled = true; |
| 30 | IPC_BEGIN_MESSAGE_MAP(TextInputClientObserver, message) |
| 31 | IPC_MESSAGE_HANDLER(TextInputClientMsg_CharacterIndexForPoint, |
| 32 | OnCharacterIndexForPoint) |
| 33 | IPC_MESSAGE_HANDLER(TextInputClientMsg_FirstRectForCharacterRange, |
| 34 | OnFirstRectForCharacterRange) |
| 35 | IPC_MESSAGE_HANDLER(TextInputClientMsg_StringForRange, OnStringForRange) |
| 36 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 37 | IPC_END_MESSAGE_MAP() |
| 38 | return handled; |
| 39 | } |
| 40 | |
| 41 | WebKit::WebView* TextInputClientObserver::webview() { |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 42 | return render_view()->GetWebView(); |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void TextInputClientObserver::OnCharacterIndexForPoint(gfx::Point point) { |
| 46 | WebKit::WebPoint web_point(point); |
| 47 | size_t index = webview()->focusedFrame()->characterIndexForPoint(web_point); |
| 48 | Send(new TextInputClientReplyMsg_GotCharacterIndexForPoint(routing_id(), |
| 49 | index)); |
| 50 | } |
| 51 | |
| 52 | void TextInputClientObserver::OnFirstRectForCharacterRange(ui::Range range) { |
[email protected] | b25b3ee | 2012-01-13 05:19:54 | [diff] [blame] | 53 | gfx::Rect rect; |
| 54 | if (!render_view_impl_->GetPpapiPluginCaretBounds(&rect)) { |
| 55 | WebKit::WebFrame* frame = webview()->focusedFrame(); |
| 56 | WebKit::WebRect web_rect; |
| 57 | frame->firstRectForCharacterRange(range.start(), range.length(), web_rect); |
| 58 | rect = web_rect; |
| 59 | } |
[email protected] | d4cff27 | 2011-05-02 15:46:01 | [diff] [blame] | 60 | Send(new TextInputClientReplyMsg_GotFirstRectForRange(routing_id(), rect)); |
| 61 | } |
| 62 | |
| 63 | void TextInputClientObserver::OnStringForRange(ui::Range range) { |
| 64 | #if defined(OS_MACOSX) |
| 65 | NSAttributedString* string = |
| 66 | WebKit::WebSubstringUtil::attributedSubstringInRange( |
| 67 | webview()->focusedFrame(), range.start(), range.length()); |
| 68 | scoped_ptr<const mac::AttributedStringCoder::EncodedString> encoded( |
| 69 | mac::AttributedStringCoder::Encode(string)); |
| 70 | Send(new TextInputClientReplyMsg_GotStringForRange(routing_id(), |
| 71 | *encoded.get())); |
| 72 | #else |
| 73 | NOTIMPLEMENTED(); |
| 74 | #endif |
| 75 | } |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 76 | |
| 77 | } // namespace content |