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 void 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;
     58   }
     59   proxy_request_ = proxy_resolver_->GetProxiesForUrl(
     60       url, base::Bind(&HttpFetcher::ProxiesResolved, base::Unretained(this)));
     61 }
     62 
     63 void HttpFetcher::NoProxyResolverCallback() {
     64   no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
     65   ProxiesResolved(deque<string>());
     66 }
     67 
     68 void HttpFetcher::ProxiesResolved(const deque<string>& proxies) {
     69   proxy_request_ = kProxyRequestIdNull;
     70   if (!proxies.empty())
     71     SetProxies(proxies);
     72   CHECK(callback_.get()) << "ProxiesResolved but none pending.";
     73   Closure* callback = callback_.release();
     74   // This may indirectly call back into ResolveProxiesForUrl():
     75   callback->Run();
     76   delete callback;
     77 }
     78 
     79 bool HttpFetcher::CancelProxyResolution() {
     80   bool ret = false;
     81   if (no_resolver_idle_id_ != MessageLoop::kTaskIdNull) {
     82     ret = MessageLoop::current()->CancelTask(no_resolver_idle_id_);
     83     no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
     84   }
     85   if (proxy_request_ && proxy_resolver_) {
     86     ret = proxy_resolver_->CancelProxyRequest(proxy_request_) || ret;
     87     proxy_request_ = kProxyRequestIdNull;
     88   }
     89   return ret;
     90 }
     91 
     92 }  // namespace chromeos_update_engine
     93