system_logs_fetcher.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2013 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_FEEDBACK_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_H_
  5. #define COMPONENTS_FEEDBACK_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "base/callback.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/sequence_checker.h"
  14. #include "base/task/sequenced_task_runner.h"
  15. #include "build/chromeos_buildflags.h"
  16. #include "components/feedback/feedback_common.h"
  17. #include "components/feedback/redaction_tool.h"
  18. #include "components/feedback/system_logs/system_logs_source.h"
  19. namespace system_logs {
  20. // Callback that the SystemLogsFetcher uses to return data.
  21. using SysLogsFetcherCallback =
  22. base::OnceCallback<void(std::unique_ptr<SystemLogsResponse>)>;
  23. // The SystemLogsFetcher fetches key-value data from a list of log sources.
  24. //
  25. // EXAMPLE:
  26. // class Example {
  27. // public:
  28. // void ProcessLogs(std::unique_ptr<SystemLogsResponse> response) {
  29. // // do something with the logs
  30. // }
  31. // void GetLogs() {
  32. // SystemLogsFetcher* fetcher = new SystemLogsFetcher(/*scrub_data=*/ true);
  33. // fetcher->AddSource(std::make_unique<LogSourceOne>());
  34. // fetcher->AddSource(std::make_unique<LogSourceTwo>());
  35. // fetcher->Fetch(base::BindOnce(&Example::ProcessLogs, this));
  36. // }
  37. // };
  38. class SystemLogsFetcher {
  39. public:
  40. // If |scrub_data| is true, logs will be redacted.
  41. // |first_party_extension_ids| is a null terminated array of all the 1st
  42. // party extension IDs whose URLs won't be redacted. It is OK to pass null for
  43. // that value if it's OK to redact those URLs or they won't be present.
  44. explicit SystemLogsFetcher(bool scrub_data,
  45. const char* const first_party_extension_ids[]);
  46. SystemLogsFetcher(const SystemLogsFetcher&) = delete;
  47. SystemLogsFetcher& operator=(const SystemLogsFetcher&) = delete;
  48. ~SystemLogsFetcher();
  49. // Adds a source to use when fetching.
  50. void AddSource(std::unique_ptr<SystemLogsSource> source);
  51. // Starts the fetch process. After the fetch completes, this instance calls
  52. // |callback|, then schedules itself to be deleted.
  53. void Fetch(SysLogsFetcherCallback callback);
  54. private:
  55. // Callback passed to all the data sources. May call Scrub(), then calls
  56. // AddResponse().
  57. void OnFetched(const std::string& source_name,
  58. std::unique_ptr<SystemLogsResponse> response);
  59. // Merges the |response| it receives into response_. When all the data sources
  60. // have responded, it deletes their objects and returns the response to the
  61. // callback_. After this it deletes this instance of the object.
  62. void AddResponse(const std::string& source_name,
  63. std::unique_ptr<SystemLogsResponse> response);
  64. #if BUILDFLAG(IS_CHROMEOS_ASH)
  65. // Merges the log entries of crash report ids of Ash and Lacros in
  66. // |response_|, so that lacros crash ids could be processed by the feedback
  67. // pre-processor at the server side in the same way it does for ash crash ids.
  68. // See details in crbug.com/1129051.
  69. void MergeAshAndLacrosCrashReportIdsInReponse();
  70. #endif
  71. // Runs the callback provided to Fetch and posts a task to delete |this|.
  72. void RunCallbackAndDeleteSoon();
  73. SEQUENCE_CHECKER(sequence_checker_);
  74. std::vector<std::unique_ptr<SystemLogsSource>> data_sources_;
  75. SysLogsFetcherCallback callback_;
  76. std::unique_ptr<SystemLogsResponse> response_; // The actual response data.
  77. size_t num_pending_requests_; // The number of callbacks it should get.
  78. scoped_refptr<base::SequencedTaskRunner> task_runner_for_redactor_;
  79. std::unique_ptr<feedback::RedactionTool> redactor_;
  80. base::WeakPtrFactory<SystemLogsFetcher> weak_ptr_factory_{this};
  81. };
  82. } // namespace system_logs
  83. #endif // COMPONENTS_FEEDBACK_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_H_