Home | History | Annotate | Download | only in util
      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 "sync/util/get_session_name.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/bind_helpers.h"
      9 #include "base/callback.h"
     10 #include "base/location.h"
     11 #include "base/sys_info.h"
     12 #include "base/task_runner.h"
     13 
     14 #if defined(OS_LINUX)
     15 #include "sync/util/get_session_name_linux.h"
     16 #elif defined(OS_IOS)
     17 #include "sync/util/get_session_name_ios.h"
     18 #elif defined(OS_MACOSX)
     19 #include "sync/util/get_session_name_mac.h"
     20 #elif defined(OS_WIN)
     21 #include "sync/util/get_session_name_win.h"
     22 #elif defined(OS_ANDROID)
     23 #include "base/android/build_info.h"
     24 #endif
     25 
     26 namespace syncer {
     27 
     28 namespace {
     29 
     30 std::string GetSessionNameSynchronously() {
     31   std::string session_name;
     32 #if defined(OS_CHROMEOS)
     33   std::string board = base::SysInfo::GetLsbReleaseBoard();
     34   // Currently, only "stumpy" type of board is considered Chromebox, and
     35   // anything else is Chromebook.  On these devices, session_name should look
     36   // like "stumpy-signed-mp-v2keys" etc. The information can be checked on
     37   // "CHROMEOS_RELEASE_BOARD" line in chrome://system.
     38   session_name = board.substr(0, 6) == "stumpy" ? "Chromebox" : "Chromebook";
     39 #elif defined(OS_LINUX)
     40   session_name = internal::GetHostname();
     41 #elif defined(OS_IOS)
     42   session_name = internal::GetComputerName();
     43 #elif defined(OS_MACOSX)
     44   session_name = internal::GetHardwareModelName();
     45 #elif defined(OS_WIN)
     46   session_name = internal::GetComputerName();
     47 #elif defined(OS_ANDROID)
     48   base::android::BuildInfo* android_build_info =
     49       base::android::BuildInfo::GetInstance();
     50   session_name = android_build_info->model();
     51 #endif
     52 
     53   if (session_name == "Unknown" || session_name.empty())
     54     session_name = base::SysInfo::OperatingSystemName();
     55 
     56   return session_name;
     57 }
     58 
     59 void FillSessionName(std::string* session_name) {
     60   *session_name = GetSessionNameSynchronously();
     61 }
     62 
     63 void OnSessionNameFilled(
     64     const base::Callback<void(const std::string&)>& done_callback,
     65     std::string* session_name) {
     66   done_callback.Run(*session_name);
     67 }
     68 
     69 }  // namespace
     70 
     71 void GetSessionName(
     72     const scoped_refptr<base::TaskRunner>& task_runner,
     73     const base::Callback<void(const std::string&)>& done_callback) {
     74   std::string* session_name = new std::string();
     75   task_runner->PostTaskAndReply(
     76       FROM_HERE,
     77       base::Bind(&FillSessionName,
     78                  base::Unretained(session_name)),
     79       base::Bind(&OnSessionNameFilled,
     80                  done_callback,
     81                  base::Owned(session_name)));
     82 }
     83 
     84 std::string GetSessionNameSynchronouslyForTesting() {
     85   return GetSessionNameSynchronously();
     86 }
     87 
     88 }  // namespace syncer
     89