browser_init_params.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2022 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_STARTUP_BROWSER_INIT_PARAMS_H_
  5. #define CHROMEOS_STARTUP_BROWSER_INIT_PARAMS_H_
  6. #include "base/no_destructor.h"
  7. #include "chromeos/crosapi/mojom/crosapi.mojom.h"
  8. namespace chromeos {
  9. // Stores and handles BrowserInitParams.
  10. // This class is not to be used directly - use BrowserParamsProxy instead.
  11. class COMPONENT_EXPORT(CHROMEOS_STARTUP) BrowserInitParams {
  12. public:
  13. BrowserInitParams(const BrowserInitParams&) = delete;
  14. BrowserInitParams& operator=(const BrowserInitParams&) = delete;
  15. // Returns BrowserInitParams which is passed from ash-chrome.
  16. // Useful for tests. Production code always needs to go
  17. // through BrowserParamsProxy instead.
  18. static const crosapi::mojom::BrowserInitParams* GetForTests();
  19. // Sets `init_params_` to the provided value.
  20. // Useful for tests that cannot setup a full Lacros test environment with a
  21. // working Mojo connection to Ash.
  22. static void SetInitParamsForTests(
  23. crosapi::mojom::BrowserInitParamsPtr init_params);
  24. // Create Mem FD from `init_params_`. This must be called after `init_params_`
  25. // has initialized by calling GetInstance().
  26. static base::ScopedFD CreateStartupData();
  27. static bool disable_crosapi_for_testing() {
  28. return disable_crosapi_for_testing_;
  29. }
  30. private:
  31. friend base::NoDestructor<BrowserInitParams>;
  32. // Returns BrowserInitParams which is passed from ash-chrome. On launching
  33. // lacros-chrome from ash-chrome, ash-chrome creates a memory backed file,
  34. // serializes the BrowserInitParams to it, and the forked/executed
  35. // lacros-chrome process inherits the file descriptor. The data is read
  36. // in the constructor so is available from the beginning.
  37. // NOTE: You should use BrowserParamsProxy to access init params instead.
  38. static const crosapi::mojom::BrowserInitParams* Get();
  39. static BrowserInitParams* GetInstance();
  40. BrowserInitParams();
  41. ~BrowserInitParams();
  42. // Needs to access |disable_crosapi_for_testing_|.
  43. friend class ScopedDisableCrosapiForTesting;
  44. // Needs to access |Get()|.
  45. friend class BrowserParamsProxy;
  46. // Tests will set this to |true| which will make all crosapi functionality
  47. // unavailable. Should be set from ScopedDisableCrosapiForTesting always.
  48. // TODO(https://crbug.com/1131722): Ideally we could stub this out or make
  49. // this functional for tests without modifying production code
  50. static bool disable_crosapi_for_testing_;
  51. // Parameters passed from ash-chrome.
  52. crosapi::mojom::BrowserInitParamsPtr init_params_;
  53. };
  54. } // namespace chromeos
  55. #endif // CHROMEOS_STARTUP_BROWSER_INIT_PARAMS_H_