| OLD | NEW | 
|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" | 
| 8 #include "chrome/common/render_messages.h" | 8 #include "chrome/common/render_messages.h" | 
| 9 #include "chrome/common/url_constants.h" | 9 #include "chrome/common/url_constants.h" | 
| 10 #include "chrome/renderer/searchbox/searchbox_extension.h" | 10 #include "chrome/renderer/searchbox/searchbox_extension.h" | 
| 11 #include "content/public/renderer/render_view.h" | 11 #include "content/public/renderer/render_view.h" | 
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" | 
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 
| 14 | 14 | 
| 15 namespace { |  | 
| 16 |  | 
| 17 // Prefix for a thumbnail URL. |  | 
| 18 const char kThumbnailUrlPrefix[] = "chrome-search://thumb/"; |  | 
| 19 |  | 
| 20 // Prefix for a thumbnail URL. |  | 
| 21 const char kFaviconUrlPrefix[] = "chrome-search://favicon/"; |  | 
| 22 |  | 
| 23 } |  | 
| 24 |  | 
| 25 SearchBox::SearchBox(content::RenderView* render_view) | 15 SearchBox::SearchBox(content::RenderView* render_view) | 
| 26     : content::RenderViewObserver(render_view), | 16     : content::RenderViewObserver(render_view), | 
| 27       content::RenderViewObserverTracker<SearchBox>(render_view), | 17       content::RenderViewObserverTracker<SearchBox>(render_view), | 
| 28       verbatim_(false), | 18       verbatim_(false), | 
| 29       selection_start_(0), | 19       selection_start_(0), | 
| 30       selection_end_(0), | 20       selection_end_(0), | 
| 31       results_base_(0), | 21       results_base_(0), | 
| 32       start_margin_(0), | 22       start_margin_(0), | 
| 33       last_results_base_(0), | 23       last_results_base_(0), | 
| 34       is_key_capture_enabled_(false), | 24       is_key_capture_enabled_(false), | 
| 35       display_instant_results_(false), | 25       display_instant_results_(false), | 
| 36       omnibox_font_size_(0), | 26       omnibox_font_size_(0) { | 
| 37       last_restricted_id_(0) { |  | 
| 38 } | 27 } | 
| 39 | 28 | 
| 40 SearchBox::~SearchBox() { | 29 SearchBox::~SearchBox() { | 
| 41 } | 30 } | 
| 42 | 31 | 
| 43 void SearchBox::SetSuggestions( | 32 void SearchBox::SetSuggestions( | 
| 44     const std::vector<InstantSuggestion>& suggestions) { | 33     const std::vector<InstantSuggestion>& suggestions) { | 
| 45   if (!suggestions.empty() && | 34   if (!suggestions.empty() && | 
| 46       suggestions[0].behavior == INSTANT_COMPLETE_REPLACE) { | 35       suggestions[0].behavior == INSTANT_COMPLETE_REPLACE) { | 
| 47     query_ = suggestions[0].text; | 36     query_ = suggestions[0].text; | 
| (...skipping 29 matching lines...) Expand all  Loading... | 
| 77 } | 66 } | 
| 78 | 67 | 
| 79 void SearchBox::NavigateToURL(const GURL& url, | 68 void SearchBox::NavigateToURL(const GURL& url, | 
| 80                               content::PageTransition transition, | 69                               content::PageTransition transition, | 
| 81                               WindowOpenDisposition disposition) { | 70                               WindowOpenDisposition disposition) { | 
| 82   render_view()->Send(new ChromeViewHostMsg_SearchBoxNavigate( | 71   render_view()->Send(new ChromeViewHostMsg_SearchBoxNavigate( | 
| 83       render_view()->GetRoutingID(), render_view()->GetPageId(), | 72       render_view()->GetRoutingID(), render_view()->GetPageId(), | 
| 84       url, transition, disposition)); | 73       url, transition, disposition)); | 
| 85 } | 74 } | 
| 86 | 75 | 
| 87 void SearchBox::DeleteMostVisitedItem(int restrict_id) { | 76 void SearchBox::DeleteMostVisitedItem(uint64 restricted_id) { | 
| 88   string16 url = RestrictedIdToURL(restrict_id); | 77   render_view()->Send(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem( | 
| 89   render_view()->Send(new ChromeViewHostMsg_InstantDeleteMostVisitedItem( | 78       render_view()->GetRoutingID(), restricted_id)); | 
| 90       render_view()->GetRoutingID(), GURL(url))); |  | 
| 91 } | 79 } | 
| 92 | 80 | 
| 93 void SearchBox::UndoMostVisitedDeletion(int restrict_id) { | 81 void SearchBox::UndoMostVisitedDeletion(uint64 restricted_id) { | 
| 94   string16 url = RestrictedIdToURL(restrict_id); | 82   render_view()->Send(new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion( | 
| 95   render_view()->Send(new ChromeViewHostMsg_InstantUndoMostVisitedDeletion( | 83       render_view()->GetRoutingID(), restricted_id)); | 
| 96       render_view()->GetRoutingID(), GURL(url))); |  | 
| 97 } | 84 } | 
| 98 | 85 | 
| 99 void SearchBox::UndoAllMostVisitedDeletions() { | 86 void SearchBox::UndoAllMostVisitedDeletions() { | 
| 100   render_view()->Send(new ChromeViewHostMsg_InstantUndoAllMostVisitedDeletions( | 87   render_view()->Send( | 
|  | 88       new ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions( | 
| 101       render_view()->GetRoutingID())); | 89       render_view()->GetRoutingID())); | 
| 102 } | 90 } | 
| 103 | 91 | 
| 104 int SearchBox::GetStartMargin() const { | 92 int SearchBox::GetStartMargin() const { | 
| 105   return static_cast<int>(start_margin_ / GetZoom()); | 93   return static_cast<int>(start_margin_ / GetZoom()); | 
| 106 } | 94 } | 
| 107 | 95 | 
| 108 gfx::Rect SearchBox::GetPopupBounds() const { | 96 gfx::Rect SearchBox::GetPopupBounds() const { | 
| 109   double zoom = GetZoom(); | 97   double zoom = GetZoom(); | 
| 110   return gfx::Rect(static_cast<int>(popup_bounds_.x() / zoom), | 98   return gfx::Rect(static_cast<int>(popup_bounds_.x() / zoom), | 
| 111                    static_cast<int>(popup_bounds_.y() / zoom), | 99                    static_cast<int>(popup_bounds_.y() / zoom), | 
| 112                    static_cast<int>(popup_bounds_.width() / zoom), | 100                    static_cast<int>(popup_bounds_.width() / zoom), | 
| 113                    static_cast<int>(popup_bounds_.height() / zoom)); | 101                    static_cast<int>(popup_bounds_.height() / zoom)); | 
| 114 } | 102 } | 
| 115 | 103 | 
| 116 const std::vector<InstantAutocompleteResult>& | 104 const std::vector<InstantAutocompleteResult>& | 
| 117     SearchBox::GetAutocompleteResults() { | 105     SearchBox::GetAutocompleteResults() { | 
| 118   // Remember the last requested autocomplete_results to account for race | 106   // Remember the last requested autocomplete_results to account for race | 
| 119   // conditions between autocomplete providers returning new data and the user | 107   // conditions between autocomplete providers returning new data and the user | 
| 120   // clicking on a suggestion. | 108   // clicking on a suggestion. | 
| 121   last_autocomplete_results_ = autocomplete_results_; | 109   last_autocomplete_results_ = autocomplete_results_; | 
| 122   last_results_base_ = results_base_; | 110   last_results_base_ = results_base_; | 
| 123   return autocomplete_results_; | 111   return autocomplete_results_; | 
| 124 } | 112 } | 
| 125 | 113 | 
| 126 const InstantAutocompleteResult* SearchBox::GetAutocompleteResultWithId( | 114 const InstantAutocompleteResult* SearchBox::GetAutocompleteResultWithId( | 
| 127     size_t restricted_id) const { | 115     uint64 restricted_id) const { | 
| 128   if (restricted_id < last_results_base_ || | 116   if (restricted_id < last_results_base_ || | 
| 129       restricted_id >= last_results_base_ + last_autocomplete_results_.size()) | 117       restricted_id >= last_results_base_ + last_autocomplete_results_.size()) | 
| 130     return NULL; | 118     return NULL; | 
| 131   return &last_autocomplete_results_[restricted_id - last_results_base_]; | 119   return &last_autocomplete_results_[restricted_id - last_results_base_]; | 
| 132 } | 120 } | 
| 133 | 121 | 
| 134 const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() { | 122 const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() { | 
| 135   return theme_info_; | 123   return theme_info_; | 
| 136 } | 124 } | 
| 137 | 125 | 
| (...skipping 17 matching lines...) Expand all  Loading... | 
| 155                         OnSetDisplayInstantResults) | 143                         OnSetDisplayInstantResults) | 
| 156     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxKeyCaptureChanged, | 144     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxKeyCaptureChanged, | 
| 157                         OnKeyCaptureChange) | 145                         OnKeyCaptureChange) | 
| 158     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxThemeChanged, | 146     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxThemeChanged, | 
| 159                         OnThemeChanged) | 147                         OnThemeChanged) | 
| 160     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxFontInformation, | 148     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxFontInformation, | 
| 161                         OnFontInformationReceived) | 149                         OnFontInformationReceived) | 
| 162     IPC_MESSAGE_HANDLER( | 150     IPC_MESSAGE_HANDLER( | 
| 163         ChromeViewMsg_SearchBoxGrantChromeSearchAccessFromOrigin, | 151         ChromeViewMsg_SearchBoxGrantChromeSearchAccessFromOrigin, | 
| 164         OnGrantChromeSearchAccessFromOrigin) | 152         OnGrantChromeSearchAccessFromOrigin) | 
| 165     IPC_MESSAGE_HANDLER(ChromeViewMsg_InstantMostVisitedItemsChanged, | 153     IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxMostVisitedItemsChanged, | 
| 166                         OnMostVisitedChanged) | 154                         OnMostVisitedChanged) | 
| 167     IPC_MESSAGE_UNHANDLED(handled = false) | 155     IPC_MESSAGE_UNHANDLED(handled = false) | 
| 168   IPC_END_MESSAGE_MAP() | 156   IPC_END_MESSAGE_MAP() | 
| 169   return handled; | 157   return handled; | 
| 170 } | 158 } | 
| 171 | 159 | 
| 172 void SearchBox::DidClearWindowObject(WebKit::WebFrame* frame) { | 160 void SearchBox::DidClearWindowObject(WebKit::WebFrame* frame) { | 
| 173   extensions_v8::SearchBoxExtension::DispatchOnWindowReady(frame); | 161   extensions_v8::SearchBoxExtension::DispatchOnWindowReady(frame); | 
| 174 } | 162 } | 
| 175 | 163 | 
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 343   is_key_capture_enabled_ = false; | 331   is_key_capture_enabled_ = false; | 
| 344   theme_info_ = ThemeBackgroundInfo(); | 332   theme_info_ = ThemeBackgroundInfo(); | 
| 345   // Don't reset display_instant_results_ to prevent clearing it on committed | 333   // Don't reset display_instant_results_ to prevent clearing it on committed | 
| 346   // results pages in extended mode. Otherwise resetting it is a no-op because | 334   // results pages in extended mode. Otherwise resetting it is a no-op because | 
| 347   // a new loader is created when it changes; see crbug.com/164662. | 335   // a new loader is created when it changes; see crbug.com/164662. | 
| 348   // Also don't reset omnibox_font_ or omnibox_font_size_ since it never | 336   // Also don't reset omnibox_font_ or omnibox_font_size_ since it never | 
| 349   // changes. | 337   // changes. | 
| 350 } | 338 } | 
| 351 | 339 | 
| 352 void SearchBox::OnMostVisitedChanged( | 340 void SearchBox::OnMostVisitedChanged( | 
| 353     const std::vector<MostVisitedItem>& items) { | 341     const std::vector<InstantMostVisitedItem>& items) { | 
| 354   most_visited_items_ = items; | 342   most_visited_items_ = items; | 
| 355 | 343 | 
| 356   if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 344   if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 
| 357     extensions_v8::SearchBoxExtension::DispatchMostVisitedChanged( | 345     extensions_v8::SearchBoxExtension::DispatchMostVisitedChanged( | 
| 358         render_view()->GetWebView()->mainFrame()); | 346         render_view()->GetWebView()->mainFrame()); | 
| 359   } | 347   } | 
| 360 } | 348 } | 
| 361 | 349 | 
| 362 const std::vector<MostVisitedItem>& SearchBox::GetMostVisitedItems() { | 350 const std::vector<InstantMostVisitedItem>& | 
|  | 351 SearchBox::GetMostVisitedItems() const { | 
| 363   return most_visited_items_; | 352   return most_visited_items_; | 
| 364 } | 353 } | 
| 365 |  | 
| 366 int SearchBox::UrlToRestrictedId(string16 url) { |  | 
| 367   if (url_to_restricted_id_map_[url]) |  | 
| 368     return url_to_restricted_id_map_[url]; |  | 
| 369 |  | 
| 370   last_restricted_id_++; |  | 
| 371   url_to_restricted_id_map_[url] = last_restricted_id_; |  | 
| 372   restricted_id_to_url_map_[last_restricted_id_] = url; |  | 
| 373 |  | 
| 374   return last_restricted_id_; |  | 
| 375 } |  | 
| 376 |  | 
| 377 string16 SearchBox::RestrictedIdToURL(int id) { |  | 
| 378   return restricted_id_to_url_map_[id]; |  | 
| 379 } |  | 
| 380 |  | 
| 381 string16 SearchBox::GenerateThumbnailUrl(int id) { |  | 
| 382   std::ostringstream ostr; |  | 
| 383   ostr << kThumbnailUrlPrefix << id; |  | 
| 384   GURL url = GURL(ostr.str()); |  | 
| 385   return UTF8ToUTF16(url.spec()); |  | 
| 386 } |  | 
| 387 |  | 
| 388 string16 SearchBox::GenerateFaviconUrl(int id) { |  | 
| 389   std::ostringstream ostr; |  | 
| 390   ostr << kFaviconUrlPrefix << id; |  | 
| 391   GURL url = GURL(ostr.str()); |  | 
| 392   return UTF8ToUTF16(url.spec()); |  | 
| 393 } |  | 
| OLD | NEW | 
|---|