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

Side by Side Diff: chrome/browser/supervised_user/experimental/supervised_user_report_url_apiary.cc

Issue 1813833002: Add report URL to safe search API functionality. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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
OLDNEW
(Empty)
1 // Copyright 2014 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/supervised_user/experimental/supervised_user_report_url _apiary.h"
6
7 #include "base/callback.h"
8 #include "base/json/json_writer.h"
9 #include "base/strings/stringprintf.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
12 #include "chrome/browser/signin/signin_manager_factory.h"
13 #include "components/signin/core/browser/profile_oauth2_token_service.h"
14 #include "components/signin/core/browser/signin_manager.h"
15 #include "components/signin/core/browser/signin_manager_base.h"
16 #include "google_apis/gaia/google_service_auth_error.h"
17 #include "net/base/load_flags.h"
18 #include "net/base/net_errors.h"
19 #include "net/http/http_status_code.h"
20 #include "net/url_request/url_fetcher.h"
21 #include "net/url_request/url_request_status.h"
22 #include "url/gurl.h"
23
24 using net::URLFetcher;
25
26 const char kApiUrl[] =
27 "https://safesearch.googleapis.com/v1:report";
28 const char kApiScope[] = "https://www.googleapis.com/auth/safesearch.reporting";
29
30 const int kNumRetries = 1;
31
32 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s";
33
34 // Request keys
35 const char kUrlKey[] = "url";
36
37
38 struct SupervisedUserReportURLApiary::Request {
39 Request(const GURL& url,
40 const SuccessCallback& callback,
41 int url_fetcher_id);
42 ~Request();
43
44 GURL url;
45 SuccessCallback callback;
46 scoped_ptr<OAuth2TokenService::Request> access_token_request;
47 std::string access_token;
48 bool access_token_expired;
49 int url_fetcher_id;
50 scoped_ptr<URLFetcher> url_fetcher;
51 };
52
53 SupervisedUserReportURLApiary::Request::Request(
54 const GURL& url,
55 const SuccessCallback& callback,
56 int url_fetcher_id)
57 : url(url),
58 callback(callback),
59 access_token_expired(false),
60 url_fetcher_id(url_fetcher_id) {}
61
62 SupervisedUserReportURLApiary::Request::~Request() {}
63
64 SupervisedUserReportURLApiary::SupervisedUserReportURLApiary(
65 OAuth2TokenService* oauth2_token_service,
66 const std::string& account_id,
67 net::URLRequestContextGetter* context)
68 : OAuth2TokenService::Consumer("report_url"),
69 oauth2_token_service_(oauth2_token_service),
70 account_id_(account_id),
71 context_(context),
72 url_fetcher_id_(0) {}
73
74 SupervisedUserReportURLApiary::~SupervisedUserReportURLApiary() {}
75
76 // static
77 scoped_ptr<ReportUrlRequestCreator>
78 SupervisedUserReportURLApiary::CreateWithProfile(Profile* profile) {
79 ProfileOAuth2TokenService* token_service =
80 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
81 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile);
82 return make_scoped_ptr(new SupervisedUserReportURLApiary(
83 token_service,
84 signin->GetAuthenticatedAccountId(),
85 profile->GetRequestContext()));
86 }
87
88 void SupervisedUserReportURLApiary::CreateReportUrlRequest(
89 const GURL& url,
90 const SuccessCallback& callback) {
91 requests_.push_back(make_scoped_ptr(
92 new Request(url, callback, url_fetcher_id_)));
93 StartFetching(requests_.back().get());
94 }
95
96 void SupervisedUserReportURLApiary::StartFetching(
97 Request* request) {
98 OAuth2TokenService::ScopeSet scopes;
99 scopes.insert(kApiScope);
100 request->access_token_request = oauth2_token_service_->StartRequest(
101 account_id_, scopes, this);
102 }
103
104 void SupervisedUserReportURLApiary::OnGetTokenSuccess(
105 const OAuth2TokenService::Request* request,
106 const std::string& access_token,
107 const base::Time& expiration_time) {
108 RequestIterator it = requests_.begin();
109 while (it != requests_.end()) {
110 if (request == (*it)->access_token_request.get())
111 break;
112 it++;
113 }
114 DCHECK(it != requests_.end());
115
116 (*it)->access_token = access_token;
117
118 (*it)->url_fetcher = URLFetcher::Create((*it)->url_fetcher_id, GURL(kApiUrl),
119 URLFetcher::POST, this);
120
121 (*it)->url_fetcher->SetRequestContext(context_);
122 (*it)->url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
123 net::LOAD_DO_NOT_SAVE_COOKIES);
124 (*it)->url_fetcher->SetAutomaticallyRetryOnNetworkChanges(kNumRetries);
125 (*it)->url_fetcher->AddExtraRequestHeader(
126 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str()));
127
128 base::DictionaryValue dict;
129 dict.SetStringWithoutPathExpansion(kUrlKey, (*it)->url.spec().c_str());
130
131 std::string body;
132 base::JSONWriter::Write(dict, &body);
133 (*it)->url_fetcher->SetUploadData("application/json", body);
134
135 (*it)->url_fetcher->Start();
136 }
137
138 void SupervisedUserReportURLApiary::OnGetTokenFailure(
139 const OAuth2TokenService::Request* request,
140 const GoogleServiceAuthError& error) {
141 RequestIterator it = requests_.begin();
142 while (it != requests_.end()) {
143 if (request == (*it)->access_token_request.get())
144 break;
145 it++;
146 }
147 DCHECK(it != requests_.end());
148 LOG(WARNING) << "Token error: " << error.ToString();
149 DispatchResult(it, false);
150 }
151
152 void SupervisedUserReportURLApiary::OnURLFetchComplete(
153 const URLFetcher* source) {
154 RequestIterator it = requests_.begin();
155 while (it != requests_.end()) {
156 if (source == (*it)->url_fetcher.get())
157 break;
158 ++it;
159 }
160 DCHECK(it != requests_.end());
161
162 const net::URLRequestStatus& status = source->GetStatus();
163 if (!status.is_success()) {
164 LOG(WARNING) << "Network error " << status.error();
165 DispatchResult(it, false);
166 return;
167 }
168
169 int response_code = source->GetResponseCode();
170 if (response_code == net::HTTP_UNAUTHORIZED && !(*it)->access_token_expired) {
171 (*it)->access_token_expired = true;
172 OAuth2TokenService::ScopeSet scopes;
173 scopes.insert(kApiScope);
174 oauth2_token_service_->InvalidateAccessToken(account_id_, scopes,
175 (*it)->access_token);
176 StartFetching((*it).get());
177 return;
178 }
179
180 if (response_code != net::HTTP_OK) {
181 LOG(WARNING) << "HTTP error " << response_code;
182 DispatchResult(it, false);
183 return;
184 }
185
186 DispatchResult(it, true);
187 }
188
189 void SupervisedUserReportURLApiary::DispatchResult(RequestIterator it,
190 bool success) {
191 (*it)->callback.Run(success);
192 requests_.erase(it);
193 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698