Chromium Code Reviews| Index: chrome/browser/local_discovery/privet_url_fetcher.cc |
| diff --git a/chrome/browser/local_discovery/privet_url_fetcher.cc b/chrome/browser/local_discovery/privet_url_fetcher.cc |
| index 349555086c6c7c569d022307267fdb998d41b69a..165b12b417adfd639008cbe9d4b4483086a42635 100644 |
| --- a/chrome/browser/local_discovery/privet_url_fetcher.cc |
| +++ b/chrome/browser/local_discovery/privet_url_fetcher.cc |
| @@ -19,10 +19,14 @@ |
| namespace local_discovery { |
| namespace { |
|
Vitaly Buka (NO REVIEWS)
2014/02/14 22:57:46
empty line after { and before } below
Noam Samuel
2014/02/14 23:26:12
Done.
|
| +typedef std::map<std::string, std::string> TokenMap; |
| + |
| const char kXPrivetTokenHeaderPrefix[] = "X-Privet-Token: "; |
| const char kXPrivetEmptyToken[] = "\"\""; |
| const int kPrivetMaxRetries = 20; |
| const int kPrivetTimeoutOnError = 5; |
| + |
| +TokenMap* g_token_map = NULL; |
| } |
| void PrivetURLFetcher::Delegate::OnNeedPrivetToken( |
| @@ -32,12 +36,11 @@ void PrivetURLFetcher::Delegate::OnNeedPrivetToken( |
| } |
| PrivetURLFetcher::PrivetURLFetcher( |
| - const std::string& token, |
| const GURL& url, |
| net::URLFetcher::RequestType request_type, |
| net::URLRequestContextGetter* request_context, |
| PrivetURLFetcher::Delegate* delegate) |
| - : privet_access_token_(token), url_(url), request_type_(request_type), |
| + : url_(url), request_type_(request_type), |
| request_context_(request_context), delegate_(delegate), |
| do_not_retry_on_transient_error_(false), allow_empty_privet_token_(false), |
| tries_(0), weak_factory_(this) { |
| @@ -46,6 +49,19 @@ PrivetURLFetcher::PrivetURLFetcher( |
| PrivetURLFetcher::~PrivetURLFetcher() { |
| } |
| +// static |
| +void PrivetURLFetcher::SetTokenForHost(const std::string& host, |
|
Vitaly Buka (NO REVIEWS)
2014/02/14 22:57:46
double white space
Noam Samuel
2014/02/14 23:26:12
Done.
|
| + const std::string& token) { |
| + if (!g_token_map) g_token_map = new TokenMap(); |
|
Vitaly Buka (NO REVIEWS)
2014/02/14 22:57:46
Can you please use Singleton for g_tocket_map?
Noam Samuel
2014/02/14 23:26:12
Done.
|
| + (*g_token_map)[host] = token; |
| +} |
| + |
| +// static |
| +void PrivetURLFetcher::ResetTokenMapForTests() { |
| + if (g_token_map) delete g_token_map; |
| + g_token_map = NULL; |
| +} |
| + |
| void PrivetURLFetcher::DoNotRetryOnTransientError() { |
| do_not_retry_on_transient_error_ = true; |
| } |
| @@ -54,10 +70,24 @@ void PrivetURLFetcher::AllowEmptyPrivetToken() { |
| allow_empty_privet_token_ = true; |
| } |
| +std::string PrivetURLFetcher::GetPrivetAccessToken() { |
| + if (!g_token_map) return std::string(); |
| + TokenMap::iterator found = g_token_map->find(GetHostString()); |
| + if (found != g_token_map->end()) { |
| + return found->second; |
| + } else { |
| + return std::string(); |
| + } |
| +} |
| + |
| +std::string PrivetURLFetcher::GetHostString() { |
| + return url_.GetOrigin().spec(); |
| +} |
| + |
| void PrivetURLFetcher::Try() { |
| tries_++; |
| if (tries_ < kPrivetMaxRetries) { |
| - std::string token = privet_access_token_; |
| + std::string token = GetPrivetAccessToken(); |
| if (token.empty()) |
| token = kXPrivetEmptyToken; |
| @@ -91,7 +121,8 @@ void PrivetURLFetcher::Try() { |
| void PrivetURLFetcher::Start() { |
| DCHECK_EQ(tries_, 0); // We haven't called |Start()| yet. |
| - if (privet_access_token_.empty() && !allow_empty_privet_token_) { |
| + std::string privet_access_token = GetPrivetAccessToken(); |
| + if (privet_access_token.empty() && !allow_empty_privet_token_) { |
| RequestTokenRefresh(); |
| } else { |
| Try(); |
| @@ -197,7 +228,7 @@ void PrivetURLFetcher::RefreshToken(const std::string& token) { |
| if (token.empty()) { |
| delegate_->OnError(this, TOKEN_ERROR); |
| } else { |
| - privet_access_token_ = token; |
| + SetTokenForHost(GetHostString(), token); |
| Try(); |
| } |
| } |
| @@ -208,20 +239,4 @@ bool PrivetURLFetcher::PrivetErrorTransient(const std::string& error) { |
| (error == kPrivetErrorPrinterBusy); |
| } |
| -PrivetURLFetcherFactory::PrivetURLFetcherFactory( |
| - net::URLRequestContextGetter* request_context) |
| - : request_context_(request_context) { |
| -} |
| - |
| -PrivetURLFetcherFactory::~PrivetURLFetcherFactory() { |
| -} |
| - |
| -scoped_ptr<PrivetURLFetcher> PrivetURLFetcherFactory::CreateURLFetcher( |
| - const GURL& url, net::URLFetcher::RequestType request_type, |
| - PrivetURLFetcher::Delegate* delegate) const { |
| - return scoped_ptr<PrivetURLFetcher>( |
| - new PrivetURLFetcher(token_, url, request_type, request_context_.get(), |
| - delegate)); |
| -} |
| - |
| } // namespace local_discovery |