| 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), |
| 67 callback_(callback) { | 68 callback_(callback) { |
| 68 } | 69 } |
| 69 | 70 |
| 70 // Tells the request to do its thang. | 71 // Tells the request to do its thang. |
| 71 void Start() { | 72 void Start() { |
| 72 OAuth2TokenService::ScopeSet oauth_scopes; | 73 OAuth2TokenService::ScopeSet oauth_scopes; |
| 73 oauth_scopes.insert(kHistoryOAuthScope); | 74 oauth_scopes.insert(kHistoryOAuthScope); |
| 74 | 75 |
| 75 OAuth2TokenService* token_service = | 76 OAuth2TokenService* token_service = |
| 76 OAuth2TokenServiceFactory::GetForProfile(profile_); | 77 OAuth2TokenServiceFactory::GetForProfile(profile_); |
| 77 token_request_ = token_service->StartRequest(oauth_scopes, this); | 78 token_request_ = token_service->StartRequest(oauth_scopes, this); |
| 78 } | 79 } |
| 79 | 80 |
| 80 // content::URLFetcherDelegate interface. | 81 // content::URLFetcherDelegate interface. |
| 81 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { | 82 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { |
| 82 DCHECK_EQ(source, url_fetcher_.get()); | 83 DCHECK_EQ(source, url_fetcher_.get()); |
| 83 response_code_ = url_fetcher_->GetResponseCode(); | 84 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 } |
| 84 url_fetcher_->GetResponseAsString(&response_body_); | 98 url_fetcher_->GetResponseAsString(&response_body_); |
| 85 url_fetcher_.reset(); | 99 url_fetcher_.reset(); |
| 86 callback_.Run(this, true); | 100 callback_.Run(this, true); |
| 87 } | 101 } |
| 88 | 102 |
| 89 // OAuth2TokenService::Consumer interface. | 103 // OAuth2TokenService::Consumer interface. |
| 90 virtual void OnGetTokenSuccess( | 104 virtual void OnGetTokenSuccess( |
| 91 const OAuth2TokenService::Request* request, | 105 const OAuth2TokenService::Request* request, |
| 92 const std::string& access_token, | 106 const std::string& access_token, |
| 93 const base::Time& expiration_time) OVERRIDE { | 107 const base::Time& expiration_time) OVERRIDE { |
| 94 token_request_.reset(); | 108 token_request_.reset(); |
| 95 DCHECK(!access_token.empty()); | 109 DCHECK(!access_token.empty()); |
| 110 access_token_ = access_token; |
| 96 | 111 |
| 97 // Got an access token -- start the actual API request. | 112 // Got an access token -- start the actual API request. |
| 98 url_fetcher_.reset(CreateUrlFetcher(access_token)); | 113 url_fetcher_.reset(CreateUrlFetcher(access_token)); |
| 99 url_fetcher_->Start(); | 114 url_fetcher_->Start(); |
| 100 } | 115 } |
| 101 | 116 |
| 102 virtual void OnGetTokenFailure( | 117 virtual void OnGetTokenFailure( |
| 103 const OAuth2TokenService::Request* request, | 118 const OAuth2TokenService::Request* request, |
| 104 const GoogleServiceAuthError& error) OVERRIDE { | 119 const GoogleServiceAuthError& error) OVERRIDE { |
| 105 token_request_.reset(); | 120 token_request_.reset(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 133 | 148 |
| 134 // The URL of the API endpoint. | 149 // The URL of the API endpoint. |
| 135 GURL url_; | 150 GURL url_; |
| 136 | 151 |
| 137 // POST data to be sent with the request (may be empty). | 152 // POST data to be sent with the request (may be empty). |
| 138 std::string post_data_; | 153 std::string post_data_; |
| 139 | 154 |
| 140 // The OAuth2 access token request. | 155 // The OAuth2 access token request. |
| 141 scoped_ptr<OAuth2TokenService::Request> token_request_; | 156 scoped_ptr<OAuth2TokenService::Request> token_request_; |
| 142 | 157 |
| 158 // The current OAuth2 access token. |
| 159 std::string access_token_; |
| 160 |
| 143 // Handles the actual API requests after the OAuth token is acquired. | 161 // Handles the actual API requests after the OAuth token is acquired. |
| 144 scoped_ptr<net::URLFetcher> url_fetcher_; | 162 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 145 | 163 |
| 146 // Holds the response code received from the server. | 164 // Holds the response code received from the server. |
| 147 int response_code_; | 165 int response_code_; |
| 148 | 166 |
| 149 // Holds the response body received from the server. | 167 // Holds the response body received from the server. |
| 150 std::string response_body_; | 168 std::string response_body_; |
| 151 | 169 |
| 170 // The number of times this request has already been retried due to |
| 171 // authorization problems. |
| 172 int auth_retry_count_; |
| 173 |
| 152 // The callback to execute when the query is complete. | 174 // The callback to execute when the query is complete. |
| 153 CompletionCallback callback_; | 175 CompletionCallback callback_; |
| 154 }; | 176 }; |
| 155 | 177 |
| 156 // Called when a query to web history has completed, successfully or not. | 178 // Called when a query to web history has completed, successfully or not. |
| 157 void QueryHistoryCompletionCallback( | 179 void QueryHistoryCompletionCallback( |
| 158 const WebHistoryService::QueryWebHistoryCallback& callback, | 180 const WebHistoryService::QueryWebHistoryCallback& callback, |
| 159 WebHistoryService::Request* request, | 181 WebHistoryService::Request* request, |
| 160 bool success) { | 182 bool success) { |
| 161 RequestImpl* request_impl = static_cast<RequestImpl*>(request); | 183 RequestImpl* request_impl = static_cast<RequestImpl*>(request); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 base::Time end_time, | 318 base::Time end_time, |
| 297 const ExpireWebHistoryCallback& callback) { | 319 const ExpireWebHistoryCallback& callback) { |
| 298 std::vector<ExpireHistoryArgs> expire_list(1); | 320 std::vector<ExpireHistoryArgs> expire_list(1); |
| 299 expire_list.back().urls = restrict_urls; | 321 expire_list.back().urls = restrict_urls; |
| 300 expire_list.back().begin_time = begin_time; | 322 expire_list.back().begin_time = begin_time; |
| 301 expire_list.back().end_time = end_time; | 323 expire_list.back().end_time = end_time; |
| 302 return ExpireHistory(expire_list, callback); | 324 return ExpireHistory(expire_list, callback); |
| 303 } | 325 } |
| 304 | 326 |
| 305 } // namespace history | 327 } // namespace history |
| OLD | NEW |