Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/renderer/text_input_client_observer.h" |
| 6 | |
| 7 | #include "base/memory/scoped_ptr.h" |
| 8 | #include "content/common/text_input_client_messages.h" |
| 9 | #include "content/renderer/render_view_impl.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 10 | #include "third_party/WebKit/public/platform/WebPoint.h" |
| 11 | #include "third_party/WebKit/public/platform/WebRect.h" |
| 12 | #include "third_party/WebKit/public/platform/WebString.h" |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 13 | #include "third_party/WebKit/public/web/mac/WebSubstringUtil.h" |
| 14 | #include "third_party/WebKit/public/web/WebFrame.h" |
| 15 | #include "third_party/WebKit/public/web/WebView.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 16 | #include "ui/gfx/rect.h" |
| 17 | |
| 18 | namespace content { |
| 19 | |
| 20 | TextInputClientObserver::TextInputClientObserver(RenderViewImpl* render_view) |
| 21 | : RenderViewObserver(render_view), |
| 22 | render_view_impl_(render_view) { |
| 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() { |
| 42 | return render_view()->GetWebView(); |
| 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) { |
| 53 | gfx::Rect rect; |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 54 | #if defined(ENABLE_PLUGINS) |
| 55 | if (!render_view_impl_->GetPepperCaretBounds(&rect)) |
| 56 | #endif |
| 57 | { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 58 | WebKit::WebFrame* frame = webview()->focusedFrame(); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 59 | if (frame) { |
| 60 | WebKit::WebRect web_rect; |
| 61 | frame->firstRectForCharacterRange(range.start(), range.length(), |
| 62 | web_rect); |
| 63 | rect = web_rect; |
| 64 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 65 | } |
| 66 | Send(new TextInputClientReplyMsg_GotFirstRectForRange(routing_id(), rect)); |
| 67 | } |
| 68 | |
| 69 | void TextInputClientObserver::OnStringForRange(ui::Range range) { |
| 70 | #if defined(OS_MACOSX) |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 71 | NSAttributedString* string = nil; |
| 72 | WebKit::WebFrame* frame = webview()->focusedFrame(); |
| 73 | if (frame) { |
| 74 | string = WebKit::WebSubstringUtil::attributedSubstringInRange( |
| 75 | frame, range.start(), range.length()); |
| 76 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 77 | scoped_ptr<const mac::AttributedStringCoder::EncodedString> encoded( |
| 78 | mac::AttributedStringCoder::Encode(string)); |
| 79 | Send(new TextInputClientReplyMsg_GotStringForRange(routing_id(), |
| 80 | *encoded.get())); |
| 81 | #else |
| 82 | NOTIMPLEMENTED(); |
| 83 | #endif |
| 84 | } |
| 85 | |
| 86 | } // namespace content |