Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 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 | #include "chrome/browser/extensions/api/log_private/log_private_api.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/json/json_writer.h" |
| 11 | #include "base/logging.h" |
| 12 | #include "base/memory/linked_ptr.h" |
| 13 | #include "base/memory/scoped_ptr.h" |
| 14 | #include "chrome/browser/chromeos/system_logs/system_logs_fetcher.h" |
| 15 | #include "chrome/browser/extensions/api/log_private/filter_handler.h" |
| 16 | #include "chrome/browser/extensions/api/log_private/log_parser.h" |
| 17 | #include "chrome/browser/extensions/api/log_private/syslog_parser.h" |
| 18 | #include "chrome/browser/extensions/extension_function.h" |
| 19 | #include "chrome/common/extensions/api/log_private.h" |
| 20 | |
| 21 | namespace extensions { |
| 22 | namespace { |
| 23 | |
| 24 | scoped_ptr<LogParser> CreateLogParser(const std::string& log_type) { |
| 25 | if (log_type == "syslog") |
| 26 | return scoped_ptr<LogParser>(new SyslogParser()); |
| 27 | // TODO(shinfan): Add more parser here |
| 28 | |
| 29 | NOTREACHED() << "Invalid log type: " << log_type; |
| 30 | return scoped_ptr<LogParser>(); |
| 31 | } |
| 32 | |
| 33 | void CollectLogInfo( |
| 34 | FilterHandler* filter_handler, |
| 35 | chromeos::SystemLogsResponse* logs, |
| 36 | std::vector<linked_ptr<api::log_private::LogEntry> >* output) { |
| 37 | for (chromeos::SystemLogsResponse::const_iterator request_it = logs->begin(); |
| 38 | request_it != logs->end(); |
| 39 | ++request_it) { |
| 40 | if (!filter_handler->IsValidSource(request_it->first)) { |
| 41 | continue; |
| 42 | } |
| 43 | scoped_ptr<LogParser> parser(CreateLogParser(request_it->first)); |
| 44 | if (parser) { |
| 45 | parser->Parse(request_it->second, output, filter_handler); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
| 52 | LogPrivateGetHistoricalFunction::LogPrivateGetHistoricalFunction() { |
| 53 | } |
| 54 | |
| 55 | LogPrivateGetHistoricalFunction::~LogPrivateGetHistoricalFunction() { |
| 56 | } |
| 57 | |
| 58 | bool LogPrivateGetHistoricalFunction::RunImpl() { |
| 59 | // Get parameters |
| 60 | scoped_ptr<api::log_private::GetHistorical::Params> params( |
| 61 | api::log_private::GetHistorical::Params::Create(*args_)); |
| 62 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 63 | filter_handler_.reset(new FilterHandler(params->filter)); |
| 64 | |
| 65 | chromeos::SystemLogsFetcher* fetcher = new chromeos::SystemLogsFetcher(); |
| 66 | fetcher->Fetch( |
| 67 | base::Bind(&LogPrivateGetHistoricalFunction::OnSystemLogsLoaded, this)); |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | void LogPrivateGetHistoricalFunction::OnSystemLogsLoaded( |
| 72 | scoped_ptr<chromeos::SystemLogsResponse> sys_info) { |
| 73 | std::vector<linked_ptr<api::log_private::LogEntry> > data; |
| 74 | |
| 75 | CollectLogInfo(filter_handler_.get(), sys_info.get(), &data); |
| 76 | |
| 77 | // Prepare result |
| 78 | api::log_private::Result result; |
| 79 | result.data = data; |
| 80 | api::log_private::Filter::Populate( |
| 81 | *((filter_handler_->GetFilter())->ToValue()), &result.filter); |
| 82 | SetResult(result.ToValue().release()); |
| 83 | SendResponse(true); |
| 84 | } |
| 85 | |
| 86 | } // namespace extensions |