OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/text_input_client_observer.h" | 5 #include "content/renderer/text_input_client_observer.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 #include "content/common/text_input_client_messages.h" | 12 #include "content/common/text_input_client_messages.h" |
13 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" | 13 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
| 14 #include "content/renderer/render_frame_impl.h" |
14 #include "content/renderer/render_view_impl.h" | 15 #include "content/renderer/render_view_impl.h" |
| 16 #include "content/renderer/render_widget.h" |
| 17 #include "ipc/ipc_message.h" |
15 #include "third_party/WebKit/public/platform/WebPoint.h" | 18 #include "third_party/WebKit/public/platform/WebPoint.h" |
16 #include "third_party/WebKit/public/platform/WebRect.h" | 19 #include "third_party/WebKit/public/platform/WebRect.h" |
17 #include "third_party/WebKit/public/platform/WebString.h" | 20 #include "third_party/WebKit/public/platform/WebString.h" |
18 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 21 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
19 #include "third_party/WebKit/public/web/WebView.h" | 22 #include "third_party/WebKit/public/web/WebView.h" |
20 #include "third_party/WebKit/public/web/mac/WebSubstringUtil.h" | 23 #include "third_party/WebKit/public/web/mac/WebSubstringUtil.h" |
21 #include "ui/gfx/geometry/rect.h" | 24 #include "ui/gfx/geometry/rect.h" |
22 | 25 |
23 namespace content { | 26 namespace content { |
24 | 27 |
25 TextInputClientObserver::TextInputClientObserver(RenderViewImpl* render_view) | 28 TextInputClientObserver::TextInputClientObserver(RenderWidget* render_widget) |
26 #if defined(ENABLE_PLUGINS) | 29 : render_widget_(render_widget) {} |
27 : RenderViewObserver(render_view), render_view_impl_(render_view) { | |
28 #else | |
29 : RenderViewObserver(render_view) { | |
30 #endif | |
31 } | |
32 | 30 |
33 TextInputClientObserver::~TextInputClientObserver() { | 31 TextInputClientObserver::~TextInputClientObserver() { |
34 } | 32 } |
35 | 33 |
36 bool TextInputClientObserver::OnMessageReceived(const IPC::Message& message) { | 34 bool TextInputClientObserver::OnMessageReceived(const IPC::Message& message) { |
37 bool handled = true; | 35 bool handled = true; |
38 IPC_BEGIN_MESSAGE_MAP(TextInputClientObserver, message) | 36 IPC_BEGIN_MESSAGE_MAP(TextInputClientObserver, message) |
39 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringAtPoint, | 37 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringAtPoint, |
40 OnStringAtPoint) | 38 OnStringAtPoint) |
41 IPC_MESSAGE_HANDLER(TextInputClientMsg_CharacterIndexForPoint, | 39 IPC_MESSAGE_HANDLER(TextInputClientMsg_CharacterIndexForPoint, |
42 OnCharacterIndexForPoint) | 40 OnCharacterIndexForPoint) |
43 IPC_MESSAGE_HANDLER(TextInputClientMsg_FirstRectForCharacterRange, | 41 IPC_MESSAGE_HANDLER(TextInputClientMsg_FirstRectForCharacterRange, |
44 OnFirstRectForCharacterRange) | 42 OnFirstRectForCharacterRange) |
45 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringForRange, OnStringForRange) | 43 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringForRange, OnStringForRange) |
46 IPC_MESSAGE_UNHANDLED(handled = false) | 44 IPC_MESSAGE_UNHANDLED(handled = false) |
47 IPC_END_MESSAGE_MAP() | 45 IPC_END_MESSAGE_MAP() |
48 return handled; | 46 return handled; |
49 } | 47 } |
50 | 48 |
51 void TextInputClientObserver::OnDestruct() { | 49 bool TextInputClientObserver::Send(IPC::Message* message) { |
52 delete this; | 50 return render_widget_->Send(message); |
53 } | 51 } |
54 | 52 |
55 blink::WebView* TextInputClientObserver::webview() { | 53 blink::WebFrameWidget* TextInputClientObserver::GetWebFrameWidget() const { |
56 return render_view()->GetWebView(); | 54 blink::WebWidget* widget = render_widget_->GetWebWidget(); |
| 55 // We should always receive a WebFrameWidget in the call above. RenderViewImpl |
| 56 // however might return a WebView if the main frame is destroyed, but as long |
| 57 // as there is a rendered page, we should not be in that state and the RVImpl |
| 58 // should be returning a frame widget. |
| 59 DCHECK(widget->isWebFrameWidget()); |
| 60 return static_cast<blink::WebFrameWidget*>(render_widget_->GetWebWidget()); |
57 } | 61 } |
58 | 62 |
| 63 blink::WebLocalFrame* TextInputClientObserver::GetFocusedFrame() const { |
| 64 blink::WebLocalFrame* focused = |
| 65 RenderFrameImpl::FromWebFrame(GetWebFrameWidget()->localRoot()) |
| 66 ->render_view() |
| 67 ->webview() |
| 68 ->focusedFrame(); |
| 69 return focused->localRoot() == GetWebFrameWidget()->localRoot() ? focused |
| 70 : nullptr; |
| 71 } |
| 72 |
| 73 #if defined(ENABLE_PLUGINS) |
| 74 PepperPluginInstanceImpl* TextInputClientObserver::GetFocusedPepperPlugin() |
| 75 const { |
| 76 blink::WebLocalFrame* focusedFrame = GetFocusedFrame(); |
| 77 return focusedFrame |
| 78 ? RenderFrameImpl::FromWebFrame(focusedFrame) |
| 79 ->focused_pepper_plugin() |
| 80 : nullptr; |
| 81 } |
| 82 #endif |
| 83 |
59 void TextInputClientObserver::OnStringAtPoint(gfx::Point point) { | 84 void TextInputClientObserver::OnStringAtPoint(gfx::Point point) { |
60 #if defined(OS_MACOSX) | 85 #if defined(OS_MACOSX) |
61 blink::WebPoint baselinePoint; | 86 blink::WebPoint baselinePoint; |
62 NSAttributedString* string = blink::WebSubstringUtil::attributedWordAtPoint( | 87 NSAttributedString* string = blink::WebSubstringUtil::attributedWordAtPoint( |
63 webview(), point, baselinePoint); | 88 GetWebFrameWidget(), point, baselinePoint); |
64 | 89 |
65 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( | 90 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( |
66 mac::AttributedStringCoder::Encode(string)); | 91 mac::AttributedStringCoder::Encode(string)); |
67 Send(new TextInputClientReplyMsg_GotStringAtPoint( | 92 Send(new TextInputClientReplyMsg_GotStringAtPoint( |
68 routing_id(), *encoded.get(), baselinePoint)); | 93 render_widget_->routing_id(), *encoded.get(), baselinePoint)); |
69 #else | 94 #else |
70 NOTIMPLEMENTED(); | 95 NOTIMPLEMENTED(); |
71 #endif | 96 #endif |
72 } | 97 } |
73 | 98 |
74 void TextInputClientObserver::OnCharacterIndexForPoint(gfx::Point point) { | 99 void TextInputClientObserver::OnCharacterIndexForPoint(gfx::Point point) { |
75 blink::WebPoint web_point(point); | 100 blink::WebPoint web_point(point); |
76 uint32_t index = static_cast<uint32_t>( | 101 uint32_t index = static_cast<uint32_t>( |
77 webview()->focusedFrame()->characterIndexForPoint(web_point)); | 102 GetFocusedFrame()->characterIndexForPoint(web_point)); |
78 Send(new TextInputClientReplyMsg_GotCharacterIndexForPoint(routing_id(), | 103 Send(new TextInputClientReplyMsg_GotCharacterIndexForPoint( |
79 index)); | 104 render_widget_->routing_id(), index)); |
80 } | 105 } |
81 | 106 |
82 void TextInputClientObserver::OnFirstRectForCharacterRange(gfx::Range range) { | 107 void TextInputClientObserver::OnFirstRectForCharacterRange(gfx::Range range) { |
83 gfx::Rect rect; | 108 gfx::Rect rect; |
84 #if defined(ENABLE_PLUGINS) | 109 #if defined(ENABLE_PLUGINS) |
85 if (render_view_impl_->GetFocusedPepperPlugin()) { | 110 PepperPluginInstanceImpl* focused_plugin = GetFocusedPepperPlugin(); |
86 rect = render_view_impl_->GetFocusedPepperPlugin()->GetCaretBounds(); | 111 if (focused_plugin) { |
| 112 rect = focused_plugin->GetCaretBounds(); |
87 } else | 113 } else |
88 #endif | 114 #endif |
89 { | 115 { |
90 blink::WebLocalFrame* frame = webview()->focusedFrame(); | 116 blink::WebLocalFrame* frame = GetFocusedFrame(); |
91 // TODO(yabinh): Null check should not be necessary. | 117 // TODO(yabinh): Null check should not be necessary. |
92 // See crbug.com/304341 | 118 // See crbug.com/304341 |
93 if (frame) { | 119 if (frame) { |
94 blink::WebRect web_rect; | 120 blink::WebRect web_rect; |
95 frame->firstRectForCharacterRange(range.start(), range.length(), | 121 frame->firstRectForCharacterRange(range.start(), range.length(), |
96 web_rect); | 122 web_rect); |
97 rect = web_rect; | 123 rect = web_rect; |
98 } | 124 } |
99 } | 125 } |
100 Send(new TextInputClientReplyMsg_GotFirstRectForRange(routing_id(), rect)); | 126 Send(new TextInputClientReplyMsg_GotFirstRectForRange( |
| 127 render_widget_->routing_id(), rect)); |
101 } | 128 } |
102 | 129 |
103 void TextInputClientObserver::OnStringForRange(gfx::Range range) { | 130 void TextInputClientObserver::OnStringForRange(gfx::Range range) { |
104 #if defined(OS_MACOSX) | 131 #if defined(OS_MACOSX) |
105 blink::WebPoint baselinePoint; | 132 blink::WebPoint baselinePoint; |
106 NSAttributedString* string = nil; | 133 NSAttributedString* string = nil; |
107 blink::WebLocalFrame* frame = webview()->focusedFrame(); | 134 blink::WebLocalFrame* frame = GetFocusedFrame(); |
108 // TODO(yabinh): Null check should not be necessary. | 135 // TODO(yabinh): Null check should not be necessary. |
109 // See crbug.com/304341 | 136 // See crbug.com/304341 |
110 if (frame) { | 137 if (frame) { |
111 string = blink::WebSubstringUtil::attributedSubstringInRange( | 138 string = blink::WebSubstringUtil::attributedSubstringInRange( |
112 frame, range.start(), range.length(), &baselinePoint); | 139 frame, range.start(), range.length(), &baselinePoint); |
113 } | 140 } |
114 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( | 141 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( |
115 mac::AttributedStringCoder::Encode(string)); | 142 mac::AttributedStringCoder::Encode(string)); |
116 Send(new TextInputClientReplyMsg_GotStringForRange(routing_id(), | 143 Send(new TextInputClientReplyMsg_GotStringForRange( |
117 *encoded.get(), baselinePoint)); | 144 render_widget_->routing_id(), *encoded.get(), baselinePoint)); |
118 #else | 145 #else |
119 NOTIMPLEMENTED(); | 146 NOTIMPLEMENTED(); |
120 #endif | 147 #endif |
121 } | 148 } |
122 | 149 |
123 } // namespace content | 150 } // namespace content |
OLD | NEW |