| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cloud_print/service/win/cloud_print_service.h" | 5 #include "cloud_print/service/win/cloud_print_service.h" |
| 6 | 6 |
| 7 #define SECURITY_WIN32 | 7 #define SECURITY_WIN32 |
| 8 #include <security.h> | 8 #include <security.h> |
| 9 | 9 |
| 10 #include <iomanip> | 10 #include <iomanip> |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 | 61 |
| 62 HRESULT HResultFromLastError() { | 62 HRESULT HResultFromLastError() { |
| 63 HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); | 63 HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); |
| 64 // Something already failed if function called. | 64 // Something already failed if function called. |
| 65 if (SUCCEEDED(hr)) | 65 if (SUCCEEDED(hr)) |
| 66 hr = E_FAIL; | 66 hr = E_FAIL; |
| 67 return hr; | 67 return hr; |
| 68 } | 68 } |
| 69 | 69 |
| 70 void InvalidUsage() { | 70 void InvalidUsage() { |
| 71 FilePath service_path; | 71 base::FilePath service_path; |
| 72 CHECK(PathService::Get(base::FILE_EXE, &service_path)); | 72 CHECK(PathService::Get(base::FILE_EXE, &service_path)); |
| 73 | 73 |
| 74 std::cout << "Usage: "; | 74 std::cout << "Usage: "; |
| 75 std::cout << service_path.BaseName().value(); | 75 std::cout << service_path.BaseName().value(); |
| 76 std::cout << " ["; | 76 std::cout << " ["; |
| 77 std::cout << "["; | 77 std::cout << "["; |
| 78 std::cout << "["; | 78 std::cout << "["; |
| 79 std::cout << " -" << kInstallSwitch; | 79 std::cout << " -" << kInstallSwitch; |
| 80 std::cout << " -" << kUserDataDirSwitch << "=DIRECTORY"; | 80 std::cout << " -" << kUserDataDirSwitch << "=DIRECTORY"; |
| 81 std::cout << "]"; | 81 std::cout << "]"; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 // TODO(vitalybuka): consider "lite" version if we don't want unregister | 186 // TODO(vitalybuka): consider "lite" version if we don't want unregister |
| 187 // printers here. | 187 // printers here. |
| 188 HRESULT hr = UninstallService(); | 188 HRESULT hr = UninstallService(); |
| 189 if (FAILED(hr)) | 189 if (FAILED(hr)) |
| 190 return hr; | 190 return hr; |
| 191 | 191 |
| 192 hr = UpdateRegistryAppId(true); | 192 hr = UpdateRegistryAppId(true); |
| 193 if (FAILED(hr)) | 193 if (FAILED(hr)) |
| 194 return hr; | 194 return hr; |
| 195 | 195 |
| 196 FilePath service_path; | 196 base::FilePath service_path; |
| 197 CHECK(PathService::Get(base::FILE_EXE, &service_path)); | 197 CHECK(PathService::Get(base::FILE_EXE, &service_path)); |
| 198 CommandLine command_line(service_path); | 198 CommandLine command_line(service_path); |
| 199 command_line.AppendSwitch(run_switch); | 199 command_line.AppendSwitch(run_switch); |
| 200 command_line.AppendSwitchPath(kUserDataDirSwitch, user_data_dir_); | 200 command_line.AppendSwitchPath(kUserDataDirSwitch, user_data_dir_); |
| 201 | 201 |
| 202 LocalSecurityPolicy local_security_policy; | 202 LocalSecurityPolicy local_security_policy; |
| 203 if (local_security_policy.Open()) { | 203 if (local_security_policy.Open()) { |
| 204 if (!local_security_policy.IsPrivilegeSet(user, kSeServiceLogonRight)) { | 204 if (!local_security_policy.IsPrivilegeSet(user, kSeServiceLogonRight)) { |
| 205 LOG(WARNING) << "Setting " << kSeServiceLogonRight << " for " << user; | 205 LOG(WARNING) << "Setting " << kSeServiceLogonRight << " for " << user; |
| 206 if (!local_security_policy.SetPrivilege(user, kSeServiceLogonRight)) { | 206 if (!local_security_policy.SetPrivilege(user, kSeServiceLogonRight)) { |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 | 339 |
| 340 void SelectWindowsAccount(string16* run_as_user, | 340 void SelectWindowsAccount(string16* run_as_user, |
| 341 string16* run_as_password) { | 341 string16* run_as_password) { |
| 342 *run_as_user = GetCurrentUserName(); | 342 *run_as_user = GetCurrentUserName(); |
| 343 for (;;) { | 343 for (;;) { |
| 344 std::cout << "\nPlease provide Windows account to run service.\n"; | 344 std::cout << "\nPlease provide Windows account to run service.\n"; |
| 345 *run_as_user = ASCIIToWide(GetOption("Account as DOMAIN\\USERNAME", | 345 *run_as_user = ASCIIToWide(GetOption("Account as DOMAIN\\USERNAME", |
| 346 WideToASCII(*run_as_user), false)); | 346 WideToASCII(*run_as_user), false)); |
| 347 *run_as_password = ASCIIToWide(GetOption("Password", "", true)); | 347 *run_as_password = ASCIIToWide(GetOption("Password", "", true)); |
| 348 | 348 |
| 349 FilePath requirements_filename(user_data_dir_); | 349 base::FilePath requirements_filename(user_data_dir_); |
| 350 requirements_filename = | 350 requirements_filename = |
| 351 requirements_filename.Append(kRequirementsFileName); | 351 requirements_filename.Append(kRequirementsFileName); |
| 352 | 352 |
| 353 file_util::Delete(requirements_filename, false); | 353 file_util::Delete(requirements_filename, false); |
| 354 if (file_util::PathExists(requirements_filename)) { | 354 if (file_util::PathExists(requirements_filename)) { |
| 355 LOG(ERROR) << "Unable to delete " << | 355 LOG(ERROR) << "Unable to delete " << |
| 356 requirements_filename.value() << "."; | 356 requirements_filename.value() << "."; |
| 357 continue; | 357 continue; |
| 358 } | 358 } |
| 359 if (FAILED(InstallService(run_as_user->c_str(), | 359 if (FAILED(InstallService(run_as_user->c_str(), |
| (...skipping 22 matching lines...) Expand all Loading... |
| 382 std::cout << "\nService requirements check result: \n"; | 382 std::cout << "\nService requirements check result: \n"; |
| 383 std::cout << printers << "\n"; | 383 std::cout << printers << "\n"; |
| 384 file_util::Delete(requirements_filename, false); | 384 file_util::Delete(requirements_filename, false); |
| 385 | 385 |
| 386 if (AskUser("Do you want to use " + WideToASCII(*run_as_user) + "?")) | 386 if (AskUser("Do you want to use " + WideToASCII(*run_as_user) + "?")) |
| 387 return; | 387 return; |
| 388 } | 388 } |
| 389 } | 389 } |
| 390 | 390 |
| 391 HRESULT SetupServiceState() { | 391 HRESULT SetupServiceState() { |
| 392 FilePath file = user_data_dir_.Append(kServiceStateFileName); | 392 base::FilePath file = user_data_dir_.Append(kServiceStateFileName); |
| 393 | 393 |
| 394 for (;;) { | 394 for (;;) { |
| 395 std::string contents; | 395 std::string contents; |
| 396 ServiceState service_state; | 396 ServiceState service_state; |
| 397 | 397 |
| 398 bool is_valid = file_util::ReadFileToString(file, &contents) && | 398 bool is_valid = file_util::ReadFileToString(file, &contents) && |
| 399 service_state.FromString(contents); | 399 service_state.FromString(contents); |
| 400 | 400 |
| 401 std::cout << "\nFile '" << file.value() << "' content:\n"; | 401 std::cout << "\nFile '" << file.value() << "' content:\n"; |
| 402 std::cout << contents << "\n"; | 402 std::cout << contents << "\n"; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 if (!::ControlService(service, SERVICE_CONTROL_STOP, &status)) | 485 if (!::ControlService(service, SERVICE_CONTROL_STOP, &status)) |
| 486 return HResultFromLastError(); | 486 return HResultFromLastError(); |
| 487 while (::QueryServiceStatus(service, &status) && | 487 while (::QueryServiceStatus(service, &status) && |
| 488 status.dwCurrentState > SERVICE_STOPPED) { | 488 status.dwCurrentState > SERVICE_STOPPED) { |
| 489 Sleep(500); | 489 Sleep(500); |
| 490 } | 490 } |
| 491 return S_OK; | 491 return S_OK; |
| 492 } | 492 } |
| 493 | 493 |
| 494 HRESULT CheckRequirements() { | 494 HRESULT CheckRequirements() { |
| 495 FilePath requirements_filename(user_data_dir_); | 495 base::FilePath requirements_filename(user_data_dir_); |
| 496 requirements_filename = requirements_filename.Append(kRequirementsFileName); | 496 requirements_filename = requirements_filename.Append(kRequirementsFileName); |
| 497 std::string output; | 497 std::string output; |
| 498 output.append("Printers available for " + | 498 output.append("Printers available for " + |
| 499 WideToASCII(GetCurrentUserName()) + ":\n"); | 499 WideToASCII(GetCurrentUserName()) + ":\n"); |
| 500 scoped_refptr<printing::PrintBackend> backend( | 500 scoped_refptr<printing::PrintBackend> backend( |
| 501 printing::PrintBackend::CreateInstance(NULL)); | 501 printing::PrintBackend::CreateInstance(NULL)); |
| 502 printing::PrinterList printer_list; | 502 printing::PrinterList printer_list; |
| 503 backend->EnumeratePrinters(&printer_list); | 503 backend->EnumeratePrinters(&printer_list); |
| 504 for (size_t i = 0; i < printer_list.size(); ++i) { | 504 for (size_t i = 0; i < printer_list.size(); ++i) { |
| 505 output += " "; | 505 output += " "; |
| 506 output += printer_list[i].printer_name; | 506 output += printer_list[i].printer_name; |
| 507 output += "\n"; | 507 output += "\n"; |
| 508 } | 508 } |
| 509 FilePath chrome = chrome_launcher_support::GetAnyChromePath(); | 509 base::FilePath chrome = chrome_launcher_support::GetAnyChromePath(); |
| 510 output.append(chrome.empty() ? kChromeIsNotAvalible : kChromeIsAvalible); | 510 output.append(chrome.empty() ? kChromeIsNotAvalible : kChromeIsAvalible); |
| 511 file_util::WriteFile(requirements_filename, output.c_str(), output.size()); | 511 file_util::WriteFile(requirements_filename, output.c_str(), output.size()); |
| 512 return S_OK; | 512 return S_OK; |
| 513 } | 513 } |
| 514 | 514 |
| 515 HRESULT StartConnector() { | 515 HRESULT StartConnector() { |
| 516 chrome_.reset(new ChromeLauncher(user_data_dir_)); | 516 chrome_.reset(new ChromeLauncher(user_data_dir_)); |
| 517 return chrome_->Start() ? S_OK : E_FAIL; | 517 return chrome_->Start() ? S_OK : E_FAIL; |
| 518 } | 518 } |
| 519 | 519 |
| 520 void StopConnector() { | 520 void StopConnector() { |
| 521 if (chrome_.get()) { | 521 if (chrome_.get()) { |
| 522 chrome_->Stop(); | 522 chrome_->Stop(); |
| 523 chrome_.reset(); | 523 chrome_.reset(); |
| 524 } | 524 } |
| 525 } | 525 } |
| 526 | 526 |
| 527 static BOOL WINAPI ConsoleCtrlHandler(DWORD type); | 527 static BOOL WINAPI ConsoleCtrlHandler(DWORD type); |
| 528 | 528 |
| 529 bool check_requirements_; | 529 bool check_requirements_; |
| 530 FilePath user_data_dir_; | 530 base::FilePath user_data_dir_; |
| 531 scoped_ptr<ChromeLauncher> chrome_; | 531 scoped_ptr<ChromeLauncher> chrome_; |
| 532 }; | 532 }; |
| 533 | 533 |
| 534 CloudPrintServiceModule _AtlModule; | 534 CloudPrintServiceModule _AtlModule; |
| 535 | 535 |
| 536 BOOL CloudPrintServiceModule::ConsoleCtrlHandler(DWORD type) { | 536 BOOL CloudPrintServiceModule::ConsoleCtrlHandler(DWORD type) { |
| 537 PostThreadMessage(_AtlModule.m_dwThreadID, WM_QUIT, 0, 0); | 537 PostThreadMessage(_AtlModule.m_dwThreadID, WM_QUIT, 0, 0); |
| 538 return TRUE; | 538 return TRUE; |
| 539 } | 539 } |
| 540 | 540 |
| 541 int main() { | 541 int main() { |
| 542 base::AtExitManager at_exit; | 542 base::AtExitManager at_exit; |
| 543 return _AtlModule.WinMain(0); | 543 return _AtlModule.WinMain(0); |
| 544 } | 544 } |
| OLD | NEW |