Chromium Code Reviews| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 virtual ~RequestImpl() { | 46 virtual ~RequestImpl() { |
| 47 } | 47 } |
| 48 | 48 |
| 49 // Returns the response code received from the server, which will only be | 49 // Returns the response code received from the server, which will only be |
| 50 // valid if the request succeeded. | 50 // valid if the request succeeded. |
| 51 int response_code() { return response_code_; } | 51 int response_code() { return response_code_; } |
| 52 | 52 |
| 53 // Returns the contents of the response body received from the server. | 53 // Returns the contents of the response body received from the server. |
| 54 const std::string& response_body() { return response_body_; } | 54 const std::string& response_body() { return response_body_; } |
| 55 | 55 |
| 56 virtual bool is_pending() OVERRIDE { return is_pending_; } | |
| 57 | |
| 56 private: | 58 private: |
| 57 friend class history::WebHistoryService; | 59 friend class history::WebHistoryService; |
| 58 | 60 |
| 59 typedef base::Callback<void(Request*, bool)> CompletionCallback; | 61 typedef base::Callback<void(Request*, bool)> CompletionCallback; |
| 60 | 62 |
| 61 RequestImpl(Profile* profile, | 63 RequestImpl(Profile* profile, |
| 62 const std::string& url, | 64 const std::string& url, |
| 63 const CompletionCallback& callback) | 65 const CompletionCallback& callback) |
| 64 : profile_(profile), | 66 : profile_(profile), |
| 65 url_(GURL(url)), | 67 url_(GURL(url)), |
| 66 response_code_(0), | 68 response_code_(0), |
| 67 auth_retry_count_(0), | 69 auth_retry_count_(0), |
| 68 callback_(callback) { | 70 callback_(callback), |
| 71 is_pending_(false) { | |
| 69 } | 72 } |
| 70 | 73 |
| 71 // Tells the request to do its thang. | 74 // Tells the request to do its thang. |
| 72 void Start() { | 75 void Start() { |
| 73 OAuth2TokenService::ScopeSet oauth_scopes; | 76 OAuth2TokenService::ScopeSet oauth_scopes; |
| 74 oauth_scopes.insert(kHistoryOAuthScope); | 77 oauth_scopes.insert(kHistoryOAuthScope); |
| 75 | 78 |
| 76 OAuth2TokenService* token_service = | 79 OAuth2TokenService* token_service = |
| 77 OAuth2TokenServiceFactory::GetForProfile(profile_); | 80 OAuth2TokenServiceFactory::GetForProfile(profile_); |
| 78 token_request_ = token_service->StartRequest(oauth_scopes, this); | 81 token_request_ = token_service->StartRequest(oauth_scopes, this); |
| 82 is_pending_ = true; | |
| 79 } | 83 } |
| 80 | 84 |
| 81 // content::URLFetcherDelegate interface. | 85 // content::URLFetcherDelegate interface. |
| 82 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { | 86 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { |
| 83 DCHECK_EQ(source, url_fetcher_.get()); | 87 DCHECK_EQ(source, url_fetcher_.get()); |
| 84 response_code_ = url_fetcher_->GetResponseCode(); | 88 response_code_ = url_fetcher_->GetResponseCode(); |
| 85 | 89 |
| 86 // If the response code indicates that the token might not be valid, | 90 // If the response code indicates that the token might not be valid, |
| 87 // invalidate the token and try again. | 91 // invalidate the token and try again. |
| 88 if (response_code_ == net::HTTP_UNAUTHORIZED && ++auth_retry_count_ <= 1) { | 92 if (response_code_ == net::HTTP_UNAUTHORIZED && ++auth_retry_count_ <= 1) { |
| 89 OAuth2TokenService::ScopeSet oauth_scopes; | 93 OAuth2TokenService::ScopeSet oauth_scopes; |
| 90 oauth_scopes.insert(kHistoryOAuthScope); | 94 oauth_scopes.insert(kHistoryOAuthScope); |
| 91 OAuth2TokenServiceFactory::GetForProfile(profile_)->InvalidateToken( | 95 OAuth2TokenServiceFactory::GetForProfile(profile_)->InvalidateToken( |
| 92 oauth_scopes, access_token_); | 96 oauth_scopes, access_token_); |
| 93 | 97 |
| 94 access_token_ = std::string(); | 98 access_token_ = std::string(); |
| 95 Start(); | 99 Start(); |
| 96 return; | 100 return; |
| 97 } | 101 } |
| 98 url_fetcher_->GetResponseAsString(&response_body_); | 102 url_fetcher_->GetResponseAsString(&response_body_); |
| 99 url_fetcher_.reset(); | 103 url_fetcher_.reset(); |
| 100 callback_.Run(this, true); | 104 callback_.Run(this, true); |
| 105 is_pending_ = false; | |
| 101 } | 106 } |
| 102 | 107 |
| 103 // OAuth2TokenService::Consumer interface. | 108 // OAuth2TokenService::Consumer interface. |
| 104 virtual void OnGetTokenSuccess( | 109 virtual void OnGetTokenSuccess( |
| 105 const OAuth2TokenService::Request* request, | 110 const OAuth2TokenService::Request* request, |
| 106 const std::string& access_token, | 111 const std::string& access_token, |
| 107 const base::Time& expiration_time) OVERRIDE { | 112 const base::Time& expiration_time) OVERRIDE { |
| 108 token_request_.reset(); | 113 token_request_.reset(); |
| 109 DCHECK(!access_token.empty()); | 114 DCHECK(!access_token.empty()); |
| 110 access_token_ = access_token; | 115 access_token_ = access_token; |
| 111 | 116 |
| 112 // Got an access token -- start the actual API request. | 117 // Got an access token -- start the actual API request. |
| 113 url_fetcher_.reset(CreateUrlFetcher(access_token)); | 118 url_fetcher_.reset(CreateUrlFetcher(access_token)); |
| 114 url_fetcher_->Start(); | 119 url_fetcher_->Start(); |
| 115 } | 120 } |
| 116 | 121 |
| 117 virtual void OnGetTokenFailure( | 122 virtual void OnGetTokenFailure( |
| 118 const OAuth2TokenService::Request* request, | 123 const OAuth2TokenService::Request* request, |
| 119 const GoogleServiceAuthError& error) OVERRIDE { | 124 const GoogleServiceAuthError& error) OVERRIDE { |
| 120 token_request_.reset(); | 125 token_request_.reset(); |
| 121 LOG(WARNING) << "Failed to get OAuth token: " << error.ToString(); | 126 LOG(WARNING) << "Failed to get OAuth token: " << error.ToString(); |
| 122 callback_.Run(this, false); | 127 callback_.Run(this, false); |
| 128 is_pending_ = false; | |
|
James Hawkins
2013/04/17 16:59:25
This seems fragile. Can we not use callback_ some
| |
| 123 } | 129 } |
| 124 | 130 |
| 125 // Helper for creating a new URLFetcher for the API request. | 131 // Helper for creating a new URLFetcher for the API request. |
| 126 net::URLFetcher* CreateUrlFetcher(const std::string& access_token) { | 132 net::URLFetcher* CreateUrlFetcher(const std::string& access_token) { |
| 127 net::URLFetcher::RequestType request_type = post_data_.empty() ? | 133 net::URLFetcher::RequestType request_type = post_data_.empty() ? |
| 128 net::URLFetcher::GET : net::URLFetcher::POST; | 134 net::URLFetcher::GET : net::URLFetcher::POST; |
| 129 net::URLFetcher* fetcher = net::URLFetcher::Create( | 135 net::URLFetcher* fetcher = net::URLFetcher::Create( |
| 130 url_, request_type, this); | 136 url_, request_type, this); |
| 131 fetcher->SetRequestContext(profile_->GetRequestContext()); | 137 fetcher->SetRequestContext(profile_->GetRequestContext()); |
| 132 fetcher->SetMaxRetriesOn5xx(kMaxRetries); | 138 fetcher->SetMaxRetriesOn5xx(kMaxRetries); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 | 172 |
| 167 // Holds the response body received from the server. | 173 // Holds the response body received from the server. |
| 168 std::string response_body_; | 174 std::string response_body_; |
| 169 | 175 |
| 170 // The number of times this request has already been retried due to | 176 // The number of times this request has already been retried due to |
| 171 // authorization problems. | 177 // authorization problems. |
| 172 int auth_retry_count_; | 178 int auth_retry_count_; |
| 173 | 179 |
| 174 // The callback to execute when the query is complete. | 180 // The callback to execute when the query is complete. |
| 175 CompletionCallback callback_; | 181 CompletionCallback callback_; |
| 182 | |
| 183 // True if the request was started and has not yet completed, otherwise false. | |
| 184 bool is_pending_; | |
| 176 }; | 185 }; |
| 177 | 186 |
| 178 // Extracts a JSON-encoded HTTP response into a DictionaryValue. | 187 // Extracts a JSON-encoded HTTP response into a DictionaryValue. |
| 179 // If |request|'s HTTP response code indicates failure, or if the response | 188 // If |request|'s HTTP response code indicates failure, or if the response |
| 180 // body is not JSON, a null pointer is returned. | 189 // body is not JSON, a null pointer is returned. |
| 181 scoped_ptr<DictionaryValue> ReadResponse(RequestImpl* request) { | 190 scoped_ptr<DictionaryValue> ReadResponse(RequestImpl* request) { |
| 182 scoped_ptr<DictionaryValue> result; | 191 scoped_ptr<DictionaryValue> result; |
| 183 if (request->response_code() == net::HTTP_OK) { | 192 if (request->response_code() == net::HTTP_OK) { |
| 184 Value* value = base::JSONReader::Read(request->response_body()); | 193 Value* value = base::JSONReader::Read(request->response_body()); |
| 185 if (value && value->IsType(base::Value::TYPE_DICTIONARY)) | 194 if (value && value->IsType(base::Value::TYPE_DICTIONARY)) |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 357 base::Time end_time, | 366 base::Time end_time, |
| 358 const ExpireWebHistoryCallback& callback) { | 367 const ExpireWebHistoryCallback& callback) { |
| 359 std::vector<ExpireHistoryArgs> expire_list(1); | 368 std::vector<ExpireHistoryArgs> expire_list(1); |
| 360 expire_list.back().urls = restrict_urls; | 369 expire_list.back().urls = restrict_urls; |
| 361 expire_list.back().begin_time = begin_time; | 370 expire_list.back().begin_time = begin_time; |
| 362 expire_list.back().end_time = end_time; | 371 expire_list.back().end_time = end_time; |
| 363 return ExpireHistory(expire_list, callback); | 372 return ExpireHistory(expire_list, callback); |
| 364 } | 373 } |
| 365 | 374 |
| 366 } // namespace history | 375 } // namespace history |
| OLD | NEW |