application_breadcrumbs_logger.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 COMPONENTS_BREADCRUMBS_CORE_APPLICATION_BREADCRUMBS_LOGGER_H_
  5. #define COMPONENTS_BREADCRUMBS_CORE_APPLICATION_BREADCRUMBS_LOGGER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/files/file_path.h"
  10. #include "base/memory/memory_pressure_listener.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/metrics/user_metrics.h"
  13. #include "components/breadcrumbs/core/breadcrumb_manager.h"
  14. #include "components/breadcrumbs/core/breadcrumb_util.h"
  15. #include "components/breadcrumbs/core/breadcrumbs_status.h"
  16. namespace base {
  17. class TimeTicks;
  18. } // namespace base
  19. namespace breadcrumbs {
  20. class BreadcrumbPersistentStorageManager;
  21. // Listens for and logs application wide breadcrumb events to the
  22. // BreadcrumbManager passed in the constructor.
  23. class ApplicationBreadcrumbsLogger {
  24. public:
  25. // Breadcrumbs will be stored in a file in |storage_dir|.
  26. explicit ApplicationBreadcrumbsLogger(
  27. const base::FilePath& storage_dir,
  28. base::RepeatingCallback<bool()> is_metrics_enabled_callback);
  29. ApplicationBreadcrumbsLogger(const ApplicationBreadcrumbsLogger&) = delete;
  30. ~ApplicationBreadcrumbsLogger();
  31. // Returns a pointer to the BreadcrumbPersistentStorageManager owned by this
  32. // instance. May be null.
  33. breadcrumbs::BreadcrumbPersistentStorageManager* GetPersistentStorageManager()
  34. const;
  35. // Return the events stored by the application-wide breadcrumb manager.
  36. std::list<std::string> GetEventsForTesting();
  37. protected:
  38. // Adds an event to |breadcrumb_manager_|.
  39. void AddEvent(const std::string& event);
  40. private:
  41. // Callback which processes and logs the user action |action| to
  42. // |breadcrumb_manager_|.
  43. void OnUserAction(const std::string& action, base::TimeTicks action_time);
  44. // Callback which processes and logs memory pressure warnings to
  45. // |breadcrumb_manager_|.
  46. void OnMemoryPressure(
  47. base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
  48. // Returns true if |action| (UMA User Action) is user triggered.
  49. static bool IsUserTriggeredAction(const std::string& action);
  50. // Stores application-wide breadcrumb events.
  51. breadcrumbs::BreadcrumbManager breadcrumb_manager_{
  52. breadcrumbs::GetStartTime()};
  53. // The callback invoked whenever a user action is registered.
  54. base::ActionCallback user_action_callback_;
  55. // A memory pressure listener which observes memory pressure events.
  56. std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_;
  57. // A strong pointer to the persistent breadcrumb manager listening for events
  58. // from |breadcrumb_manager_| to store to disk.
  59. std::unique_ptr<breadcrumbs::BreadcrumbPersistentStorageManager>
  60. persistent_storage_manager_;
  61. };
  62. } // namespace breadcrumbs
  63. #endif // COMPONENTS_BREADCRUMBS_CORE_APPLICATION_BREADCRUMBS_LOGGER_H_