OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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/interests/interests_fetcher.h" | |
6 | |
7 #include "base/json/json_reader.h" | |
8 #include "base/logging.h" | |
9 #include "base/values.h" | |
10 #include "net/base/load_flags.h" | |
11 #include "net/http/http_status_code.h" | |
12 #include "net/url_request/url_fetcher.h" | |
13 #include "net/url_request/url_fetcher_factory.h" | |
Marc Treib
2015/09/22 12:02:59
not needed
tache
2015/09/29 09:33:34
Done.
| |
14 #include "net/url_request/url_request_context_getter.h" | |
15 #include "net/url_request/url_request_status.h" | |
16 | |
17 using net::URLFetcher; | |
18 using net::URLFetcherDelegate; | |
19 using net::URLRequestContextGetter; | |
20 using net::URLRequestStatus; | |
21 using net::URLFetcherFactory; | |
Marc Treib
2015/09/22 12:02:59
not needed
tache
2015/09/29 09:33:33
Done.
| |
22 | |
23 namespace { | |
24 | |
25 const int kNumRetries = 1; | |
26 const char kIdInterests[] = "interest"; | |
27 const char kIdInterestName[] = "name"; | |
28 const char kIdInterestImageUrl[] = "imageUrl"; | |
29 const char kIdInterestRelevance[] = "relevance"; | |
30 | |
31 const char kInterestsUrl[] = "https://www.googleapis.com/TBD/v1/interests"; | |
Marc Treib
2015/09/22 12:02:59
Do we know the correct URL? (Even if it doesn't ac
tache
2015/09/29 09:33:34
This should be https://www-googleapis-test.sandbox
| |
32 | |
33 std::vector<InterestsFetcher::Interest> EmptyResponse() { | |
34 return std::vector<InterestsFetcher::Interest>(); | |
35 } | |
36 | |
37 } // namespace | |
38 | |
39 InterestsFetcher::InterestsFetcher( | |
40 net::URLRequestContextGetter* url_request_context, | |
41 const std::string& access_token) | |
42 : url_request_context_(url_request_context), | |
43 access_token_(access_token) { | |
44 } | |
45 | |
46 void InterestsFetcher::Start( | |
47 const InterestsFetcher::InterestsCallback& callback) { | |
48 DCHECK(callback_.is_null()); | |
49 callback_ = callback; | |
50 | |
51 fetcher_ = CreateFetcher(); | |
52 | |
53 // Setup fetcher | |
54 fetcher_->SetRequestContext(url_request_context_); | |
55 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
56 net::LOAD_DO_NOT_SAVE_COOKIES); | |
57 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); | |
58 | |
59 // Add oauth access token | |
60 fetcher_->AddExtraRequestHeader(std::string("Authorization: Bearer ") + | |
61 access_token_); | |
62 | |
63 fetcher_->Start(); | |
64 } | |
65 | |
66 scoped_ptr<URLFetcher> InterestsFetcher::CreateFetcher() { | |
67 return URLFetcher::Create(GURL(kInterestsUrl), URLFetcher::GET, this); | |
68 } | |
69 | |
70 void InterestsFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
71 const URLRequestStatus& status = source->GetStatus(); | |
72 | |
73 if (!status.is_success()) { | |
74 DLOG(WARNING) << "URL request failed!"; | |
75 callback_.Run(EmptyResponse()); | |
76 return; | |
77 } | |
78 | |
79 std::string response_body; | |
80 source->GetResponseAsString(&response_body); | |
81 | |
82 callback_.Run(ExtractInterests(response_body)); | |
83 } | |
84 | |
85 std::vector<InterestsFetcher::Interest> InterestsFetcher::ExtractInterests( | |
86 const std::string& response) { | |
87 scoped_ptr<base::Value> value = base::JSONReader::Read(response); | |
88 | |
89 const base::DictionaryValue* dict = nullptr; | |
90 if (!value || !value->GetAsDictionary(&dict)) { | |
91 DLOG(WARNING) << "ExtractInterests failed to parse global dictionary"; | |
92 return EmptyResponse(); | |
93 } | |
94 | |
95 const base::ListValue* interests_list = nullptr; | |
96 std::vector<Interest> res; | |
97 | |
98 if (!dict->GetList(kIdInterests, &interests_list)) { | |
99 DLOG(WARNING) << "ExtractInterests failed to parse interests list"; | |
100 return EmptyResponse(); | |
101 } | |
102 | |
103 for (const base::Value* entry : *interests_list) { | |
104 const base::DictionaryValue* interest_dict = nullptr; | |
105 if (!entry->GetAsDictionary(&interest_dict)) { | |
106 DLOG(WARNING) << "ExtractInterests failed to parse interest dictionary"; | |
107 return EmptyResponse(); | |
108 } | |
109 | |
110 std::string name; | |
111 if (!interest_dict->GetString(kIdInterestName, &name)) { | |
112 DLOG(WARNING) << "ExtractInterests failed to parse interest name"; | |
113 return EmptyResponse(); | |
114 } | |
115 | |
116 std::string image_url; | |
117 if (!interest_dict->GetString(kIdInterestImageUrl, &image_url)) { | |
118 // image_url is allowed to be missing. | |
119 // | |
120 // However this is still logged as a warning, since, currently, the server | |
121 // should always provide an image_url. | |
122 // | |
Marc Treib
2015/09/22 12:02:59
nit: remove empty comment line
tache
2015/09/29 09:33:33
Done.
| |
123 DLOG(WARNING) << "ExtractInterests failed to parse interest image URL"; | |
124 } | |
125 | |
126 double relevance; | |
127 if (!interest_dict->GetDouble(kIdInterestRelevance, &relevance)) { | |
128 DLOG(WARNING) << "ExtractInterests failed to parse interest relevance"; | |
129 return EmptyResponse(); | |
130 } | |
131 | |
132 res.push_back(Interest{name, image_url, relevance}); | |
133 } | |
134 | |
135 return res; | |
136 } | |
137 | |
138 InterestsFetcher::~InterestsFetcher() {} | |
139 | |
140 bool InterestsFetcher::Interest::operator==(const Interest& interest) const { | |
141 return name == interest.name && | |
142 image_url == interest.image_url && | |
143 relevance == interest.relevance; | |
144 } | |
OLD | NEW |