chrome_browser_proxy_resolver.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // Copyright (C) 2011 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. #include "update_engine/chrome_browser_proxy_resolver.h"
  17. #include <utility>
  18. #include <base/bind.h>
  19. #include <base/memory/ptr_util.h>
  20. #include <base/strings/string_util.h>
  21. #include <brillo/http/http_proxy.h>
  22. #include "update_engine/dbus_connection.h"
  23. namespace chromeos_update_engine {
  24. ChromeBrowserProxyResolver::ChromeBrowserProxyResolver()
  25. : next_request_id_(kProxyRequestIdNull + 1), weak_ptr_factory_(this) {}
  26. ChromeBrowserProxyResolver::~ChromeBrowserProxyResolver() = default;
  27. ProxyRequestId ChromeBrowserProxyResolver::GetProxiesForUrl(
  28. const std::string& url, const ProxiesResolvedFn& callback) {
  29. const ProxyRequestId id = next_request_id_++;
  30. brillo::http::GetChromeProxyServersAsync(
  31. DBusConnection::Get()->GetDBus(),
  32. url,
  33. base::Bind(&ChromeBrowserProxyResolver::OnGetChromeProxyServers,
  34. weak_ptr_factory_.GetWeakPtr(),
  35. id));
  36. pending_callbacks_[id] = callback;
  37. return id;
  38. }
  39. bool ChromeBrowserProxyResolver::CancelProxyRequest(ProxyRequestId request) {
  40. return pending_callbacks_.erase(request) != 0;
  41. }
  42. void ChromeBrowserProxyResolver::OnGetChromeProxyServers(
  43. ProxyRequestId request_id,
  44. bool success,
  45. const std::vector<std::string>& proxies) {
  46. // If |success| is false, |proxies| will still hold the direct proxy option
  47. // which is what we do in our error case.
  48. auto it = pending_callbacks_.find(request_id);
  49. if (it == pending_callbacks_.end())
  50. return;
  51. ProxiesResolvedFn callback = it->second;
  52. pending_callbacks_.erase(it);
  53. callback.Run(std::deque<std::string>(proxies.begin(), proxies.end()));
  54. }
  55. } // namespace chromeos_update_engine