blob: 87c7486f7b291aed8db0115ee5ffa40698821763 [file] [log] [blame]
[email protected]b25b3ee2012-01-13 05:19:541// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d4cff272011-05-02 15:46:012// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]86a7d3c2011-09-12 16:45:325#include "content/renderer/text_input_client_observer.h"
[email protected]d4cff272011-05-02 15:46:016
7#include "base/memory/scoped_ptr.h"
[email protected]86a7d3c2011-09-12 16:45:328#include "content/common/text_input_client_messages.h"
[email protected]e16c7a12013-12-05 16:30:089#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
[email protected]310ebd6302011-10-10 19:06:2810#include "content/renderer/render_view_impl.h"
[email protected]5c30b5e02013-05-30 03:46:0811#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]c258f1b2014-04-12 09:25:3714#include "third_party/WebKit/public/web/WebLocalFrame.h"
[email protected]2255a9332013-06-17 05:12:3115#include "third_party/WebKit/public/web/WebView.h"
[email protected]c258f1b2014-04-12 09:25:3716#include "third_party/WebKit/public/web/mac/WebSubstringUtil.h"
tfarina3b0452d2014-12-31 15:20:0917#include "ui/gfx/geometry/rect.h"
[email protected]d4cff272011-05-02 15:46:0118
[email protected]e9ff79c2012-10-19 21:31:2619namespace content {
20
[email protected]310ebd6302011-10-10 19:06:2821TextInputClientObserver::TextInputClientObserver(RenderViewImpl* render_view)
[email protected]e9ff79c2012-10-19 21:31:2622 : RenderViewObserver(render_view),
[email protected]b25b3ee2012-01-13 05:19:5423 render_view_impl_(render_view) {
[email protected]d4cff272011-05-02 15:46:0124}
25
26TextInputClientObserver::~TextInputClientObserver() {
27}
28
29bool TextInputClientObserver::OnMessageReceived(const IPC::Message& message) {
30 bool handled = true;
31 IPC_BEGIN_MESSAGE_MAP(TextInputClientObserver, message)
[email protected]6623d872014-03-18 17:43:1332 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringAtPoint,
33 OnStringAtPoint)
[email protected]d4cff272011-05-02 15:46:0134 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]180ef242013-11-07 06:50:4644blink::WebView* TextInputClientObserver::webview() {
[email protected]a2ef54c2011-10-10 16:20:3145 return render_view()->GetWebView();
[email protected]d4cff272011-05-02 15:46:0146}
47
[email protected]6623d872014-03-18 17:43:1348void 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]d4cff272011-05-02 15:46:0163void TextInputClientObserver::OnCharacterIndexForPoint(gfx::Point point) {
[email protected]180ef242013-11-07 06:50:4664 blink::WebPoint web_point(point);
[email protected]d4cff272011-05-02 15:46:0165 size_t index = webview()->focusedFrame()->characterIndexForPoint(web_point);
66 Send(new TextInputClientReplyMsg_GotCharacterIndexForPoint(routing_id(),
67 index));
68}
69
[email protected]db4fc1e2013-09-06 20:01:5170void TextInputClientObserver::OnFirstRectForCharacterRange(gfx::Range range) {
[email protected]b25b3ee2012-01-13 05:19:5471 gfx::Rect rect;
[email protected]ea2fb972013-08-07 05:44:2672#if defined(ENABLE_PLUGINS)
[email protected]e16c7a12013-12-05 16:30:0873 if (render_view_impl_->focused_pepper_plugin()) {
74 rect = render_view_impl_->focused_pepper_plugin()->GetCaretBounds();
75 } else
[email protected]ea2fb972013-08-07 05:44:2676#endif
77 {
[email protected]180ef242013-11-07 06:50:4678 blink::WebFrame* frame = webview()->focusedFrame();
[email protected]1b12cf32013-08-07 23:35:1679 if (frame) {
[email protected]180ef242013-11-07 06:50:4680 blink::WebRect web_rect;
[email protected]1b12cf32013-08-07 23:35:1681 frame->firstRectForCharacterRange(range.start(), range.length(),
82 web_rect);
83 rect = web_rect;
84 }
[email protected]b25b3ee2012-01-13 05:19:5485 }
[email protected]d4cff272011-05-02 15:46:0186 Send(new TextInputClientReplyMsg_GotFirstRectForRange(routing_id(), rect));
87}
88
[email protected]db4fc1e2013-09-06 20:01:5189void TextInputClientObserver::OnStringForRange(gfx::Range range) {
[email protected]d4cff272011-05-02 15:46:0190#if defined(OS_MACOSX)
shuchen3146b3a2015-09-22 16:08:2491 blink::WebPoint baselinePoint;
[email protected]1b12cf32013-08-07 23:35:1692 NSAttributedString* string = nil;
[email protected]c258f1b2014-04-12 09:25:3793 blink::WebLocalFrame* frame = webview()->focusedFrame()->toWebLocalFrame();
[email protected]1b12cf32013-08-07 23:35:1694 if (frame) {
[email protected]180ef242013-11-07 06:50:4695 string = blink::WebSubstringUtil::attributedSubstringInRange(
shuchen3146b3a2015-09-22 16:08:2496 frame, range.start(), range.length(), &baselinePoint);
[email protected]1b12cf32013-08-07 23:35:1697 }
[email protected]d4cff272011-05-02 15:46:0198 scoped_ptr<const mac::AttributedStringCoder::EncodedString> encoded(
99 mac::AttributedStringCoder::Encode(string));
100 Send(new TextInputClientReplyMsg_GotStringForRange(routing_id(),
shuchen3146b3a2015-09-22 16:08:24101 *encoded.get(), baselinePoint));
[email protected]d4cff272011-05-02 15:46:01102#else
103 NOTIMPLEMENTED();
104#endif
105}
[email protected]e9ff79c2012-10-19 21:31:26106
107} // namespace content