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