Home | History | Annotate | Download | only in common
      1 //
      2 // Copyright (C) 2009 The Android Open Source Project
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 
     17 #include "update_engine/common/http_fetcher.h"
     18 
     19 #include <base/bind.h>
     20 
     21 using base::Closure;
     22 using brillo::MessageLoop;
     23 using std::deque;
     24 using std::string;
     25 
     26 namespace chromeos_update_engine {
     27 
     28 HttpFetcher::~HttpFetcher() {
     29   CancelProxyResolution();
     30 }
     31 
     32 void HttpFetcher::SetPostData(const void* data, size_t size,
     33                               HttpContentType type) {
     34   post_data_set_ = true;
     35   post_data_.clear();
     36   const char* char_data = reinterpret_cast<const char*>(data);
     37   post_data_.insert(post_data_.end(), char_data, char_data + size);
     38   post_content_type_ = type;
     39 }
     40 
     41 void HttpFetcher::SetPostData(const void* data, size_t size) {
     42   SetPostData(data, size, kHttpContentTypeUnspecified);
     43 }
     44 
     45 // Proxy methods to set the proxies, then to pop them off.
     46 bool HttpFetcher::ResolveProxiesForUrl(const string& url,
     47                                        const Closure& callback) {
     48   CHECK_EQ(static_cast<Closure*>(nullptr), callback_.get());
     49   callback_.reset(new Closure(callback));
     50 
     51   if (!proxy_resolver_) {
     52     LOG(INFO) << "Not resolving proxies (no proxy resolver).";
     53     no_resolver_idle_id_ = MessageLoop::current()->PostTask(
     54         FROM_HERE,
     55         base::Bind(&HttpFetcher::NoProxyResolverCallback,
     56                    base::Unretained(this)));
     57     return true;
     58   }
     59   proxy_request_ = proxy_resolver_->GetProxiesForUrl(
     60       url, base::Bind(&HttpFetcher::ProxiesResolved, base::Unretained(this)));
     61   if (proxy_request_ == kProxyRequestIdNull) {
     62     callback_.reset();
     63     return false;
     64   }
     65   return true;
     66 }
     67 
     68 void HttpFetcher::NoProxyResolverCallback() {
     69   no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
     70   ProxiesResolved(deque<string>());
     71 }
     72 
     73 void HttpFetcher::ProxiesResolved(const deque<string>& proxies) {
     74   proxy_request_ = kProxyRequestIdNull;
     75   if (!proxies.empty())
     76     SetProxies(proxies);
     77   CHECK(callback_.get()) << "ProxiesResolved but none pending.";
     78   Closure* callback = callback_.release();
     79   // This may indirectly call back into ResolveProxiesForUrl():
     80   callback->Run();
     81   delete callback;
     82 }
     83 
     84 bool HttpFetcher::CancelProxyResolution() {
     85   bool ret = false;
     86   if (no_resolver_idle_id_ != MessageLoop::kTaskIdNull) {
     87     ret = MessageLoop::current()->CancelTask(no_resolver_idle_id_);
     88     no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
     89   }
     90   if (proxy_request_ && proxy_resolver_) {
     91     ret = proxy_resolver_->CancelProxyRequest(proxy_request_) || ret;
     92     proxy_request_ = kProxyRequestIdNull;
     93   }
     94   return ret;
     95 }
     96 
     97 }  // namespace chromeos_update_engine
     98