| 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/browser/ui/webui/ntp/suggestions_combiner.h" | 5 #include "chrome/browser/ui/webui/ntp/suggestions_combiner.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/extensions/api/discovery/suggested_links_registry.h" | 10 #include "chrome/browser/extensions/api/discovery/suggested_links_registry.h" |
| 11 #include "chrome/browser/extensions/api/discovery/suggested_links_registry_facto
ry.h" | 11 #include "chrome/browser/extensions/api/discovery/suggested_links_registry_facto
ry.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_list.h" |
| 13 #include "chrome/browser/ui/webui/ntp/suggestions_page_handler.h" | 15 #include "chrome/browser/ui/webui/ntp/suggestions_page_handler.h" |
| 14 #include "chrome/browser/ui/webui/ntp/suggestions_source.h" | 16 #include "chrome/browser/ui/webui/ntp/suggestions_source.h" |
| 15 #include "chrome/browser/ui/webui/ntp/suggestions_source_discovery.h" | 17 #include "chrome/browser/ui/webui/ntp/suggestions_source_discovery.h" |
| 16 #include "chrome/browser/ui/webui/ntp/suggestions_source_top_sites.h" | 18 #include "chrome/browser/ui/webui/ntp/suggestions_source_top_sites.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 17 | 20 |
| 18 namespace { | 21 namespace { |
| 19 | 22 |
| 20 static const size_t kSuggestionsCount = 8; | 23 static const size_t kSuggestionsCount = 8; |
| 21 | 24 |
| 22 } // namespace | 25 } // namespace |
| 23 | 26 |
| 24 SuggestionsCombiner::SuggestionsCombiner( | 27 SuggestionsCombiner::SuggestionsCombiner( |
| 25 SuggestionsCombiner::Delegate* delegate) | 28 SuggestionsCombiner::Delegate* delegate, |
| 29 Profile* profile) |
| 26 : sources_fetching_count_(0), | 30 : sources_fetching_count_(0), |
| 27 delegate_(delegate), | 31 delegate_(delegate), |
| 28 suggestions_count_(kSuggestionsCount), | 32 suggestions_count_(kSuggestionsCount), |
| 29 page_values_(new base::ListValue()), | 33 page_values_(new base::ListValue()), |
| 30 debug_enabled_(false) { | 34 debug_enabled_(false), |
| 35 profile_(profile) { |
| 31 } | 36 } |
| 32 | 37 |
| 33 SuggestionsCombiner::~SuggestionsCombiner() { | 38 SuggestionsCombiner::~SuggestionsCombiner() { |
| 34 } | 39 } |
| 35 | 40 |
| 36 void SuggestionsCombiner::AddSource(SuggestionsSource* source) { | 41 void SuggestionsCombiner::AddSource(SuggestionsSource* source) { |
| 37 source->SetCombiner(this); | 42 source->SetCombiner(this); |
| 38 source->SetDebug(debug_enabled_); | 43 source->SetDebug(debug_enabled_); |
| 39 sources_.push_back(source); | 44 sources_.push_back(source); |
| 40 } | 45 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 66 } | 71 } |
| 67 } | 72 } |
| 68 | 73 |
| 69 void SuggestionsCombiner::SetSuggestionsCount(size_t suggestions_count) { | 74 void SuggestionsCombiner::SetSuggestionsCount(size_t suggestions_count) { |
| 70 suggestions_count_ = suggestions_count; | 75 suggestions_count_ = suggestions_count; |
| 71 } | 76 } |
| 72 | 77 |
| 73 // static | 78 // static |
| 74 SuggestionsCombiner* SuggestionsCombiner::Create( | 79 SuggestionsCombiner* SuggestionsCombiner::Create( |
| 75 SuggestionsCombiner::Delegate* delegate, Profile* profile) { | 80 SuggestionsCombiner::Delegate* delegate, Profile* profile) { |
| 76 SuggestionsCombiner* combiner = new SuggestionsCombiner(delegate); | 81 SuggestionsCombiner* combiner = new SuggestionsCombiner(delegate, profile); |
| 77 combiner->AddSource(new SuggestionsSourceTopSites()); | 82 combiner->AddSource(new SuggestionsSourceTopSites()); |
| 78 | 83 |
| 79 extensions::SuggestedLinksRegistry* registry = | 84 extensions::SuggestedLinksRegistry* registry = |
| 80 extensions::SuggestedLinksRegistryFactory::GetForProfile(profile); | 85 extensions::SuggestedLinksRegistryFactory::GetForProfile(profile); |
| 81 scoped_ptr<std::vector<std::string> > list = registry->GetExtensionIds(); | 86 scoped_ptr<std::vector<std::string> > list = registry->GetExtensionIds(); |
| 82 for (std::vector<std::string>::iterator it = list->begin(); | 87 for (std::vector<std::string>::iterator it = list->begin(); |
| 83 it != list->end(); ++it) { | 88 it != list->end(); ++it) { |
| 84 combiner->AddSource(new SuggestionsSourceDiscovery(*it)); | 89 combiner->AddSource(new SuggestionsSourceDiscovery(*it)); |
| 85 } | 90 } |
| 86 | 91 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 for (size_t i = 0; i < sources_.size() && | 126 for (size_t i = 0; i < sources_.size() && |
| 122 page_values_->GetSize() < suggestions_count_; ++i) { | 127 page_values_->GetSize() < suggestions_count_; ++i) { |
| 123 | 128 |
| 124 size_t index = next_item_index_for_source[i] + extra_items_added; | 129 size_t index = next_item_index_for_source[i] + extra_items_added; |
| 125 while (page_values_->GetSize() < suggestions_count_ && | 130 while (page_values_->GetSize() < suggestions_count_ && |
| 126 (item = sources_[i]->PopItem())) { | 131 (item = sources_[i]->PopItem())) { |
| 127 page_values_->Insert(index++, item); | 132 page_values_->Insert(index++, item); |
| 128 extra_items_added++; | 133 extra_items_added++; |
| 129 } | 134 } |
| 130 } | 135 } |
| 136 |
| 137 // Add page value information common to all sources. |
| 138 for (size_t i = 0; i < page_values_->GetSize(); i++) { |
| 139 base::DictionaryValue* page_value; |
| 140 if (page_values_->GetDictionary(i, &page_value)) |
| 141 AddExtendedInformation(page_value); |
| 142 } |
| 131 } | 143 } |
| 144 |
| 145 void SuggestionsCombiner::AddExtendedInformation( |
| 146 base::DictionaryValue* page_value) { |
| 147 if (debug_enabled_) { |
| 148 std::string url_string; |
| 149 if (page_value->GetString("url", &url_string)) { |
| 150 GURL url(url_string); |
| 151 page_value->SetBoolean("already_open", IsURLAlreadyOpen(url)); |
| 152 } |
| 153 } |
| 154 } |
| 155 |
| 156 bool SuggestionsCombiner::IsURLAlreadyOpen(const GURL &url) { |
| 157 for (BrowserList::const_iterator it = BrowserList::begin(); |
| 158 it != BrowserList::end(); ++it) { |
| 159 const Browser* browser = *it; |
| 160 if (browser->profile()->IsOffTheRecord() || |
| 161 !browser->profile()->IsSameProfile(profile_)) |
| 162 continue; |
| 163 |
| 164 for (int i = 0; i < browser->tab_count(); i++) { |
| 165 const content::WebContents* tab = browser->GetWebContentsAt(i); |
| 166 if (tab->GetURL() == url) |
| 167 return true; |
| 168 } |
| 169 } |
| 170 return false; |
| 171 } |
| OLD | NEW |