Move IsRunningOnChromeOS to SysInfo
Also, use lsb-release/CHROMEOS_RELEASE_NAME instead of the
USER env variable.

This CL depends on https://codereview.chromium.org/23588009/

BUG=270135
For trivial base/ changes:

R=derat@chromium.org, satorux@chromium.org, sky@chromium.org
TBR=brettw@chromium.org

Review URL: https://codereview.chromium.org/23904025

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@225761 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: 49c4cf85a2b9ae985227799db540dd771f39ab26
diff --git a/base/base.gypi b/base/base.gypi
index 8621208..9b941a8 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -110,8 +110,6 @@
           'callback_internal.h',
           'callback_list.h',
           'cancelable_callback.h',
-          'chromeos/chromeos_version.cc',
-          'chromeos/chromeos_version.h',
           'command_line.cc',
           'command_line.h',
           'compiler_specific.h',
diff --git a/base/chromeos/chromeos_version.cc b/base/chromeos/chromeos_version.cc
deleted file mode 100644
index 4a70cd5..0000000
--- a/base/chromeos/chromeos_version.cc
+++ /dev/null
@@ -1,24 +0,0 @@
-// Copyright (c) 2012 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 "base/chromeos/chromeos_version.h"
-
-#include <stdlib.h>
-#include <string.h>
-
-#include "base/logging.h"
-
-namespace base {
-namespace chromeos {
-
-bool IsRunningOnChromeOS() {
-  // Check if the user name is chronos. Note that we don't go with
-  // getuid() + getpwuid_r() as it may end up reading /etc/passwd, which
-  // can be expensive.
-  const char* user = getenv("USER");
-  return user && strcmp(user, "chronos") == 0;
-}
-
-}  // namespace chromeos
-}  // namespace base
diff --git a/base/chromeos/chromeos_version.h b/base/chromeos/chromeos_version.h
deleted file mode 100644
index 25acd43..0000000
--- a/base/chromeos/chromeos_version.h
+++ /dev/null
@@ -1,20 +0,0 @@
-// Copyright (c) 2012 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 BASE_CHROMEOS_CHROMEOS_VERSION_H_
-#define BASE_CHROMEOS_CHROMEOS_VERSION_H_
-
-#include "base/base_export.h"
-
-namespace base {
-namespace chromeos {
-
-// Returns true if the browser is running on Chrome OS.
-// Useful for implementing stubs for Linux desktop.
-BASE_EXPORT bool IsRunningOnChromeOS();
-
-}  // namespace chromeos
-}  // namespace base
-
-#endif  // BASE_CHROMEOS_CHROMEOS_VERSION_H_
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc
index 762700a..296950b 100644
--- a/base/file_util_posix.cc
+++ b/base/file_util_posix.cc
@@ -43,6 +43,7 @@
 #include "base/strings/stringprintf.h"
 #include "base/strings/sys_string_conversions.h"
 #include "base/strings/utf_string_conversions.h"
+#include "base/sys_info.h"
 #include "base/threading/thread_restrictions.h"
 #include "base/time/time.h"
 
@@ -54,10 +55,6 @@
 #include <grp.h>
 #endif
 
