1 // Copyright (c) 2012 The Chromium 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.
5 #include "chrome/browser/chromeos/login/version_info_updater.h"
10 #include "base/bind_helpers.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/sys_info.h"
15 #include "base/task_runner_util.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
18 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h"
19 #include "chrome/browser/chromeos/settings/cros_settings.h"
20 #include "chrome/grit/chromium_strings.h"
21 #include "chrome/grit/generated_resources.h"
22 #include "chromeos/settings/cros_settings_names.h"
23 #include "components/version_info/version_info.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "ui/base/l10n/l10n_util.h"
31 const char* const kReportingFlags
[] = {
32 chromeos::kReportDeviceVersionInfo
,
33 chromeos::kReportDeviceActivityTimes
,
34 chromeos::kReportDeviceBootMode
,
35 chromeos::kReportDeviceLocation
,
38 // Strings used to generate the serial number part of the version string.
39 const char kSerialNumberPrefix
[] = "SN:";
43 ///////////////////////////////////////////////////////////////////////////////
44 // VersionInfoUpdater public:
46 VersionInfoUpdater::VersionInfoUpdater(Delegate
* delegate
)
47 : cros_settings_(chromeos::CrosSettings::Get()),
49 weak_pointer_factory_(this) {
52 VersionInfoUpdater::~VersionInfoUpdater() {
53 policy::BrowserPolicyConnectorChromeOS
* connector
=
54 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
55 policy::DeviceCloudPolicyManagerChromeOS
* policy_manager
=
56 connector
->GetDeviceCloudPolicyManager();
58 policy_manager
->core()->store()->RemoveObserver(this);
61 void VersionInfoUpdater::StartUpdate(bool is_official_build
) {
62 if (base::SysInfo::IsRunningOnChromeOS()) {
63 base::PostTaskAndReplyWithResult(
64 content::BrowserThread::GetBlockingPool(),
66 base::Bind(&version_loader::GetVersion
,
67 is_official_build
? version_loader::VERSION_SHORT_WITH_DATE
68 : version_loader::VERSION_FULL
),
69 base::Bind(&VersionInfoUpdater::OnVersion
,
70 weak_pointer_factory_
.GetWeakPtr()));
75 policy::BrowserPolicyConnectorChromeOS
* connector
=
76 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
77 policy::DeviceCloudPolicyManagerChromeOS
* policy_manager
=
78 connector
->GetDeviceCloudPolicyManager();
80 policy_manager
->core()->store()->AddObserver(this);
82 // Ensure that we have up-to-date enterprise info in case enterprise policy
83 // is already fetched and has finished initialization.
84 UpdateEnterpriseInfo();
87 // Watch for changes to the reporting flags.
88 base::Closure callback
=
89 base::Bind(&VersionInfoUpdater::UpdateEnterpriseInfo
,
90 base::Unretained(this));
91 for (unsigned int i
= 0; i
< arraysize(kReportingFlags
); ++i
) {
92 subscriptions_
.push_back(
93 cros_settings_
->AddSettingsObserver(kReportingFlags
[i
],
98 void VersionInfoUpdater::UpdateVersionLabel() {
99 if (version_text_
.empty())
102 UpdateSerialNumberInfo();
104 std::string label_text
= l10n_util::GetStringFUTF8(
105 IDS_LOGIN_VERSION_LABEL_FORMAT
,
106 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME
),
107 base::UTF8ToUTF16(version_info::GetVersionNumber()),
108 base::UTF8ToUTF16(version_text_
), base::UTF8ToUTF16(serial_number_text_
));
110 // Workaround over incorrect width calculation in old fonts.
111 // TODO(glotov): remove the following line when new fonts are used.
115 delegate_
->OnOSVersionLabelTextUpdated(label_text
);
118 void VersionInfoUpdater::UpdateEnterpriseInfo() {
119 policy::BrowserPolicyConnectorChromeOS
* connector
=
120 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
121 SetEnterpriseInfo(connector
->GetEnterpriseDomain(),
122 connector
->GetDeviceAssetID());
125 void VersionInfoUpdater::SetEnterpriseInfo(const std::string
& domain_name
,
126 const std::string
& asset_id
) {
127 // Update the notification about device status reporting.
128 if (delegate_
&& !domain_name
.empty()) {
129 std::string enterprise_info
;
130 enterprise_info
= l10n_util::GetStringFUTF8(
131 IDS_DEVICE_OWNED_BY_NOTICE
,
132 base::UTF8ToUTF16(domain_name
));
133 delegate_
->OnEnterpriseInfoUpdated(enterprise_info
, asset_id
);
137 void VersionInfoUpdater::UpdateSerialNumberInfo() {
138 std::string sn
= policy::DeviceCloudPolicyManagerChromeOS::GetMachineID();
140 serial_number_text_
= kSerialNumberPrefix
;
141 serial_number_text_
.append(sn
);
145 void VersionInfoUpdater::OnVersion(const std::string
& version
) {
146 version_text_
= version
;
147 UpdateVersionLabel();
150 void VersionInfoUpdater::OnStoreLoaded(policy::CloudPolicyStore
* store
) {
151 UpdateEnterpriseInfo();
154 void VersionInfoUpdater::OnStoreError(policy::CloudPolicyStore
* store
) {
155 UpdateEnterpriseInfo();
158 } // namespace chromeos