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.h" | 5 #include "chrome/renderer/searchbox.h" |
6 | 6 |
7 #include "chrome/common/render_messages.h" | 7 #include "chrome/common/render_messages.h" |
8 #include "chrome/renderer/searchbox_extension.h" | 8 #include "chrome/renderer/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 using WebKit::WebView; | |
13 | |
14 SearchBox::SearchBox(content::RenderView* render_view) | 12 SearchBox::SearchBox(content::RenderView* render_view) |
15 : content::RenderViewObserver(render_view), | 13 : content::RenderViewObserver(render_view), |
16 content::RenderViewObserverTracker<SearchBox>(render_view), | 14 content::RenderViewObserverTracker<SearchBox>(render_view), |
17 verbatim_(false), | 15 verbatim_(false), |
18 selection_start_(0), | 16 selection_start_(0), |
19 selection_end_(0) { | 17 selection_end_(0) { |
20 } | 18 } |
21 | 19 |
22 SearchBox::~SearchBox() { | 20 SearchBox::~SearchBox() { |
23 } | 21 } |
24 | 22 |
25 void SearchBox::SetSuggestions(const std::vector<std::string>& suggestions, | 23 void SearchBox::SetSuggestions(const std::vector<string16>& suggestions, |
26 InstantCompleteBehavior behavior) { | 24 InstantCompleteBehavior behavior) { |
27 // Explicitly allow empty vector to be sent to the browser. | 25 // Explicitly allow empty vector to be sent to the browser. |
28 render_view()->Send(new ChromeViewHostMsg_SetSuggestions( | 26 render_view()->Send(new ChromeViewHostMsg_SetSuggestions( |
29 render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions, | 27 render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions, |
30 behavior)); | 28 behavior)); |
31 } | 29 } |
32 | 30 |
33 gfx::Rect SearchBox::GetRect() { | 31 gfx::Rect SearchBox::GetRect() { |
34 // Need to adjust for scale. | 32 // Need to adjust for scale. |
35 if (rect_.IsEmpty()) | 33 if (rect_.IsEmpty()) |
36 return rect_; | 34 return rect_; |
37 WebView* web_view = render_view()->GetWebView(); | 35 WebKit::WebView* web_view = render_view()->GetWebView(); |
38 if (!web_view) | 36 if (!web_view) |
39 return rect_; | 37 return rect_; |
40 double zoom = WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); | 38 double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); |
41 if (zoom == 0) | 39 if (zoom == 0) |
42 return rect_; | 40 return rect_; |
43 return gfx::Rect(static_cast<int>(static_cast<float>(rect_.x()) / zoom), | 41 return gfx::Rect(static_cast<int>(static_cast<float>(rect_.x()) / zoom), |
44 static_cast<int>(static_cast<float>(rect_.y()) / zoom), | 42 static_cast<int>(static_cast<float>(rect_.y()) / zoom), |
45 static_cast<int>(static_cast<float>(rect_.width()) / zoom), | 43 static_cast<int>(static_cast<float>(rect_.width()) / zoom), |
46 static_cast<int>(static_cast<float>(rect_.height()) / zoom)); | 44 static_cast<int>(static_cast<float>(rect_.height()) / zoom)); |
47 } | 45 } |
48 | 46 |
49 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 47 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
50 bool handled = true; | 48 bool handled = true; |
51 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 49 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
52 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 50 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
53 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) | 51 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) |
54 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) | 52 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) |
55 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize, OnResize) | 53 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize, OnResize) |
56 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, | 54 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, |
57 OnDetermineIfPageSupportsInstant) | 55 OnDetermineIfPageSupportsInstant) |
58 IPC_MESSAGE_UNHANDLED(handled = false) | 56 IPC_MESSAGE_UNHANDLED(handled = false) |
59 IPC_END_MESSAGE_MAP() | 57 IPC_END_MESSAGE_MAP() |
60 return handled; | 58 return handled; |
61 } | 59 } |
62 | 60 |
63 void SearchBox::OnChange(const string16& value, | 61 void SearchBox::OnChange(const string16& value, |
64 bool verbatim, | 62 bool verbatim, |
65 int selection_start, | 63 size_t selection_start, |
66 int selection_end) { | 64 size_t selection_end) { |
67 value_ = value; | 65 value_ = value; |
68 verbatim_ = verbatim; | 66 verbatim_ = verbatim; |
69 selection_start_ = selection_start; | 67 selection_start_ = selection_start; |
70 selection_end_ = selection_end; | 68 selection_end_ = selection_end; |
71 if (!render_view()->GetWebView() || !render_view()->GetWebView()->mainFrame()) | 69 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
72 return; | 70 extensions_v8::SearchBoxExtension::DispatchChange( |
73 extensions_v8::SearchBoxExtension::DispatchChange( | 71 render_view()->GetWebView()->mainFrame()); |
74 render_view()->GetWebView()->mainFrame()); | 72 } |
75 } | 73 } |
76 | 74 |
77 void SearchBox::OnSubmit(const string16& value, bool verbatim) { | 75 void SearchBox::OnSubmit(const string16& value) { |
78 value_ = value; | 76 value_ = value; |
79 verbatim_ = verbatim; | 77 verbatim_ = true; |
| 78 selection_start_ = selection_end_ = value_.size(); |
80 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 79 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
81 extensions_v8::SearchBoxExtension::DispatchSubmit( | 80 extensions_v8::SearchBoxExtension::DispatchSubmit( |
82 render_view()->GetWebView()->mainFrame()); | 81 render_view()->GetWebView()->mainFrame()); |
83 } | 82 } |
84 Reset(); | 83 Reset(); |
85 } | 84 } |
86 | 85 |
87 void SearchBox::OnCancel() { | 86 void SearchBox::OnCancel(const string16& value) { |
88 verbatim_ = false; | 87 value_ = value; |
| 88 verbatim_ = true; |
| 89 selection_start_ = selection_end_ = value_.size(); |
89 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 90 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
90 extensions_v8::SearchBoxExtension::DispatchCancel( | 91 extensions_v8::SearchBoxExtension::DispatchCancel( |
91 render_view()->GetWebView()->mainFrame()); | 92 render_view()->GetWebView()->mainFrame()); |
92 } | 93 } |
93 Reset(); | 94 Reset(); |
94 } | 95 } |
95 | 96 |
96 void SearchBox::OnResize(const gfx::Rect& bounds) { | 97 void SearchBox::OnResize(const gfx::Rect& bounds) { |
97 rect_ = bounds; | 98 rect_ = bounds; |
98 if (!render_view()->GetWebView() || !render_view()->GetWebView()->mainFrame()) | 99 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
99 return; | 100 extensions_v8::SearchBoxExtension::DispatchResize( |
100 extensions_v8::SearchBoxExtension::DispatchResize( | 101 render_view()->GetWebView()->mainFrame()); |
101 render_view()->GetWebView()->mainFrame()); | 102 } |
102 } | 103 } |
103 | 104 |
104 void SearchBox::OnDetermineIfPageSupportsInstant(const string16& value, | 105 void SearchBox::OnDetermineIfPageSupportsInstant() { |
105 bool verbatim, | 106 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
106 int selection_start, | 107 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( |
107 int selection_end) { | 108 render_view()->GetWebView()->mainFrame()); |
108 value_ = value; | 109 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( |
109 verbatim_ = verbatim; | 110 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); |
110 selection_start_ = selection_start; | 111 } |
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)); | |
116 } | 112 } |
117 | 113 |
118 void SearchBox::Reset() { | 114 void SearchBox::Reset() { |
| 115 value_.clear(); |
119 verbatim_ = false; | 116 verbatim_ = false; |
120 value_ = string16(); | |
121 selection_start_ = selection_end_ = 0; | 117 selection_start_ = selection_end_ = 0; |
122 rect_ = gfx::Rect(); | 118 rect_ = gfx::Rect(); |
123 } | 119 } |
OLD | NEW |