| 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_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_H_ | |
| 6 #define CHROME_BROWSER_CHROMEOS_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_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 | |
| 15 namespace chromeos { | |
| 16 | |
| 17 typedef std::map<std::string, std::string> SystemLogsResponse; | |
| 18 | |
| 19 // Callback that the data sources use to return data. | |
| 20 typedef base::Callback<void(SystemLogsResponse* response)> | |
| 21 SysLogsSourceCallback; | |
| 22 | |
| 23 // Callback that the SystemLogsFetcher uses to return data. | |
| 24 typedef base::Callback<void(scoped_ptr<SystemLogsResponse> response)> | |
| 25 SysLogsFetcherCallback; | |
| 26 | |
| 27 // The SystemLogsSource provides a interface for the data sources that | |
| 28 // the SystemLogsFetcher class uses to fetch logs and other | |
| 29 // information. | |
| 30 class 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 SystemLogsFetcher creates a list of data sources which must be | |
| 38 // classes that implement the SystemLogsSource. It's Fetch function | |
| 39 // receives as a parameter a callback that takes only one parameter | |
| 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 // | |
| 45 // EXAMPLE: | |
| 46 // class Example { | |
| 47 // public: | |
| 48 // void ProcessLogs(SystemLogsResponse* response) { | |
| 49 // //do something with the logs | |
| 50 // } | |
| 51 // void GetLogs() { | |
| 52 // SystemLogsFetcher* fetcher = new SystemLogsFetcher(); | |
| 53 // fetcher->Fetch(base::Bind(&Example::ProcessLogs, this)); | |
| 54 // } | |
| 55 class SystemLogsFetcher { | |
| 56 public: | |
| 57 SystemLogsFetcher(); | |
| 58 ~SystemLogsFetcher(); | |
| 59 | |
| 60 void Fetch(const SysLogsFetcherCallback& callback); | |
| 61 | |
| 62 private: | |
| 63 // 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 | |
| 65 // objects and returns the response to the callback_. After this it | |
| 66 // deletes this instance of the object. | |
| 67 void AddResponse(SystemLogsResponse* response); | |
| 68 | |
| 69 ScopedVector<SystemLogsSource> data_sources_; | |
| 70 SysLogsFetcherCallback callback_; | |
| 71 | |
| 72 scoped_ptr<SystemLogsResponse> response_; // The actual response data. | |
| 73 size_t num_pending_requests_; // The number of callbacks it should get. | |
| 74 | |
| 75 base::WeakPtrFactory<SystemLogsFetcher> weak_ptr_factory_; | |
| 76 | |
| 77 DISALLOW_COPY_AND_ASSIGN(SystemLogsFetcher); | |
| 78 }; | |
| 79 | |
| 80 } // namespace chromeos | |
| 81 | |
| 82 #endif // CHROME_BROWSER_CHROMEOS_SYSTEM_LOGS_SYSTEM_LOGS_FETCHER_H_ | |
| 83 | |
| OLD | NEW |