OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "update_engine/omaha_request_params.h" | 5 #include "update_engine/omaha_request_params.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/utsname.h> | 9 #include <sys/utsname.h> |
10 | 10 |
11 #include <map> | 11 #include <map> |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/file_util.h" | 14 #include <base/file_util.h> |
15 #include "base/string_util.h" | 15 #include <base/string_util.h> |
| 16 |
16 #include "update_engine/simple_key_value_store.h" | 17 #include "update_engine/simple_key_value_store.h" |
17 #include "update_engine/utils.h" | 18 #include "update_engine/utils.h" |
18 | 19 |
| 20 #define CALL_MEMBER_FN(object, member) ((object).*(member)) |
| 21 |
19 using std::map; | 22 using std::map; |
20 using std::string; | 23 using std::string; |
21 | 24 |
22 namespace chromeos_update_engine { | 25 namespace chromeos_update_engine { |
23 | 26 |
| 27 const char OmahaRequestParams::kUpdateTrackKey[] = "CHROMEOS_RELEASE_TRACK"; |
| 28 |
24 const char* const OmahaRequestParams::kAppId( | 29 const char* const OmahaRequestParams::kAppId( |
25 "{87efface-864d-49a5-9bb3-4b050a7c227a}"); | 30 "{87efface-864d-49a5-9bb3-4b050a7c227a}"); |
26 const char* const OmahaRequestParams::kOsPlatform("Chrome OS"); | 31 const char* const OmahaRequestParams::kOsPlatform("Chrome OS"); |
27 const char* const OmahaRequestParams::kOsVersion("Indy"); | 32 const char* const OmahaRequestParams::kOsVersion("Indy"); |
28 const char* const OmahaRequestParams::kUpdateUrl( | 33 const char* const OmahaRequestParams::kUpdateUrl( |
29 "https://tools.google.com/service/update2"); | 34 "https://tools.google.com/service/update2"); |
30 | 35 |
31 static const char kHWIDPath[] = "/sys/devices/platform/chromeos_acpi/HWID"; | 36 static const char kHWIDPath[] = "/sys/devices/platform/chromeos_acpi/HWID"; |
32 | 37 |
| 38 OmahaRequestDeviceParams::OmahaRequestDeviceParams() : |
| 39 force_build_type_(false), |
| 40 forced_official_build_(false) {} |
| 41 |
33 bool OmahaRequestDeviceParams::Init(const std::string& in_app_version, | 42 bool OmahaRequestDeviceParams::Init(const std::string& in_app_version, |
34 const std::string& in_update_url) { | 43 const std::string& in_update_url) { |
35 os_platform = OmahaRequestParams::kOsPlatform; | 44 os_platform = OmahaRequestParams::kOsPlatform; |
36 os_version = OmahaRequestParams::kOsVersion; | 45 os_version = OmahaRequestParams::kOsVersion; |
37 app_version = in_app_version.empty() ? | 46 app_version = in_app_version.empty() ? |
38 GetLsbValue("CHROMEOS_RELEASE_VERSION", "") : in_app_version; | 47 GetLsbValue("CHROMEOS_RELEASE_VERSION", "", NULL) : in_app_version; |
39 os_sp = app_version + "_" + GetMachineType(); | 48 os_sp = app_version + "_" + GetMachineType(); |
40 os_board = GetLsbValue("CHROMEOS_RELEASE_BOARD", ""); | 49 os_board = GetLsbValue("CHROMEOS_RELEASE_BOARD", "", NULL); |
41 app_id = OmahaRequestParams::kAppId; | 50 app_id = OmahaRequestParams::kAppId; |
42 app_lang = "en-US"; | 51 app_lang = "en-US"; |
43 app_track = GetLsbValue("CHROMEOS_RELEASE_TRACK", ""); | 52 app_track = GetLsbValue( |
| 53 kUpdateTrackKey, |
| 54 "", |
| 55 &chromeos_update_engine::OmahaRequestDeviceParams::IsValidTrack); |
44 hardware_class = GetHardwareClass(); | 56 hardware_class = GetHardwareClass(); |
45 struct stat stbuf; | 57 struct stat stbuf; |
46 | 58 |
47 // Deltas are only okay if the /.nodelta file does not exist. | 59 // Deltas are only okay if the /.nodelta file does not exist. |
48 // If we don't know (i.e. stat() returns some unexpected error), | 60 // If we don't know (i.e. stat() returns some unexpected error), |
49 // then err on the side of caution and say deltas are not okay | 61 // then err on the side of caution and say deltas are not okay |
50 delta_okay = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) && | 62 delta_okay = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) && |
51 (errno == ENOENT); | 63 (errno == ENOENT); |
52 | 64 |
53 update_url = in_update_url.empty() ? | 65 update_url = in_update_url.empty() ? |
54 GetLsbValue("CHROMEOS_AUSERVER", OmahaRequestParams::kUpdateUrl) : | 66 GetLsbValue("CHROMEOS_AUSERVER", OmahaRequestParams::kUpdateUrl, NULL) : |
55 in_update_url; | 67 in_update_url; |
56 return true; | 68 return true; |
57 } | 69 } |
58 | 70 |
59 string OmahaRequestDeviceParams::GetLsbValue( | 71 bool OmahaRequestDeviceParams::SetTrack(const std::string& track) { |
60 const string& key, const string& default_value) const { | 72 TEST_AND_RETURN_FALSE(IsValidTrack(track)); |
| 73 FilePath kFile(root_ + utils::kStatefulPartition + "/etc/lsb-release"); |
| 74 string file_data; |
| 75 map<string, string> data; |
| 76 if (file_util::ReadFileToString(kFile, &file_data)) { |
| 77 data = simple_key_value_store::ParseString(file_data); |
| 78 } |
| 79 data[kUpdateTrackKey] = track; |
| 80 file_data = simple_key_value_store::AssembleString(data); |
| 81 TEST_AND_RETURN_FALSE(file_util::CreateDirectory(kFile.DirName())); |
| 82 TEST_AND_RETURN_FALSE( |
| 83 file_util::WriteFile(kFile, file_data.data(), file_data.size()) == |
| 84 static_cast<int>(file_data.size())); |
| 85 app_track = track; |
| 86 return true; |
| 87 } |
| 88 |
| 89 bool OmahaRequestDeviceParams::SetDeviceTrack(const std::string& track) { |
| 90 OmahaRequestDeviceParams params; |
| 91 TEST_AND_RETURN_FALSE(params.Init("", "")); |
| 92 return params.SetTrack(track); |
| 93 } |
| 94 |
| 95 string OmahaRequestDeviceParams::GetLsbValue(const string& key, |
| 96 const string& default_value, |
| 97 ValueValidator validator) const { |
61 string files[] = {string(utils::kStatefulPartition) + "/etc/lsb-release", | 98 string files[] = {string(utils::kStatefulPartition) + "/etc/lsb-release", |
62 "/etc/lsb-release"}; | 99 "/etc/lsb-release"}; |
63 for (unsigned int i = 0; i < arraysize(files); ++i) { | 100 for (unsigned int i = 0; i < arraysize(files); ++i) { |
64 // TODO(adlr): make sure files checked are owned as root (and all | 101 // TODO(adlr): make sure files checked are owned as root (and all |
65 // their parents are recursively, too). | 102 // their parents are recursively, too). |
66 string file_data; | 103 string file_data; |
67 if (!utils::ReadFileToString(root_ + files[i], &file_data)) | 104 if (!utils::ReadFileToString(root_ + files[i], &file_data)) |
68 continue; | 105 continue; |
69 | 106 |
70 map<string, string> data = simple_key_value_store::ParseString(file_data); | 107 map<string, string> data = simple_key_value_store::ParseString(file_data); |
71 if (utils::MapContainsKey(data, key)) | 108 if (utils::MapContainsKey(data, key)) { |
72 return data[key]; | 109 const string& value = data[key]; |
| 110 if (validator && !CALL_MEMBER_FN(*this, validator)(value)) { |
| 111 continue; |
| 112 } |
| 113 return value; |
| 114 } |
73 } | 115 } |
74 // not found | 116 // not found |
75 return default_value; | 117 return default_value; |
76 } | 118 } |
77 | 119 |
78 string OmahaRequestDeviceParams::GetMachineType() const { | 120 string OmahaRequestDeviceParams::GetMachineType() const { |
79 struct utsname buf; | 121 struct utsname buf; |
80 string ret; | 122 string ret; |
81 if (uname(&buf) == 0) | 123 if (uname(&buf) == 0) |
82 ret = buf.machine; | 124 ret = buf.machine; |
83 return ret; | 125 return ret; |
84 } | 126 } |
85 | 127 |
86 string OmahaRequestDeviceParams::GetHardwareClass() const { | 128 string OmahaRequestDeviceParams::GetHardwareClass() const { |
87 string hwid; | 129 string hwid; |
88 if (!file_util::ReadFileToString(FilePath(root_ + kHWIDPath), &hwid)) { | 130 if (!file_util::ReadFileToString(FilePath(root_ + kHWIDPath), &hwid)) { |
89 LOG(ERROR) << "Unable to determine the system hardware qualification ID."; | 131 LOG(ERROR) << "Unable to determine the system hardware qualification ID."; |
90 return ""; | 132 return ""; |
91 } | 133 } |
92 TrimWhitespaceASCII(hwid, TRIM_ALL, &hwid); | 134 TrimWhitespaceASCII(hwid, TRIM_ALL, &hwid); |
93 return hwid; | 135 return hwid; |
94 } | 136 } |
95 | 137 |
| 138 bool OmahaRequestDeviceParams::IsOfficialBuild() const { |
| 139 return force_build_type_ ? forced_official_build_ : utils::IsOfficialBuild(); |
| 140 } |
| 141 |
| 142 bool OmahaRequestDeviceParams::IsValidTrack(const std::string& track) const { |
| 143 return IsOfficialBuild() ? |
| 144 (track == "beta-channel" || track == "dev-channel") : true; |
| 145 } |
| 146 |
| 147 void OmahaRequestDeviceParams::SetBuildTypeOfficial(bool is_official) { |
| 148 force_build_type_ = true; |
| 149 forced_official_build_ = is_official; |
| 150 } |
| 151 |
96 } // namespace chromeos_update_engine | 152 } // namespace chromeos_update_engine |
OLD | NEW |