test_protocol_handler_registry_delegate.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2019 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 "components/custom_handlers/test_protocol_handler_registry_delegate.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/check_op.h"
  8. #include "base/threading/thread_task_runner_handle.h"
  9. namespace custom_handlers {
  10. TestProtocolHandlerRegistryDelegate::TestProtocolHandlerRegistryDelegate() =
  11. default;
  12. TestProtocolHandlerRegistryDelegate::~TestProtocolHandlerRegistryDelegate() =
  13. default;
  14. // ProtocolHandlerRegistry::Delegate:
  15. void TestProtocolHandlerRegistryDelegate::RegisterExternalHandler(
  16. const std::string& protocol) {
  17. bool inserted = registered_protocols_.insert(protocol).second;
  18. DCHECK(inserted);
  19. }
  20. void TestProtocolHandlerRegistryDelegate::DeregisterExternalHandler(
  21. const std::string& protocol) {
  22. size_t removed = registered_protocols_.erase(protocol);
  23. DCHECK_EQ(removed, 1u);
  24. }
  25. bool TestProtocolHandlerRegistryDelegate::IsExternalHandlerRegistered(
  26. const std::string& protocol) {
  27. return registered_protocols_.find(protocol) != registered_protocols_.end();
  28. }
  29. void TestProtocolHandlerRegistryDelegate::RegisterWithOSAsDefaultClient(
  30. const std::string& protocol,
  31. DefaultClientCallback callback) {
  32. // Do as-if the registration has to run on another sequence and post back
  33. // the result with a task to the current thread.
  34. base::ThreadTaskRunnerHandle::Get()->PostTask(
  35. FROM_HERE, base::BindOnce(std::move(callback), !force_os_failure_));
  36. if (!force_os_failure_)
  37. os_registered_protocols_.insert(protocol);
  38. }
  39. void TestProtocolHandlerRegistryDelegate::CheckDefaultClientWithOS(
  40. const std::string& protocol,
  41. DefaultClientCallback callback) {
  42. // Respond asynchronously to mimic the real behavior.
  43. base::ThreadTaskRunnerHandle::Get()->PostTask(
  44. FROM_HERE, base::BindOnce(std::move(callback), true));
  45. }
  46. bool TestProtocolHandlerRegistryDelegate::ShouldRemoveHandlersNotInOS() {
  47. return true;
  48. }
  49. bool TestProtocolHandlerRegistryDelegate::IsFakeRegisteredWithOS(
  50. const std::string& protocol) {
  51. return os_registered_protocols_.find(protocol) !=
  52. os_registered_protocols_.end();
  53. }
  54. void TestProtocolHandlerRegistryDelegate::Reset() {
  55. registered_protocols_.clear();
  56. os_registered_protocols_.clear();
  57. force_os_failure_ = false;
  58. }
  59. } // namespace custom_handlers