summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos
diff options
context:
space:
mode:
authorsatorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-07-10 06:24:52 +0000
committersatorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-07-10 06:24:52 +0000
commitd43970a7ceee5fc5433787b0f28b64234a4039f2 (patch)
tree0cfd2581dc24c226cab75da6b683458dac540199 /chrome/browser/chromeos
parent7237bfcc6ded653b4ae2defaee6f1303b1744ede (diff)
downloadchromium_src-d43970a7ceee5fc5433787b0f28b64234a4039f2.zip
chromium_src-d43970a7ceee5fc5433787b0f28b64234a4039f2.tar.gz
chromium_src-d43970a7ceee5fc5433787b0f28b64234a4039f2.tar.bz2
Split SystemAccess into TimezoneSettings, StatisticsProvider, and SyslogsProvider.
SystemAccess contained three separate things: timezone settings, statistics provider, and syslogs provider. We should have them in separate classes. This patch touches many files but changes are mechanical. No logic changes introduced. BUG=none TEST=chrome builds and try bots pass Review URL: http://codereview.chromium.org/7324017 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@91971 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos')
-rw-r--r--chrome/browser/chromeos/customization_document.cc23
-rw-r--r--chrome/browser/chromeos/customization_document.h8
-rw-r--r--chrome/browser/chromeos/customization_document_unittest.cc27
-rw-r--r--chrome/browser/chromeos/login/base_login_display_host.cc4
-rw-r--r--chrome/browser/chromeos/login/helper.cc1
-rw-r--r--chrome/browser/chromeos/mock_system_access.h37
-rw-r--r--chrome/browser/chromeos/status/clock_menu_button.cc6
-rw-r--r--chrome/browser/chromeos/status/clock_menu_button.h6
-rw-r--r--chrome/browser/chromeos/status/clock_menu_button_browsertest.cc6
-rw-r--r--chrome/browser/chromeos/system/mock_statistics_provider.cc (renamed from chrome/browser/chromeos/mock_system_access.cc)8
-rw-r--r--chrome/browser/chromeos/system/mock_statistics_provider.h31
-rw-r--r--chrome/browser/chromeos/system/name_value_pairs_parser.cc (renamed from chrome/browser/chromeos/name_value_pairs_parser.cc)4
-rw-r--r--chrome/browser/chromeos/system/name_value_pairs_parser.h (renamed from chrome/browser/chromeos/name_value_pairs_parser.h)8
-rw-r--r--chrome/browser/chromeos/system/name_value_pairs_parser_unittest.cc (renamed from chrome/browser/chromeos/name_value_pairs_parser_unittest.cc)4
-rw-r--r--chrome/browser/chromeos/system/statistics_provider.cc108
-rw-r--r--chrome/browser/chromeos/system/statistics_provider.h32
-rw-r--r--chrome/browser/chromeos/system/syslogs_provider.cc (renamed from chrome/browser/chromeos/system_access.cc)234
-rw-r--r--chrome/browser/chromeos/system/syslogs_provider.h (renamed from chrome/browser/chromeos/system_access.h)40
-rw-r--r--chrome/browser/chromeos/system/timezone_settings.cc164
-rw-r--r--chrome/browser/chromeos/system/timezone_settings.h45
20 files changed, 482 insertions, 314 deletions
diff --git a/chrome/browser/chromeos/customization_document.cc b/chrome/browser/chromeos/customization_document.cc
index 8af62dc..36c198a 100644
--- a/chrome/browser/chromeos/customization_document.cc
+++ b/chrome/browser/chromeos/customization_document.cc
@@ -16,7 +16,7 @@
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/browser/chromeos/cros/network_library.h"
#include "chrome/browser/chromeos/login/wizard_controller.h"
-#include "chrome/browser/chromeos/system_access.h"
+#include "chrome/browser/chromeos/system/statistics_provider.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "content/browser/browser_thread.h"
@@ -147,13 +147,14 @@ StartupCustomizationDocument::StartupCustomizationDocument() {
base::ThreadRestrictions::ScopedAllowIO allow_io;
LoadManifestFromFile(FilePath(kStartupCustomizationManifestPath));
}
- Init(SystemAccess::GetInstance());
+ Init(chromeos::system::StatisticsProvider::GetInstance());
}
StartupCustomizationDocument::StartupCustomizationDocument(
- SystemAccess* system_access, const std::string& manifest) {
+ chromeos::system::StatisticsProvider* statistics_provider,
+ const std::string& manifest) {
LoadManifestFromString(manifest);
- Init(system_access);
+ Init(statistics_provider);
}
StartupCustomizationDocument::~StartupCustomizationDocument() {}
@@ -163,7 +164,8 @@ StartupCustomizationDocument* StartupCustomizationDocument::GetInstance() {
DefaultSingletonTraits<StartupCustomizationDocument> >::get();
}
-void StartupCustomizationDocument::Init(SystemAccess* system_access) {
+void StartupCustomizationDocument::Init(
+ chromeos::system::StatisticsProvider* statistics_provider) {
if (!IsReady())
return;
@@ -173,7 +175,7 @@ void StartupCustomizationDocument::Init(SystemAccess* system_access) {
root_->GetString(kRegistrationUrlAttr, &registration_url_);
std::string hwid;
- if (system_access->GetMachineStatistic(kHardwareClass, &hwid)) {
+ if (statistics_provider->GetMachineStatistic(kHardwareClass, &hwid)) {
ListValue* hwid_list = NULL;
if (root_->GetList(kHwidMapAttr, &hwid_list)) {
for (size_t i = 0; i < hwid_list->GetSize(); ++i) {
@@ -204,9 +206,12 @@ void StartupCustomizationDocument::Init(SystemAccess* system_access) {
LOG(ERROR) << "HWID is missing in machine statistics";
}
- system_access->GetMachineStatistic(kInitialLocaleAttr, &initial_locale_);
- system_access->GetMachineStatistic(kInitialTimezoneAttr, &initial_timezone_);
- system_access->GetMachineStatistic(kKeyboardLayoutAttr, &keyboard_layout_);
+ statistics_provider->GetMachineStatistic(kInitialLocaleAttr,
+ &initial_locale_);
+ statistics_provider->GetMachineStatistic(kInitialTimezoneAttr,
+ &initial_timezone_);
+ statistics_provider->GetMachineStatistic(kKeyboardLayoutAttr,
+ &keyboard_layout_);
}
std::string StartupCustomizationDocument::GetHelpPage(
diff --git a/chrome/browser/chromeos/customization_document.h b/chrome/browser/chromeos/customization_document.h
index ea12c89b..0da9347 100644
--- a/chrome/browser/chromeos/customization_document.h
+++ b/chrome/browser/chromeos/customization_document.h
@@ -28,7 +28,9 @@ namespace base {
namespace chromeos {
-class SystemAccess;
+namespace system {
+class StatisticsProvider;
+} // system
// Base class for OEM customization document classes.
class CustomizationDocument {
@@ -80,12 +82,12 @@ class StartupCustomizationDocument : public CustomizationDocument {
StartupCustomizationDocument();
// C-tor for test construction.
- StartupCustomizationDocument(SystemAccess* system_access,
+ StartupCustomizationDocument(system::StatisticsProvider* provider,
const std::string& manifest);
virtual ~StartupCustomizationDocument();
- void Init(SystemAccess* system_access);
+ void Init(system::StatisticsProvider* provider);
// If |attr| exists in machine stat, assign it to |value|.
void InitFromMachineStatistic(const char* attr, std::string* value);
diff --git a/chrome/browser/chromeos/customization_document_unittest.cc b/chrome/browser/chromeos/customization_document_unittest.cc
index 262e0f5..8e5bbbd 100644
--- a/chrome/browser/chromeos/customization_document_unittest.cc
+++ b/chrome/browser/chromeos/customization_document_unittest.cc
@@ -5,7 +5,7 @@
#include "chrome/browser/chromeos/customization_document.h"
#include "base/time.h"
-#include "chrome/browser/chromeos/mock_system_access.h"
+#include "chrome/browser/chromeos/system/mock_statistics_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
@@ -116,14 +116,14 @@ using ::testing::Return;
using ::testing::SetArgumentPointee;
TEST(StartupCustomizationDocumentTest, Basic) {
- MockSystemAccess mock_system_access;
- EXPECT_CALL(mock_system_access, GetMachineStatistic(_, NotNull()))
+ system::MockStatisticsProvider mock_statistics_provider;
+ EXPECT_CALL(mock_statistics_provider, GetMachineStatistic(_, NotNull()))
.WillRepeatedly(Return(false));
- EXPECT_CALL(mock_system_access,
+ EXPECT_CALL(mock_statistics_provider,
GetMachineStatistic(std::string("hardware_class"), NotNull()))
.WillOnce(DoAll(SetArgumentPointee<1>(std::string("Mario 12345")),
Return(true)));
- StartupCustomizationDocument customization(&mock_system_access,
+ StartupCustomizationDocument customization(&mock_statistics_provider,
kGoodStartupManifest);
EXPECT_EQ("ru-RU", customization.initial_locale());
EXPECT_EQ("Europe/Moscow", customization.initial_timezone());
@@ -146,24 +146,24 @@ TEST(StartupCustomizationDocumentTest, Basic) {
}
TEST(StartupCustomizationDocumentTest, VPD) {
- MockSystemAccess mock_system_access;
- EXPECT_CALL(mock_system_access,
+ system::MockStatisticsProvider mock_statistics_provider;
+ EXPECT_CALL(mock_statistics_provider,
GetMachineStatistic(std::string("hardware_class"), NotNull()))
.WillOnce(DoAll(SetArgumentPointee<1>(std::string("Mario 12345")),
Return(true)));
- EXPECT_CALL(mock_system_access,
+ EXPECT_CALL(mock_statistics_provider,
GetMachineStatistic(std::string("initial_locale"), NotNull()))
.WillOnce(DoAll(SetArgumentPointee<1>(std::string("ja")),
Return(true)));
- EXPECT_CALL(mock_system_access,
+ EXPECT_CALL(mock_statistics_provider,
GetMachineStatistic(std::string("initial_timezone"), NotNull()))
.WillOnce(DoAll(SetArgumentPointee<1>(std::string("Asia/Tokyo")),
Return(true)));
- EXPECT_CALL(mock_system_access,
+ EXPECT_CALL(mock_statistics_provider,
GetMachineStatistic(std::string("keyboard_layout"), NotNull()))
.WillOnce(DoAll(SetArgumentPointee<1>(std::string("mozc-jp")),
Return(true)));
- StartupCustomizationDocument customization(&mock_system_access,
+ StartupCustomizationDocument customization(&mock_statistics_provider,
kGoodStartupManifest);
EXPECT_TRUE(customization.IsReady());
EXPECT_EQ("ja", customization.initial_locale());
@@ -172,8 +172,9 @@ TEST(StartupCustomizationDocumentTest, VPD) {
}
TEST(StartupCustomizationDocumentTest, BadManifest) {
- MockSystemAccess mock_system_access;
- StartupCustomizationDocument customization(&mock_system_access, kBadManifest);
+ system::MockStatisticsProvider mock_statistics_provider;
+ StartupCustomizationDocument customization(&mock_statistics_provider,
+ kBadManifest);
EXPECT_FALSE(customization.IsReady());
}
diff --git a/chrome/browser/chromeos/login/base_login_display_host.cc b/chrome/browser/chromeos/login/base_login_display_host.cc
index 0338e19..2b763b8 100644
--- a/chrome/browser/chromeos/login/base_login_display_host.cc
+++ b/chrome/browser/chromeos/login/base_login_display_host.cc
@@ -23,7 +23,7 @@
#include "chrome/browser/chromeos/login/views_login_display_host.h"
#include "chrome/browser/chromeos/login/webui_login_display_host.h"
#include "chrome/browser/chromeos/login/wizard_controller.h"
-#include "chrome/browser/chromeos/system_access.h"
+#include "chrome/browser/chromeos/system/timezone_settings.h"
#include "chrome/browser/chromeos/wm_ipc.h"
#include "chrome/browser/policy/browser_policy_connector.h"
#include "chrome/browser/prefs/pref_service.h"
@@ -325,7 +325,7 @@ void ShowLoginWizard(const std::string& first_screen_name,
icu::TimeZone* timezone = icu::TimeZone::createTimeZone(
icu::UnicodeString::fromUTF8(timezone_name));
CHECK(timezone) << "Timezone could not be set for " << timezone_name;
- chromeos::SystemAccess::GetInstance()->SetTimezone(*timezone);
+ chromeos::system::TimezoneSettings::GetInstance()->SetTimezone(*timezone);
}
}
}
diff --git a/chrome/browser/chromeos/login/helper.cc b/chrome/browser/chromeos/login/helper.cc
index 6b17544..afc563a 100644
--- a/chrome/browser/chromeos/login/helper.cc
+++ b/chrome/browser/chromeos/login/helper.cc
@@ -6,7 +6,6 @@
#include "base/file_util.h"
#include "chrome/browser/chromeos/cros/network_library.h"
-#include "chrome/browser/chromeos/system_access.h"
#include "chrome/browser/google/google_util.h"
#include "googleurl/src/gurl.h"
#include "grit/generated_resources.h"
diff --git a/chrome/browser/chromeos/mock_system_access.h b/chrome/browser/chromeos/mock_system_access.h
deleted file mode 100644
index 4d30e86..0000000
--- a/chrome/browser/chromeos/mock_system_access.h
+++ /dev/null
@@ -1,37 +0,0 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_CHROMEOS_MOCK_SYSTEM_ACCESS_H_
-#define CHROME_BROWSER_CHROMEOS_MOCK_SYSTEM_ACCESS_H_
-#pragma once
-
-#include "base/basictypes.h"
-#include "chrome/browser/chromeos/system_access.h"
-#include "testing/gmock/include/gmock/gmock.h"
-
-namespace chromeos {
-
-class MockSystemAccess : public SystemAccess {
- public:
- MockSystemAccess();
- virtual ~MockSystemAccess();
-
- MOCK_METHOD0(GetTimezone, const icu::TimeZone&());
- MOCK_METHOD1(SetTimezone, void(const icu::TimeZone& timezone));
- MOCK_METHOD2(GetMachineStatistic, bool(const std::string& name,
- std::string* result));
- MOCK_METHOD1(AddObserver, void(Observer* observer));
- MOCK_METHOD1(RemoveObserver, void(Observer* observer));
- MOCK_METHOD4(RequestSyslogs, Handle(bool compress_logs,
- SyslogsContext context,
- CancelableRequestConsumerBase* consumer,
- ReadCompleteCallback* callback));
-
- private:
- DISALLOW_COPY_AND_ASSIGN(MockSystemAccess);
-};
-
-} // namespace chromeos
-
-#endif // CHROME_BROWSER_CHROMEOS_MOCK_SYSTEM_ACCESS_H_
diff --git a/chrome/browser/chromeos/status/clock_menu_button.cc b/chrome/browser/chromeos/status/clock_menu_button.cc
index a183792..5880b0b 100644
--- a/chrome/browser/chromeos/status/clock_menu_button.cc
+++ b/chrome/browser/chromeos/status/clock_menu_button.cc
@@ -40,8 +40,8 @@ const int kTimerSlopSeconds = 1;
ClockMenuButton::ClockMenuButton(StatusAreaHost* host)
: StatusAreaButton(host, this) {
- // Add as SystemAccess observer. We update the clock if timezone changes.
- SystemAccess::GetInstance()->AddObserver(this);
+ // Add as TimezoneSettings observer. We update the clock if timezone changes.
+ system::TimezoneSettings::GetInstance()->AddObserver(this);
CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this);
// Start monitoring the kUse24HourClock preference.
if (host->GetProfile()) { // This can be NULL in the login screen.
@@ -54,7 +54,7 @@ ClockMenuButton::ClockMenuButton(StatusAreaHost* host)
ClockMenuButton::~ClockMenuButton() {
CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this);
- SystemAccess::GetInstance()->RemoveObserver(this);
+ system::TimezoneSettings::GetInstance()->RemoveObserver(this);
}
void ClockMenuButton::UpdateTextAndSetNextTimer() {
diff --git a/chrome/browser/chromeos/status/clock_menu_button.h b/chrome/browser/chromeos/status/clock_menu_button.h
index ebba360..d6fd4ee 100644
--- a/chrome/browser/chromeos/status/clock_menu_button.h
+++ b/chrome/browser/chromeos/status/clock_menu_button.h
@@ -14,7 +14,7 @@
#include "chrome/browser/prefs/pref_member.h"
#include "content/common/notification_observer.h"
#include "content/common/notification_type.h"
-#include "chrome/browser/chromeos/system_access.h"
+#include "chrome/browser/chromeos/system/timezone_settings.h"
#include "unicode/calendar.h"
#include "views/controls/button/menu_button.h"
#include "views/controls/menu/menu_delegate.h"
@@ -35,7 +35,7 @@ class ClockMenuButton : public StatusAreaButton,
public views::ViewMenuDelegate,
public NotificationObserver,
public PowerLibrary::Observer,
- public SystemAccess::Observer {
+ public system::TimezoneSettings::Observer {
public:
explicit ClockMenuButton(StatusAreaHost* host);
virtual ~ClockMenuButton();
@@ -49,7 +49,7 @@ class ClockMenuButton : public StatusAreaButton,
virtual void PowerChanged(PowerLibrary* obj) {}
virtual void SystemResumed();
- // Overridden from SystemAccess::Observer:
+ // Overridden from TimezoneSettings::Observer:
virtual void TimezoneChanged(const icu::TimeZone& timezone);
// views::View
diff --git a/chrome/browser/chromeos/status/clock_menu_button_browsertest.cc b/chrome/browser/chromeos/status/clock_menu_button_browsertest.cc
index d913c63..67c77c4 100644
--- a/chrome/browser/chromeos/status/clock_menu_button_browsertest.cc
+++ b/chrome/browser/chromeos/status/clock_menu_button_browsertest.cc
@@ -8,7 +8,7 @@
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/browser/chromeos/frame/browser_view.h"
#include "chrome/browser/chromeos/status/status_area_view.h"
-#include "chrome/browser/chromeos/system_access.h"
+#include "chrome/browser/chromeos/system/timezone_settings.h"
#include "chrome/browser/chromeos/view_ids.h"
#include "chrome/browser/prefs/pref_member.h"
#include "chrome/browser/ui/browser.h"
@@ -42,11 +42,11 @@ IN_PROC_BROWSER_TEST_F(ClockMenuButtonTest, TimezoneTest) {
// Update timezone and make sure clock text changes.
scoped_ptr<icu::TimeZone> timezone_first(icu::TimeZone::createTimeZone(
icu::UnicodeString::fromUTF8("Asia/Hong_Kong")));
- SystemAccess::GetInstance()->SetTimezone(*timezone_first);
+ system::TimezoneSettings::GetInstance()->SetTimezone(*timezone_first);
std::wstring text_before = clock->text();
scoped_ptr<icu::TimeZone> timezone_second(icu::TimeZone::createTimeZone(
icu::UnicodeString::fromUTF8("Pacific/Samoa")));
- SystemAccess::GetInstance()->SetTimezone(*timezone_second);
+ system::TimezoneSettings::GetInstance()->SetTimezone(*timezone_second);
std::wstring text_after = clock->text();
EXPECT_NE(text_before, text_after);
}
diff --git a/chrome/browser/chromeos/mock_system_access.cc b/chrome/browser/chromeos/system/mock_statistics_provider.cc
index fd51c7f..26c2d22 100644
--- a/chrome/browser/chromeos/mock_system_access.cc
+++ b/chrome/browser/chromeos/system/mock_statistics_provider.cc
@@ -2,12 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "chrome/browser/chromeos/mock_system_access.h"
+#include "chrome/browser/chromeos/system/mock_statistics_provider.h"
namespace chromeos {
+namespace system {
-MockSystemAccess::MockSystemAccess() {}
+MockStatisticsProvider::MockStatisticsProvider() {}
-MockSystemAccess::~MockSystemAccess() {}
+MockStatisticsProvider::~MockStatisticsProvider() {}
+} // namespace system
} // namespace chromeos
diff --git a/chrome/browser/chromeos/system/mock_statistics_provider.h b/chrome/browser/chromeos/system/mock_statistics_provider.h
new file mode 100644
index 0000000..e0acd0a
--- /dev/null
+++ b/chrome/browser/chromeos/system/mock_statistics_provider.h
@@ -0,0 +1,31 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_CHROMEOS_SYSTEM_MOCK_STATISTICS_PROVIDER_H_
+#define CHROME_BROWSER_CHROMEOS_SYSTEM_MOCK_STATISTICS_PROVIDER_H_
+#pragma once
+
+#include "base/basictypes.h"
+#include "chrome/browser/chromeos/system/statistics_provider.h"
+#include "testing/gmock/include/gmock/gmock.h"
+
+namespace chromeos {
+namespace system {
+
+class MockStatisticsProvider : public system::StatisticsProvider {
+ public:
+ MockStatisticsProvider();
+ virtual ~MockStatisticsProvider();
+
+ MOCK_METHOD2(GetMachineStatistic, bool(const std::string& name,
+ std::string* result));
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockStatisticsProvider);
+};
+
+} // namespace system
+} // namespace chromeos
+
+#endif // CHROME_BROWSER_CHROMEOS_SYSTEM_MOCK_STATISTICS_PROVIDER_H_
diff --git a/chrome/browser/chromeos/name_value_pairs_parser.cc b/chrome/browser/chromeos/system/name_value_pairs_parser.cc
index be38daf..700ebac 100644
--- a/chrome/browser/chromeos/name_value_pairs_parser.cc
+++ b/chrome/browser/chromeos/system/name_value_pairs_parser.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "chrome/browser/chromeos/name_value_pairs_parser.h"
+#include "chrome/browser/chromeos/system/name_value_pairs_parser.h"
#include "base/command_line.h"
#include "base/logging.h"
@@ -12,6 +12,7 @@
#include "base/threading/thread_restrictions.h"
namespace chromeos { // NOLINT
+namespace system {
namespace {
@@ -100,4 +101,5 @@ bool NameValuePairsParser::ParseNameValuePairsFromTool(
return true;
}
+} // namespace system
} // namespace chromeos
diff --git a/chrome/browser/chromeos/name_value_pairs_parser.h b/chrome/browser/chromeos/system/name_value_pairs_parser.h
index ae8823b..bbaf97a 100644
--- a/chrome/browser/chromeos/name_value_pairs_parser.h
+++ b/chrome/browser/chromeos/system/name_value_pairs_parser.h
@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef CHROME_BROWSER_CHROMEOS_NAME_VALUE_PAIRS_PARSER_H_
-#define CHROME_BROWSER_CHROMEOS_NAME_VALUE_PAIRS_PARSER_H_
+#ifndef CHROME_BROWSER_CHROMEOS_SYSTEM_NAME_VALUE_PAIRS_PARSER_H_
+#define CHROME_BROWSER_CHROMEOS_SYSTEM_NAME_VALUE_PAIRS_PARSER_H_
#pragma once
#include <map>
@@ -13,6 +13,7 @@
#include "base/basictypes.h"
namespace chromeos {
+namespace system {
// The parser is used to get machine info as name-value pairs. Defined
// here to be accessable by tests.
@@ -47,6 +48,7 @@ class NameValuePairsParser {
DISALLOW_COPY_AND_ASSIGN(NameValuePairsParser);
};
+} // namespace system
} // namespace chromeos
-#endif // CHROME_BROWSER_CHROMEOS_NAME_VALUE_PAIRS_PARSER_H_
+#endif // CHROME_BROWSER_CHROMEOS_SYSTEM_NAME_VALUE_PAIRS_PARSER_H_
diff --git a/chrome/browser/chromeos/name_value_pairs_parser_unittest.cc b/chrome/browser/chromeos/system/name_value_pairs_parser_unittest.cc
index 0973920..65918d0 100644
--- a/chrome/browser/chromeos/name_value_pairs_parser_unittest.cc
+++ b/chrome/browser/chromeos/system/name_value_pairs_parser_unittest.cc
@@ -2,12 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "chrome/browser/chromeos/name_value_pairs_parser.h"
+#include "chrome/browser/chromeos/system/name_value_pairs_parser.h"
#include "base/basictypes.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
+namespace system {
TEST(NameValuePairsParser, TestGetSingleValueFromTool) {
NameValuePairsParser::NameValueMap map;
@@ -61,4 +62,5 @@ TEST(NameValuePairsParser, TestParseNameValuePairsFromTool) {
EXPECT_EQ("mozc-jp", map["keyboard_layout"]);
}
+} // namespace system
} // namespace chromeos
diff --git a/chrome/browser/chromeos/system/statistics_provider.cc b/chrome/browser/chromeos/system/statistics_provider.cc
new file mode 100644
index 0000000..4a85699
--- /dev/null
+++ b/chrome/browser/chromeos/system/statistics_provider.cc
@@ -0,0 +1,108 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/chromeos/system/statistics_provider.h"
+
+#include "base/memory/singleton.h"
+#include "base/task.h"
+#include "chrome/browser/chromeos/system/name_value_pairs_parser.h"
+
+namespace chromeos {
+namespace system {
+namespace {
+
+// The system command that returns the hardware class.
+const char kHardwareClassKey[] = "hardware_class";
+const char* kHardwareClassTool[] = { "crossystem", "hwid" };
+const char kUnknownHardwareClass[] = "unknown";
+
+// Command to get machine hardware info and key/value delimiters.
+// /tmp/machine-info is generated by platform/init/chromeos_startup.
+const char* kMachineHardwareInfoTool[] = { "cat", "/tmp/machine-info" };
+const char kMachineHardwareInfoEq[] = "=";
+const char kMachineHardwareInfoDelim[] = " \n";
+
+// Command to get machine OS info and key/value delimiters.
+const char* kMachineOSInfoTool[] = { "cat", "/etc/lsb-release" };
+const char kMachineOSInfoEq[] = "=";
+const char kMachineOSInfoDelim[] = "\n";
+
+// Command to get VPD info and key/value delimiters.
+const char* kVpdTool[] = { "cat", "/var/log/vpd_2.0.txt" };
+const char kVpdEq[] = "=";
+const char kVpdDelim[] = "\n";
+
+} // namespace
+
+class StatisticsProviderImpl : public StatisticsProvider {
+ public:
+ // StatisticsProvider implementation:
+ virtual bool GetMachineStatistic(const std::string& name,
+ std::string* result);
+
+ static StatisticsProviderImpl* GetInstance();
+
+ private:
+ friend struct DefaultSingletonTraits<StatisticsProviderImpl>;
+
+ StatisticsProviderImpl();
+
+ // Updates the machine statistcs by examining the system.
+ void UpdateMachineStatistics();
+
+ NameValuePairsParser::NameValueMap machine_info_;
+
+ DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl);
+};
+
+bool StatisticsProviderImpl::GetMachineStatistic(
+ const std::string& name, std::string* result) {
+ NameValuePairsParser::NameValueMap::iterator iter = machine_info_.find(name);
+ if (iter != machine_info_.end()) {
+ *result = iter->second;
+ return true;
+ }
+ return false;
+}
+
+StatisticsProviderImpl::StatisticsProviderImpl() {
+ // Get Statistics
+ UpdateMachineStatistics();
+}
+
+void StatisticsProviderImpl::UpdateMachineStatistics() {
+ NameValuePairsParser parser(&machine_info_);
+ if (!parser.GetSingleValueFromTool(arraysize(kHardwareClassTool),
+ kHardwareClassTool,
+ kHardwareClassKey)) {
+ // Use kUnknownHardwareClass if the hardware class command fails.
+ parser.AddNameValuePair(kHardwareClassKey, kUnknownHardwareClass);
+ }
+ parser.ParseNameValuePairsFromTool(arraysize(kMachineHardwareInfoTool),
+ kMachineHardwareInfoTool,
+ kMachineHardwareInfoEq,
+ kMachineHardwareInfoDelim);
+ parser.ParseNameValuePairsFromTool(arraysize(kMachineOSInfoTool),
+ kMachineOSInfoTool,
+ kMachineOSInfoEq,
+ kMachineOSInfoDelim);
+ parser.ParseNameValuePairsFromTool(
+ arraysize(kVpdTool), kVpdTool, kVpdEq, kVpdDelim);
+}
+
+StatisticsProviderImpl* StatisticsProviderImpl::GetInstance() {
+ return Singleton<StatisticsProviderImpl,
+ DefaultSingletonTraits<StatisticsProviderImpl> >::get();
+}
+
+StatisticsProvider* StatisticsProvider::GetInstance() {
+ return StatisticsProviderImpl::GetInstance();
+}
+
+} // namespace system
+} // namespace chromeos
+
+// Allows InvokeLater without adding refcounting. StatisticsProviderImpl is a
+// Singleton and won't be deleted until it's last InvokeLater is run.
+DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::system::StatisticsProviderImpl);
diff --git a/chrome/browser/chromeos/system/statistics_provider.h b/chrome/browser/chromeos/system/statistics_provider.h
new file mode 100644
index 0000000..1908087
--- /dev/null
+++ b/chrome/browser/chromeos/system/statistics_provider.h
@@ -0,0 +1,32 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_CHROMEOS_SYSTEM_STATISTICS_PROVIDER_H_
+#define CHROME_BROWSER_CHROMEOS_SYSTEM_STATISTICS_PROVIDER_H_
+#pragma once
+
+#include <string>
+
+namespace chromeos {
+namespace system {
+
+// This interface provides access to Chrome OS statistics.
+class StatisticsProvider {
+ public:
+ // Retrieve the named machine statistic (e.g. "hardware_class").
+ // This does not update the statistcs. If the |name| is not set, |result|
+ // preserves old value.
+ virtual bool GetMachineStatistic(const std::string& name,
+ std::string* result) = 0;
+
+ static StatisticsProvider* GetInstance();
+
+ protected:
+ virtual ~StatisticsProvider() {}
+};
+
+} // namespace system
+} // namespace chromeos
+
+#endif // CHROME_BROWSER_CHROMEOS_SYSTEM_STATISTICS_PROVIDER_H_
diff --git a/chrome/browser/chromeos/system_access.cc b/chrome/browser/chromeos/system/syslogs_provider.cc
index 04cada6..57c76cf 100644
--- a/chrome/browser/chromeos/system_access.cc
+++ b/chrome/browser/chromeos/system/syslogs_provider.cc
@@ -2,56 +2,21 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "chrome/browser/chromeos/system_access.h"
+#include "chrome/browser/chromeos/system/syslogs_provider.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/logging.h"
-#include "base/observer_list.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h"
#include "base/string_util.h"
-#include "base/utf_string_conversions.h"
-#include "chrome/browser/chromeos/name_value_pairs_parser.h"
#include "chrome/common/chrome_switches.h"
#include "content/browser/browser_thread.h"
-namespace chromeos { // NOLINT
-
-namespace { // NOLINT
-
-// The filepath to the timezone file that symlinks to the actual timezone file.
-const char kTimezoneSymlink[] = "/var/lib/timezone/localtime";
-const char kTimezoneSymlink2[] = "/var/lib/timezone/localtime2";
-
-// The directory that contains all the timezone files. So for timezone
-// "US/Pacific", the actual timezone file is: "/usr/share/zoneinfo/US/Pacific"
-const char kTimezoneFilesDir[] = "/usr/share/zoneinfo/";
-
-// The system command that returns the hardware class.
-const char kHardwareClassKey[] = "hardware_class";
-const char* kHardwareClassTool[] = { "crossystem", "hwid" };
-const char kUnknownHardwareClass[] = "unknown";
-
-// Command to get machine hardware info and key/value delimiters.
-// /tmp/machine-info is generated by platform/init/chromeos_startup.
-const char* kMachineHardwareInfoTool[] = { "cat", "/tmp/machine-info" };
-const char kMachineHardwareInfoEq[] = "=";
-const char kMachineHardwareInfoDelim[] = " \n";
-
-// Command to get machine OS info and key/value delimiters.
-const char* kMachineOSInfoTool[] = { "cat", "/etc/lsb-release" };
-const char kMachineOSInfoEq[] = "=";
-const char kMachineOSInfoDelim[] = "\n";
-
-// Command to get VPD info and key/value delimiters.
-const char* kVpdTool[] = { "cat", "/var/log/vpd_2.0.txt" };
-const char kVpdEq[] = "=";
-const char kVpdDelim[] = "\n";
-
-// Fallback time zone ID used in case of an unexpected error.
-const char kFallbackTimeZoneId[] = "America/Los_Angeles";
+namespace chromeos {
+namespace system {
+namespace {
const char kSysLogsScript[] =
"/usr/share/userfeedback/scripts/sysinfo_script_runner";
@@ -124,6 +89,9 @@ std::string ReadValue(std::string* data) {
//
// Parameters:
// temp_filename: This is an out parameter that holds the name of a file in
+// Reads a value from the input string; erasing the read values from
+// the initial string; detects if the value is multiline and reads
+// accordingly
// /tmp that contains the system logs in a KEY=VALUE format.
// If this parameter is NULL, system logs are not retained on
// the filesystem after this call completes.
@@ -145,14 +113,14 @@ LogDictionaryType* GetSystemLogs(FilePath* zip_file_name,
// Ignore the return value - if the script execution didn't work
// stderr won't go into the output file anyway.
- if (system(cmd.c_str()) == -1)
+ if (::system(cmd.c_str()) == -1)
LOG(WARNING) << "Command " << cmd << " failed to run";
// Compress the logs file if requested.
if (zip_file_name) {
cmd = std::string(kBzip2Command) + " -c " + temp_filename.value() + " > " +
zip_file_name->value();
- if (system(cmd.c_str()) == -1)
+ if (::system(cmd.c_str()) == -1)
LOG(WARNING) << "Command " << cmd << " failed to run";
}
// Read logs from the temp file
@@ -192,16 +160,11 @@ LogDictionaryType* GetSystemLogs(FilePath* zip_file_name,
return logs;
}
-class SystemAccessImpl : public SystemAccess {
- public:
- // SystemAccess.implementation:
- virtual const icu::TimeZone& GetTimezone();
- virtual void SetTimezone(const icu::TimeZone& timezone);
- virtual bool GetMachineStatistic(const std::string& name,
- std::string* result);
- virtual void AddObserver(Observer* observer);
- virtual void RemoveObserver(Observer* observer);
+} // namespace
+class SyslogsProviderImpl : public SyslogsProvider {
+ public:
+ // SyslogsProvider implementation:
virtual Handle RequestSyslogs(
bool compress_logs,
SyslogsContext context,
@@ -219,154 +182,23 @@ class SystemAccessImpl : public SystemAccess {
void LoadCompressedLogs(const FilePath& zip_file,
std::string* zip_content);
- static SystemAccessImpl* GetInstance();
+ static SyslogsProviderImpl* GetInstance();
private:
- friend struct DefaultSingletonTraits<SystemAccessImpl>;
-
- SystemAccessImpl();
+ friend struct DefaultSingletonTraits<SyslogsProviderImpl>;
- // Updates the machine statistcs by examining the system.
- void UpdateMachineStatistics();
+ SyslogsProviderImpl();
// Gets syslogs context string from the enum value.
const char* GetSyslogsContextString(SyslogsContext context);
- scoped_ptr<icu::TimeZone> timezone_;
- ObserverList<Observer> observers_;
- NameValuePairsParser::NameValueMap machine_info_;
-
- DISALLOW_COPY_AND_ASSIGN(SystemAccessImpl);
+ DISALLOW_COPY_AND_ASSIGN(SyslogsProviderImpl);
};
-std::string GetTimezoneIDAsString() {
- // Look at kTimezoneSymlink, see which timezone we are symlinked to.
- char buf[256];
- const ssize_t len = readlink(kTimezoneSymlink, buf,
- sizeof(buf)-1);
- if (len == -1) {
- LOG(ERROR) << "GetTimezoneID: Cannot read timezone symlink "
- << kTimezoneSymlink;
- return std::string();
- }
-
- std::string timezone(buf, len);
- // Remove kTimezoneFilesDir from the beginning.
- if (timezone.find(kTimezoneFilesDir) != 0) {
- LOG(ERROR) << "GetTimezoneID: Timezone symlink is wrong "
- << timezone;
- return std::string();
- }
-
- return timezone.substr(strlen(kTimezoneFilesDir));
-}
-
-void SetTimezoneIDFromString(const std::string& id) {
- // Change the kTimezoneSymlink symlink to the path for this timezone.
- // We want to do this in an atomic way. So we are going to create the symlink
- // at kTimezoneSymlink2 and then move it to kTimezoneSymlink
-
- FilePath timezone_symlink(kTimezoneSymlink);
- FilePath timezone_symlink2(kTimezoneSymlink2);
- FilePath timezone_file(kTimezoneFilesDir + id);
-
- // Make sure timezone_file exists.
- if (!file_util::PathExists(timezone_file)) {
- LOG(ERROR) << "SetTimezoneID: Cannot find timezone file "
- << timezone_file.value();
- return;
- }
-
- // Delete old symlink2 if it exists.
- file_util::Delete(timezone_symlink2, false);
-
- // Create new symlink2.
- if (symlink(timezone_file.value().c_str(),
- timezone_symlink2.value().c_str()) == -1) {
- LOG(ERROR) << "SetTimezoneID: Unable to create symlink "
- << timezone_symlink2.value() << " to " << timezone_file.value();
- return;
- }
-
- // Move symlink2 to symlink.
- if (!file_util::ReplaceFile(timezone_symlink2, timezone_symlink)) {
- LOG(ERROR) << "SetTimezoneID: Unable to move symlink "
- << timezone_symlink2.value() << " to "
- << timezone_symlink.value();
- }
-}
-
-const icu::TimeZone& SystemAccessImpl::GetTimezone() {
- return *timezone_.get();
-}
-
-void SystemAccessImpl::SetTimezone(const icu::TimeZone& timezone) {
- timezone_.reset(timezone.clone());
- icu::UnicodeString unicode;
- timezone.getID(unicode);
- std::string id;
- UTF16ToUTF8(unicode.getBuffer(), unicode.length(), &id);
- VLOG(1) << "Setting timezone to " << id;
- chromeos::SetTimezoneIDFromString(id);
- icu::TimeZone::setDefault(timezone);
- FOR_EACH_OBSERVER(Observer, observers_, TimezoneChanged(timezone));
-}
-
-bool SystemAccessImpl::GetMachineStatistic(
- const std::string& name, std::string* result) {
- NameValuePairsParser::NameValueMap::iterator iter = machine_info_.find(name);
- if (iter != machine_info_.end()) {
- *result = iter->second;
- return true;
- }
- return false;
-}
-
-void SystemAccessImpl::AddObserver(Observer* observer) {
- observers_.AddObserver(observer);
-}
-
-void SystemAccessImpl::RemoveObserver(Observer* observer) {
- observers_.RemoveObserver(observer);
-}
-
-SystemAccessImpl::SystemAccessImpl() {
- // Get Statistics
- UpdateMachineStatistics();
- // Get Timezone
- std::string id = GetTimezoneIDAsString();
- if (id.empty()) {
- id = kFallbackTimeZoneId;
- LOG(ERROR) << "Got an empty string for timezone, default to " << id;
- }
- icu::TimeZone* timezone =
- icu::TimeZone::createTimeZone(icu::UnicodeString::fromUTF8(id));
- timezone_.reset(timezone);
- icu::TimeZone::setDefault(*timezone);
- VLOG(1) << "Timezone is " << id;
+SyslogsProviderImpl::SyslogsProviderImpl() {
}
-void SystemAccessImpl::UpdateMachineStatistics() {
- NameValuePairsParser parser(&machine_info_);
- if (!parser.GetSingleValueFromTool(arraysize(kHardwareClassTool),
- kHardwareClassTool,
- kHardwareClassKey)) {
- // Use kUnknownHardwareClass if the hardware class command fails.
- parser.AddNameValuePair(kHardwareClassKey, kUnknownHardwareClass);
- }
- parser.ParseNameValuePairsFromTool(arraysize(kMachineHardwareInfoTool),
- kMachineHardwareInfoTool,
- kMachineHardwareInfoEq,
- kMachineHardwareInfoDelim);
- parser.ParseNameValuePairsFromTool(arraysize(kMachineOSInfoTool),
- kMachineOSInfoTool,
- kMachineOSInfoEq,
- kMachineOSInfoDelim);
- parser.ParseNameValuePairsFromTool(
- arraysize(kVpdTool), kVpdTool, kVpdEq, kVpdDelim);
-}
-
-CancelableRequestProvider::Handle SystemAccessImpl::RequestSyslogs(
+CancelableRequestProvider::Handle SyslogsProviderImpl::RequestSyslogs(
bool compress_logs,
SyslogsContext context,
CancelableRequestConsumerBase* consumer,
@@ -381,14 +213,14 @@ CancelableRequestProvider::Handle SystemAccessImpl::RequestSyslogs(
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
NewRunnableMethod(
- this, &SystemAccessImpl::ReadSyslogs, request,
+ this, &SyslogsProviderImpl::ReadSyslogs, request,
compress_logs, context));
return request->handle();
}
// Called from FILE thread.
-void SystemAccessImpl::ReadSyslogs(
+void SyslogsProviderImpl::ReadSyslogs(
scoped_refptr<CancelableRequest<ReadCompleteCallback> > request,
bool compress_logs,
SyslogsContext context) {
@@ -409,7 +241,7 @@ void SystemAccessImpl::ReadSyslogs(
}
LogDictionaryType* logs = NULL;
- logs = chromeos::GetSystemLogs(
+ logs = GetSystemLogs(
compress_logs ? &zip_file : NULL,
GetSyslogsContextString(context));
@@ -427,7 +259,7 @@ void SystemAccessImpl::ReadSyslogs(
}
-void SystemAccessImpl::LoadCompressedLogs(const FilePath& zip_file,
+void SyslogsProviderImpl::LoadCompressedLogs(const FilePath& zip_file,
std::string* zip_content) {
DCHECK(zip_content);
if (!file_util::ReadFileToString(zip_file, zip_content)) {
@@ -436,7 +268,8 @@ void SystemAccessImpl::LoadCompressedLogs(const FilePath& zip_file,
}
}
-const char* SystemAccessImpl::GetSyslogsContextString(SyslogsContext context) {
+const char* SyslogsProviderImpl::GetSyslogsContextString(
+ SyslogsContext context) {
switch (context) {
case(SYSLOGS_FEEDBACK):
return kContextFeedback;
@@ -452,19 +285,18 @@ const char* SystemAccessImpl::GetSyslogsContextString(SyslogsContext context) {
}
}
-SystemAccessImpl* SystemAccessImpl::GetInstance() {
- return Singleton<SystemAccessImpl,
- DefaultSingletonTraits<SystemAccessImpl> >::get();
+SyslogsProviderImpl* SyslogsProviderImpl::GetInstance() {
+ return Singleton<SyslogsProviderImpl,
+ DefaultSingletonTraits<SyslogsProviderImpl> >::get();
}
-} // namespace
-
-SystemAccess* SystemAccess::GetInstance() {
- return SystemAccessImpl::GetInstance();
+SyslogsProvider* SyslogsProvider::GetInstance() {
+ return SyslogsProviderImpl::GetInstance();
}
+} // namespace system
} // namespace chromeos
-// Allows InvokeLater without adding refcounting. SystemAccessImpl is a
+// Allows InvokeLater without adding refcounting. SyslogsProviderImpl is a
// Singleton and won't be deleted until it's last InvokeLater is run.
-DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::SystemAccessImpl);
+DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::system::SyslogsProviderImpl);
diff --git a/chrome/browser/chromeos/system_access.h b/chrome/browser/chromeos/system/syslogs_provider.h
index 857c433..ecb92b6 100644
--- a/chrome/browser/chromeos/system_access.h
+++ b/chrome/browser/chromeos/system/syslogs_provider.h
@@ -2,45 +2,26 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef CHROME_BROWSER_CHROMEOS_SYSTEM_ACCESS_H_
-#define CHROME_BROWSER_CHROMEOS_SYSTEM_ACCESS_H_
+#ifndef CHROME_BROWSER_CHROMEOS_SYSTEM_SYSLOGS_PROVIDER_H_
+#define CHROME_BROWSER_CHROMEOS_SYSTEM_SYSLOGS_PROVIDER_H_
#pragma once
#include <string>
#include "base/callback_old.h"
#include "content/browser/cancelable_request.h"
-#include "unicode/timezone.h"
class CancelableRequestConsumerBase;
namespace chromeos {
+namespace system {
typedef std::map<std::string, std::string> LogDictionaryType;
-// This interface provides access to Chrome OS system APIs such as the
-// timezone setting.
-class SystemAccess : public CancelableRequestProvider {
+// This interface provides access to Chrome OS syslogs.
+class SyslogsProvider : public CancelableRequestProvider {
public:
- class Observer {
- public:
- // Called when the timezone has changed. |timezone| is non-null.
- virtual void TimezoneChanged(const icu::TimeZone& timezone) = 0;
- };
-
- static SystemAccess* GetInstance();
-
- // Returns the current timezone as an icu::Timezone object.
- virtual const icu::TimeZone& GetTimezone() = 0;
-
- // Sets the current timezone. |timezone| must be non-null.
- virtual void SetTimezone(const icu::TimeZone& timezone) = 0;
-
- // Retrieve the named machine statistic (e.g. "hardware_class").
- // This does not update the statistcs. If the |name| is not set, |result|
- // preserves old value.
- virtual bool GetMachineStatistic(const std::string& name,
- std::string* result) = 0;
+ static SyslogsProvider* GetInstance();
// The callback type used with RequestSyslogs().
typedef Callback2<LogDictionaryType*,
@@ -65,14 +46,11 @@ class SystemAccess : public CancelableRequestProvider {
CancelableRequestConsumerBase* consumer,
ReadCompleteCallback* callback) = 0;
- // The observer is used to monitor timezone changes.
- virtual void AddObserver(Observer* observer) = 0;
- virtual void RemoveObserver(Observer* observer) = 0;
-
protected:
- virtual ~SystemAccess() {}
+ virtual ~SyslogsProvider() {}
};
+} // namespace system
} // namespace chromeos
-#endif // CHROME_BROWSER_CHROMEOS_SYSTEM_ACCESS_H_
+#endif // CHROME_BROWSER_CHROMEOS_SYSTEM_SYSLOGS_PROVIDER_H_
diff --git a/chrome/browser/chromeos/system/timezone_settings.cc b/chrome/browser/chromeos/system/timezone_settings.cc
new file mode 100644
index 0000000..03703a9
--- /dev/null
+++ b/chrome/browser/chromeos/system/timezone_settings.cc
@@ -0,0 +1,164 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/chromeos/system/timezone_settings.h"
+
+#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/logging.h"
+#include "base/observer_list.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/singleton.h"
+#include "base/string_util.h"
+#include "base/utf_string_conversions.h"
+
+namespace chromeos {
+namespace system {
+
+namespace {
+
+// The filepath to the timezone file that symlinks to the actual timezone file.
+const char kTimezoneSymlink[] = "/var/lib/timezone/localtime";
+const char kTimezoneSymlink2[] = "/var/lib/timezone/localtime2";
+
+// The directory that contains all the timezone files. So for timezone
+// "US/Pacific", the actual timezone file is: "/usr/share/zoneinfo/US/Pacific"
+const char kTimezoneFilesDir[] = "/usr/share/zoneinfo/";
+
+// Fallback time zone ID used in case of an unexpected error.
+const char kFallbackTimeZoneId[] = "America/Los_Angeles";
+
+} // namespace
+
+class TimezoneSettingsImpl : public TimezoneSettings {
+ public:
+ // TimezoneSettings.implementation:
+ virtual const icu::TimeZone& GetTimezone();
+ virtual void SetTimezone(const icu::TimeZone& timezone);
+ virtual void AddObserver(Observer* observer);
+ virtual void RemoveObserver(Observer* observer);
+
+ static TimezoneSettingsImpl* GetInstance();
+
+ private:
+ friend struct DefaultSingletonTraits<TimezoneSettingsImpl>;
+
+ TimezoneSettingsImpl();
+
+ scoped_ptr<icu::TimeZone> timezone_;
+ ObserverList<Observer> observers_;
+
+ DISALLOW_COPY_AND_ASSIGN(TimezoneSettingsImpl);
+};
+
+std::string GetTimezoneIDAsString() {
+ // Look at kTimezoneSymlink, see which timezone we are symlinked to.
+ char buf[256];
+ const ssize_t len = readlink(kTimezoneSymlink, buf,
+ sizeof(buf)-1);
+ if (len == -1) {
+ LOG(ERROR) << "GetTimezoneID: Cannot read timezone symlink "
+ << kTimezoneSymlink;
+ return std::string();
+ }
+
+ std::string timezone(buf, len);
+ // Remove kTimezoneFilesDir from the beginning.
+ if (timezone.find(kTimezoneFilesDir) != 0) {
+ LOG(ERROR) << "GetTimezoneID: Timezone symlink is wrong "
+ << timezone;
+ return std::string();
+ }
+
+ return timezone.substr(strlen(kTimezoneFilesDir));
+}
+
+void SetTimezoneIDFromString(const std::string& id) {
+ // Change the kTimezoneSymlink symlink to the path for this timezone.
+ // We want to do this in an atomic way. So we are going to create the symlink
+ // at kTimezoneSymlink2 and then move it to kTimezoneSymlink
+
+ FilePath timezone_symlink(kTimezoneSymlink);
+ FilePath timezone_symlink2(kTimezoneSymlink2);
+ FilePath timezone_file(kTimezoneFilesDir + id);
+
+ // Make sure timezone_file exists.
+ if (!file_util::PathExists(timezone_file)) {
+ LOG(ERROR) << "SetTimezoneID: Cannot find timezone file "
+ << timezone_file.value();
+ return;
+ }
+
+ // Delete old symlink2 if it exists.
+ file_util::Delete(timezone_symlink2, false);
+
+ // Create new symlink2.
+ if (symlink(timezone_file.value().c_str(),
+ timezone_symlink2.value().c_str()) == -1) {
+ LOG(ERROR) << "SetTimezoneID: Unable to create symlink "
+ << timezone_symlink2.value() << " to " << timezone_file.value();
+ return;
+ }
+
+ // Move symlink2 to symlink.
+ if (!file_util::ReplaceFile(timezone_symlink2, timezone_symlink)) {
+ LOG(ERROR) << "SetTimezoneID: Unable to move symlink "
+ << timezone_symlink2.value() << " to "
+ << timezone_symlink.value();
+ }
+}
+
+const icu::TimeZone& TimezoneSettingsImpl::GetTimezone() {
+ return *timezone_.get();
+}
+
+void TimezoneSettingsImpl::SetTimezone(const icu::TimeZone& timezone) {
+ timezone_.reset(timezone.clone());
+ icu::UnicodeString unicode;
+ timezone.getID(unicode);
+ std::string id;
+ UTF16ToUTF8(unicode.getBuffer(), unicode.length(), &id);
+ VLOG(1) << "Setting timezone to " << id;
+ SetTimezoneIDFromString(id);
+ icu::TimeZone::setDefault(timezone);
+ FOR_EACH_OBSERVER(Observer, observers_, TimezoneChanged(timezone));
+}
+
+void TimezoneSettingsImpl::AddObserver(Observer* observer) {
+ observers_.AddObserver(observer);
+}
+
+void TimezoneSettingsImpl::RemoveObserver(Observer* observer) {
+ observers_.RemoveObserver(observer);
+}
+
+TimezoneSettingsImpl::TimezoneSettingsImpl() {
+ // Get Timezone
+ std::string id = GetTimezoneIDAsString();
+ if (id.empty()) {
+ id = kFallbackTimeZoneId;
+ LOG(ERROR) << "Got an empty string for timezone, default to " << id;
+ }
+ icu::TimeZone* timezone =
+ icu::TimeZone::createTimeZone(icu::UnicodeString::fromUTF8(id));
+ timezone_.reset(timezone);
+ icu::TimeZone::setDefault(*timezone);
+ VLOG(1) << "Timezone is " << id;
+}
+
+TimezoneSettingsImpl* TimezoneSettingsImpl::GetInstance() {
+ return Singleton<TimezoneSettingsImpl,
+ DefaultSingletonTraits<TimezoneSettingsImpl> >::get();
+}
+
+TimezoneSettings* TimezoneSettings::GetInstance() {
+ return TimezoneSettingsImpl::GetInstance();
+}
+
+} // namespace system
+} // namespace chromeos
+
+// Allows InvokeLater without adding refcounting. TimezoneSettingsImpl is a
+// Singleton and won't be deleted until it's last InvokeLater is run.
+DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::system::TimezoneSettingsImpl);
diff --git a/chrome/browser/chromeos/system/timezone_settings.h b/chrome/browser/chromeos/system/timezone_settings.h
new file mode 100644
index 0000000..06c4626
--- /dev/null
+++ b/chrome/browser/chromeos/system/timezone_settings.h
@@ -0,0 +1,45 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_CHROMEOS_SYSTEM_TIMEZONE_SETTINGS_H_
+#define CHROME_BROWSER_CHROMEOS_SYSTEM_TIMEZONE_SETTINGS_H_
+#pragma once
+
+#include <string>
+
+#include "base/callback_old.h"
+#include "content/browser/cancelable_request.h"
+#include "unicode/timezone.h"
+
+namespace chromeos {
+namespace system {
+
+// This interface provides access to Chrome OS timezone settings.
+class TimezoneSettings : public CancelableRequestProvider {
+ public:
+ class Observer {
+ public:
+ // Called when the timezone has changed. |timezone| is non-null.
+ virtual void TimezoneChanged(const icu::TimeZone& timezone) = 0;
+ };
+
+ static TimezoneSettings* GetInstance();
+
+ // Returns the current timezone as an icu::Timezone object.
+ virtual const icu::TimeZone& GetTimezone() = 0;
+
+ // Sets the current timezone. |timezone| must be non-null.
+ virtual void SetTimezone(const icu::TimeZone& timezone) = 0;
+
+ virtual void AddObserver(Observer* observer) = 0;
+ virtual void RemoveObserver(Observer* observer) = 0;
+
+ protected:
+ virtual ~TimezoneSettings() {}
+};
+
+} // namespace system
+} // namespace chromeos
+
+#endif // CHROME_BROWSER_CHROMEOS_SYSTEM_TIMEZONE_SETTINGS_H_