summaryrefslogtreecommitdiffstats
path: root/cloud_print/service/win/service_utils.cc
blob: 6bced8098dd4ffb2c7530ae48847eaeaf69bdad9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2013 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 "cloud_print/service/win/service_utils.h"
#include "google_apis/gaia/gaia_switches.h"

#include <windows.h>
#include <security.h>  // NOLINT

#include "base/command_line.h"
#include "base/strings/string_util.h"
#include "chrome/common/chrome_switches.h"
#include "components/cloud_devices/common/cloud_devices_switches.h"

base::string16 GetLocalComputerName() {
  DWORD size = 0;
  base::string16 result;
  ::GetComputerName(NULL, &size);
  result.resize(size);
  if (result.empty())
    return result;
  if (!::GetComputerName(&result[0], &size))
    return base::string16();
  result.resize(size);
  return result;
}

base::string16 ReplaceLocalHostInName(const base::string16& user_name) {
  static const wchar_t kLocalDomain[] = L".\\";
  if (StartsWith(user_name, kLocalDomain, true)) {
    return GetLocalComputerName() +
           user_name.substr(arraysize(kLocalDomain) - 2);
  }
  return user_name;
}

base::string16 GetCurrentUserName() {
  ULONG size = 0;
  base::string16 result;
  ::GetUserNameEx(::NameSamCompatible, NULL, &size);
  result.resize(size);
  if (result.empty())
    return result;
  if (!::GetUserNameEx(::NameSamCompatible, &result[0], &size))
    return base::string16();
  result.resize(size);
  return result;
}

void CopyChromeSwitchesFromCurrentProcess(base::CommandLine* destination) {
  static const char* const kSwitchesToCopy[] = {
    switches::kCloudPrintURL,
    switches::kCloudPrintXmppEndpoint,
    switches::kEnableCloudPrintXps,
    switches::kEnableLogging,
    switches::kIgnoreUrlFetcherCertRequests,
    switches::kLsoUrl,
    switches::kV,
  };
  destination->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
                                kSwitchesToCopy,
                                arraysize(kSwitchesToCopy));
}