1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file. 4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#ifndef COMPONENTS_METRICS_CHROMEOS_SERIALIZATION_UTILS_H_ 6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#define COMPONENTS_METRICS_CHROMEOS_SERIALIZATION_UTILS_H_ 7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <string> 9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/memory/scoped_vector.h" 12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace metrics { 14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class MetricSample; 16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Metrics helpers to serialize and deserialize metrics collected by 18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// ChromeOS. 19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace SerializationUtils { 20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Deserializes a sample passed as a string and return a sample. 22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// The return value will either be a scoped_ptr to a Metric sample (if the 23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// deserialization was successful) or a NULL scoped_ptr. 24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)scoped_ptr<MetricSample> ParseSample(const std::string& sample); 25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Reads all samples from a file and truncate the file when done. 27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void ReadAndTruncateMetricsFromFile(const std::string& filename, 28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ScopedVector<MetricSample>* metrics); 29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Serializes a sample and write it to filename. 31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// The format for the message is: 32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// message_size, serialized_message 33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// where 34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// * message_size is the total length of the message (message_size + 35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// serialized_message) on 4 bytes 36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// * serialized_message is the serialized version of sample (using ToString) 37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// 38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// NB: the file will never leave the device so message_size will be written 39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// with the architecture's endianness. 40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool WriteMetricToFile(const MetricSample& sample, const std::string& filename); 41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Maximum length of a serialized message 43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)static const int kMessageMaxLength = 1024; 44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} // namespace SerializationUtils 46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} // namespace metrics 47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif // COMPONENTS_METRICS_CHROMEOS_SERIALIZATION_UTILS_H_ 49