| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_SYSLOGS_SYSLOGS_FETCHER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_SYSLOGS_SYSLOGS_FETCHER_H_ |
| 7 |
| 8 #include <string> |
| 9 #include <map> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/callback.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 |
| 15 namespace chromeos { |
| 16 |
| 17 typedef std::map<std::string, std::string> SysLogsResponse; |
| 18 typedef base::Callback<void(SysLogsResponse*)> SysLogsFetcherCallback; |
| 19 |
| 20 // The SysLogsInterface provides a interface for the data sources that the |
| 21 // SysLogsFetcher class uses to fetch logs and other information. |
| 22 class SysLogsInterface { |
| 23 public: |
| 24 // Fetches data and passes it by to the callback |
| 25 virtual void Fetch(const SysLogsFetcherCallback& request) = 0; |
| 26 virtual ~SysLogsInterface() {} |
| 27 }; |
| 28 |
| 29 typedef std::vector<SysLogsInterface*> SysLogsDataSources; |
| 30 |
| 31 // The SysLogsFetcher receives a list of data sources which must be classes that |
| 32 // implement the SysLogsInterface. It's Fetch function receives as a parameter |
| 33 // a callback that takes only one parameter SysLogsResponse that is a map of |
| 34 // keys and values. |
| 35 // Each data source also returns a SysLogsResponse. If two data sources return |
| 36 // the same key, the last one will replace the previous one. |
| 37 // Each data source gets deleted after all of them are called, the |
| 38 // SysLogsFetcher deletes itself after calling the callback it received. |
| 39 // The class runs on the UI thread. |
| 40 // |
| 41 // EXAMPLE: |
| 42 // class Example { |
| 43 // public: |
| 44 // ProcessLogs(chrome::SysLogsResponse) { |
| 45 // //do something with the logs |
| 46 // } |
| 47 // GetLogs() { |
| 48 // chrome::SysLogsFetcher* fetcher = |
| 49 // chrome::SysLogsFetcherFactory::GetInstance()->CreateFetcher(); |
| 50 // fetcher->Fetch(base::Bind(&Example::ProcessLogs,this)); |
| 51 // } |
| 52 class SysLogsFetcher { |
| 53 public: |
| 54 explicit SysLogsFetcher(SysLogsDataSources backends); |
| 55 ~SysLogsFetcher() {} |
| 56 |
| 57 void Fetch(const SysLogsFetcherCallback& request); |
| 58 |
| 59 private: |
| 60 // Callback passed to all the data sources. It merges the |data| it recieves |
| 61 // into response_. When all the data sources have responded, it deletes their |
| 62 // objects and returns the response to the request_ callback. After this it |
| 63 // deletes itself. |
| 64 void AddData(SysLogsResponse* data); |
| 65 |
| 66 SysLogsDataSources data_sources_; |
| 67 SysLogsFetcherCallback request_; |
| 68 |
| 69 SysLogsResponse* response_; // The actual response data. |
| 70 size_t responses_; // The number of callbacks it should get. |
| 71 |
| 72 base::WeakPtrFactory<SysLogsFetcher> weak_ptr_factory_; |
| 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(SysLogsFetcher); |
| 75 }; |
| 76 |
| 77 |
| 78 } // namespace chromeos |
| 79 |
| 80 #endif // CHROME_BROWSER_CHROMEOS_SYSLOGS_SYSLOGS_FETCHER_H_ |
| 81 |
| OLD | NEW |