theme_manager.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2020 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 FUCHSIA_WEB_WEBENGINE_BROWSER_THEME_MANAGER_H_
  5. #define FUCHSIA_WEB_WEBENGINE_BROWSER_THEME_MANAGER_H_
  6. #include <fuchsia/settings/cpp/fidl.h>
  7. #include <fuchsia/web/cpp/fidl.h>
  8. #include <lib/sys/cpp/component_context.h>
  9. #include "base/fuchsia/process_context.h"
  10. #include "content/public/browser/web_contents.h"
  11. #include "fuchsia_web/webengine/web_engine_export.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. #include "third_party/blink/public/common/web_preferences/web_preferences.h"
  14. class WEB_ENGINE_EXPORT ThemeManager {
  15. public:
  16. explicit ThemeManager(content::WebContents* web_contents,
  17. base::OnceClosure on_display_error);
  18. ~ThemeManager();
  19. ThemeManager(const ThemeManager&) = delete;
  20. ThemeManager& operator=(const ThemeManager&) = delete;
  21. // Sets the |theme| requested by the FIDL caller, but does not apply the
  22. // theme. Call |WebContents::OnWebPreferencesChanged| to apply the theme to
  23. // web contents.
  24. //
  25. // If the |theme| is DEFAULT, then the theme provided by |display_service_|
  26. // will be used. |on_display_error_| is run if |display_service_| is required
  27. // but unavailable.
  28. void SetTheme(fuchsia::settings::ThemeType theme);
  29. // Override |blink_prefs| with theme set by |SetTheme|.
  30. void ApplyThemeToWebPreferences(blink::web_pref::WebPreferences* web_prefs);
  31. private:
  32. // Attempts to connect to the fuchsia.settings.Display service.
  33. // Returns true if a connection was created, or if one already exists.
  34. // Return false if the service is unavailable.
  35. bool EnsureDisplayService();
  36. void WatchForDisplayChanges();
  37. void OnWatchResultReceived(fuchsia::settings::DisplaySettings settings);
  38. void OnDisplayServiceMissing();
  39. bool observed_display_service_error_ = false;
  40. bool did_receive_first_watch_result_ = false;
  41. absl::optional<fuchsia::settings::ThemeType> requested_theme_;
  42. absl::optional<fuchsia::settings::ThemeType> system_theme_;
  43. content::WebContents* web_contents_;
  44. fuchsia::settings::DisplayPtr display_service_;
  45. base::OnceClosure on_display_error_;
  46. };
  47. #endif // FUCHSIA_WEB_WEBENGINE_BROWSER_THEME_MANAGER_H_