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/extensions/api/log_private/filter_handler.h" 15 #include "chrome/browser/extensions/api/log_private/log_parser.h" 16 #include "chrome/browser/extensions/api/log_private/syslog_parser.h" 17 #include "chrome/browser/extensions/extension_function.h" 18 #include "chrome/common/extensions/api/log_private.h" 19 20 namespace extensions { 21 namespace { 22 23 scoped_ptr<LogParser> CreateLogParser(const std::string& log_type) { 24 if (log_type == "syslog") 25 return scoped_ptr<LogParser>(new SyslogParser()); 26 // TODO(shinfan): Add more parser here 27 28 NOTREACHED() << "Invalid log type: " << log_type; 29 return scoped_ptr<LogParser>(); 30 } 31 32 void CollectLogInfo( 33 FilterHandler* filter_handler, 34 chromeos::SystemLogsResponse* logs, 35 std::vector<linked_ptr<api::log_private::LogEntry> >* output) { 36 for (chromeos::SystemLogsResponse::const_iterator request_it = logs->begin(); 37 request_it != logs->end(); 38 ++request_it) { 39 if (!filter_handler->IsValidSource(request_it->first)) { 40 continue; 41 } 42 scoped_ptr<LogParser> parser(CreateLogParser(request_it->first)); 43 if (parser) { 44 parser->Parse(request_it->second, output, filter_handler); 45 } 46 } 47 } 48 49 } // namespace 50 51 LogPrivateGetHistoricalFunction::LogPrivateGetHistoricalFunction() { 52 } 53 54 LogPrivateGetHistoricalFunction::~LogPrivateGetHistoricalFunction() { 55 } 56 57 bool LogPrivateGetHistoricalFunction::RunImpl() { 58 // Get parameters 59 scoped_ptr<api::log_private::GetHistorical::Params> params( 60 api::log_private::GetHistorical::Params::Create(*args_)); 61 EXTENSION_FUNCTION_VALIDATE(params.get()); 62 filter_handler_.reset(new FilterHandler(params->filter)); 63 64 chromeos::AboutSystemLogsFetcher* fetcher = 65 new chromeos::AboutSystemLogsFetcher(); 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 87