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.
4
5#include "chrome/browser/chromeos/login/version_info_updater.h"
6
7#include <vector>
8
9#include "base/bind.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 "chrome/browser/browser_process.h"
16#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
17#include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h"
18#include "chrome/browser/chromeos/settings/cros_settings.h"
19#include "chrome/common/chrome_version_info.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 "ui/base/l10n/l10n_util.h"
24
25namespace chromeos {
26
27namespace {
28
29const char* kReportingFlags[] = {
30  chromeos::kReportDeviceVersionInfo,
31  chromeos::kReportDeviceActivityTimes,
32  chromeos::kReportDeviceBootMode,
33  chromeos::kReportDeviceLocation,
34};
35
36}
37
38///////////////////////////////////////////////////////////////////////////////
39// VersionInfoUpdater public:
40
41VersionInfoUpdater::VersionInfoUpdater(Delegate* delegate)
42    : cros_settings_(chromeos::CrosSettings::Get()),
43      delegate_(delegate),
44      weak_pointer_factory_(this) {
45}
46
47VersionInfoUpdater::~VersionInfoUpdater() {
48  policy::BrowserPolicyConnectorChromeOS* connector =
49      g_browser_process->platform_part()->browser_policy_connector_chromeos();
50  policy::DeviceCloudPolicyManagerChromeOS* policy_manager =
51      connector->GetDeviceCloudPolicyManager();
52  if (policy_manager)
53    policy_manager->core()->store()->RemoveObserver(this);
54}
55
56void VersionInfoUpdater::StartUpdate(bool is_official_build) {
57  if (base::SysInfo::IsRunningOnChromeOS()) {
58    version_loader_.GetVersion(
59        is_official_build ? VersionLoader::VERSION_SHORT_WITH_DATE
60                          : VersionLoader::VERSION_FULL,
61        base::Bind(&VersionInfoUpdater::OnVersion,
62                   weak_pointer_factory_.GetWeakPtr()),
63        &tracker_);
64  } else {
65    UpdateVersionLabel();
66  }
67
68  policy::BrowserPolicyConnectorChromeOS* connector =
69      g_browser_process->platform_part()->browser_policy_connector_chromeos();
70  policy::DeviceCloudPolicyManagerChromeOS* policy_manager =
71      connector->GetDeviceCloudPolicyManager();
72  if (policy_manager) {
73    policy_manager->core()->store()->AddObserver(this);
74
75    // Ensure that we have up-to-date enterprise info in case enterprise policy
76    // is already fetched and has finished initialization.
77    UpdateEnterpriseInfo();
78  }
79
80  // Watch for changes to the reporting flags.
81  base::Closure callback =
82      base::Bind(&VersionInfoUpdater::UpdateEnterpriseInfo,
83                 base::Unretained(this));
84  for (unsigned int i = 0; i < arraysize(kReportingFlags); ++i) {
85    subscriptions_.push_back(
86        cros_settings_->AddSettingsObserver(kReportingFlags[i],
87                                            callback).release());
88  }
89}
90
91void VersionInfoUpdater::UpdateVersionLabel() {
92  if (version_text_.empty())
93    return;
94
95  chrome::VersionInfo version_info;
96  std::string label_text = l10n_util::GetStringFUTF8(
97      IDS_LOGIN_VERSION_LABEL_FORMAT,
98      l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
99      base::UTF8ToUTF16(version_info.Version()),
100      base::UTF8ToUTF16(version_text_));
101
102  // Workaround over incorrect width calculation in old fonts.
103  // TODO(glotov): remove the following line when new fonts are used.
104  label_text += ' ';
105
106  if (delegate_)
107    delegate_->OnOSVersionLabelTextUpdated(label_text);
108}
109
110void VersionInfoUpdater::UpdateEnterpriseInfo() {
111  policy::BrowserPolicyConnectorChromeOS* connector =
112      g_browser_process->platform_part()->browser_policy_connector_chromeos();
113  SetEnterpriseInfo(connector->GetEnterpriseDomain());
114}
115
116void VersionInfoUpdater::SetEnterpriseInfo(const std::string& domain_name) {
117  // Update the notification about device status reporting.
118  if (delegate_ && !domain_name.empty()) {
119    std::string enterprise_info;
120    enterprise_info = l10n_util::GetStringFUTF8(
121        IDS_DEVICE_OWNED_BY_NOTICE,
122        base::UTF8ToUTF16(domain_name));
123    delegate_->OnEnterpriseInfoUpdated(enterprise_info);
124  }
125}
126
127void VersionInfoUpdater::OnVersion(const std::string& version) {
128  version_text_ = version;
129  UpdateVersionLabel();
130}
131
132void VersionInfoUpdater::OnStoreLoaded(policy::CloudPolicyStore* store) {
133  UpdateEnterpriseInfo();
134}
135
136void VersionInfoUpdater::OnStoreError(policy::CloudPolicyStore* store) {
137  UpdateEnterpriseInfo();
138}
139
140}  // namespace chromeos
141