Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: chrome/browser/ui/webui/ntp/suggestions_source_discovery.cc

Issue 10438004: Hooking the discovery API to the NTP. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added OWNERS for experimental discovery api. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_source_discovery.h" 5 #include "chrome/browser/ui/webui/ntp/suggestions_source_discovery.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/json/json_reader.h" 11 #include "base/json/json_reader.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "base/utf_string_conversions.h"
13 #include "base/values.h" 14 #include "base/values.h"
14 #include "chrome/browser/browser_process.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_facto ry.h"
15 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" 20 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
17 #include "chrome/browser/ui/webui/ntp/suggestions_combiner.h" 21 #include "chrome/browser/ui/webui/ntp/suggestions_combiner.h"
18 #include "content/public/common/url_fetcher.h"
19 #include "googleurl/src/gurl.h"
20 #include "net/base/load_flags.h" 22 #include "net/base/load_flags.h"
21 #include "net/http/http_status_code.h"
22 #include "net/url_request/url_fetcher_delegate.h"
23 #include "net/url_request/url_request_status.h"
24 23
25 namespace { 24 namespace {
26 25
27 const char kResponseDataValue[] = "responseData"; 26 typedef extensions::SuggestedLinksRegistry::SuggestedLinkList SuggestedLinkList;
28 const char kResultsValue[] = "results";
29 const char kUnescapedUrlValue[] = "unescapedUrl";
30 const char kDiscoveryBackendURL[] =
31 "https://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=a&rsz=8";
32 27
33 // The weight used by the combiner to determine which ratio of suggestions 28 // The weight used by the combiner to determine which ratio of suggestions
34 // should be obtained from this source. 29 // should be obtained from this source.
35 const int kSuggestionsDiscoveryWeight = 1; 30 const int kSuggestionsDiscoveryWeight = 1;
36 31
37 } // namespace 32 } // namespace
38 33
39 SuggestionsSourceDiscovery::SuggestionsSourceDiscovery() : combiner_(NULL) { 34 SuggestionsSourceDiscovery::SuggestionsSourceDiscovery(
40 } 35 const std::string& extension_id)
36 : combiner_(NULL),
37 extension_id_(extension_id) {}
41 38
42 SuggestionsSourceDiscovery::~SuggestionsSourceDiscovery() { 39 SuggestionsSourceDiscovery::~SuggestionsSourceDiscovery() {
43 STLDeleteElements(&items_); 40 STLDeleteElements(&items_);
44 } 41 }
45 42
46 inline int SuggestionsSourceDiscovery::GetWeight() { 43 inline int SuggestionsSourceDiscovery::GetWeight() {
47 return kSuggestionsDiscoveryWeight; 44 return kSuggestionsDiscoveryWeight;
48 } 45 }
49 46
50 int SuggestionsSourceDiscovery::GetItemCount() { 47 int SuggestionsSourceDiscovery::GetItemCount() {
51 return items_.size(); 48 return items_.size();
52 } 49 }
53 50
54 DictionaryValue* SuggestionsSourceDiscovery::PopItem() { 51 DictionaryValue* SuggestionsSourceDiscovery::PopItem() {
55 if (items_.empty()) 52 if (items_.empty())
56 return NULL; 53 return NULL;
57 DictionaryValue* item = items_.front(); 54 DictionaryValue* item = items_.front();
58 items_.pop_front(); 55 items_.pop_front();
59 return item; 56 return item;
60 } 57 }
61 58
62 void SuggestionsSourceDiscovery::FetchItems(Profile* profile) { 59 void SuggestionsSourceDiscovery::FetchItems(Profile* profile) {
63 DCHECK(combiner_); 60 DCHECK(combiner_);
64 61
65 // If a fetch is already in progress, we don't have to start a new one. 62 extensions::SuggestedLinksRegistry* registry =
66 if (recommended_fetcher_ != NULL) 63 extensions::SuggestedLinksRegistryFactory::GetForProfile(profile);
67 return; 64 const SuggestedLinkList* list = registry->GetAll(extension_id_);
68 65
69 // TODO(beaudoin): Extract the URL to some preference. Use a better service. 66 items_.clear();
70 recommended_fetcher_.reset(content::URLFetcher::Create( 67 for (SuggestedLinkList::const_iterator it = list->begin();
71 GURL(kDiscoveryBackendURL), net::URLFetcher::GET, this)); 68 it != list->end(); ++it) {
72 recommended_fetcher_->SetRequestContext( 69 DictionaryValue* page_value = new DictionaryValue();
73 g_browser_process->system_request_context()); 70 NewTabUI::SetURLTitleAndDirection(page_value,
74 recommended_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 71 ASCIIToUTF16((*it)->link_text()),
75 net::LOAD_DO_NOT_SAVE_COOKIES); 72 GURL((*it)->link_url()));
76 recommended_fetcher_->Start(); 73 page_value->SetDouble("score", (*it)->score());
74 items_.push_back(page_value);
75 }
76 combiner_->OnItemsReady();
77 } 77 }
78 78
79 void SuggestionsSourceDiscovery::SetCombiner(SuggestionsCombiner* combiner) { 79 void SuggestionsSourceDiscovery::SetCombiner(SuggestionsCombiner* combiner) {
80 DCHECK(!combiner_); 80 DCHECK(!combiner_);
81 combiner_ = combiner; 81 combiner_ = combiner;
82 } 82 }
83
84 void SuggestionsSourceDiscovery::OnURLFetchComplete(
85 const net::URLFetcher* source) {
86 DCHECK(combiner_);
87 STLDeleteElements(&items_);
88 if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS &&
89 source->GetResponseCode() == net::HTTP_OK) {
90 std::string data;
91 source->GetResponseAsString(&data);
92 scoped_ptr<Value> message_value(base::JSONReader::Read(data, false));
93
94 DictionaryValue* response_dict;
95 DictionaryValue* response_data;
96 ListValue* results;
97 if (message_value.get() &&
98 message_value->GetAsDictionary(&response_dict) &&
99 response_dict->GetDictionary(kResponseDataValue, &response_data) &&
100 response_data->GetList(kResultsValue, &results)) {
101 for (size_t i = 0; i < results->GetSize(); ++i) {
102 DictionaryValue* result;
103 if (!results->GetDictionary(i, &result) || !result)
104 continue;
105
106 string16 unescaped_url;
107 if (!result->GetString(kUnescapedUrlValue, &unescaped_url))
108 continue;
109
110 DictionaryValue* page_value = new DictionaryValue();
111 NewTabUI::SetURLTitleAndDirection(page_value,
112 unescaped_url,
113 GURL(unescaped_url));
114 items_.push_back(page_value);
115 }
116 }
117 }
118
119 recommended_fetcher_.reset();
120 combiner_->OnItemsReady();
121 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/ntp/suggestions_source_discovery.h ('k') | chrome/browser/ui/webui/ntp/suggestions_source_top_sites.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698