diff options
author | vitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-22 18:05:57 +0000 |
---|---|---|
committer | vitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-22 18:05:57 +0000 |
commit | 5ee218ba0f38ed488c68b599c75ebd2764fdba18 (patch) | |
tree | 46eb94c495d95d56eadf7e3cac79f8a09c497e41 /cloud_print/service/win/cloud_print_service.cc | |
parent | 8ae3ef1747ddc6833dfd7f91de503370edb14bad (diff) | |
download | chromium_src-5ee218ba0f38ed488c68b599c75ebd2764fdba18.zip chromium_src-5ee218ba0f38ed488c68b599c75ebd2764fdba18.tar.gz chromium_src-5ee218ba0f38ed488c68b599c75ebd2764fdba18.tar.bz2 |
Validates service state file and ask user to update information if necessarily.
Connects to Google servers to get authentication tokens.
BUG=125026
TEST=service_state_unittest.cc
Review URL: https://chromiumcodereview.appspot.com/10414020
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@138294 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'cloud_print/service/win/cloud_print_service.cc')
-rw-r--r-- | cloud_print/service/win/cloud_print_service.cc | 96 |
1 files changed, 94 insertions, 2 deletions
diff --git a/cloud_print/service/win/cloud_print_service.cc b/cloud_print/service/win/cloud_print_service.cc index 976e598..2eeac75 100644 --- a/cloud_print/service/win/cloud_print_service.cc +++ b/cloud_print/service/win/cloud_print_service.cc @@ -10,9 +10,12 @@ #include "base/at_exit.h" #include "base/command_line.h" #include "base/file_path.h" +#include "base/file_util.h" #include "base/path_service.h" +#include "base/string_util.h" #include "base/win/scoped_handle.h" #include "cloud_print/service/win/resource.h" +#include "cloud_print/service/win/service_state.h" namespace { @@ -26,6 +29,8 @@ const char kServiceSwitch[] = "service"; const char kUserDataDirSwitch[] = "user-data-dir"; const char kQuietSwitch[] = "quiet"; +const wchar_t kServiceStateFileName[] = L"Service State"; + // The traits class for Windows Service. class ServiceHandleTraits { public: @@ -98,6 +103,34 @@ void InvalidUsage() { std::cout << "\n"; } +std::string GetOption(const std::string& name, const std::string& default, + bool secure) { + std::cout << "Input \'" << name << "\'"; + if (!default.empty()) { + std::cout << ", press [ENTER] to keep '"; + std::cout << default; + std::cout << "'"; + } + std::cout << ":"; + std::string tmp; + + if (secure) { + DWORD saved_mode = 0; + // Don't close. + HANDLE stdin_handle = ::GetStdHandle(STD_INPUT_HANDLE); + ::GetConsoleMode(stdin_handle, &saved_mode);
+ ::SetConsoleMode(stdin_handle, saved_mode & ~ENABLE_ECHO_INPUT); + std::getline(std::cin, tmp); + ::SetConsoleMode(stdin_handle, saved_mode); + std::cout << "\n"; + } else { + std::getline(std::cin, tmp); + } + if (tmp.empty()) + return default; + return tmp; +} + } // namespace class CloudPrintServiceModule @@ -118,7 +151,7 @@ class CloudPrintServiceModule HRESULT Install(const FilePath& user_data_dir) { // TODO(vitalybuka): consider "lite" version if we don't want unregister // printers here. - if (!Uninstall())
+ if (!Uninstall()) return E_FAIL; FilePath service_path; @@ -196,7 +229,11 @@ class CloudPrintServiceModule } FilePath data_dir = command_line.GetSwitchValuePath(kUserDataDirSwitch); - HRESULT hr = Install(data_dir); + HRESULT hr = ProcessServiceState(data_dir, + command_line.HasSwitch(kQuietSwitch)); + if (FAILED(hr)) + return hr; + hr = Install(data_dir); if (SUCCEEDED(hr) && command_line.HasSwitch(kStartSwitch)) return StartService(); @@ -216,6 +253,61 @@ class CloudPrintServiceModule return S_FALSE; } + HRESULT ProcessServiceState(const FilePath& user_data_dir, bool quiet) { + FilePath file = user_data_dir.Append(kServiceStateFileName); + + for (;;) { + std::string contents; + ServiceState service_state; + + bool is_valid = file_util::ReadFileToString(file, &contents) && + service_state.FromString(contents); + + if (!quiet) { + std::cout << file.value() << ":\n"; + std::cout << contents << "\n"; + } + + if (!is_valid) + LOG(ERROR) << "Invalid file: " << file.value(); + + if (quiet) + return is_valid ? S_OK : HRESULT_FROM_WIN32(ERROR_FILE_INVALID); + + std::cout << "Do you want to use this file [y/n]:"; + for (;;) { + std::string input; + std::getline(std::cin, input); + StringToLowerASCII(&input); + if (input == "y") { + return S_OK; + } else if (input == "n") { + is_valid = false; + break; + } + } + + while (!is_valid) { + std::string email = GetOption("email", service_state.email(), false); + std::string password = GetOption("password", "", true); + std::string proxy_id = GetOption("connector_id", + service_state.proxy_id(), false); + is_valid = service_state.Configure(email, password, proxy_id); + if (is_valid) { + std::string new_contents = service_state.ToString(); + if (new_contents != contents) { + if (file_util::WriteFile(file, new_contents.c_str(), + new_contents.size()) <= 0) { + return HResultFromLastError(); + } + } + } + } + } + + return S_OK; + } + HRESULT OpenServiceManager(ServiceHandle* service_manager) { if (!service_manager) return E_POINTER; |