remote_open_url_client.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #ifndef REMOTING_HOST_REMOTE_OPEN_URL_REMOTE_OPEN_URL_CLIENT_H_
  5. #define REMOTING_HOST_REMOTE_OPEN_URL_REMOTE_OPEN_URL_CLIENT_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/command_line.h"
  9. #include "base/time/time.h"
  10. #include "base/timer/timer.h"
  11. #include "mojo/public/cpp/bindings/remote.h"
  12. #include "remoting/host/mojom/remote_url_opener.mojom.h"
  13. #include "url/gurl.h"
  14. namespace remoting {
  15. class ChromotingHostServicesProvider;
  16. // A helper to allow the standalone open URL binary to open a URL remotely and
  17. // handle local fallback.
  18. class RemoteOpenUrlClient final {
  19. public:
  20. // An interface to support platform-specific implementation.
  21. class Delegate {
  22. public:
  23. Delegate() = default;
  24. virtual ~Delegate() = default;
  25. // Opens |url| on the fallback browser. If |url| is empty, simply opens the
  26. // browser without a URL.
  27. virtual void OpenUrlOnFallbackBrowser(const GURL& url) = 0;
  28. // Shows an error message that indicates that |url| fails to be opened
  29. // remotely.
  30. virtual void ShowOpenUrlError(const GURL& url) = 0;
  31. };
  32. RemoteOpenUrlClient();
  33. ~RemoteOpenUrlClient();
  34. // Simply opens the fallback browser with no arguments.
  35. void OpenFallbackBrowser();
  36. // Opens |arg| (which can be either a URL or an absolute file path) and calls
  37. // |done| when done.
  38. void Open(const base::CommandLine::StringType& arg, base::OnceClosure done);
  39. RemoteOpenUrlClient(const RemoteOpenUrlClient&) = delete;
  40. RemoteOpenUrlClient& operator=(const RemoteOpenUrlClient&) = delete;
  41. private:
  42. friend class RemoteOpenUrlClientTest;
  43. // Ctor for unittests.
  44. RemoteOpenUrlClient(
  45. std::unique_ptr<Delegate> delegate,
  46. std::unique_ptr<ChromotingHostServicesProvider> api_provider,
  47. base::TimeDelta request_timeout);
  48. void OnOpenUrlResponse(mojom::OpenUrlResult result);
  49. void OnRequestTimeout();
  50. void OnIpcDisconnected();
  51. std::unique_ptr<Delegate> delegate_;
  52. std::unique_ptr<ChromotingHostServicesProvider> api_provider_;
  53. base::TimeDelta request_timeout_;
  54. base::OneShotTimer timeout_timer_;
  55. GURL url_;
  56. base::OnceClosure done_;
  57. mojo::Remote<mojom::RemoteUrlOpener> remote_;
  58. };
  59. } // namespace remoting
  60. #endif // REMOTING_HOST_REMOTE_OPEN_URL_REMOTE_OPEN_URL_CLIENT_H_