Home | History | Annotate | Download | only in system_logs
      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 #include "chrome/browser/chromeos/system_logs/lsb_release_log_source.h"
      6 
      7 #include <string>
      8 
      9 #include "base/bind.h"
     10 #include "base/bind_helpers.h"
     11 #include "base/file_util.h"
     12 #include "base/files/file_path.h"
     13 #include "base/logging.h"
     14 #include "base/strings/string_split.h"
     15 #include "base/strings/string_util.h"
     16 #include "content/public/browser/browser_thread.h"
     17 
     18 using content::BrowserThread;
     19 
     20 const char kInvalidLogEntry[] = "<invalid characters in log entry>";
     21 const char kEmptyLogEntry[] = "<no value>";
     22 
     23 namespace {
     24 
     25 typedef std::pair<std::string, std::string> KeyValuePair;
     26 
     27 }  // namespace
     28 
     29 namespace chromeos {
     30 
     31 void LsbReleaseLogSource::Fetch(const SysLogsSourceCallback& callback) {
     32   DCHECK(!callback.is_null());
     33 
     34   SystemLogsResponse* response = new SystemLogsResponse;
     35   BrowserThread::PostBlockingPoolTaskAndReply(
     36       FROM_HERE,
     37       base::Bind(&LsbReleaseLogSource::ReadLSBRelease, response),
     38       base::Bind(callback,
     39                  base::Owned(response)));
     40 }
     41 
     42 void LsbReleaseLogSource::ReadLSBRelease(SystemLogsResponse* response) {
     43   DCHECK(response);
     44 
     45   const base::FilePath lsb_release_file("/etc/lsb-release");
     46   std::string lsb_data;
     47   bool read_success = file_util::ReadFileToString(lsb_release_file, &lsb_data);
     48   // if we were using an internal temp file, the user does not need the
     49   // logs to stay past the ReadFile call - delete the file
     50   if (!read_success) {
     51     LOG(ERROR) << "Can't access /etc/lsb-release file.";
     52     return;
     53   }
     54   ParseLSBRelease(lsb_data, response);
     55 }
     56 
     57 void LsbReleaseLogSource::ParseLSBRelease(const std::string& lsb_data,
     58                                           SystemLogsResponse* response) {
     59   std::vector<KeyValuePair> pairs;
     60   base::SplitStringIntoKeyValuePairs(lsb_data, '=', '\n', &pairs);
     61   for (size_t i = 0; i  < pairs.size(); ++i) {
     62     std::string key, value;
     63     TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key);
     64     TrimWhitespaceASCII(pairs[i].second, TRIM_ALL, &value);
     65 
     66     if (key.empty())
     67       continue;
     68     if (!IsStringUTF8(value) || !IsStringUTF8(key)) {
     69       LOG(WARNING) << "Invalid characters in system log entry: " << key;
     70       (*response)[key] = kInvalidLogEntry;
     71       continue;
     72     }
     73 
     74     if (value.empty())
     75       (*response)[key] = kEmptyLogEntry;
     76     else
     77       (*response)[key] = value;
     78   }
     79 }
     80 
     81 }  // namespace chromeos
     82 
     83