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..60d18de7e46ff4dead6846ca96b0ddcab7110476 100644 |
--- a/chrome/browser/local_discovery/privet_url_fetcher.cc |
+++ b/chrome/browser/local_discovery/privet_url_fetcher.cc |
@@ -8,6 +8,7 @@ |
#include "base/bind.h" |
#include "base/json/json_reader.h" |
+#include "base/memory/singleton.h" |
#include "base/message_loop/message_loop.h" |
#include "base/rand_util.h" |
#include "chrome/browser/browser_process.h" |
@@ -19,11 +20,24 @@ |
namespace local_discovery { |
namespace { |
+ |
+typedef std::map<std::string, std::string> TokenMap; |
+ |
+struct TokenMapHolder { |
+ public: |
+ static TokenMapHolder* GetInstance() { |
+ return Singleton<TokenMapHolder>::get(); |
+ } |
+ |
+ TokenMap map; |
+}; |
+ |
const char kXPrivetTokenHeaderPrefix[] = "X-Privet-Token: "; |
const char kXPrivetEmptyToken[] = "\"\""; |
const int kPrivetMaxRetries = 20; |
const int kPrivetTimeoutOnError = 5; |
-} |
+ |
+} // namespace |
void PrivetURLFetcher::Delegate::OnNeedPrivetToken( |
PrivetURLFetcher* fetcher, |
@@ -32,12 +46,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 +59,17 @@ PrivetURLFetcher::PrivetURLFetcher( |
PrivetURLFetcher::~PrivetURLFetcher() { |
} |
+// static |
+void PrivetURLFetcher::SetTokenForHost(const std::string& host, |
+ const std::string& token) { |
+ TokenMapHolder::GetInstance()->map[host] = token; |
+} |
+ |
+// static |
+void PrivetURLFetcher::ResetTokenMapForTests() { |
+ TokenMapHolder::GetInstance()->map.clear(); |
+} |
+ |
void PrivetURLFetcher::DoNotRetryOnTransientError() { |
do_not_retry_on_transient_error_ = true; |
} |
@@ -54,10 +78,24 @@ void PrivetURLFetcher::AllowEmptyPrivetToken() { |
allow_empty_privet_token_ = true; |
} |
+std::string PrivetURLFetcher::GetPrivetAccessToken() { |
+ TokenMapHolder* token_map_holder = TokenMapHolder::GetInstance(); |
+ TokenMap::iterator found = token_map_holder->map.find(GetHostString()); |
+ if (found != token_map_holder->map.end()) { |
Vitaly Buka (NO REVIEWS)
2014/02/14 23:34:12
return found != token_map_holder->map.end() ? foun
Noam Samuel
2014/02/15 00:22:15
Done.
|
+ 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 +129,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 +236,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 +247,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 |