Home | History | Annotate | Download | only in net
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/net/sdch_dictionary_fetcher.h"
      6 
      7 #include "base/compiler_specific.h"
      8 #include "chrome/browser/profiles/profile.h"
      9 #include "net/url_request/url_request_status.h"
     10 
     11 SdchDictionaryFetcher::SdchDictionaryFetcher()
     12     : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
     13       task_is_pending_(false) {
     14 }
     15 
     16 SdchDictionaryFetcher::~SdchDictionaryFetcher() {
     17 }
     18 
     19 // static
     20 void SdchDictionaryFetcher::Shutdown() {
     21   net::SdchManager::Shutdown();
     22 }
     23 
     24 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) {
     25   // Avoid pushing duplicate copy onto queue.  We may fetch this url again later
     26   // and get a different dictionary, but there is no reason to have it in the
     27   // queue twice at one time.
     28   if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) {
     29     net::SdchManager::SdchErrorRecovery(
     30         net::SdchManager::DICTIONARY_ALREADY_SCHEDULED_TO_DOWNLOAD);
     31     return;
     32   }
     33   if (attempted_load_.find(dictionary_url) != attempted_load_.end()) {
     34     net::SdchManager::SdchErrorRecovery(
     35         net::SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD);
     36     return;
     37   }
     38   attempted_load_.insert(dictionary_url);
     39   fetch_queue_.push(dictionary_url);
     40   ScheduleDelayedRun();
     41 }
     42 
     43 void SdchDictionaryFetcher::ScheduleDelayedRun() {
     44   if (fetch_queue_.empty() || current_fetch_.get() || task_is_pending_)
     45     return;
     46   MessageLoop::current()->PostDelayedTask(FROM_HERE,
     47       method_factory_.NewRunnableMethod(&SdchDictionaryFetcher::StartFetching),
     48       kMsDelayFromRequestTillDownload);
     49   task_is_pending_ = true;
     50 }
     51 
     52 void SdchDictionaryFetcher::StartFetching() {
     53   DCHECK(task_is_pending_);
     54   task_is_pending_ = false;
     55 
     56   net::URLRequestContextGetter* context = Profile::GetDefaultRequestContext();
     57   if (!context) {
     58     // Shutdown in progress.
     59     // Simulate handling of all dictionary requests by clearing queue.
     60     while (!fetch_queue_.empty())
     61       fetch_queue_.pop();
     62     return;
     63   }
     64 
     65   current_fetch_.reset(new URLFetcher(fetch_queue_.front(), URLFetcher::GET,
     66                                       this));
     67   fetch_queue_.pop();
     68   current_fetch_->set_request_context(context);
     69   current_fetch_->Start();
     70 }
     71 
     72 void SdchDictionaryFetcher::OnURLFetchComplete(
     73     const URLFetcher* source,
     74     const GURL& url,
     75     const net::URLRequestStatus& status,
     76     int response_code,
     77     const ResponseCookies& cookies,
     78     const std::string& data) {
     79   if ((200 == response_code) &&
     80       (status.status() == net::URLRequestStatus::SUCCESS))
     81     net::SdchManager::Global()->AddSdchDictionary(data, url);
     82   current_fetch_.reset(NULL);
     83   ScheduleDelayedRun();
     84 }
     85