breadcrumb_manager_browser_agent.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 COMPONENTS_BREADCRUMBS_CORE_BREADCRUMB_MANAGER_BROWSER_AGENT_H_
  5. #define COMPONENTS_BREADCRUMBS_CORE_BREADCRUMB_MANAGER_BROWSER_AGENT_H_
  6. #include <string>
  7. #include "third_party/abseil-cpp/absl/types/optional.h"
  8. namespace breadcrumbs {
  9. // Logs activity for the associated Browser's underlying list of tabs based on
  10. // callbacks from various observers. Event logs are sent to the
  11. // BreadcrumbManagerKeyedService for the BrowserState (iOS) or BrowserContext
  12. // (desktop). For example:
  13. // Browser1 Insert active Tab2 at 0
  14. // which indicates that a tab with identifier 2 (from
  15. // BreadcrumbManagerTabHelper) was inserted into the Browser with identifier 1
  16. // (from BreadcrumbManagerBrowserAgent) at index 0.
  17. class BreadcrumbManagerBrowserAgent {
  18. public:
  19. BreadcrumbManagerBrowserAgent();
  20. // Gets and sets whether or not logging is enabled. Disabling logging is used
  21. // to prevent the over-collection of breadcrumb events during known states
  22. // such as a clean shutdown.
  23. // `IsLoggingEnabled()` defaults to true on initialization.
  24. bool IsLoggingEnabled();
  25. void SetLoggingEnabled(bool enabled);
  26. protected:
  27. // Logs the breadcrumb event for inserting `num_tabs` tabs.
  28. void LogTabsInserted(int num_tabs);
  29. // Logs the breadcrumb event for inserting the tab identified by `tab_id` at
  30. // position `index`, including whether the tab is currently active per
  31. // `is_tab_active`.
  32. void LogTabInsertedAt(int tab_id, int index, bool is_tab_active);
  33. // Logs the breadcrumb event for closing `num_tabs` tabs.
  34. void LogTabsClosed(int num_tabs);
  35. // Logs the breadcrumb event for closing the tab identified by `tab_id` at
  36. // position `index`.
  37. void LogTabClosedAt(int tab_id, int index);
  38. // Logs the breadcrumb event for moving the tab identified by `tab_id` from
  39. // position `from_index` to position `to_index`.
  40. void LogTabMoved(int tab_id, int from_index, int to_index);
  41. // Logs the breadcrumb event for replacing the tab identified by `old_tab_id`
  42. // with the tab identified by `new_tab_id` at position `index`.
  43. void LogTabReplaced(int old_tab_id, int new_tab_id, int index);
  44. // Logs the breadcrumb event for changing the active tab from the tab
  45. // identified by `old_tab_id` to the tab identified by `new_tab_id` at
  46. // position `index`.
  47. void LogActiveTabChanged(absl::optional<int> old_tab_id,
  48. absl::optional<int> new_tab_id,
  49. absl::optional<size_t> index);
  50. // Logs a breadcrumb event with message data `event` for the associated
  51. // browser. NOTE: `event` must not include newline characters, as newlines are
  52. // used by BreadcrumbPersistentStorageManager as a deliminator.
  53. void LogEvent(const std::string& event);
  54. int unique_id() { return unique_id_; }
  55. private:
  56. // Logs the given `event` for the associated browser by retrieving the
  57. // breadcrumb manager from BrowserState (iOS) or BrowserContext (desktop).
  58. // This should not be used directly to log events; use LogEvent() instead.
  59. virtual void PlatformLogEvent(const std::string& event) = 0;
  60. // Whether or not events will be logged.
  61. bool is_logging_enabled_ = true;
  62. // Unique (across this application run only) identifier for logs associated
  63. // with the associated browser instance. Used to identify logs associated with
  64. // the same underlying BrowserState (iOS) or BrowserContext (desktop).
  65. int unique_id_ = -1;
  66. };
  67. } // namespace breadcrumbs
  68. #endif // COMPONENTS_BREADCRUMBS_CORE_BREADCRUMB_MANAGER_BROWSER_AGENT_H_