app_list_model_provider.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 ASH_APP_LIST_APP_LIST_MODEL_PROVIDER_H_
  5. #define ASH_APP_LIST_APP_LIST_MODEL_PROVIDER_H_
  6. #include "ash/app_list/model/app_list_model.h"
  7. #include "ash/app_list/model/search/search_model.h"
  8. #include "ash/ash_export.h"
  9. #include "base/observer_list.h"
  10. namespace ash {
  11. // Used by app list views hierarchy to track the active app list model, and the
  12. // app list search model. The active model state is maintained by
  13. // `AppListControllerImpl`, which also serves as an `AppListViewDelegate`.
  14. // Models are owned by ash embedder (chrome), which use `AppListController`
  15. // interface to update active models. Main motivation is effectively handling
  16. // model changes when the active user changes - app list model contains user
  17. // specific data, and the model information shown in the UI should be updated
  18. // whenever the active user changes. This class supports this without a need to
  19. // rebuild the currently used model from scratch.
  20. // Only one instance is expected to exist at a time, and it can be retrieved
  21. // using `AppListModelProvider::Get()`.
  22. class ASH_EXPORT AppListModelProvider {
  23. public:
  24. class Observer : public base::CheckedObserver {
  25. public:
  26. ~Observer() override = default;
  27. // Called when the active app list model changes.
  28. virtual void OnActiveAppListModelsChanged(AppListModel* model,
  29. SearchModel* search_model) = 0;
  30. };
  31. AppListModelProvider();
  32. AppListModelProvider(const AppListModelProvider&) = delete;
  33. AppListModelProvider& operator=(const AppListModelProvider&) = delete;
  34. ~AppListModelProvider();
  35. // Returns a global app list model provider. Only one instance is expected to
  36. // exist at a time. In production and ash based tests, it's the instance owned
  37. // by `AppListControllerImpl`.
  38. static AppListModelProvider* Get();
  39. // Sets active app list and app list search model.
  40. // NOTE: This method is expected to be primarily called by the class that
  41. // owns the `AppListModelProvider` instance (in production, that's
  42. // `AppListControllerImpl`).
  43. //
  44. // Both `model` and `search_model` can be null, in which case active models
  45. // will fallback to default models. This should generally only be done during
  46. // shutdown.
  47. void SetActiveModel(AppListModel* model, SearchModel* search_model);
  48. // Resets active app list and search model to the default one.
  49. void ClearActiveModel();
  50. void AddObserver(Observer* observer);
  51. void RemoveObserver(Observer* observer);
  52. // Gets the active app list model.
  53. // If an active model has not been set, it returns a default model.
  54. AppListModel* model() { return model_; }
  55. // Gets the active search model.
  56. // If an active model has not been set, it returns a default model.
  57. SearchModel* search_model() { return search_model_; }
  58. private:
  59. // Default, empty models that get returned if the provided models are null.
  60. // Primarily used for convenience, to avoid need for null checks in code that
  61. // uses app list model, and search model.
  62. AppListModel default_model_{nullptr};
  63. SearchModel default_search_model_;
  64. AppListModel* model_ = &default_model_;
  65. SearchModel* search_model_ = &default_search_model_;
  66. base::ObserverList<Observer> observers_;
  67. };
  68. } // namespace ash
  69. #endif // ASH_APP_LIST_APP_LIST_MODEL_PROVIDER_H_