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 "chrome/browser/net/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" | 10 #include "chrome/browser/profiles/profile.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) { | 42 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) { |
43 net::SdchManager::SdchErrorRecovery( | 43 net::SdchManager::SdchErrorRecovery( |
44 net::SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD); | 44 net::SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD); |
45 return; | 45 return; |
46 } | 46 } |
47 attempted_load_.insert(dictionary_url); | 47 attempted_load_.insert(dictionary_url); |
48 fetch_queue_.push(dictionary_url); | 48 fetch_queue_.push(dictionary_url); |
49 ScheduleDelayedRun(); | 49 ScheduleDelayedRun(); |
50 } | 50 } |
51 | 51 |
52 void SdchDictionaryFetcher::Cancel() { | |
53 DCHECK(CalledOnValidThread()); | |
54 | |
55 while (!fetch_queue_.empty()) | |
56 fetch_queue_.pop(); | |
57 attempted_load_.clear(); | |
58 weak_factory_.InvalidateWeakPtrs(); | |
59 current_fetch_.reset(NULL); | |
60 } | |
61 | |
62 void SdchDictionaryFetcher::ScheduleDelayedRun() { | 52 void SdchDictionaryFetcher::ScheduleDelayedRun() { |
63 if (fetch_queue_.empty() || current_fetch_.get() || task_is_pending_) | 53 if (fetch_queue_.empty() || current_fetch_.get() || task_is_pending_) |
64 return; | 54 return; |
65 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, | 55 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
66 base::Bind(&SdchDictionaryFetcher::StartFetching, | 56 base::Bind(&SdchDictionaryFetcher::StartFetching, |
67 weak_factory_.GetWeakPtr()), | 57 weak_factory_.GetWeakPtr()), |
68 base::TimeDelta::FromMilliseconds(kMsDelayFromRequestTillDownload)); | 58 base::TimeDelta::FromMilliseconds(kMsDelayFromRequestTillDownload)); |
69 task_is_pending_ = true; | 59 task_is_pending_ = true; |
70 } | 60 } |
71 | 61 |
72 void SdchDictionaryFetcher::StartFetching() { | 62 void SdchDictionaryFetcher::StartFetching() { |
73 DCHECK(CalledOnValidThread()); | 63 DCHECK(CalledOnValidThread()); |
74 DCHECK(task_is_pending_); | 64 DCHECK(task_is_pending_); |
75 task_is_pending_ = false; | 65 task_is_pending_ = false; |
76 | 66 |
77 // Handle losing the race against Cancel(). | |
78 if (fetch_queue_.empty()) | |
79 return; | |
80 | |
81 DCHECK(context_.get()); | 67 DCHECK(context_.get()); |
82 current_fetch_.reset(net::URLFetcher::Create( | 68 current_fetch_.reset(net::URLFetcher::Create( |
83 fetch_queue_.front(), net::URLFetcher::GET, this)); | 69 fetch_queue_.front(), net::URLFetcher::GET, this)); |
84 fetch_queue_.pop(); | 70 fetch_queue_.pop(); |
85 current_fetch_->SetRequestContext(context_.get()); | 71 current_fetch_->SetRequestContext(context_.get()); |
86 current_fetch_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 72 current_fetch_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
87 net::LOAD_DO_NOT_SAVE_COOKIES); | 73 net::LOAD_DO_NOT_SAVE_COOKIES); |
88 current_fetch_->Start(); | 74 current_fetch_->Start(); |
89 } | 75 } |
90 | 76 |
91 void SdchDictionaryFetcher::OnURLFetchComplete( | 77 void SdchDictionaryFetcher::OnURLFetchComplete( |
92 const net::URLFetcher* source) { | 78 const net::URLFetcher* source) { |
93 DCHECK(CalledOnValidThread()); | 79 DCHECK(CalledOnValidThread()); |
94 if ((200 == source->GetResponseCode()) && | 80 if ((200 == source->GetResponseCode()) && |
95 (source->GetStatus().status() == net::URLRequestStatus::SUCCESS)) { | 81 (source->GetStatus().status() == net::URLRequestStatus::SUCCESS)) { |
96 std::string data; | 82 std::string data; |
97 source->GetResponseAsString(&data); | 83 source->GetResponseAsString(&data); |
98 manager_->AddSdchDictionary(data, source->GetURL()); | 84 manager_->AddSdchDictionary(data, source->GetURL()); |
99 } | 85 } |
100 current_fetch_.reset(NULL); | 86 current_fetch_.reset(NULL); |
101 ScheduleDelayedRun(); | 87 ScheduleDelayedRun(); |
102 } | 88 } |
OLD | NEW |