Home | History | Annotate | Download | only in renderer
      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/mac/WebSubstringUtil.h"
     15 #include "third_party/WebKit/public/web/WebFrame.h"
     16 #include "third_party/WebKit/public/web/WebView.h"
     17 #include "ui/gfx/rect.h"
     18 
     19 namespace content {
     20 
     21 TextInputClientObserver::TextInputClientObserver(RenderViewImpl* render_view)
     22     : RenderViewObserver(render_view),
     23       render_view_impl_(render_view) {
     24 }
     25 
     26 TextInputClientObserver::~TextInputClientObserver() {
     27 }
     28 
     29 bool TextInputClientObserver::OnMessageReceived(const IPC::Message& message) {
     30   bool handled = true;
     31   IPC_BEGIN_MESSAGE_MAP(TextInputClientObserver, message)
     32     IPC_MESSAGE_HANDLER(TextInputClientMsg_CharacterIndexForPoint,
     33                         OnCharacterIndexForPoint)
     34     IPC_MESSAGE_HANDLER(TextInputClientMsg_FirstRectForCharacterRange,
     35                         OnFirstRectForCharacterRange)
     36     IPC_MESSAGE_HANDLER(TextInputClientMsg_StringForRange, OnStringForRange)
     37     IPC_MESSAGE_UNHANDLED(handled = false)
     38   IPC_END_MESSAGE_MAP()
     39   return handled;
     40 }
     41 
     42 blink::WebView* TextInputClientObserver::webview() {
     43   return render_view()->GetWebView();
     44 }
     45 
     46 void TextInputClientObserver::OnCharacterIndexForPoint(gfx::Point point) {
     47   blink::WebPoint web_point(point);
     48   size_t index = webview()->focusedFrame()->characterIndexForPoint(web_point);
     49   Send(new TextInputClientReplyMsg_GotCharacterIndexForPoint(routing_id(),
     50       index));
     51 }
     52 
     53 void TextInputClientObserver::OnFirstRectForCharacterRange(gfx::Range range) {
     54   gfx::Rect rect;
     55 #if defined(ENABLE_PLUGINS)
     56   if (render_view_impl_->focused_pepper_plugin()) {
     57     rect = render_view_impl_->focused_pepper_plugin()->GetCaretBounds();
     58   } else
     59 #endif
     60   {
     61     blink::WebFrame* frame = webview()->focusedFrame();
     62     if (frame) {
     63       blink::WebRect web_rect;
     64       frame->firstRectForCharacterRange(range.start(), range.length(),
     65                                         web_rect);
     66       rect = web_rect;
     67     }
     68   }
     69   Send(new TextInputClientReplyMsg_GotFirstRectForRange(routing_id(), rect));
     70 }
     71 
     72 void TextInputClientObserver::OnStringForRange(gfx::Range range) {
     73 #if defined(OS_MACOSX)
     74   NSAttributedString* string = nil;
     75   blink::WebFrame* frame = webview()->focusedFrame();
     76   if (frame) {
     77     string = blink::WebSubstringUtil::attributedSubstringInRange(
     78         frame, range.start(), range.length());
     79   }
     80   scoped_ptr<const mac::AttributedStringCoder::EncodedString> encoded(
     81       mac::AttributedStringCoder::Encode(string));
     82   Send(new TextInputClientReplyMsg_GotStringForRange(routing_id(),
     83       *encoded.get()));
     84 #else
     85   NOTIMPLEMENTED();
     86 #endif
     87 }
     88 
     89 }  // namespace content
     90