1 // Copyright (c) 2011 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.
5 #include "chrome/renderer/searchbox.h"
7 #include "chrome/common/render_messages.h"
8 #include "chrome/renderer/searchbox_extension.h"
9 #include "content/public/renderer/render_view.h"
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
12 using WebKit::WebView
;
14 SearchBox::SearchBox(content::RenderView
* render_view
)
15 : content::RenderViewObserver(render_view
),
16 content::RenderViewObserverTracker
<SearchBox
>(render_view
),
22 SearchBox::~SearchBox() {
25 void SearchBox::SetSuggestions(const std::vector
<std::string
>& suggestions
,
26 InstantCompleteBehavior behavior
) {
27 // Explicitly allow empty vector to be sent to the browser.
28 render_view()->Send(new ChromeViewHostMsg_SetSuggestions(
29 render_view()->GetRoutingId(), render_view()->GetPageId(), suggestions
,
33 gfx::Rect
SearchBox::GetRect() {
34 // Need to adjust for scale.
37 WebView
* web_view
= render_view()->GetWebView();
40 double zoom
= WebView::zoomLevelToZoomFactor(web_view
->zoomLevel());
43 return gfx::Rect(static_cast<int>(static_cast<float>(rect_
.x()) / zoom
),
44 static_cast<int>(static_cast<float>(rect_
.y()) / zoom
),
45 static_cast<int>(static_cast<float>(rect_
.width()) / zoom
),
46 static_cast<int>(static_cast<float>(rect_
.height()) / zoom
));
49 bool SearchBox::OnMessageReceived(const IPC::Message
& message
) {
51 IPC_BEGIN_MESSAGE_MAP(SearchBox
, message
)
52 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange
, OnChange
)
53 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit
, OnSubmit
)
54 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel
, OnCancel
)
55 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize
, OnResize
)
56 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant
,
57 OnDetermineIfPageSupportsInstant
)
58 IPC_MESSAGE_UNHANDLED(handled
= false)
63 void SearchBox::OnChange(const string16
& value
,
69 selection_start_
= selection_start
;
70 selection_end_
= selection_end
;
71 if (!render_view()->GetWebView() || !render_view()->GetWebView()->mainFrame())
73 extensions_v8::SearchBoxExtension::DispatchChange(
74 render_view()->GetWebView()->mainFrame());
77 void SearchBox::OnSubmit(const string16
& value
, bool verbatim
) {
80 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
81 extensions_v8::SearchBoxExtension::DispatchSubmit(
82 render_view()->GetWebView()->mainFrame());
87 void SearchBox::OnCancel() {
89 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
90 extensions_v8::SearchBoxExtension::DispatchCancel(
91 render_view()->GetWebView()->mainFrame());
96 void SearchBox::OnResize(const gfx::Rect
& bounds
) {
98 if (!render_view()->GetWebView() || !render_view()->GetWebView()->mainFrame())
100 extensions_v8::SearchBoxExtension::DispatchResize(
101 render_view()->GetWebView()->mainFrame());
104 void SearchBox::OnDetermineIfPageSupportsInstant(const string16
& value
,
109 verbatim_
= verbatim
;
110 selection_start_
= selection_start
;
111 selection_end_
= selection_end
;
112 bool result
= extensions_v8::SearchBoxExtension::PageSupportsInstant(
113 render_view()->GetWebView()->mainFrame());
114 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined(
115 render_view()->GetRoutingId(), render_view()->GetPageId(), result
));
118 void SearchBox::Reset() {
121 selection_start_
= selection_end_
= 0;