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
5#ifndef CHROME_BROWSER_FEEDBACK_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_BASE_H_
6#define CHROME_BROWSER_FEEDBACK_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_BASE_H_
7
8#include <map>
9#include <string>
10
11#include "base/callback.h"
12#include "base/memory/scoped_vector.h"
13#include "base/memory/weak_ptr.h"
14
15namespace system_logs {
16
17typedef std::map<std::string, std::string> SystemLogsResponse;
18
19// Callback that the data sources use to return data.
20typedef base::Callback<void(SystemLogsResponse* response)>
21    SysLogsSourceCallback;
22
23// Callback that the SystemLogsFetcherBase uses to return data.
24typedef base::Callback<void(scoped_ptr<SystemLogsResponse> response)>
25    SysLogsFetcherCallback;
26
27// The SystemLogsSource provides a interface for the data sources that
28// the SystemLogsFetcherBase class uses to fetch logs and other
29// information.
30class SystemLogsSource {
31 public:
32  // Fetches data and passes it by to the callback
33  virtual void Fetch(const SysLogsSourceCallback& callback) = 0;
34  virtual ~SystemLogsSource() {}
35};
36
37// The SystemLogsFetcherBaseBase specifies an interface for LogFetcher classes.
38// Derived LogFetcher classes aggregate the logs from a list of SystemLogSource
39// classes.
40//
41// EXAMPLE:
42// class Example {
43//  public:
44//   void ProcessLogs(SystemLogsResponse* response) {
45//      //do something with the logs
46//   }
47//   void GetLogs() {
48//     SystemLogsFetcherBase* fetcher = new SystemLogsFetcherBase();
49//     fetcher->Fetch(base::Bind(&Example::ProcessLogs, this));
50//   }
51class SystemLogsFetcherBase
52    : public base::SupportsWeakPtr<SystemLogsFetcherBase> {
53 public:
54  SystemLogsFetcherBase();
55  ~SystemLogsFetcherBase();
56
57  void Fetch(const SysLogsFetcherCallback& callback);
58
59 protected:
60  // Callback passed to all the data sources. It merges the |data| it receives
61  // into response_. When all the data sources have responded, it deletes their
62  // objects and returns the response to the callback_. After this it
63  // deletes this instance of the object.
64  void AddResponse(SystemLogsResponse* response);
65
66  ScopedVector<SystemLogsSource> data_sources_;
67  SysLogsFetcherCallback callback_;
68
69  scoped_ptr<SystemLogsResponse> response_;  // The actual response data.
70  size_t num_pending_requests_;   // The number of callbacks it should get.
71
72 private:
73
74  DISALLOW_COPY_AND_ASSIGN(SystemLogsFetcherBase);
75};
76
77}  // namespace system_logs
78
79#endif  // CHROME_BROWSER_FEEDBACK_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_BASE_H_
80