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