blob: 0789fbb06426485ae2d2f79e7d6c68741b63eb42 [file] [log] [blame]
Darin Petkova4a8a8c2010-07-15 22:21:12 -07001// 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
3// found in the LICENSE file.
4
5#include "update_engine/omaha_request_params.h"
6
7#include <errno.h>
8#include <fcntl.h>
9#include <sys/utsname.h>
10
11#include <map>
12#include <string>
13
14#include "base/string_util.h"
15#include "update_engine/simple_key_value_store.h"
16#include "update_engine/utils.h"
17
18using std::map;
19using std::string;
20
Darin Petkova4a8a8c2010-07-15 22:21:12 -070021namespace chromeos_update_engine {
22
Darin Petkov5a7f5652010-07-22 21:40:09 -070023const char* const OmahaRequestParams::kAppId(
24 "{87efface-864d-49a5-9bb3-4b050a7c227a}");
25const char* const OmahaRequestParams::kOsPlatform("Chrome OS");
26const char* const OmahaRequestParams::kOsVersion("Indy");
27const char* const OmahaRequestParams::kUpdateUrl(
28 "https://tools.google.com/service/update2");
29
30bool OmahaRequestDeviceParams::Init(const std::string& in_app_version,
31 const std::string& in_update_url) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -070032 os_platform = OmahaRequestParams::kOsPlatform;
33 os_version = OmahaRequestParams::kOsVersion;
Darin Petkov5a7f5652010-07-22 21:40:09 -070034 app_version = in_app_version.empty() ?
35 GetLsbValue("CHROMEOS_RELEASE_VERSION", "") : in_app_version;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070036 os_sp = app_version + "_" + GetMachineType();
37 os_board = GetLsbValue("CHROMEOS_RELEASE_BOARD", "");
38 app_id = OmahaRequestParams::kAppId;
39 app_lang = "en-US";
40 app_track = GetLsbValue("CHROMEOS_RELEASE_TRACK", "");
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070041 struct stat stbuf;
Darin Petkov5a7f5652010-07-22 21:40:09 -070042
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070043 // Deltas are only okay if the /.nodelta file does not exist.
44 // If we don't know (i.e. stat() returns some unexpected error),
45 // then err on the side of caution and say deltas are not okay
46 delta_okay = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) &&
47 (errno == ENOENT);
48
Darin Petkov5a7f5652010-07-22 21:40:09 -070049 update_url = in_update_url.empty() ?
50 GetLsbValue("CHROMEOS_AUSERVER", OmahaRequestParams::kUpdateUrl) :
51 in_update_url;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070052 return true;
53}
54
Darin Petkova4a8a8c2010-07-15 22:21:12 -070055string OmahaRequestDeviceParams::GetLsbValue(
56 const string& key, const string& default_value) const {
57 string files[] = {string(utils::kStatefulPartition) + "/etc/lsb-release",
58 "/etc/lsb-release"};
59 for (unsigned int i = 0; i < arraysize(files); ++i) {
60 // TODO(adlr): make sure files checked are owned as root (and all
61 // their parents are recursively, too).
62 string file_data;
63 if (!utils::ReadFileToString(root_ + files[i], &file_data))
64 continue;
65
66 map<string, string> data = simple_key_value_store::ParseString(file_data);
67 if (utils::MapContainsKey(data, key))
68 return data[key];
69 }
70 // not found
71 return default_value;
72}
73
74string OmahaRequestDeviceParams::GetMachineType() const {
75 struct utsname buf;
76 string ret;
77 if (uname(&buf) == 0)
78 ret = buf.machine;
79 return ret;
80}
81
82} // namespace chromeos_update_engine