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 <string> | 7 #include <string> |
8 | 8 |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 10 matching lines...) Expand all Loading... | |
21 #include "third_party/WebKit/public/web/WebDocument.h" | 21 #include "third_party/WebKit/public/web/WebDocument.h" |
22 #include "third_party/WebKit/public/web/WebFrame.h" | 22 #include "third_party/WebKit/public/web/WebFrame.h" |
23 #include "third_party/WebKit/public/web/WebView.h" | 23 #include "third_party/WebKit/public/web/WebView.h" |
24 #include "ui/base/resource/resource_bundle.h" | 24 #include "ui/base/resource/resource_bundle.h" |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 // Size of the results cache. | 28 // Size of the results cache. |
29 const size_t kMaxInstantAutocompleteResultItemCacheSize = 100; | 29 const size_t kMaxInstantAutocompleteResultItemCacheSize = 100; |
30 | 30 |
31 // Returns true if |items_a| and |items_b| are equal. | |
32 bool AreMostVisitedItemsEqual( | |
33 const std::vector<InstantMostVisitedItem>& items_a, | |
34 const std::vector<InstantMostVisitedItem>& items_b) { | |
35 if (items_a.size() != items_b.size()) | |
36 return false; | |
37 | |
38 for (size_t i = 0; i < items_b.size(); ++i) { | |
39 if (items_b[i].url != items_a[i].url || | |
40 items_b[i].title != items_a[i].title) { | |
41 return false; | |
42 } | |
43 } | |
44 return true; | |
45 } | |
46 | |
31 } // namespace | 47 } // namespace |
32 | 48 |
33 namespace internal { // for testing | 49 namespace internal { // for testing |
34 | 50 |
35 // Parses |url| and fills in |id| with the InstantRestrictedID obtained from the | 51 // Parses |url| and fills in |id| with the InstantRestrictedID obtained from the |
36 // |url|. |render_view_id| is the ID of the associated RenderView. | 52 // |url|. |render_view_id| is the ID of the associated RenderView. |
37 // | 53 // |
38 // Valid |url| forms: | 54 // Valid |url| forms: |
39 // chrome-search://favicon/<view_id>/<restricted_id> | 55 // chrome-search://favicon/<view_id>/<restricted_id> |
40 // chrome-search://thumb/<view_id>/<restricted_id> | 56 // chrome-search://thumb/<view_id>/<restricted_id> |
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
490 } | 506 } |
491 | 507 |
492 void SearchBox::SetQuery(const string16& query, bool verbatim) { | 508 void SearchBox::SetQuery(const string16& query, bool verbatim) { |
493 query_ = query; | 509 query_ = query; |
494 verbatim_ = verbatim; | 510 verbatim_ = verbatim; |
495 query_is_restricted_ = false; | 511 query_is_restricted_ = false; |
496 } | 512 } |
497 | 513 |
498 void SearchBox::OnMostVisitedChanged( | 514 void SearchBox::OnMostVisitedChanged( |
499 const std::vector<InstantMostVisitedItem>& items) { | 515 const std::vector<InstantMostVisitedItem>& items) { |
516 std::vector<InstantMostVisitedItemIDPair> item_id_pairs; | |
517 GetMostVisitedItems(&item_id_pairs); | |
518 | |
519 std::vector<InstantMostVisitedItem> last_known_items; | |
520 for (std::vector<InstantMostVisitedItemIDPair>::const_iterator it = | |
samarth
2013/06/21 20:51:20
Why bother converting the list? Can't you change A
kmadhusu
2013/06/21 21:35:32
Done.
| |
521 item_id_pairs.begin(); | |
522 it != item_id_pairs.end(); ++it) { | |
523 last_known_items.push_back(it->second); | |
524 } | |
525 | |
526 if (AreMostVisitedItemsEqual(last_known_items, items)) | |
527 return; // Do not send duplicate onmostvisitedchange events. | |
528 | |
500 most_visited_items_cache_.AddItems(items); | 529 most_visited_items_cache_.AddItems(items); |
501 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 530 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
502 extensions_v8::SearchBoxExtension::DispatchMostVisitedChanged( | 531 extensions_v8::SearchBoxExtension::DispatchMostVisitedChanged( |
503 render_view()->GetWebView()->mainFrame()); | 532 render_view()->GetWebView()->mainFrame()); |
504 } | 533 } |
505 } | 534 } |
506 | 535 |
507 void SearchBox::GetMostVisitedItems( | 536 void SearchBox::GetMostVisitedItems( |
508 std::vector<InstantMostVisitedItemIDPair>* items) const { | 537 std::vector<InstantMostVisitedItemIDPair>* items) const { |
509 return most_visited_items_cache_.GetCurrentItems(items); | 538 return most_visited_items_cache_.GetCurrentItems(items); |
(...skipping 10 matching lines...) Expand all Loading... | |
520 InstantMostVisitedItem item; | 549 InstantMostVisitedItem item; |
521 return GetMostVisitedItemWithID(item_id, &item) ? item.url : GURL(); | 550 return GetMostVisitedItemWithID(item_id, &item) ? item.url : GURL(); |
522 } | 551 } |
523 | 552 |
524 void SearchBox::OnToggleVoiceSearch() { | 553 void SearchBox::OnToggleVoiceSearch() { |
525 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 554 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
526 extensions_v8::SearchBoxExtension::DispatchToggleVoiceSearch( | 555 extensions_v8::SearchBoxExtension::DispatchToggleVoiceSearch( |
527 render_view()->GetWebView()->mainFrame()); | 556 render_view()->GetWebView()->mainFrame()); |
528 } | 557 } |
529 } | 558 } |
OLD | NEW |