• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
~HttpFetcher()28 HttpFetcher::~HttpFetcher() {
29   CancelProxyResolution();
30 }
31 
SetPostData(const void * data,size_t size,HttpContentType type)32 void HttpFetcher::SetPostData(const void* data,
33                               size_t size,
34                               HttpContentType type) {
35   post_data_set_ = true;
36   post_data_.clear();
37   const char* char_data = reinterpret_cast<const char*>(data);
38   post_data_.insert(post_data_.end(), char_data, char_data + size);
39   post_content_type_ = type;
40 }
41 
SetPostData(const void * data,size_t size)42 void HttpFetcher::SetPostData(const void* data, size_t size) {
43   SetPostData(data, size, kHttpContentTypeUnspecified);
44 }
45 
46 // Proxy methods to set the proxies, then to pop them off.
ResolveProxiesForUrl(const string & url,const Closure & callback)47 void HttpFetcher::ResolveProxiesForUrl(const string& url,
48                                        const Closure& callback) {
49   CHECK_EQ(static_cast<Closure*>(nullptr), callback_.get());
50   callback_.reset(new Closure(callback));
51 
52   if (!proxy_resolver_) {
53     LOG(INFO) << "Not resolving proxies (no proxy resolver).";
54     no_resolver_idle_id_ = MessageLoop::current()->PostTask(
55         FROM_HERE,
56         base::Bind(&HttpFetcher::NoProxyResolverCallback,
57                    base::Unretained(this)));
58     return;
59   }
60   proxy_request_ = proxy_resolver_->GetProxiesForUrl(
61       url, base::Bind(&HttpFetcher::ProxiesResolved, base::Unretained(this)));
62 }
63 
NoProxyResolverCallback()64 void HttpFetcher::NoProxyResolverCallback() {
65   no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
66   ProxiesResolved(deque<string>());
67 }
68 
ProxiesResolved(const deque<string> & proxies)69 void HttpFetcher::ProxiesResolved(const deque<string>& proxies) {
70   proxy_request_ = kProxyRequestIdNull;
71   if (!proxies.empty())
72     SetProxies(proxies);
73   CHECK(callback_.get()) << "ProxiesResolved but none pending.";
74   Closure* callback = callback_.release();
75   // This may indirectly call back into ResolveProxiesForUrl():
76   callback->Run();
77   delete callback;
78 }
79 
CancelProxyResolution()80 bool HttpFetcher::CancelProxyResolution() {
81   bool ret = false;
82   if (no_resolver_idle_id_ != MessageLoop::kTaskIdNull) {
83     ret = MessageLoop::current()->CancelTask(no_resolver_idle_id_);
84     no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
85   }
86   if (proxy_request_ && proxy_resolver_) {
87     ret = proxy_resolver_->CancelProxyRequest(proxy_request_) || ret;
88     proxy_request_ = kProxyRequestIdNull;
89   }
90   return ret;
91 }
92 
93 }  // namespace chromeos_update_engine
94