OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/safe_search_url_reporter.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[] = "https://safesearch.googleapis.com/v1:report"; |
| 27 const char kApiScope[] = "https://www.googleapis.com/auth/safesearch.reporting"; |
| 28 |
| 29 const int kNumRetries = 1; |
| 30 |
| 31 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; |
| 32 |
| 33 // Request keys |
| 34 const char kUrlKey[] = "url"; |
| 35 |
| 36 struct SafeSearchUrlReporter::Report { |
| 37 Report(const GURL& url, const SuccessCallback& callback, int url_fetcher_id); |
| 38 ~Report(); |
| 39 |
| 40 GURL url; |
| 41 SuccessCallback callback; |
| 42 scoped_ptr<OAuth2TokenService::Request> access_token_request; |
| 43 std::string access_token; |
| 44 bool access_token_expired; |
| 45 int url_fetcher_id; |
| 46 scoped_ptr<URLFetcher> url_fetcher; |
| 47 }; |
| 48 |
| 49 SafeSearchUrlReporter::Report::Report(const GURL& url, |
| 50 const SuccessCallback& callback, |
| 51 int url_fetcher_id) |
| 52 : url(url), |
| 53 callback(callback), |
| 54 access_token_expired(false), |
| 55 url_fetcher_id(url_fetcher_id) {} |
| 56 |
| 57 SafeSearchUrlReporter::Report::~Report() {} |
| 58 |
| 59 SafeSearchUrlReporter::SafeSearchUrlReporter( |
| 60 OAuth2TokenService* oauth2_token_service, |
| 61 const std::string& account_id, |
| 62 net::URLRequestContextGetter* context) |
| 63 : OAuth2TokenService::Consumer("safe_search_url_reporter"), |
| 64 oauth2_token_service_(oauth2_token_service), |
| 65 account_id_(account_id), |
| 66 context_(context), |
| 67 url_fetcher_id_(0) {} |
| 68 |
| 69 SafeSearchUrlReporter::~SafeSearchUrlReporter() {} |
| 70 |
| 71 // static |
| 72 scoped_ptr<SafeSearchUrlReporter> SafeSearchUrlReporter::CreateWithProfile( |
| 73 Profile* profile) { |
| 74 ProfileOAuth2TokenService* token_service = |
| 75 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 76 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); |
| 77 return make_scoped_ptr(new SafeSearchUrlReporter( |
| 78 token_service, signin->GetAuthenticatedAccountId(), |
| 79 profile->GetRequestContext())); |
| 80 } |
| 81 |
| 82 void SafeSearchUrlReporter::ReportUrl(const GURL& url, |
| 83 const SuccessCallback& callback) { |
| 84 reports_.push_back( |
| 85 make_scoped_ptr(new Report(url, callback, url_fetcher_id_))); |
| 86 StartFetching(reports_.back().get()); |
| 87 } |
| 88 |
| 89 void SafeSearchUrlReporter::StartFetching(Report* report) { |
| 90 OAuth2TokenService::ScopeSet scopes; |
| 91 scopes.insert(kApiScope); |
| 92 report->access_token_request = |
| 93 oauth2_token_service_->StartRequest(account_id_, scopes, this); |
| 94 } |
| 95 |
| 96 void SafeSearchUrlReporter::OnGetTokenSuccess( |
| 97 const OAuth2TokenService::Request* request, |
| 98 const std::string& access_token, |
| 99 const base::Time& expiration_time) { |
| 100 ReportIterator it = reports_.begin(); |
| 101 while (it != reports_.end()) { |
| 102 if (request == (*it)->access_token_request.get()) |
| 103 break; |
| 104 it++; |
| 105 } |
| 106 DCHECK(it != reports_.end()); |
| 107 |
| 108 (*it)->access_token = access_token; |
| 109 |
| 110 (*it)->url_fetcher = URLFetcher::Create((*it)->url_fetcher_id, GURL(kApiUrl), |
| 111 URLFetcher::POST, this); |
| 112 |
| 113 (*it)->url_fetcher->SetRequestContext(context_); |
| 114 (*it)->url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 115 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 116 (*it)->url_fetcher->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); |
| 117 (*it)->url_fetcher->AddExtraRequestHeader( |
| 118 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str())); |
| 119 |
| 120 base::DictionaryValue dict; |
| 121 dict.SetStringWithoutPathExpansion(kUrlKey, (*it)->url.spec().c_str()); |
| 122 |
| 123 std::string body; |
| 124 base::JSONWriter::Write(dict, &body); |
| 125 (*it)->url_fetcher->SetUploadData("application/json", body); |
| 126 |
| 127 (*it)->url_fetcher->Start(); |
| 128 } |
| 129 |
| 130 void SafeSearchUrlReporter::OnGetTokenFailure( |
| 131 const OAuth2TokenService::Request* request, |
| 132 const GoogleServiceAuthError& error) { |
| 133 ReportIterator it = reports_.begin(); |
| 134 while (it != reports_.end()) { |
| 135 if (request == (*it)->access_token_request.get()) |
| 136 break; |
| 137 it++; |
| 138 } |
| 139 DCHECK(it != reports_.end()); |
| 140 LOG(WARNING) << "Token error: " << error.ToString(); |
| 141 DispatchResult(it, false); |
| 142 } |
| 143 |
| 144 void SafeSearchUrlReporter::OnURLFetchComplete(const URLFetcher* source) { |
| 145 ReportIterator it = reports_.begin(); |
| 146 while (it != reports_.end()) { |
| 147 if (source == (*it)->url_fetcher.get()) |
| 148 break; |
| 149 ++it; |
| 150 } |
| 151 DCHECK(it != reports_.end()); |
| 152 |
| 153 const net::URLRequestStatus& status = source->GetStatus(); |
| 154 if (!status.is_success()) { |
| 155 LOG(WARNING) << "Network error " << status.error(); |
| 156 DispatchResult(it, false); |
| 157 return; |
| 158 } |
| 159 |
| 160 int response_code = source->GetResponseCode(); |
| 161 if (response_code == net::HTTP_UNAUTHORIZED && !(*it)->access_token_expired) { |
| 162 (*it)->access_token_expired = true; |
| 163 OAuth2TokenService::ScopeSet scopes; |
| 164 scopes.insert(kApiScope); |
| 165 oauth2_token_service_->InvalidateAccessToken(account_id_, scopes, |
| 166 (*it)->access_token); |
| 167 StartFetching((*it).get()); |
| 168 return; |
| 169 } |
| 170 |
| 171 if (response_code != net::HTTP_OK) { |
| 172 LOG(WARNING) << "HTTP error " << response_code; |
| 173 DispatchResult(it, false); |
| 174 return; |
| 175 } |
| 176 |
| 177 DispatchResult(it, true); |
| 178 } |
| 179 |
| 180 void SafeSearchUrlReporter::DispatchResult(ReportIterator it, bool success) { |
| 181 (*it)->callback.Run(success); |
| 182 reports_.erase(it); |
| 183 } |
OLD | NEW |