forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter_installer.cc
66 lines (52 loc) · 2.1 KB
/
printer_installer.cc
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
66
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/services/cups_proxy/printer_installer.h"
#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/services/cups_proxy/public/cpp/cups_util.h"
#include "chromeos/printing/printer_configuration.h"
namespace cups_proxy {
PrinterInstaller::PrinterInstaller(CupsProxyServiceDelegate* const delegate)
: delegate_(delegate) {}
PrinterInstaller::~PrinterInstaller() = default;
void PrinterInstaller::InstallPrinter(std::string printer_id,
InstallPrinterCallback cb) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto printer = delegate_->GetPrinter(printer_id);
if (!printer) {
// If the requested printer DNE, we proxy to CUPSd and allow it to
// handle the error.
Finish(std::move(cb), InstallPrinterResult::kUnknownPrinterFound);
return;
}
if (delegate_->IsPrinterInstalled(*printer)) {
Finish(std::move(cb), InstallPrinterResult::kSuccess);
return;
}
// Install printer.
delegate_->SetupPrinter(
*printer,
base::BindOnce(&PrinterInstaller::OnInstallPrinter,
weak_factory_.GetWeakPtr(), std::move(cb), *printer));
}
void PrinterInstaller::OnInstallPrinter(InstallPrinterCallback cb,
const chromeos::Printer& printer,
bool success) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Finish(std::move(cb),
success ? InstallPrinterResult::kSuccess
: InstallPrinterResult::kPrinterInstallationFailure);
}
void PrinterInstaller::Finish(InstallPrinterCallback cb,
InstallPrinterResult res) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(cb), res));
}
} // namespace cups_proxy