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