system_profile_cache.h revision e6b96d6d194b707ab8c5d9f7c54e7bd7c8a87302
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef METRICS_UPLOADER_SYSTEM_PROFILE_CACHE_H_
18#define METRICS_UPLOADER_SYSTEM_PROFILE_CACHE_H_
19
20#include <stdint.h>
21
22#include <string>
23
24#include "base/compiler_specific.h"
25#include "base/files/file_path.h"
26#include "base/gtest_prod_util.h"
27#include "base/memory/scoped_ptr.h"
28#include "persistent_integer.h"
29#include "uploader/proto/system_profile.pb.h"
30#include "uploader/system_profile_setter.h"
31
32namespace metrics {
33class ChromeUserMetricsExtension;
34}
35
36struct SystemProfile {
37  std::string version;
38  std::string model_manifest_id;
39  std::string client_id;
40  int session_id;
41  metrics::SystemProfileProto::Channel channel;
42  std::string product_id;
43};
44
45// Retrieves general system informations needed by the protobuf for context and
46// remembers them to avoid expensive calls.
47//
48// The cache is populated lazily. The only method needed is Populate.
49class SystemProfileCache : public SystemProfileSetter {
50 public:
51  SystemProfileCache();
52
53  SystemProfileCache(bool testing, const base::FilePath& metrics_directory);
54
55  // Populates the ProfileSystem protobuf with system information.
56  bool Populate(metrics::ChromeUserMetricsExtension* metrics_proto) override;
57
58  // Converts a string representation of the channel to a
59  // SystemProfileProto_Channel
60  static metrics::SystemProfileProto_Channel ProtoChannelFromString(
61      const std::string& channel);
62
63  // Gets the persistent GUID and create it if it has not been created yet.
64  static std::string GetPersistentGUID(const std::string& filename);
65
66 private:
67  friend class UploadServiceTest;
68  FRIEND_TEST(UploadServiceTest, ExtractChannelFromDescription);
69  FRIEND_TEST(UploadServiceTest, ReadKeyValueFromFile);
70  FRIEND_TEST(UploadServiceTest, SessionIdIncrementedAtInitialization);
71  FRIEND_TEST(UploadServiceTest, ValuesInConfigFileAreSent);
72
73  // Fetches all informations and populates |profile_|
74  bool Initialize();
75
76  // Initializes |profile_| only if it has not been yet initialized.
77  bool InitializeOrCheck();
78
79  bool initialized_;
80  bool testing_;
81  base::FilePath metrics_directory_;
82  scoped_ptr<chromeos_metrics::PersistentInteger> session_id_;
83  SystemProfile profile_;
84};
85
86#endif  // METRICS_UPLOADER_SYSTEM_PROFILE_CACHE_H_
87