-#if defined(OS_CHROMEOS)
-#include "base/chromeos/chromeos_version.h"
-#endif
-
 namespace base {
 
 namespace {
@@ -771,7 +768,7 @@
 
 FilePath GetHomeDir() {
 #if defined(OS_CHROMEOS)
-  if (base::chromeos::IsRunningOnChromeOS())
+  if (base::SysInfo::IsRunningOnChromeOS())
     return FilePath("/home/chronos/user");
 #endif
 
diff --git a/base/sys_info.h b/base/sys_info.h
index 564832d..ba45bfc 100644
--- a/base/sys_info.h
+++ b/base/sys_info.h
@@ -96,6 +96,9 @@
   // time of the Chrome OS build).
   static Time GetLsbReleaseTime();
 
+  // Returns true when actually running in a Chrome OS environment.
+  static bool IsRunningOnChromeOS();
+
   // Test method to force re-parsing of lsb-release.
   static void SetChromeOSVersionInfoForTest(const std::string& lsb_release,
                                             const Time& lsb_release_time);
diff --git a/base/sys_info_chromeos.cc b/base/sys_info_chromeos.cc
index 9d7505e..e0f8b4f 100644
--- a/base/sys_info_chromeos.cc
+++ b/base/sys_info_chromeos.cc
@@ -25,8 +25,13 @@
   "GOOGLE_RELEASE",
   "DISTRIB_RELEASE",
 };
-const size_t kLinuxStandardBaseVersionKeysLength =
-    arraysize(kLinuxStandardBaseVersionKeys);
+
+const char kChromeOsReleaseNameKey[] = "CHROMEOS_RELEASE_NAME";
+
+const char* const kChromeOsReleaseNames[] = {
+  "Chrome OS",
+  "Chromium OS",
+};
 
 const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release";
 
@@ -48,6 +53,7 @@
     major_version_ = 0;
     minor_version_ = 0;
     bugfix_version_ = 0;
+    is_running_on_chromeos_ = false;
 
     std::string lsb_release, lsb_release_time_str;
     scoped_ptr<base::Environment> env(base::Environment::Create());
@@ -95,6 +101,7 @@
   const SysInfo::LsbReleaseMap& lsb_release_map() const {
     return lsb_release_map_;
   }
+  bool is_running_on_chromeos() const { return is_running_on_chromeos_; }
 
  private:
   void ParseLsbRelease(const std::string& lsb_release) {
@@ -113,7 +120,7 @@
     }
     // Parse the version from the first matching recognized version key.
     std::string version;
-    for (size_t i = 0; i < kLinuxStandardBaseVersionKeysLength; ++i) {
+    for (size_t i = 0; i < arraysize(kLinuxStandardBaseVersionKeys); ++i) {
       std::string key = kLinuxStandardBaseVersionKeys[i];
       if (GetLsbReleaseValue(key, &version) && !version.empty())
         break;
@@ -131,6 +138,17 @@
       StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()),
                   &bugfix_version_);
     }
+
+    // Check release name for Chrome OS.
+    std::string release_name;
+    if (GetLsbReleaseValue(kChromeOsReleaseNameKey, &release_name)) {
+      for (size_t i = 0; i < arraysize(kChromeOsReleaseNames); ++i) {
+        if (release_name == kChromeOsReleaseNames[i]) {
+          is_running_on_chromeos_ = true;
+          break;
+        }
+      }
+    }
   }
 
   base::Time lsb_release_time_;
@@ -138,6 +156,7 @@
   int32 major_version_;
   int32 minor_version_;
   int32 bugfix_version_;
+  bool is_running_on_chromeos_;
 };
 
 static LazyInstance<ChromeOSVersionInfo>
@@ -182,6 +201,11 @@
 }
 
 // static
+bool SysInfo::IsRunningOnChromeOS() {
+  return GetChromeOSVersionInfo().is_running_on_chromeos();
+}
+
+// static
 void SysInfo::SetChromeOSVersionInfoForTest(const std::string& lsb_release,
                                             const Time& lsb_release_time) {
   scoped_ptr<base::Environment> env(base::Environment::Create());
diff --git a/base/sys_info_unittest.cc b/base/sys_info_unittest.cc
index 6dc325f..eacf76c 100644
--- a/base/sys_info_unittest.cc
+++ b/base/sys_info_unittest.cc
@@ -121,4 +121,26 @@
                    parsed_lsb_release_time.ToDoubleT());
 }
 
+TEST_F(SysInfoTest, IsRunningOnChromeOS) {
+  base::SysInfo::SetChromeOSVersionInfoForTest("", base::Time());
+  EXPECT_FALSE(base::SysInfo::IsRunningOnChromeOS());
+
+  const char* kLsbRelease1 =
+      "CHROMEOS_RELEASE_NAME=Non Chrome OS\n"
+      "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
+  base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease1, base::Time());
+  EXPECT_FALSE(base::SysInfo::IsRunningOnChromeOS());
+
+  const char* kLsbRelease2 =
+      "CHROMEOS_RELEASE_NAME=Chrome OS\n"
+      "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
+  base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease2, base::Time());
+  EXPECT_TRUE(base::SysInfo::IsRunningOnChromeOS());
+
+  const char* kLsbRelease3 =
+      "CHROMEOS_RELEASE_NAME=Chromium OS\n";
+  base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease3, base::Time());
+  EXPECT_TRUE(base::SysInfo::IsRunningOnChromeOS());
+}
+
 #endif  // OS_CHROMEOS