offline_event_logger.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2016 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_OFFLINE_PAGES_CORE_OFFLINE_EVENT_LOGGER_H_
  5. #define COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_EVENT_LOGGER_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/containers/circular_deque.h"
  9. #include "base/memory/raw_ptr.h"
  10. namespace offline_pages {
  11. // Maximum number of recorded Logs to keep track of at any moment.
  12. constexpr size_t kMaxLogCount = 50;
  13. // Facilitates the logging of events. Subclasses should create methods that
  14. // call RecordActivity to write into the log. |SetIsLogging|, |GetLogs|, and
  15. // |Clear| are called from the chrome://offline-internals page.
  16. //
  17. // Logging should be done by calling the corresponding subclass methods when
  18. // a loggable event occurs (i.e. when status has changed for a save request
  19. // or when an offlined page has been accessed/saved).
  20. //
  21. // This log only keeps track of the last |kMaxLogCount| events.
  22. class OfflineEventLogger {
  23. public:
  24. // This client interface should be implemented by the class which provides the
  25. // ability to pipe the log somewhere else (Eg. a java class which can write
  26. // logs into a file). It's optional and uses SetClient() to attach the client
  27. // to the event logger instance.
  28. class Client {
  29. public:
  30. virtual ~Client() {}
  31. virtual void CustomLog(const std::string& message) = 0;
  32. };
  33. OfflineEventLogger();
  34. ~OfflineEventLogger();
  35. // Clears the recorded activities.
  36. void Clear();
  37. // Turns logging on/off.
  38. void SetIsLogging(bool is_logging);
  39. // Returns whether we are currently logging.
  40. bool GetIsLogging();
  41. // Dumps the current activity list into |records|.
  42. void GetLogs(std::vector<std::string>* records);
  43. // Write the activity into the cycling log if we are currently logging.
  44. void RecordActivity(const std::string& activity);
  45. // Sets the client for custom logging process if needed.
  46. void SetClient(Client* client);
  47. private:
  48. // Recorded offline page activities.
  49. base::circular_deque<std::string> activities_;
  50. // Whether we are currently recording logs or not.
  51. bool is_logging_;
  52. // Not owned.
  53. raw_ptr<Client> client_;
  54. };
  55. } // namespace offline_pages
  56. #endif // COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_EVENT_LOGGER_H_