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