summaryrefslogtreecommitdiffstats
path: root/chrome/browser/feedback/system_logs/system_logs_fetcher_base.h
blob: 521e15dd5ca4f12990a88bcc9fb37a8b467db0f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_FEEDBACK_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_BASE_H_
#define CHROME_BROWSER_FEEDBACK_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_BASE_H_

#include <map>
#include <string>

#include "base/callback.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"

namespace system_logs {

typedef std::map<std::string, std::string> SystemLogsResponse;

// Callback that the data sources use to return data.
typedef base::Callback<void(SystemLogsResponse* response)>
    SysLogsSourceCallback;

// Callback that the SystemLogsFetcherBase uses to return data.
typedef base::Callback<void(scoped_ptr<SystemLogsResponse> response)>
    SysLogsFetcherCallback;

// The SystemLogsSource provides a interface for the data sources that
// the SystemLogsFetcherBase class uses to fetch logs and other
// information.
class SystemLogsSource {
 public:
  // |source_name| provides a descriptive identifier for debugging.
  explicit SystemLogsSource(const std::string& source_name);
  virtual ~SystemLogsSource();

  // Fetches data and passes it by to the callback
  virtual void Fetch(const SysLogsSourceCallback& callback) = 0;

  const std::string& source_name() const { return source_name_; }

 private:
  std::string source_name_;
};

// The SystemLogsFetcherBaseBase specifies an interface for LogFetcher classes.
// Derived LogFetcher classes aggregate the logs from a list of SystemLogSource
// classes.
//
// EXAMPLE:
// class Example {
//  public:
//   void ProcessLogs(SystemLogsResponse* response) {
//      //do something with the logs
//   }
//   void GetLogs() {
//     SystemLogsFetcherBase* fetcher = new SystemLogsFetcherBase();
//     fetcher->Fetch(base::Bind(&Example::ProcessLogs, this));
//   }
class SystemLogsFetcherBase
    : public base::SupportsWeakPtr<SystemLogsFetcherBase> {
 public:
  SystemLogsFetcherBase();
  ~SystemLogsFetcherBase();

  void Fetch(const SysLogsFetcherCallback& callback);

 protected:
  // Callback passed to all the data sources. It merges the |data| it receives
  // into response_. When all the data sources have responded, it deletes their
  // objects and returns the response to the callback_. After this it
  // deletes this instance of the object.
  void AddResponse(const std::string& source_name,
                   SystemLogsResponse* response);

  ScopedVector<SystemLogsSource> data_sources_;
  SysLogsFetcherCallback callback_;

  scoped_ptr<SystemLogsResponse> response_;  // The actual response data.
  size_t num_pending_requests_;   // The number of callbacks it should get.

 private:

  DISALLOW_COPY_AND_ASSIGN(SystemLogsFetcherBase);
};

}  // namespace system_logs

#endif  // CHROME_BROWSER_FEEDBACK_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_BASE_H_