| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/history/web_history_service.h" | 5 #include "chrome/browser/history/web_history_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 friend class history::WebHistoryService; | 57 friend class history::WebHistoryService; |
| 58 | 58 |
| 59 typedef base::Callback<void(Request*, bool)> CompletionCallback; | 59 typedef base::Callback<void(Request*, bool)> CompletionCallback; |
| 60 | 60 |
| 61 RequestImpl(Profile* profile, | 61 RequestImpl(Profile* profile, |
| 62 const std::string& url, | 62 const std::string& url, |
| 63 const CompletionCallback& callback) | 63 const CompletionCallback& callback) |
| 64 : profile_(profile), | 64 : profile_(profile), |
| 65 url_(GURL(url)), | 65 url_(GURL(url)), |
| 66 response_code_(0), | 66 response_code_(0), |
| 67 auth_retry_count_(0), | |
| 68 callback_(callback) { | 67 callback_(callback) { |
| 69 } | 68 } |
| 70 | 69 |
| 71 // Tells the request to do its thang. | 70 // Tells the request to do its thang. |
| 72 void Start() { | 71 void Start() { |
| 73 OAuth2TokenService::ScopeSet oauth_scopes; | 72 OAuth2TokenService::ScopeSet oauth_scopes; |
| 74 oauth_scopes.insert(kHistoryOAuthScope); | 73 oauth_scopes.insert(kHistoryOAuthScope); |
| 75 | 74 |
| 76 OAuth2TokenService* token_service = | 75 OAuth2TokenService* token_service = |
| 77 OAuth2TokenServiceFactory::GetForProfile(profile_); | 76 OAuth2TokenServiceFactory::GetForProfile(profile_); |
| 78 token_request_ = token_service->StartRequest(oauth_scopes, this); | 77 token_request_ = token_service->StartRequest(oauth_scopes, this); |
| 79 } | 78 } |
| 80 | 79 |
| 81 // content::URLFetcherDelegate interface. | 80 // content::URLFetcherDelegate interface. |
| 82 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { | 81 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { |
| 83 DCHECK_EQ(source, url_fetcher_.get()); | 82 DCHECK_EQ(source, url_fetcher_.get()); |
| 84 response_code_ = url_fetcher_->GetResponseCode(); | 83 response_code_ = url_fetcher_->GetResponseCode(); |
| 85 | |
| 86 // If the response code indicates that the token might not be valid, | |
| 87 // invalidate the token and try again. | |
| 88 if (response_code_ == net::HTTP_UNAUTHORIZED && ++auth_retry_count_ <= 1) { | |
| 89 OAuth2TokenService::ScopeSet oauth_scopes; | |
| 90 oauth_scopes.insert(kHistoryOAuthScope); | |
| 91 OAuth2TokenServiceFactory::GetForProfile(profile_)->InvalidateToken( | |
| 92 oauth_scopes, access_token_); | |
| 93 | |
| 94 access_token_ = std::string(); | |
| 95 Start(); | |
| 96 return; | |
| 97 } | |
| 98 url_fetcher_->GetResponseAsString(&response_body_); | 84 url_fetcher_->GetResponseAsString(&response_body_); |
| 99 url_fetcher_.reset(); | 85 url_fetcher_.reset(); |
| 100 callback_.Run(this, true); | 86 callback_.Run(this, true); |
| 101 } | 87 } |
| 102 | 88 |
| 103 // OAuth2TokenService::Consumer interface. | 89 // OAuth2TokenService::Consumer interface. |
| 104 virtual void OnGetTokenSuccess( | 90 virtual void OnGetTokenSuccess( |
| 105 const OAuth2TokenService::Request* request, | 91 const OAuth2TokenService::Request* request, |
| 106 const std::string& access_token, | 92 const std::string& access_token, |
| 107 const base::Time& expiration_time) OVERRIDE { | 93 const base::Time& expiration_time) OVERRIDE { |
| 108 token_request_.reset(); | 94 token_request_.reset(); |
| 109 DCHECK(!access_token.empty()); | 95 DCHECK(!access_token.empty()); |
| 110 access_token_ = access_token; | |
| 111 | 96 |
| 112 // Got an access token -- start the actual API request. | 97 // Got an access token -- start the actual API request. |
| 113 url_fetcher_.reset(CreateUrlFetcher(access_token)); | 98 url_fetcher_.reset(CreateUrlFetcher(access_token)); |
| 114 url_fetcher_->Start(); | 99 url_fetcher_->Start(); |
| 115 } | 100 } |
| 116 | 101 |
| 117 virtual void OnGetTokenFailure( | 102 virtual void OnGetTokenFailure( |
| 118 const OAuth2TokenService::Request* request, | 103 const OAuth2TokenService::Request* request, |
| 119 const GoogleServiceAuthError& error) OVERRIDE { | 104 const GoogleServiceAuthError& error) OVERRIDE { |
| 120 token_request_.reset(); | 105 token_request_.reset(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 148 | 133 |
| 149 // The URL of the API endpoint. | 134 // The URL of the API endpoint. |
| 150 GURL url_; | 135 GURL url_; |
| 151 | 136 |
| 152 // POST data to be sent with the request (may be empty). | 137 // POST data to be sent with the request (may be empty). |
| 153 std::string post_data_; | 138 std::string post_data_; |
| 154 | 139 |
| 155 // The OAuth2 access token request. | 140 // The OAuth2 access token request. |
| 156 scoped_ptr<OAuth2TokenService::Request> token_request_; | 141 scoped_ptr<OAuth2TokenService::Request> token_request_; |
| 157 | 142 |
| 158 // The current OAuth2 access token. | |
| 159 std::string access_token_; | |
| 160 | |
| 161 // Handles the actual API requests after the OAuth token is acquired. | 143 // Handles the actual API requests after the OAuth token is acquired. |
| 162 scoped_ptr<net::URLFetcher> url_fetcher_; | 144 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 163 | 145 |
| 164 // Holds the response code received from the server. | 146 // Holds the response code received from the server. |
| 165 int response_code_; | 147 int response_code_; |
| 166 | 148 |
| 167 // Holds the response body received from the server. | 149 // Holds the response body received from the server. |
| 168 std::string response_body_; | 150 std::string response_body_; |
| 169 | 151 |
| 170 // The number of times this request has already been retried due to | |
| 171 // authorization problems. | |
| 172 int auth_retry_count_; | |
| 173 | |
| 174 // The callback to execute when the query is complete. | 152 // The callback to execute when the query is complete. |
| 175 CompletionCallback callback_; | 153 CompletionCallback callback_; |
| 176 }; | 154 }; |
| 177 | 155 |
| 178 // Called when a query to web history has completed, successfully or not. | 156 // Called when a query to web history has completed, successfully or not. |
| 179 void QueryHistoryCompletionCallback( | 157 void QueryHistoryCompletionCallback( |
| 180 const WebHistoryService::QueryWebHistoryCallback& callback, | 158 const WebHistoryService::QueryWebHistoryCallback& callback, |
| 181 WebHistoryService::Request* request, | 159 WebHistoryService::Request* request, |
| 182 bool success) { | 160 bool success) { |
| 183 RequestImpl* request_impl = static_cast<RequestImpl*>(request); | 161 RequestImpl* request_impl = static_cast<RequestImpl*>(request); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 base::Time end_time, | 296 base::Time end_time, |
| 319 const ExpireWebHistoryCallback& callback) { | 297 const ExpireWebHistoryCallback& callback) { |
| 320 std::vector<ExpireHistoryArgs> expire_list(1); | 298 std::vector<ExpireHistoryArgs> expire_list(1); |
| 321 expire_list.back().urls = restrict_urls; | 299 expire_list.back().urls = restrict_urls; |
| 322 expire_list.back().begin_time = begin_time; | 300 expire_list.back().begin_time = begin_time; |
| 323 expire_list.back().end_time = end_time; | 301 expire_list.back().end_time = end_time; |
| 324 return ExpireHistory(expire_list, callback); | 302 return ExpireHistory(expire_list, callback); |
| 325 } | 303 } |
| 326 | 304 |
| 327 } // namespace history | 305 } // namespace history |
| OLD | NEW |