Home | History | Annotate | Download | only in ntp
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/ui/webui/ntp/suggestions_combiner.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "base/values.h"
     10 #include "chrome/browser/extensions/api/discovery/suggested_links_registry.h"
     11 #include "chrome/browser/extensions/api/discovery/suggested_links_registry_factory.h"
     12 #include "chrome/browser/profiles/profile.h"
     13 #include "chrome/browser/ui/browser.h"
     14 #include "chrome/browser/ui/browser_iterator.h"
     15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     16 #include "chrome/browser/ui/webui/ntp/suggestions_page_handler.h"
     17 #include "chrome/browser/ui/webui/ntp/suggestions_source.h"
     18 #include "chrome/browser/ui/webui/ntp/suggestions_source_discovery.h"
     19 #include "content/public/browser/web_contents.h"
     20 
     21 namespace {
     22 
     23 static const size_t kSuggestionsCount = 8;
     24 
     25 }  // namespace
     26 
     27 SuggestionsCombiner::SuggestionsCombiner(
     28     SuggestionsCombiner::Delegate* delegate,
     29     Profile* profile)
     30     : sources_fetching_count_(0),
     31       delegate_(delegate),
     32       suggestions_count_(kSuggestionsCount),
     33       page_values_(new base::ListValue()),
     34       debug_enabled_(false),
     35       profile_(profile) {
     36 }
     37 
     38 SuggestionsCombiner::~SuggestionsCombiner() {
     39 }
     40 
     41 void SuggestionsCombiner::AddSource(SuggestionsSource* source) {
     42   source->SetCombiner(this);
     43   source->SetDebug(debug_enabled_);
     44   sources_.push_back(source);
     45 }
     46 
     47 void SuggestionsCombiner::EnableDebug(bool enable) {
     48   debug_enabled_ = enable;
     49   for (size_t i = 0; i < sources_.size(); ++i) {
     50     sources_[i]->SetDebug(enable);
     51   }
     52 }
     53 
     54 void SuggestionsCombiner::FetchItems(Profile* profile) {
     55   sources_fetching_count_ = sources_.size();
     56   for (size_t i = 0; i < sources_.size(); ++i) {
     57     sources_[i]->FetchItems(profile);
     58   }
     59 }
     60 
     61 base::ListValue* SuggestionsCombiner::GetPageValues() {
     62   return page_values_.get();
     63 }
     64 
     65 void SuggestionsCombiner::OnItemsReady() {
     66   DCHECK_GT(sources_fetching_count_, 0);
     67   sources_fetching_count_--;
     68   if (sources_fetching_count_ == 0) {
     69     FillPageValues();
     70     delegate_->OnSuggestionsReady();
     71   }
     72 }
     73 
     74 void SuggestionsCombiner::SetSuggestionsCount(size_t suggestions_count) {
     75   suggestions_count_ = suggestions_count;
     76 }
     77 
     78 // static
     79 SuggestionsCombiner* SuggestionsCombiner::Create(
     80     SuggestionsCombiner::Delegate* delegate, Profile* profile) {
     81   SuggestionsCombiner* combiner = new SuggestionsCombiner(delegate, profile);
     82   extensions::SuggestedLinksRegistry* registry =
     83       extensions::SuggestedLinksRegistryFactory::GetForProfile(profile);
     84   scoped_ptr<std::vector<std::string> > list = registry->GetExtensionIds();
     85   for (std::vector<std::string>::iterator it = list->begin();
     86       it != list->end(); ++it) {
     87     combiner->AddSource(new SuggestionsSourceDiscovery(*it));
     88   }
     89 
     90   return combiner;
     91 }
     92 
     93 void SuggestionsCombiner::FillPageValues() {
     94   int total_weight = 0;
     95   for (size_t i = 0; i < sources_.size(); ++i)
     96     total_weight += sources_[i]->GetWeight();
     97   DCHECK_GT(total_weight, 0);
     98 
     99   page_values_.reset(new base::ListValue());
    100 
    101   // Evaluate how many items to obtain from each source. We use error diffusion
    102   // to ensure that we get the total desired number of items.
    103   int error = 0;
    104 
    105   // Holds the index at which the next item should be added for each source.
    106   std::vector<size_t> next_item_index_for_source;
    107   next_item_index_for_source.reserve(sources_.size());
    108   for (size_t i = 0; i < sources_.size(); ++i) {
    109     int numerator = sources_[i]->GetWeight() * suggestions_count_ + error;
    110     error = numerator % total_weight;
    111     int item_count = std::min(numerator / total_weight,
    112         sources_[i]->GetItemCount());
    113 
    114     for (int j = 0; j < item_count; ++j)
    115       page_values_->Append(sources_[i]->PopItem());
    116     next_item_index_for_source.push_back(page_values_->GetSize());
    117   }
    118 
    119   // Fill in extra items, prioritizing the first source.
    120   DictionaryValue* item;
    121   // Rather than updating |next_item_index_for_source| we keep track of the
    122   // number of extra items that were added and offset indices by that much.
    123   size_t extra_items_added = 0;
    124   for (size_t i = 0; i < sources_.size() &&
    125       page_values_->GetSize() < suggestions_count_; ++i) {
    126 
    127     size_t index = next_item_index_for_source[i] + extra_items_added;
    128     while (page_values_->GetSize() < suggestions_count_ &&
    129         (item = sources_[i]->PopItem())) {
    130       page_values_->Insert(index++, item);
    131       extra_items_added++;
    132     }
    133   }
    134 
    135   // Add page value information common to all sources.
    136   for (size_t i = 0; i < page_values_->GetSize(); i++) {
    137     base::DictionaryValue* page_value;
    138     if (page_values_->GetDictionary(i, &page_value))
    139       AddExtendedInformation(page_value);
    140   }
    141 }
    142 
    143 void SuggestionsCombiner::AddExtendedInformation(
    144     base::DictionaryValue* page_value) {
    145   if (debug_enabled_) {
    146     std::string url_string;
    147     if (page_value->GetString("url", &url_string)) {
    148       GURL url(url_string);
    149       page_value->SetBoolean("already_open", IsUrlAlreadyOpen(url));
    150     }
    151   }
    152 }
    153 
    154 bool SuggestionsCombiner::IsUrlAlreadyOpen(const GURL &url) {
    155   for (chrome::BrowserIterator it; !it.done(); it.Next()) {
    156     const Browser* browser = *it;
    157     if (browser->profile()->IsOffTheRecord() ||
    158         !browser->profile()->IsSameProfile(profile_))
    159       continue;
    160 
    161     for (int i = 0; i < browser->tab_strip_model()->count(); i++) {
    162       const content::WebContents* tab =
    163           browser->tab_strip_model()->GetWebContentsAt(i);
    164       if (tab->GetURL() == url)
    165         return true;
    166     }
    167   }
    168   return false;
    169 }
    170