Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / text_input_client_observer.cc
blob049f87c5e025f26fdb51d285074d7d3d5f5a16ea
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.
5 #include "content/renderer/text_input_client_observer.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "content/common/text_input_client_messages.h"
9 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
10 #include "content/renderer/render_view_impl.h"
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"
14 #include "third_party/WebKit/public/web/WebLocalFrame.h"
15 #include "third_party/WebKit/public/web/WebView.h"
16 #include "third_party/WebKit/public/web/mac/WebSubstringUtil.h"
17 #include "ui/gfx/geometry/rect.h"
19 namespace content {
21 TextInputClientObserver::TextInputClientObserver(RenderViewImpl* render_view)
22 : RenderViewObserver(render_view),
23 render_view_impl_(render_view) {
26 TextInputClientObserver::~TextInputClientObserver() {
29 bool TextInputClientObserver::OnMessageReceived(const IPC::Message& message) {
30 bool handled = true;
31 IPC_BEGIN_MESSAGE_MAP(TextInputClientObserver, message)
32 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringAtPoint,
33 OnStringAtPoint)
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;
44 blink::WebView* TextInputClientObserver::webview() {
45 return render_view()->GetWebView();
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);
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
63 void TextInputClientObserver::OnCharacterIndexForPoint(gfx::Point point) {
64 blink::WebPoint web_point(point);
65 size_t index = webview()->focusedFrame()->characterIndexForPoint(web_point);
66 Send(new TextInputClientReplyMsg_GotCharacterIndexForPoint(routing_id(),
67 index));
70 void TextInputClientObserver::OnFirstRectForCharacterRange(gfx::Range range) {
71 gfx::Rect rect;
72 #if defined(ENABLE_PLUGINS)
73 if (render_view_impl_->focused_pepper_plugin()) {
74 rect = render_view_impl_->focused_pepper_plugin()->GetCaretBounds();
75 } else
76 #endif
78 blink::WebFrame* frame = webview()->focusedFrame();
79 if (frame) {
80 blink::WebRect web_rect;
81 frame->firstRectForCharacterRange(range.start(), range.length(),
82 web_rect);
83 rect = web_rect;
86 Send(new TextInputClientReplyMsg_GotFirstRectForRange(routing_id(), rect));
89 void TextInputClientObserver::OnStringForRange(gfx::Range range) {
90 #if defined(OS_MACOSX)
91 NSAttributedString* string = nil;
92 blink::WebLocalFrame* frame = webview()->focusedFrame()->toWebLocalFrame();
93 if (frame) {
94 string = blink::WebSubstringUtil::attributedSubstringInRange(
95 frame, range.start(), range.length());
97 scoped_ptr<const mac::AttributedStringCoder::EncodedString> encoded(
98 mac::AttributedStringCoder::Encode(string));
99 Send(new TextInputClientReplyMsg_GotStringForRange(routing_id(),
100 *encoded.get()));
101 #else
102 NOTIMPLEMENTED();
103 #endif
106 } // namespace content