lacros_service_never_blocking_state.h 2.5 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. #ifndef CHROMEOS_LACROS_LACROS_SERVICE_NEVER_BLOCKING_STATE_H_
  5. #define CHROMEOS_LACROS_LACROS_SERVICE_NEVER_BLOCKING_STATE_H_
  6. #include <utility>
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/sequence_checker.h"
  9. #include "chromeos/crosapi/mojom/crosapi.mojom.h"
  10. #include "mojo/public/cpp/bindings/pending_receiver.h"
  11. #include "mojo/public/cpp/bindings/pending_remote.h"
  12. #include "mojo/public/cpp/bindings/remote.h"
  13. namespace chromeos {
  14. // This class that holds all state associated with LacrosChromeService that is
  15. // affine to a single, never-blocking sequence. The sequence must be
  16. // never-blocking to avoid deadlocks, see https://crbug.com/1103765.
  17. //
  18. // This class is considered an implementation detail of LacrosService.
  19. // It exists as a standalone class/file because template member functions must
  20. // be defined in header files.
  21. class LacrosServiceNeverBlockingState {
  22. public:
  23. LacrosServiceNeverBlockingState();
  24. ~LacrosServiceNeverBlockingState();
  25. // Crosapi is the interface that lacros-chrome uses to message
  26. // ash-chrome. This method binds the remote, which allows queuing of message
  27. // to ash-chrome. The messages will not go through until
  28. // RequestCrosapiReceiver() is invoked.
  29. void BindCrosapi();
  30. void FusePipeCrosapi(
  31. mojo::PendingRemote<crosapi::mojom::Crosapi> pending_remote);
  32. void OnBrowserStartup(crosapi::mojom::BrowserInfoPtr browser_info);
  33. // Calls the indicated Bind* function on the crosapi interface with the given
  34. // receiver.
  35. template <typename ReceiverType,
  36. void (crosapi::mojom::Crosapi::*bind_func)(ReceiverType)>
  37. void BindCrosapiFeatureReceiver(ReceiverType receiver) {
  38. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  39. (crosapi_.get()->*bind_func)(std::move(receiver));
  40. }
  41. base::WeakPtr<LacrosServiceNeverBlockingState> GetWeakPtr();
  42. private:
  43. // This remote allows lacros-chrome to send messages to ash-chrome.
  44. mojo::Remote<crosapi::mojom::Crosapi> crosapi_;
  45. // This class holds onto the receiver for Crosapi until ash-chrome
  46. // is ready to bind it.
  47. mojo::PendingReceiver<crosapi::mojom::Crosapi> pending_crosapi_receiver_;
  48. SEQUENCE_CHECKER(sequence_checker_);
  49. base::WeakPtrFactory<LacrosServiceNeverBlockingState> weak_factory_{this};
  50. };
  51. } // namespace chromeos
  52. #endif // CHROMEOS_LACROS_LACROS_SERVICE_NEVER_BLOCKING_STATE_H_