| 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), |   18       results_base_(0), | 
 |   19       start_margin_(0), | 
 |   20       end_margin_(0), | 
|   19       last_results_base_(0), |   21       last_results_base_(0), | 
|   20       is_focused_(false), |   22       is_focused_(false), | 
|   21       active_tab_is_ntp_(false) { |   23       active_tab_is_ntp_(false) { | 
|   22 } |   24 } | 
|   23  |   25  | 
|   24 SearchBox::~SearchBox() { |   26 SearchBox::~SearchBox() { | 
|   25 } |   27 } | 
|   26  |   28  | 
|   27 void SearchBox::SetSuggestions( |   29 void SearchBox::SetSuggestions( | 
|   28     const std::vector<InstantSuggestion>& suggestions) { |   30     const std::vector<InstantSuggestion>& suggestions) { | 
|   29   if (!suggestions.empty() && |   31   if (!suggestions.empty() && | 
|   30       suggestions[0].behavior == INSTANT_COMPLETE_REPLACE) { |   32       suggestions[0].behavior == INSTANT_COMPLETE_REPLACE) { | 
|   31     query_ = suggestions[0].text; |   33     query_ = suggestions[0].text; | 
|   32     verbatim_ = true; |   34     verbatim_ = true; | 
|   33     selection_start_ = selection_end_ = query_.size(); |   35     selection_start_ = selection_end_ = query_.size(); | 
|   34   } |   36   } | 
|   35   // Explicitly allow empty vector to be sent to the browser. |   37   // Explicitly allow empty vector to be sent to the browser. | 
|   36   render_view()->Send(new ChromeViewHostMsg_SetSuggestions( |   38   render_view()->Send(new ChromeViewHostMsg_SetSuggestions( | 
|   37       render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions)); |   39       render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions)); | 
|   38 } |   40 } | 
|   39  |   41  | 
|   40 void SearchBox::ShowInstantPreview(InstantShownReason reason, |   42 void SearchBox::ShowInstantPreview(InstantShownReason reason, | 
|   41                                    int height, |   43                                    int height, | 
|   42                                    InstantSizeUnits units) { |   44                                    InstantSizeUnits units) { | 
|   43   render_view()->Send(new ChromeViewHostMsg_ShowInstantPreview( |   45   render_view()->Send(new ChromeViewHostMsg_ShowInstantPreview( | 
|   44       render_view()->GetRoutingID(), render_view()->GetPageId(), reason, |   46       render_view()->GetRoutingID(), render_view()->GetPageId(), reason, | 
|   45       height, units)); |   47       height, units)); | 
|   46 } |   48 } | 
|   47  |   49  | 
|   48 gfx::Rect SearchBox::GetRect() { |   50 int SearchBox::GetStartMargin() { | 
|   49   // Need to adjust for scale. |   51   return static_cast<int>(start_margin_ / GetZoom()); | 
|   50   if (rect_.IsEmpty()) |   52 } | 
|   51     return rect_; |   53  | 
|   52   WebKit::WebView* web_view = render_view()->GetWebView(); |   54 int SearchBox::GetEndMargin() { | 
|   53   if (!web_view) |   55   return static_cast<int>(end_margin_ / GetZoom()); | 
|   54     return rect_; |   56 } | 
|   55   double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); |   57  | 
|   56   if (zoom == 0) |   58 gfx::Rect SearchBox::GetPopupBounds() { | 
|   57     return rect_; |   59   double zoom = GetZoom(); | 
|   58   return gfx::Rect(static_cast<int>(static_cast<float>(rect_.x()) / zoom), |   60   return gfx::Rect(static_cast<int>(popup_bounds_.x() / zoom), | 
|   59                    static_cast<int>(static_cast<float>(rect_.y()) / zoom), |   61                    static_cast<int>(popup_bounds_.y() / zoom), | 
|   60                    static_cast<int>(static_cast<float>(rect_.width()) / zoom), |   62                    static_cast<int>(popup_bounds_.width() / zoom), | 
|   61                    static_cast<int>(static_cast<float>(rect_.height()) / zoom)); |   63                    static_cast<int>(popup_bounds_.height() / zoom)); | 
|   62 } |   64 } | 
|   63  |   65  | 
|   64 const std::vector<InstantAutocompleteResult>& |   66 const std::vector<InstantAutocompleteResult>& | 
|   65     SearchBox::GetAutocompleteResults() { |   67     SearchBox::GetAutocompleteResults() { | 
|   66   // Remember the last requested autocomplete_results to account for race |   68   // Remember the last requested autocomplete_results to account for race | 
|   67   // conditions between autocomplete providers returning new data and the user |   69   // conditions between autocomplete providers returning new data and the user | 
|   68   // clicking on a suggestion. |   70   // clicking on a suggestion. | 
|   69   last_autocomplete_results_ = autocomplete_results_; |   71   last_autocomplete_results_ = autocomplete_results_; | 
|   70   last_results_base_ = results_base_; |   72   last_results_base_ = results_base_; | 
|   71   return autocomplete_results_; |   73   return autocomplete_results_; | 
|   72 } |   74 } | 
|   73  |   75  | 
|   74 const InstantAutocompleteResult* SearchBox::GetAutocompleteResultWithId( |   76 const InstantAutocompleteResult* SearchBox::GetAutocompleteResultWithId( | 
|   75     size_t restricted_id) const { |   77     size_t restricted_id) const { | 
|   76   if (restricted_id < last_results_base_ || |   78   if (restricted_id < last_results_base_ || | 
|   77       restricted_id >= last_results_base_ + last_autocomplete_results_.size()) |   79       restricted_id >= last_results_base_ + last_autocomplete_results_.size()) | 
|   78     return NULL; |   80     return NULL; | 
|   79   return &last_autocomplete_results_[restricted_id - last_results_base_]; |   81   return &last_autocomplete_results_[restricted_id - last_results_base_]; | 
|   80 } |   82 } | 
|   81  |   83  | 
|   82 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |   84 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 
|   83   bool handled = true; |   85   bool handled = true; | 
|   84   IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |   86   IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 
|   85     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |   87     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 
|   86     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) |   88     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) | 
|   87     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) |   89     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) | 
|   88     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize, OnResize) |   90     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize) | 
 |   91     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxMarginChange, OnMarginChange) | 
