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"
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
) {
31 IPC_BEGIN_MESSAGE_MAP(TextInputClientObserver
, message
)
32 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringAtPoint
,
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)
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
));
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(),
70 void TextInputClientObserver::OnFirstRectForCharacterRange(gfx::Range range
) {
72 #if defined(ENABLE_PLUGINS)
73 if (render_view_impl_
->focused_pepper_plugin()) {
74 rect
= render_view_impl_
->focused_pepper_plugin()->GetCaretBounds();
78 blink::WebFrame
* frame
= webview()->focusedFrame();
80 blink::WebRect web_rect
;
81 frame
->firstRectForCharacterRange(range
.start(), range
.length(),
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();
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(),
106 } // namespace content