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
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83

ash / webui / camera_app_ui / document_scanner_installer.cc [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/webui/camera_app_ui/document_scanner_installer.h"

#include "ash/webui/camera_app_ui/document_scanner_service_client.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/task/single_thread_task_runner.h"

namespace ash {

namespace {

constexpr char kDocumentScannerDlcId[] = "cros-camera-document-scanner-dlc";

}  // namespace

// static
DocumentScannerInstaller* DocumentScannerInstaller::GetInstance() {
  return base::Singleton<DocumentScannerInstaller>::get();
}

DocumentScannerInstaller::~DocumentScannerInstaller() = default;

void DocumentScannerInstaller::RegisterLibraryPathCallback(
    LibraryPathCallback callback) {
  base::AutoLock auto_lock(library_path_lock_);
  if (library_path_.empty()) {
    if (!installing_) {
      ui_task_runner_->PostTask(
          FROM_HERE, base::BindOnce(&DocumentScannerInstaller::TriggerInstall,
                                    base::Unretained(this)));
    }
    library_path_callbacks_.push_back(std::move(callback));
  } else {
    std::move(callback).Run(library_path_);
  }
}

void DocumentScannerInstaller::TriggerInstall() {
  DCHECK(ui_task_runner_->RunsTasksInCurrentSequence());

  if (!DocumentScannerServiceClient::IsSupportedByDlc()) {
    return;
  }

  base::AutoLock auto_lock(library_path_lock_);
  if (installing_) {
    return;
  }
  installing_ = true;

  dlcservice::InstallRequest install_request;
  install_request.set_id(kDocumentScannerDlcId);
  DlcserviceClient::Get()->Install(
      install_request,
      base::BindOnce(&DocumentScannerInstaller::OnInstalled,
                     base::Unretained(this)),
      base::DoNothing());
}

DocumentScannerInstaller::DocumentScannerInstaller()
    : ui_task_runner_(base::SingleThreadTaskRunner::GetCurrentDefault()) {}

void DocumentScannerInstaller::OnInstalled(
    const DlcserviceClient::InstallResult& install_result) {
  base::AutoLock auto_lock(library_path_lock_);
  if (install_result.error == dlcservice::kErrorNone) {
    library_path_ = install_result.root_path;
  } else {
    LOG(ERROR) << "Failed to install document scanner DLC: "
               << install_result.error;
  }
  for (auto& callback : library_path_callbacks_) {
    std::move(callback).Run(library_path_);
  }
  library_path_callbacks_.clear();
  installing_ = false;
}

}  // namespace ash