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 <memory>
23 #include <string>
24 
25 #include "base/compiler_specific.h"
26 #include "base/files/file_path.h"
27 #include "base/gtest_prod_util.h"
28 #include "persistent_integer.h"
29 #include "uploader/proto/system_profile.pb.h"
30 #include "uploader/system_profile_setter.h"
31 
32 namespace metrics {
33 class ChromeUserMetricsExtension;
34 }
35 
36 struct 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.
49 class 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   FRIEND_TEST(UploadServiceTest, ProductIdMandatory);
73 
74   // Fetches all informations and populates |profile_|
75   bool Initialize();
76 
77   // Initializes |profile_| only if it has not been yet initialized.
78   bool InitializeOrCheck();
79 
80   bool initialized_;
81   bool testing_;
82   base::FilePath metrics_directory_;
83   std::unique_ptr<chromeos_metrics::PersistentInteger> session_id_;
84   SystemProfile profile_;
85 };
86 
87 #endif  // METRICS_UPLOADER_SYSTEM_PROFILE_CACHE_H_
88