|   89     IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, |   92     IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, | 
|   90                         OnDetermineIfPageSupportsInstant) |   93                         OnDetermineIfPageSupportsInstant) | 
|   91     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxAutocompleteResults, |   94     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxAutocompleteResults, | 
|   92                         OnAutocompleteResults) |   95                         OnAutocompleteResults) | 
|   93     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxUpOrDownKeyPressed, |   96     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxUpOrDownKeyPressed, | 
|   94                         OnUpOrDownKeyPressed) |   97                         OnUpOrDownKeyPressed) | 
|   95     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxFocus, OnFocus) |   98     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxFocus, OnFocus) | 
|   96     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxBlur, OnBlur) |   99     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxBlur, OnBlur) | 
|   97     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxActiveTabModeChanged, |  100     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxActiveTabModeChanged, | 
|   98                         OnActiveTabModeChanged) |  101                         OnActiveTabModeChanged) | 
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  130   query_ = query; |  133   query_ = query; | 
|  131   verbatim_ = true; |  134   verbatim_ = true; | 
|  132   selection_start_ = selection_end_ = query_.size(); |  135   selection_start_ = selection_end_ = query_.size(); | 
|  133   if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |  136   if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 
|  134     extensions_v8::SearchBoxExtension::DispatchCancel( |  137     extensions_v8::SearchBoxExtension::DispatchCancel( | 
|  135         render_view()->GetWebView()->mainFrame()); |  138         render_view()->GetWebView()->mainFrame()); | 
|  136   } |  139   } | 
|  137   Reset(); |  140   Reset(); | 
|  138 } |  141 } | 
|  139  |  142  | 
|  140 void SearchBox::OnResize(const gfx::Rect& bounds) { |  143 void SearchBox::OnPopupResize(const gfx::Rect& bounds) { | 
|  141   rect_ = bounds; |  144   popup_bounds_ = bounds; | 
|  142   if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |  145   if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 
|  143     extensions_v8::SearchBoxExtension::DispatchResize( |  146     extensions_v8::SearchBoxExtension::DispatchResize( | 
|  144         render_view()->GetWebView()->mainFrame()); |  147         render_view()->GetWebView()->mainFrame()); | 
|  145   } |  148   } | 
|  146 } |  149 } | 
|  147  |  150  | 
 |  151 void SearchBox::OnMarginChange(int start, int end) { | 
 |  152   start_margin_ = start; | 
 |  153   end_margin_ = end; | 
 |  154   if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 
 |  155     extensions_v8::SearchBoxExtension::DispatchMarginChange( | 
 |  156         render_view()->GetWebView()->mainFrame()); | 
 |  157   } | 
 |  158 } | 
 |  159  | 
|  148 void SearchBox::OnDetermineIfPageSupportsInstant() { |  160 void SearchBox::OnDetermineIfPageSupportsInstant() { | 
|  149   if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |  161   if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 
|  150     bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( |  162     bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( | 
|  151         render_view()->GetWebView()->mainFrame()); |  163         render_view()->GetWebView()->mainFrame()); | 
|  152     render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( |  164     render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( | 
|  153         render_view()->GetRoutingID(), render_view()->GetPageId(), result)); |  165         render_view()->GetRoutingID(), render_view()->GetPageId(), result)); | 
|  154   } |  166   } | 
|  155 } |  167 } | 
|  156  |  168  | 
|  157 void SearchBox::OnAutocompleteResults( |  169 void SearchBox::OnAutocompleteResults( | 
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  193     extensions_v8::SearchBoxExtension::DispatchContextChange( |  205     extensions_v8::SearchBoxExtension::DispatchContextChange( | 
|  194         render_view()->GetWebView()->mainFrame()); |  206         render_view()->GetWebView()->mainFrame()); | 
|  195   } |  207   } | 
|  196 } |  208 } | 
|  197  |  209  | 
|  198 void SearchBox::Reset() { |  210 void SearchBox::Reset() { | 
|  199   query_.clear(); |  211   query_.clear(); | 
|  200   verbatim_ = false; |  212   verbatim_ = false; | 
|  201   selection_start_ = selection_end_ = 0; |  213   selection_start_ = selection_end_ = 0; | 
|  202   results_base_ = 0; |  214   results_base_ = 0; | 
|  203   rect_ = gfx::Rect(); |  215   popup_bounds_ = gfx::Rect(); | 
 |  216   start_margin_ = 0; | 
 |  217   end_margin_ = 0; | 
|  204   autocomplete_results_.clear(); |  218   autocomplete_results_.clear(); | 
|  205   is_focused_ = false; |  219   is_focused_ = false; | 
|  206 } |  220 } | 
 |  221  | 
 |  222 double SearchBox::GetZoom() { | 
 |  223   WebKit::WebView* web_view = render_view()->GetWebView(); | 
 |  224   if (web_view) { | 
 |  225     double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); | 
 |  226     if (zoom != 0) | 
 |  227       return zoom; | 
 |  228   } | 
 |  229   return 1.0; | 
 |  230 } | 
| OLD | NEW |