update_block_check_win.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "chrome/updater/update_block_check.h"
  5. #include <netlistmgr.h>
  6. #include <wrl/client.h>
  7. #include <utility>
  8. #include "base/callback.h"
  9. #include "base/task/task_traits.h"
  10. #include "base/task/thread_pool.h"
  11. #include "base/win/windows_version.h"
  12. #include "chrome/updater/update_service.h"
  13. namespace updater {
  14. namespace {
  15. // Returns true in situations where we allow background updates on metered
  16. // networks.
  17. // TODO(crbug.com/1254481): Modify this function to enable background updates on
  18. // metered networks when a toggle is set in the browser.
  19. bool AllowBackgroundUpdatesOnMeteredNetwork() {
  20. return true;
  21. }
  22. // TODO(crbug.com/1254492): Protect against deadlocks in NLM.
  23. bool IsConnectionedMetered() {
  24. // No NLM before Win 8.1. Connections will be considered non-metered.
  25. // Also, NLM could deadlock in Win10 versions pre-RS5, so we don't run the
  26. // code for those versions.
  27. if (base::win::GetVersion() < base::win::Version::WIN10_RS5)
  28. return false;
  29. Microsoft::WRL::ComPtr<INetworkCostManager> network_cost_manager;
  30. HRESULT hr =
  31. ::CoCreateInstance(CLSID_NetworkListManager, nullptr, CLSCTX_ALL,
  32. IID_INetworkCostManager, &network_cost_manager);
  33. if (FAILED(hr))
  34. return false;
  35. DWORD cost = NLM_CONNECTION_COST_UNKNOWN;
  36. hr = network_cost_manager->GetCost(&cost, nullptr);
  37. if (FAILED(hr))
  38. return false;
  39. return cost != NLM_CONNECTION_COST_UNKNOWN &&
  40. (cost & NLM_CONNECTION_COST_UNRESTRICTED) == 0;
  41. }
  42. } // namespace
  43. void ShouldBlockUpdateForMeteredNetwork(
  44. UpdateService::Priority priority,
  45. base::OnceCallback<void(bool)> callback) {
  46. if (priority == UpdateService::Priority::kForeground ||
  47. AllowBackgroundUpdatesOnMeteredNetwork()) {
  48. std::move(callback).Run(false);
  49. } else {
  50. base::ThreadPool::PostTaskAndReplyWithResult(
  51. FROM_HERE, {base::MayBlock()}, base::BindOnce(&IsConnectionedMetered),
  52. std::move(callback));
  53. }
  54. }
  55. } // namespace updater