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_source_discovery.h"
      6 
      7 #include <string>
      8 
      9 #include "base/bind.h"
     10 #include "base/bind_helpers.h"
     11 #include "base/json/json_reader.h"
     12 #include "base/stl_util.h"
     13 #include "base/strings/utf_string_conversions.h"
     14 #include "base/values.h"
     15 #include "chrome/browser/browser_process.h"
     16 #include "chrome/browser/extensions/api/discovery/suggested_link.h"
     17 #include "chrome/browser/extensions/api/discovery/suggested_links_registry.h"
     18 #include "chrome/browser/extensions/api/discovery/suggested_links_registry_factory.h"
     19 #include "chrome/browser/profiles/profile.h"
     20 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
     21 #include "chrome/browser/ui/webui/ntp/suggestions_combiner.h"
     22 #include "net/base/load_flags.h"
     23 
     24 namespace {
     25 
     26 typedef extensions::SuggestedLinksRegistry::SuggestedLinkList SuggestedLinkList;
     27 
     28 // The weight used by the combiner to determine which ratio of suggestions
     29 // should be obtained from this source.
     30 const int kSuggestionsDiscoveryWeight = 1;
     31 
     32 }  // namespace
     33 
     34 SuggestionsSourceDiscovery::SuggestionsSourceDiscovery(
     35     const std::string& extension_id)
     36     : combiner_(NULL),
     37       extension_id_(extension_id),
     38       debug_(false) {}
     39 
     40 SuggestionsSourceDiscovery::~SuggestionsSourceDiscovery() {
     41   STLDeleteElements(&items_);
     42 }
     43 
     44 void SuggestionsSourceDiscovery::SetDebug(bool enable) {
     45   debug_ = enable;
     46 }
     47 
     48 inline int SuggestionsSourceDiscovery::GetWeight() {
     49   return kSuggestionsDiscoveryWeight;
     50 }
     51 
     52 int SuggestionsSourceDiscovery::GetItemCount() {
     53   return items_.size();
     54 }
     55 
     56 DictionaryValue* SuggestionsSourceDiscovery::PopItem() {
     57   if (items_.empty())
     58     return NULL;
     59   DictionaryValue* item = items_.front();
     60   items_.pop_front();
     61   return item;
     62 }
     63 
     64 void SuggestionsSourceDiscovery::FetchItems(Profile* profile) {
     65   DCHECK(combiner_);
     66 
     67   extensions::SuggestedLinksRegistry* registry =
     68       extensions::SuggestedLinksRegistryFactory::GetForProfile(profile);
     69   const SuggestedLinkList* list = registry->GetAll(extension_id_);
     70 
     71   items_.clear();
     72   for (SuggestedLinkList::const_iterator it = list->begin();
     73        it != list->end(); ++it) {
     74     DictionaryValue* page_value = new DictionaryValue();
     75     NewTabUI::SetUrlTitleAndDirection(page_value,
     76                                       ASCIIToUTF16((*it)->link_text()),
     77                                       GURL((*it)->link_url()));
     78     page_value->SetDouble("score", (*it)->score());
     79     const std::string& url_image = (*it)->url_image();
     80     if (url_image.length() > 0)
     81       page_value->SetString("urlImage", url_image);
     82     items_.push_back(page_value);
     83   }
     84   combiner_->OnItemsReady();
     85 }
     86 
     87 void SuggestionsSourceDiscovery::SetCombiner(SuggestionsCombiner* combiner) {
     88   DCHECK(!combiner_);
     89   combiner_ = combiner;
     90 }
     91