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/browser/renderer_host/text_input_client_message_filter.h"
6
7#include "base/strings/string16.h"
8#include "content/browser/renderer_host/render_view_host_impl.h"
9#include "content/browser/renderer_host/text_input_client_mac.h"
10#include "content/common/text_input_client_messages.h"
11#include "content/public/browser/render_widget_host_view.h"
12#include "ipc/ipc_message_macros.h"
13#include "ui/gfx/point.h"
14#include "ui/gfx/range/range.h"
15
16namespace content {
17
18TextInputClientMessageFilter::TextInputClientMessageFilter(int child_id)
19    : BrowserMessageFilter(TextInputClientMsgStart),
20      child_process_id_(child_id) {
21}
22
23bool TextInputClientMessageFilter::OnMessageReceived(
24    const IPC::Message& message) {
25  bool handled = true;
26  IPC_BEGIN_MESSAGE_MAP(TextInputClientMessageFilter, message)
27    IPC_MESSAGE_HANDLER(TextInputClientReplyMsg_GotStringAtPoint,
28                        OnGotStringAtPoint)
29    IPC_MESSAGE_HANDLER(TextInputClientReplyMsg_GotCharacterIndexForPoint,
30                        OnGotCharacterIndexForPoint)
31    IPC_MESSAGE_HANDLER(TextInputClientReplyMsg_GotFirstRectForRange,
32                        OnGotFirstRectForRange)
33    IPC_MESSAGE_HANDLER(TextInputClientReplyMsg_GotStringForRange,
34                        OnGotStringFromRange)
35    IPC_MESSAGE_UNHANDLED(handled = false)
36  IPC_END_MESSAGE_MAP()
37  return handled;
38}
39
40TextInputClientMessageFilter::~TextInputClientMessageFilter() {}
41
42void TextInputClientMessageFilter::OnGotStringAtPoint(
43    const mac::AttributedStringCoder::EncodedString& encoded_string,
44    const gfx::Point& point) {
45  TextInputClientMac* service = TextInputClientMac::GetInstance();
46  NSAttributedString* string =
47      mac::AttributedStringCoder::Decode(&encoded_string);
48  service->GetStringAtPointReply(string, NSPointFromCGPoint(point.ToCGPoint()));
49}
50
51void TextInputClientMessageFilter::OnGotCharacterIndexForPoint(size_t index) {
52  TextInputClientMac* service = TextInputClientMac::GetInstance();
53  // |index| could be WTF::notFound (-1) and its value is different from
54  // NSNotFound so we need to convert it.
55  if (index == static_cast<size_t>(-1)) {
56    index = NSNotFound;
57  }
58  service->SetCharacterIndexAndSignal(index);
59}
60
61void TextInputClientMessageFilter::OnGotFirstRectForRange(
62    const gfx::Rect& rect) {
63  TextInputClientMac* service = TextInputClientMac::GetInstance();
64  service->SetFirstRectAndSignal(NSRectFromCGRect(rect.ToCGRect()));
65}
66
67void TextInputClientMessageFilter::OnGotStringFromRange(
68    const mac::AttributedStringCoder::EncodedString& encoded_string) {
69  TextInputClientMac* service = TextInputClientMac::GetInstance();
70  NSAttributedString* string =
71      mac::AttributedStringCoder::Decode(&encoded_string);
72  if (![string length])
73    string = nil;
74  service->SetSubstringAndSignal(string);
75}
76
77}  // namespace content
78