| 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/net/sdch_dictionary_fetcher.h" | 5 #include "net/base/sdch_dictionary_fetcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
| 12 #include "net/url_request/url_fetcher.h" | 11 #include "net/url_request/url_fetcher.h" |
| 13 #include "net/url_request/url_request_context_getter.h" | 12 #include "net/url_request/url_request_context_getter.h" |
| 14 #include "net/url_request/url_request_status.h" | 13 #include "net/url_request/url_request_status.h" |
| 15 | 14 |
| 15 namespace net { |
| 16 |
| 16 SdchDictionaryFetcher::SdchDictionaryFetcher( | 17 SdchDictionaryFetcher::SdchDictionaryFetcher( |
| 17 net::SdchManager* manager, | 18 SdchManager* manager, |
| 18 net::URLRequestContextGetter* context) | 19 URLRequestContextGetter* context) |
| 19 : manager_(manager), | 20 : manager_(manager), |
| 20 weak_factory_(this), | 21 weak_factory_(this), |
| 21 task_is_pending_(false), | 22 task_is_pending_(false), |
| 22 context_(context) { | 23 context_(context) { |
| 23 DCHECK(CalledOnValidThread()); | 24 DCHECK(CalledOnValidThread()); |
| 24 DCHECK(manager); | 25 DCHECK(manager); |
| 25 } | 26 } |
| 26 | 27 |
| 27 SdchDictionaryFetcher::~SdchDictionaryFetcher() { | 28 SdchDictionaryFetcher::~SdchDictionaryFetcher() { |
| 28 DCHECK(CalledOnValidThread()); | 29 DCHECK(CalledOnValidThread()); |
| 29 } | 30 } |
| 30 | 31 |
| 31 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { | 32 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { |
| 32 DCHECK(CalledOnValidThread()); | 33 DCHECK(CalledOnValidThread()); |
| 33 | 34 |
| 34 // Avoid pushing duplicate copy onto queue. We may fetch this url again later | 35 // Avoid pushing duplicate copy onto queue. We may fetch this url again later |
| 35 // and get a different dictionary, but there is no reason to have it in the | 36 // and get a different dictionary, but there is no reason to have it in the |
| 36 // queue twice at one time. | 37 // queue twice at one time. |
| 37 if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) { | 38 if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) { |
| 38 net::SdchManager::SdchErrorRecovery( | 39 SdchManager::SdchErrorRecovery( |
| 39 net::SdchManager::DICTIONARY_ALREADY_SCHEDULED_TO_DOWNLOAD); | 40 SdchManager::DICTIONARY_ALREADY_SCHEDULED_TO_DOWNLOAD); |
| 40 return; | 41 return; |
| 41 } | 42 } |
| 42 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) { | 43 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) { |
| 43 net::SdchManager::SdchErrorRecovery( | 44 SdchManager::SdchErrorRecovery( |
| 44 net::SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD); | 45 SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD); |
| 45 return; | 46 return; |
| 46 } | 47 } |
| 47 attempted_load_.insert(dictionary_url); | 48 attempted_load_.insert(dictionary_url); |
| 48 fetch_queue_.push(dictionary_url); | 49 fetch_queue_.push(dictionary_url); |
| 49 ScheduleDelayedRun(); | 50 ScheduleDelayedRun(); |
| 50 } | 51 } |
| 51 | 52 |
| 52 void SdchDictionaryFetcher::Cancel() { | 53 void SdchDictionaryFetcher::Cancel() { |
| 53 DCHECK(CalledOnValidThread()); | 54 DCHECK(CalledOnValidThread()); |
| 54 | 55 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 72 void SdchDictionaryFetcher::StartFetching() { | 73 void SdchDictionaryFetcher::StartFetching() { |
| 73 DCHECK(CalledOnValidThread()); | 74 DCHECK(CalledOnValidThread()); |
| 74 DCHECK(task_is_pending_); | 75 DCHECK(task_is_pending_); |
| 75 task_is_pending_ = false; | 76 task_is_pending_ = false; |
| 76 | 77 |
| 77 // Handle losing the race against Cancel(). | 78 // Handle losing the race against Cancel(). |
| 78 if (fetch_queue_.empty()) | 79 if (fetch_queue_.empty()) |
| 79 return; | 80 return; |
| 80 | 81 |
| 81 DCHECK(context_.get()); | 82 DCHECK(context_.get()); |
| 82 current_fetch_.reset(net::URLFetcher::Create( | 83 current_fetch_.reset(URLFetcher::Create( |
| 83 fetch_queue_.front(), net::URLFetcher::GET, this)); | 84 fetch_queue_.front(), URLFetcher::GET, this)); |
| 84 fetch_queue_.pop(); | 85 fetch_queue_.pop(); |
| 85 current_fetch_->SetRequestContext(context_.get()); | 86 current_fetch_->SetRequestContext(context_.get()); |
| 86 current_fetch_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 87 current_fetch_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | |
| 87 net::LOAD_DO_NOT_SAVE_COOKIES); | 88 LOAD_DO_NOT_SAVE_COOKIES); |
| 88 current_fetch_->Start(); | 89 current_fetch_->Start(); |
| 89 } | 90 } |
| 90 | 91 |
| 91 void SdchDictionaryFetcher::OnURLFetchComplete( | 92 void SdchDictionaryFetcher::OnURLFetchComplete( |
| 92 const net::URLFetcher* source) { | 93 const URLFetcher* source) { |
| 93 DCHECK(CalledOnValidThread()); | 94 DCHECK(CalledOnValidThread()); |
| 94 if ((200 == source->GetResponseCode()) && | 95 if ((200 == source->GetResponseCode()) && |
| 95 (source->GetStatus().status() == net::URLRequestStatus::SUCCESS)) { | 96 (source->GetStatus().status() == URLRequestStatus::SUCCESS)) { |
| 96 std::string data; | 97 std::string data; |
| 97 source->GetResponseAsString(&data); | 98 source->GetResponseAsString(&data); |
| 98 manager_->AddSdchDictionary(data, source->GetURL()); | 99 manager_->AddSdchDictionary(data, source->GetURL()); |
| 99 } | 100 } |
| 100 current_fetch_.reset(NULL); | 101 current_fetch_.reset(NULL); |
| 101 ScheduleDelayedRun(); | 102 ScheduleDelayedRun(); |
| 102 } | 103 } |
| 104 |
| 105 } // namespace net |
| OLD | NEW |