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 "chrome/renderer/searchbox/searchbox.h" | 5 #include "chrome/renderer/searchbox/searchbox.h" |
6 | 6 |
7 #include "chrome/common/render_messages.h" | 7 #include "chrome/common/render_messages.h" |
8 #include "chrome/renderer/searchbox/searchbox_extension.h" | 8 #include "chrome/renderer/searchbox/searchbox_extension.h" |
9 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
11 | 11 |
12 SearchBox::SearchBox(content::RenderView* render_view) | 12 SearchBox::SearchBox(content::RenderView* render_view) |
13 : content::RenderViewObserver(render_view), | 13 : content::RenderViewObserver(render_view), |
14 content::RenderViewObserverTracker<SearchBox>(render_view), | 14 content::RenderViewObserverTracker<SearchBox>(render_view), |
15 verbatim_(false), | 15 verbatim_(false), |
16 selection_start_(0), | 16 selection_start_(0), |
17 selection_end_(0) { | 17 selection_end_(0), |
| 18 results_base_(0), |
| 19 key_code_(0) { |
18 } | 20 } |
19 | 21 |
20 SearchBox::~SearchBox() { | 22 SearchBox::~SearchBox() { |
21 } | 23 } |
22 | 24 |
23 void SearchBox::SetSuggestions(const std::vector<string16>& suggestions, | 25 void SearchBox::SetSuggestions( |
24 InstantCompleteBehavior behavior) { | 26 const std::vector<InstantSuggestion>& suggestions) { |
| 27 if (!suggestions.empty() && |
| 28 suggestions[0].behavior == INSTANT_COMPLETE_REPLACE) { |
| 29 query_ = suggestions[0].text; |
| 30 verbatim_ = true; |
| 31 selection_start_ = selection_end_ = query_.size(); |
| 32 } |
25 // Explicitly allow empty vector to be sent to the browser. | 33 // Explicitly allow empty vector to be sent to the browser. |
26 render_view()->Send(new ChromeViewHostMsg_SetSuggestions( | 34 render_view()->Send(new ChromeViewHostMsg_SetSuggestions( |
27 render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions, | 35 render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions)); |
28 behavior)); | 36 } |
| 37 |
| 38 void SearchBox::SetInstantPreviewHeight(int height, InstantSizeUnits units) { |
| 39 render_view()->Send(new ChromeViewHostMsg_SetInstantPreviewHeight( |
| 40 render_view()->GetRoutingID(), render_view()->GetPageId(), height, |
| 41 units)); |
29 } | 42 } |
30 | 43 |
31 gfx::Rect SearchBox::GetRect() { | 44 gfx::Rect SearchBox::GetRect() { |
32 // Need to adjust for scale. | 45 // Need to adjust for scale. |
33 if (rect_.IsEmpty()) | 46 if (rect_.IsEmpty()) |
34 return rect_; | 47 return rect_; |
35 WebKit::WebView* web_view = render_view()->GetWebView(); | 48 WebKit::WebView* web_view = render_view()->GetWebView(); |
36 if (!web_view) | 49 if (!web_view) |
37 return rect_; | 50 return rect_; |
38 double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); | 51 double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); |
39 if (zoom == 0) | 52 if (zoom == 0) |
40 return rect_; | 53 return rect_; |
41 return gfx::Rect(static_cast<int>(static_cast<float>(rect_.x()) / zoom), | 54 return gfx::Rect(static_cast<int>(static_cast<float>(rect_.x()) / zoom), |
42 static_cast<int>(static_cast<float>(rect_.y()) / zoom), | 55 static_cast<int>(static_cast<float>(rect_.y()) / zoom), |
43 static_cast<int>(static_cast<float>(rect_.width()) / zoom), | 56 static_cast<int>(static_cast<float>(rect_.width()) / zoom), |
44 static_cast<int>(static_cast<float>(rect_.height()) / zoom)); | 57 static_cast<int>(static_cast<float>(rect_.height()) / zoom)); |
45 } | 58 } |
46 | 59 |
47 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 60 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
48 bool handled = true; | 61 bool handled = true; |
49 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 62 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
50 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 63 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
51 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) | 64 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) |
52 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) | 65 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) |
53 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize, OnResize) | 66 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize, OnResize) |
54 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, | 67 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, |
55 OnDetermineIfPageSupportsInstant) | 68 OnDetermineIfPageSupportsInstant) |
| 69 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxAutocompleteResults, |
| 70 OnAutocompleteResults) |
| 71 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxKeyPress, OnKeyPress) |
56 IPC_MESSAGE_UNHANDLED(handled = false) | 72 IPC_MESSAGE_UNHANDLED(handled = false) |
57 IPC_END_MESSAGE_MAP() | 73 IPC_END_MESSAGE_MAP() |
58 return handled; | 74 return handled; |
59 } | 75 } |
60 | 76 |
61 void SearchBox::OnChange(const string16& value, | 77 void SearchBox::OnChange(const string16& query, |
62 bool verbatim, | 78 bool verbatim, |
63 size_t selection_start, | 79 size_t selection_start, |
64 size_t selection_end) { | 80 size_t selection_end) { |
65 value_ = value; | 81 query_ = query; |
66 verbatim_ = verbatim; | 82 verbatim_ = verbatim; |
67 selection_start_ = selection_start; | 83 selection_start_ = selection_start; |
68 selection_end_ = selection_end; | 84 selection_end_ = selection_end; |
69 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 85 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
70 extensions_v8::SearchBoxExtension::DispatchChange( | 86 extensions_v8::SearchBoxExtension::DispatchChange( |
71 render_view()->GetWebView()->mainFrame()); | 87 render_view()->GetWebView()->mainFrame()); |
72 } | 88 } |
73 } | 89 } |
74 | 90 |
75 void SearchBox::OnSubmit(const string16& value) { | 91 void SearchBox::OnSubmit(const string16& query) { |
76 value_ = value; | 92 query_ = query; |
77 verbatim_ = true; | 93 verbatim_ = true; |
78 selection_start_ = selection_end_ = value_.size(); | 94 selection_start_ = selection_end_ = query_.size(); |
79 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 95 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
80 extensions_v8::SearchBoxExtension::DispatchSubmit( | 96 extensions_v8::SearchBoxExtension::DispatchSubmit( |
81 render_view()->GetWebView()->mainFrame()); | 97 render_view()->GetWebView()->mainFrame()); |
82 } | 98 } |
83 Reset(); | 99 Reset(); |
84 } | 100 } |
85 | 101 |
86 void SearchBox::OnCancel(const string16& value) { | 102 void SearchBox::OnCancel(const string16& query) { |
87 value_ = value; | 103 query_ = query; |
88 verbatim_ = true; | 104 verbatim_ = true; |
89 selection_start_ = selection_end_ = value_.size(); | 105 selection_start_ = selection_end_ = query_.size(); |
90 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 106 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
91 extensions_v8::SearchBoxExtension::DispatchCancel( | 107 extensions_v8::SearchBoxExtension::DispatchCancel( |
92 render_view()->GetWebView()->mainFrame()); | 108 render_view()->GetWebView()->mainFrame()); |
93 } | 109 } |
94 Reset(); | 110 Reset(); |
95 } | 111 } |
96 | 112 |
97 void SearchBox::OnResize(const gfx::Rect& bounds) { | 113 void SearchBox::OnResize(const gfx::Rect& bounds) { |
98 rect_ = bounds; | 114 rect_ = bounds; |
99 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 115 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
100 extensions_v8::SearchBoxExtension::DispatchResize( | 116 extensions_v8::SearchBoxExtension::DispatchResize( |
101 render_view()->GetWebView()->mainFrame()); | 117 render_view()->GetWebView()->mainFrame()); |
102 } | 118 } |
103 } | 119 } |
104 | 120 |
105 void SearchBox::OnDetermineIfPageSupportsInstant() { | 121 void SearchBox::OnDetermineIfPageSupportsInstant() { |
106 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 122 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
107 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( | 123 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( |
108 render_view()->GetWebView()->mainFrame()); | 124 render_view()->GetWebView()->mainFrame()); |
109 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( | 125 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( |
110 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); | 126 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); |
111 } | 127 } |
112 } | 128 } |
113 | 129 |
| 130 void SearchBox::OnAutocompleteResults( |
| 131 const std::vector<InstantAutocompleteResult>& results) { |
| 132 results_base_ += autocomplete_results_.size(); |
| 133 autocomplete_results_ = results; |
| 134 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 135 extensions_v8::SearchBoxExtension::DispatchAutocompleteResults( |
| 136 render_view()->GetWebView()->mainFrame()); |
| 137 } |
| 138 } |
| 139 |
| 140 void SearchBox::OnKeyPress(int key_code) { |
| 141 key_code_ = key_code; |
| 142 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 143 extensions_v8::SearchBoxExtension::DispatchKeyPress( |
| 144 render_view()->GetWebView()->mainFrame()); |
| 145 } |
| 146 } |
| 147 |
114 void SearchBox::Reset() { | 148 void SearchBox::Reset() { |
115 value_.clear(); | 149 query_.clear(); |
116 verbatim_ = false; | 150 verbatim_ = false; |
117 selection_start_ = selection_end_ = 0; | 151 selection_start_ = selection_end_ = 0; |
| 152 results_base_ = 0; |
118 rect_ = gfx::Rect(); | 153 rect_ = gfx::Rect(); |
| 154 autocomplete_results_.clear(); |
| 155 key_code_ = 0; |
119 } | 156 } |
OLD | NEW |