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/filter_handler.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "chrome/common/extensions/api/log_private.h" |
| 11 | |
| 12 | namespace extensions { |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | template <typename T> |
| 17 | bool IsValidField(const std::vector<T>& filter, const T& field) { |
| 18 | return (!filter.size() || |
| 19 | std::find(filter.begin(), filter.end(), field) != filter.end()); |
| 20 | } |
| 21 | |
| 22 | } // namespace |
| 23 | |
| 24 | FilterHandler::FilterHandler(const api::log_private::Filter& filter) { |
| 25 | scoped_ptr<base::DictionaryValue> filter_value = filter.ToValue(); |
| 26 | api::log_private::Filter::Populate(*filter_value, &filter_); |
| 27 | } |
| 28 | |
| 29 | FilterHandler::~FilterHandler() {} |
| 30 | |
| 31 | bool FilterHandler::IsValidLogEntry( |
| 32 | const api::log_private::LogEntry& entry) const { |
| 33 | return (IsValidProcess(entry.process) && IsValidLevel(entry.level) && |
| 34 | IsValidTime(entry.timestamp)); |
| 35 | } |
| 36 | |
| 37 | bool FilterHandler::IsValidTime(double time) const { |
| 38 | const double kInvalidTime = 0; |
| 39 | if (filter_.start_timestamp != kInvalidTime && |
| 40 | (filter_.start_timestamp > time || filter_.end_timestamp < time)) { |
| 41 | return false; |
| 42 | } |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | bool FilterHandler::IsValidSource(const std::string& source) const { |
| 47 | return IsValidField(filter_.sources, source); |
| 48 | } |
| 49 | |
| 50 | bool FilterHandler::IsValidLevel(const std::string& level) const { |
| 51 | return IsValidField(filter_.level, level); |
| 52 | } |
| 53 | |
| 54 | bool FilterHandler::IsValidProcess(const std::string& process) const { |
| 55 | return IsValidField(filter_.process, process); |
| 56 | } |
| 57 | |
| 58 | } // namespace extensions |