• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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/rect.h"
18 
19 namespace content {
20 
TextInputClientObserver(RenderViewImpl * render_view)21 TextInputClientObserver::TextInputClientObserver(RenderViewImpl* render_view)
22     : RenderViewObserver(render_view),
23       render_view_impl_(render_view) {
24 }
25 
~TextInputClientObserver()26 TextInputClientObserver::~TextInputClientObserver() {
27 }
28 
OnMessageReceived(const IPC::Message & message)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;
42 }
43 
webview()44 blink::WebView* TextInputClientObserver::webview() {
45   return render_view()->GetWebView();
46 }
47 
OnStringAtPoint(gfx::Point point)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);
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 
OnCharacterIndexForPoint(gfx::Point point)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));
68 }
69 
OnFirstRectForCharacterRange(gfx::Range range)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
77   {
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;
84     }
85   }
86   Send(new TextInputClientReplyMsg_GotFirstRectForRange(routing_id(), rect));
87 }
88 
OnStringForRange(gfx::Range range)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());
96   }
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
104 }
105 
106 }  // namespace content
107