| 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 { |
| 56 public: | 52 public: |
| 57 SystemLogsFetcher(); | 53 SystemLogsFetcherBase(); |
| 58 ~SystemLogsFetcher(); | 54 ~SystemLogsFetcherBase(); |
| 59 | 55 |
| 60 void Fetch(const SysLogsFetcherCallback& callback); | 56 void Fetch(const SysLogsFetcherCallback& callback); |
| 61 | 57 |
| 62 private: | 58 protected: |
| 59 // Used to get a weak ptr for a derived class instance. |
| 60 virtual base::WeakPtr<SystemLogsFetcherBase> GetWeakPtr() = 0; |
| 61 |
| 63 // Callback passed to all the data sources. It merges the |data| it recieves | 62 // 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 | 63 // into response_. When all the data sources have responded, it deletes their |
| 65 // objects and returns the response to the callback_. After this it | 64 // objects and returns the response to the callback_. After this it |
| 66 // deletes this instance of the object. | 65 // deletes this instance of the object. |
| 67 void AddResponse(SystemLogsResponse* response); | 66 void AddResponse(SystemLogsResponse* response); |
| 68 | 67 |
| 69 ScopedVector<SystemLogsSource> data_sources_; | 68 ScopedVector<SystemLogsSource> data_sources_; |
| 70 SysLogsFetcherCallback callback_; | 69 SysLogsFetcherCallback callback_; |
| 71 | 70 |
| 72 scoped_ptr<SystemLogsResponse> response_; // The actual response data. | 71 scoped_ptr<SystemLogsResponse> response_; // The actual response data. |
| 73 size_t num_pending_requests_; // The number of callbacks it should get. | 72 size_t num_pending_requests_; // The number of callbacks it should get. |
| 74 | 73 |
| 75 base::WeakPtrFactory<SystemLogsFetcher> weak_ptr_factory_; | 74 private: |
| 76 | 75 |
| 77 DISALLOW_COPY_AND_ASSIGN(SystemLogsFetcher); | 76 DISALLOW_COPY_AND_ASSIGN(SystemLogsFetcherBase); |
| 78 }; | 77 }; |
| 79 | 78 |
| 80 } // namespace chromeos | 79 } // namespace chromeos |
| 81 | 80 |
| 82 #endif // CHROME_BROWSER_CHROMEOS_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_H_ | 81 #endif // CHROME_BROWSER_CHROMEOS_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_BASE_H_ |
| 83 | 82 |
| OLD | NEW |