Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / google / google_update_settings_posix.cc
index b861576..80e453f 100644 (file)
@@ -4,33 +4,55 @@
 
 #include "chrome/installer/util/google_update_settings.h"
 
-#include "base/file_util.h"
+#include "base/files/file_util.h"
+#include "base/lazy_instance.h"
 #include "base/path_service.h"
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
+#include "base/synchronization/lock.h"
 #include "chrome/common/chrome_paths.h"
 
-namespace google_update {
+namespace {
 
-static std::string& posix_guid() {
-  CR_DEFINE_STATIC_LOCAL(std::string, guid, ());
-  return guid;
-}
-
-}  // namespace google_update
+base::LazyInstance<std::string>::Leaky g_posix_client_id =
+    LAZY_INSTANCE_INITIALIZER;
+base::LazyInstance<base::Lock>::Leaky g_posix_client_id_lock =
+    LAZY_INSTANCE_INITIALIZER;
 
 // File name used in the user data dir to indicate consent.
-static const char kConsentToSendStats[] = "Consent To Send Stats";
+const char kConsentToSendStats[] = "Consent To Send Stats";
+
+void SetConsentFilePermissionIfNeeded(const base::FilePath& consent_file) {
+#if defined(OS_CHROMEOS)
+  // The consent file needs to be world readable. See http://crbug.com/383003
+  int permissions;
+  if (base::GetPosixFilePermissions(consent_file, &permissions) &&
+      (permissions & base::FILE_PERMISSION_READ_BY_OTHERS) == 0) {
+    permissions |= base::FILE_PERMISSION_READ_BY_OTHERS;
+    base::SetPosixFilePermissions(consent_file, permissions);
+  }
+#endif
+}
+
+}  // namespace
 
 // static
 bool GoogleUpdateSettings::GetCollectStatsConsent() {
   base::FilePath consent_file;
   PathService::Get(chrome::DIR_USER_DATA, &consent_file);
   consent_file = consent_file.Append(kConsentToSendStats);
+
+  if (!base::DirectoryExists(consent_file.DirName()))
+    return false;
+
   std::string tmp_guid;
   bool consented = base::ReadFileToString(consent_file, &tmp_guid);
-  if (consented)
-    google_update::posix_guid().assign(tmp_guid);
+  if (consented) {
+    SetConsentFilePermissionIfNeeded(consent_file);
+
+    base::AutoLock lock(g_posix_client_id_lock.Get());
+    g_posix_client_id.Get().assign(tmp_guid);
+  }
   return consented;
 }
 
@@ -41,41 +63,53 @@ bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) {
   if (!base::DirectoryExists(consent_dir))
     return false;
 
+  base::AutoLock lock(g_posix_client_id_lock.Get());
+
   base::FilePath consent_file = consent_dir.AppendASCII(kConsentToSendStats);
-  if (consented) {
-    if ((!base::PathExists(consent_file)) ||
-        (base::PathExists(consent_file) &&
-         !google_update::posix_guid().empty())) {
-      const char* c_str = google_update::posix_guid().c_str();
-      int size = google_update::posix_guid().size();
-      return file_util::WriteFile(consent_file, c_str, size) == size;
-    }
-  } else {
-    google_update::posix_guid().clear();
+  if (!consented) {
+    g_posix_client_id.Get().clear();
     return base::DeleteFile(consent_file, false);
   }
-  return true;
+
+  const std::string& client_id = g_posix_client_id.Get();
+  if (base::PathExists(consent_file) && client_id.empty())
+    return true;
+
+  int size = client_id.size();
+  bool write_success =
+      base::WriteFile(consent_file, client_id.c_str(), size) == size;
+  if (write_success)
+    SetConsentFilePermissionIfNeeded(consent_file);
+  return write_success;
 }
 
 // static
-bool GoogleUpdateSettings::GetMetricsId(std::string* metrics_id) {
-  *metrics_id = google_update::posix_guid();
-  return true;
+// TODO(gab): Implement storing/loading for all ClientInfo fields on POSIX.
+scoped_ptr<metrics::ClientInfo> GoogleUpdateSettings::LoadMetricsClientInfo() {
+  scoped_ptr<metrics::ClientInfo> client_info(new metrics::ClientInfo);
+
+  base::AutoLock lock(g_posix_client_id_lock.Get());
+  if (g_posix_client_id.Get().empty())
+    return scoped_ptr<metrics::ClientInfo>();
+  client_info->client_id = g_posix_client_id.Get();
+
+  return client_info.Pass();
 }
 
 // static
-bool GoogleUpdateSettings::SetMetricsId(const std::string& client_id) {
+// TODO(gab): Implement storing/loading for all ClientInfo fields on POSIX.
+void GoogleUpdateSettings::StoreMetricsClientInfo(
+    const metrics::ClientInfo& client_info) {
   // Make sure that user has consented to send crashes.
-  base::FilePath consent_dir;
-  PathService::Get(chrome::DIR_USER_DATA, &consent_dir);
-  if (!base::DirectoryExists(consent_dir) ||
-      !GoogleUpdateSettings::GetCollectStatsConsent()) {
-    return false;
-  }
+  if (!GoogleUpdateSettings::GetCollectStatsConsent())
+    return;
 
-  // Since user has consented, write the metrics id to the file.
-  google_update::posix_guid() = client_id;
-  return GoogleUpdateSettings::SetCollectStatsConsent(true);
+  {
+    // Since user has consented, write the metrics id to the file.
+    base::AutoLock lock(g_posix_client_id_lock.Get());
+    g_posix_client_id.Get() = client_info.client_id;
+  }
+  GoogleUpdateSettings::SetCollectStatsConsent(true);
 }
 
 // GetLastRunTime and SetLastRunTime are not implemented for posix. Their