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/command_line.h" | |
8 #include "base/json/json_reader.h" | |
9 #include "base/logging.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "base/values.h" | |
12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
13 #include "chrome/browser/signin/signin_manager_factory.h" | |
14 #include "chrome/common/chrome_switches.h" | |
15 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
16 #include "net/base/load_flags.h" | |
17 #include "net/http/http_status_code.h" | |
18 #include "net/url_request/url_fetcher.h" | |
19 #include "net/url_request/url_request_context_getter.h" | |
20 #include "net/url_request/url_request_status.h" | |
21 | |
22 namespace { | |
23 | |
24 const int kNumRetries = 1; | |
25 const char kIdInterests[] = "interests"; | |
26 const char kIdInterestName[] = "name"; | |
27 const char kIdInterestImageUrl[] = "imageUrl"; | |
28 const char kIdInterestRelevance[] = "relevance"; | |
29 | |
30 const char kApiScope[] = "https://www.googleapis.com/auth/googlenow"; | |
31 | |
32 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; | |
33 | |
34 std::vector<InterestsFetcher::Interest> EmptyResponse() { | |
35 return std::vector<InterestsFetcher::Interest>(); | |
36 } | |
37 | |
38 GURL GetInterestsURL() { | |
39 const base::CommandLine* command_line = | |
40 base::CommandLine::ForCurrentProcess(); | |
41 return GURL(command_line->GetSwitchValueASCII(switches::kInterestsURL)); | |
42 } | |
43 | |
44 } // namespace | |
45 | |
46 InterestsFetcher::Interest::Interest(const std::string& name, | |
47 const GURL& image_url, | |
48 double relevance) | |
49 : name(name), image_url(image_url), relevance(relevance) {} | |
50 | |
51 InterestsFetcher::Interest::~Interest() {} | |
52 | |
53 bool InterestsFetcher::Interest::operator==(const Interest& interest) const { | |
54 return name == interest.name && image_url == interest.image_url && | |
55 relevance == interest.relevance; | |
56 } | |
57 | |
58 InterestsFetcher::InterestsFetcher( | |
59 OAuth2TokenService* oauth2_token_service, | |
60 const std::string& account_id, | |
61 net::URLRequestContextGetter* url_request_context) | |
62 : OAuth2TokenService::Consumer("interests_fetcher"), | |
63 account_id_(account_id), | |
64 url_request_context_(url_request_context), | |
65 access_token_expired_(false), | |
66 token_service_(oauth2_token_service) { | |
67 } | |
68 | |
69 InterestsFetcher::~InterestsFetcher() {} | |
70 | |
71 // static | |
72 scoped_ptr<InterestsFetcher> InterestsFetcher::CreateFromProfile( | |
73 Profile* profile) { | |
74 ProfileOAuth2TokenService* token_service = | |
75 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
76 | |
77 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); | |
78 | |
79 return make_scoped_ptr(new InterestsFetcher( | |
80 token_service, | |
81 signin->GetAuthenticatedAccountId(), | |
82 profile->GetRequestContext())); | |
83 } | |
84 | |
85 void InterestsFetcher::FetchInterests( | |
86 const InterestsFetcher::InterestsCallback& callback) { | |
87 DCHECK(callback_.is_null()); | |
88 callback_ = callback; | |
89 StartOAuth2Request(); | |
90 } | |
91 | |
92 void InterestsFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
93 const net::URLRequestStatus& status = source->GetStatus(); | |
94 if (!status.is_success()) { | |
95 VLOG(2) << "Network error " << status.error(); | |
96 callback_.Run(EmptyResponse()); | |
97 return; | |
98 } | |
99 | |
100 int response_code = source->GetResponseCode(); | |
101 // If we get an authorization error, refresh token and retry once. | |
102 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) { | |
103 access_token_expired_ = true; | |
104 token_service_ | |
105 ->InvalidateAccessToken(account_id_, GetApiScopes(), access_token_); | |
Marc Treib
2015/10/06 13:38:32
Did "git cl format" do this? Ugh. Oh well.
PEConn
2015/10/06 14:21:29
Done.
| |
106 StartOAuth2Request(); | |
107 return; | |
108 } | |
109 | |
110 if (response_code != net::HTTP_OK) { | |
111 VLOG(2) << "HTTP error " << response_code; | |
112 callback_.Run(EmptyResponse()); | |
113 return; | |
114 } | |
115 | |
116 std::string response_body; | |
117 source->GetResponseAsString(&response_body); | |
118 | |
119 callback_.Run(ExtractInterests(response_body)); | |
120 } | |
121 | |
122 void InterestsFetcher::OnGetTokenSuccess( | |
123 const OAuth2TokenService::Request* request, | |
124 const std::string& access_token, | |
125 const base::Time& expiration_time) { | |
126 access_token_ = access_token; | |
127 | |
128 fetcher_ = CreateFetcher(); | |
129 fetcher_->SetRequestContext(url_request_context_); | |
130 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
131 net::LOAD_DO_NOT_SAVE_COOKIES); | |
132 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); | |
133 | |
134 fetcher_->AddExtraRequestHeader( | |
135 base::StringPrintf(kAuthorizationHeaderFormat, access_token_.c_str())); | |
136 | |
137 fetcher_->Start(); | |
138 } | |
139 | |
140 void InterestsFetcher::OnGetTokenFailure( | |
141 const OAuth2TokenService::Request* request, | |
142 const GoogleServiceAuthError& error) { | |
143 DLOG(WARNING) << error.ToString(); | |
144 | |
145 callback_.Run(EmptyResponse()); | |
146 } | |
147 | |
148 void InterestsFetcher::StartOAuth2Request() { | |
149 oauth_request_ = | |
150 token_service_->StartRequest(account_id_, GetApiScopes(), this); | |
151 } | |
152 | |
153 OAuth2TokenService::ScopeSet InterestsFetcher::GetApiScopes() { | |
154 OAuth2TokenService::ScopeSet scopes; | |
155 scopes.insert(kApiScope); | |
156 return scopes; | |
157 } | |
158 | |
159 scoped_ptr<net::URLFetcher> InterestsFetcher::CreateFetcher() { | |
160 return | |
161 net::URLFetcher::Create(0, GetInterestsURL(), net::URLFetcher::GET, this); | |
162 } | |
163 | |
164 std::vector<InterestsFetcher::Interest> InterestsFetcher::ExtractInterests( | |
165 const std::string& response) { | |
166 scoped_ptr<base::Value> value = base::JSONReader::Read(response); | |
167 DVLOG(2) << response; | |
168 | |
169 const base::DictionaryValue* dict = nullptr; | |
170 if (!value || !value->GetAsDictionary(&dict)) { | |
171 DLOG(WARNING) << "Failed to parse global dictionary."; | |
172 return EmptyResponse(); | |
173 } | |
174 | |
175 const base::ListValue* interests_list = nullptr; | |
176 if (!dict->GetList(kIdInterests, &interests_list)) { | |
177 DLOG(WARNING) << "Failed to parse interests list."; | |
178 return EmptyResponse(); | |
179 } | |
180 | |
181 std::vector<Interest> res; | |
182 for (const base::Value* entry : *interests_list) { | |
183 const base::DictionaryValue* interest_dict = nullptr; | |
184 if (!entry->GetAsDictionary(&interest_dict)) { | |
185 DLOG(WARNING) << "Failed to parse interest dictionary."; | |
186 continue; | |
187 } | |
188 | |
189 // Extract the parts of the interest. | |
190 std::string name; | |
191 std::string image_url; | |
192 double relevance; | |
Marc Treib
2015/10/06 13:38:32
nit: Initialize?
PEConn
2015/10/06 14:21:29
Done.
| |
193 | |
194 if (!interest_dict->GetString(kIdInterestName, &name)) { | |
195 DLOG(WARNING) << "Failed to parse interest name."; | |
196 continue; | |
197 } | |
198 | |
199 if (!interest_dict->GetString(kIdInterestImageUrl, &image_url)) { | |
200 DLOG(WARNING) << "Failed to parse interest image URL."; | |
201 // image_url is not mandatory, however warn if omitted. | |
202 } | |
203 | |
204 if (!interest_dict->GetDouble(kIdInterestRelevance, &relevance)) { | |
205 DLOG(WARNING) << "Failed to parse interest relevance."; | |
206 continue; | |
207 } | |
208 | |
209 res.push_back(Interest{name, GURL(image_url), relevance}); | |
210 } | |
211 | |
212 return res; | |
213 } | |
OLD | NEW |