OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_CHROMEOS_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_BASE_H_ |
6 #define CHROME_BROWSER_CHROMEOS_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_BASE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 | 14 |
15 namespace chromeos { | 15 namespace chromeos { |
16 | 16 |
17 typedef std::map<std::string, std::string> SystemLogsResponse; | 17 typedef std::map<std::string, std::string> SystemLogsResponse; |
18 | 18 |
19 // Callback that the data sources use to return data. | 19 // Callback that the data sources use to return data. |
20 typedef base::Callback<void(SystemLogsResponse* response)> | 20 typedef base::Callback<void(SystemLogsResponse* response)> |
21 SysLogsSourceCallback; | 21 SysLogsSourceCallback; |
22 | 22 |
23 // Callback that the SystemLogsFetcher uses to return data. | 23 // Callback that the SystemLogsFetcherBase uses to return data. |
24 typedef base::Callback<void(scoped_ptr<SystemLogsResponse> response)> | 24 typedef base::Callback<void(scoped_ptr<SystemLogsResponse> response)> |
25 SysLogsFetcherCallback; | 25 SysLogsFetcherCallback; |
26 | 26 |
27 // The SystemLogsSource provides a interface for the data sources that | 27 // The SystemLogsSource provides a interface for the data sources that |
28 // the SystemLogsFetcher class uses to fetch logs and other | 28 // the SystemLogsFetcherBase class uses to fetch logs and other |
29 // information. | 29 // information. |
30 class SystemLogsSource { | 30 class SystemLogsSource { |
31 public: | 31 public: |
32 // Fetches data and passes it by to the callback | 32 // Fetches data and passes it by to the callback |
33 virtual void Fetch(const SysLogsSourceCallback& callback) = 0; | 33 virtual void Fetch(const SysLogsSourceCallback& callback) = 0; |
34 virtual ~SystemLogsSource() {} | 34 virtual ~SystemLogsSource() {} |
35 }; | 35 }; |
36 | 36 |
37 // The SystemLogsFetcher creates a list of data sources which must be | 37 // The SystemLogsFetcherBaseBase specifies an interface for LogFetcher classes. |
38 // classes that implement the SystemLogsSource. It's Fetch function | 38 // Derived LogFetcher classes aggregate the logs from a list of SystemLogSource |
39 // receives as a parameter a callback that takes only one parameter | 39 // classes. |
40 // SystemLogsResponse that is a map of keys and values. | |
41 // Each data source also returns a SystemLogsResponse. If two data sources | |
42 // return the same key, only the first one will be stored. | |
43 // The class runs on the UI thread. | |
44 // | 40 // |
45 // EXAMPLE: | 41 // EXAMPLE: |
46 // class Example { | 42 // class Example { |
47 // public: | 43 // public: |
48 // void ProcessLogs(SystemLogsResponse* response) { | 44 // void ProcessLogs(SystemLogsResponse* response) { |
49 // //do something with the logs | 45 // //do something with the logs |
50 // } | 46 // } |
51 // void GetLogs() { | 47 // void GetLogs() { |
52 // SystemLogsFetcher* fetcher = new SystemLogsFetcher(); | 48 // SystemLogsFetcherBase* fetcher = new SystemLogsFetcherBase(); |
53 // fetcher->Fetch(base::Bind(&Example::ProcessLogs, this)); | 49 // fetcher->Fetch(base::Bind(&Example::ProcessLogs, this)); |
54 // } | 50 // } |
55 class SystemLogsFetcher { | 51 class SystemLogsFetcherBase |
| 52 : public base::SupportsWeakPtr<SystemLogsFetcherBase> { |
56 public: | 53 public: |
57 SystemLogsFetcher(); | 54 SystemLogsFetcherBase(); |
58 ~SystemLogsFetcher(); | 55 ~SystemLogsFetcherBase(); |
59 | 56 |
60 void Fetch(const SysLogsFetcherCallback& callback); | 57 void Fetch(const SysLogsFetcherCallback& callback); |
61 | 58 |
62 private: | 59 protected: |
63 // Callback passed to all the data sources. It merges the |data| it recieves | 60 // Callback passed to all the data sources. It merges the |data| it recieves |
64 // into response_. When all the data sources have responded, it deletes their | 61 // into response_. When all the data sources have responded, it deletes their |
65 // objects and returns the response to the callback_. After this it | 62 // objects and returns the response to the callback_. After this it |
66 // deletes this instance of the object. | 63 // deletes this instance of the object. |
67 void AddResponse(SystemLogsResponse* response); | 64 void AddResponse(SystemLogsResponse* response); |
68 | 65 |
69 ScopedVector<SystemLogsSource> data_sources_; | 66 ScopedVector<SystemLogsSource> data_sources_; |
70 SysLogsFetcherCallback callback_; | 67 SysLogsFetcherCallback callback_; |
71 | 68 |
72 scoped_ptr<SystemLogsResponse> response_; // The actual response data. | 69 scoped_ptr<SystemLogsResponse> response_; // The actual response data. |
73 size_t num_pending_requests_; // The number of callbacks it should get. | 70 size_t num_pending_requests_; // The number of callbacks it should get. |
74 | 71 |
75 base::WeakPtrFactory<SystemLogsFetcher> weak_ptr_factory_; | 72 private: |
76 | 73 |
77 DISALLOW_COPY_AND_ASSIGN(SystemLogsFetcher); | 74 DISALLOW_COPY_AND_ASSIGN(SystemLogsFetcherBase); |
78 }; | 75 }; |
79 | 76 |
80 } // namespace chromeos | 77 } // namespace chromeos |
81 | 78 |
82 #endif // CHROME_BROWSER_CHROMEOS_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_H_ | 79 #endif // CHROME_BROWSER_CHROMEOS_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_BASE_H_ |
83 | 80 |
OLD | NEW |