12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cloud_print/service/win/service_utils.h"
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "google_apis/gaia/gaia_switches.h"
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <windows.h>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <security.h>  // NOLINT
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/command_line.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/chrome_switches.h"
140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/cloud_devices/common/cloud_devices_switches.h"
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
16a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 GetLocalComputerName() {
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DWORD size = 0;
18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 result;
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ::GetComputerName(NULL, &size);
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  result.resize(size);
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (result.empty())
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return result;
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!::GetComputerName(&result[0], &size))
24a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return base::string16();
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  result.resize(size);
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return result;
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
29a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 ReplaceLocalHostInName(const base::string16& user_name) {
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  static const wchar_t kLocalDomain[] = L".\\";
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (StartsWith(user_name, kLocalDomain, true)) {
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return GetLocalComputerName() +
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)           user_name.substr(arraysize(kLocalDomain) - 2);
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return user_name;
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 GetCurrentUserName() {
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ULONG size = 0;
40a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 result;
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ::GetUserNameEx(::NameSamCompatible, NULL, &size);
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  result.resize(size);
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (result.empty())
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return result;
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!::GetUserNameEx(::NameSamCompatible, &result[0], &size))
46a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return base::string16();
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  result.resize(size);
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return result;
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void CopyChromeSwitchesFromCurrentProcess(CommandLine* destination) {
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  static const char* const kSwitchesToCopy[] = {
530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    switches::kCloudPrintURL,
540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    switches::kCloudPrintXmppEndpoint,
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    switches::kEnableCloudPrintXps,
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    switches::kEnableLogging,
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    switches::kIgnoreUrlFetcherCertRequests,
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    switches::kLsoUrl,
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    switches::kV,
60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  destination->CopySwitchesFrom(*CommandLine::ForCurrentProcess(),
62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                kSwitchesToCopy,
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                arraysize(kSwitchesToCopy));
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
66