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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
// 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/browser/ash/arc/print_spooler/arc_print_spooler_bridge.h"
#include <utility>
#include "base/files/file_path.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/ash/arc/arc_util.h"
#include "chrome/browser/ash/arc/print_spooler/arc_print_spooler_util.h"
#include "chrome/browser/ash/arc/print_spooler/print_session_impl.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/ash/experiences/arc/arc_browser_context_keyed_service_factory_base.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "net/base/filename_util.h"
#include "ui/aura/window.h"
#include "url/gurl.h"
namespace arc {
namespace {
// Singleton factory for ArcPrintSpoolerBridge.
class ArcPrintSpoolerBridgeFactory
: public internal::ArcBrowserContextKeyedServiceFactoryBase<
ArcPrintSpoolerBridge, ArcPrintSpoolerBridgeFactory> {
public:
// Factory name used by ArcBrowserContextKeyedServiceFactoryBase.
static constexpr const char* kName = "ArcPrintSpoolerBridgeFactory";
static ArcPrintSpoolerBridgeFactory* GetInstance() {
return base::Singleton<ArcPrintSpoolerBridgeFactory>::get();
}
private:
friend base::DefaultSingletonTraits<ArcPrintSpoolerBridgeFactory>;
ArcPrintSpoolerBridgeFactory() = default;
~ArcPrintSpoolerBridgeFactory() override = default;
};
} // namespace
// static
ArcPrintSpoolerBridge* ArcPrintSpoolerBridge::GetForBrowserContext(
content::BrowserContext* context) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return ArcPrintSpoolerBridgeFactory::GetForBrowserContext(context);
}
ArcPrintSpoolerBridge::ArcPrintSpoolerBridge(content::BrowserContext* context,
ArcBridgeService* bridge_service)
: arc_bridge_service_(bridge_service),
profile_(Profile::FromBrowserContext(context)) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
arc_bridge_service_->print_spooler()->SetHost(this);
}
ArcPrintSpoolerBridge::~ArcPrintSpoolerBridge() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
arc_bridge_service_->print_spooler()->SetHost(nullptr);
}
void ArcPrintSpoolerBridge::StartPrintInCustomTab(
mojo::ScopedHandle scoped_handle,
int32_t task_id,
mojo::PendingRemote<mojom::PrintSessionInstance> instance,
StartPrintInCustomTabCallback callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()},
base::BindOnce(&SavePrintDocument, std::move(scoped_handle)),
base::BindOnce(&ArcPrintSpoolerBridge::OnPrintDocumentSaved,
weak_ptr_factory_.GetWeakPtr(), task_id,
std::move(instance), std::move(callback)));
}
void ArcPrintSpoolerBridge::OnPrintDocumentSaved(
int32_t task_id,
mojo::PendingRemote<mojom::PrintSessionInstance> instance,
StartPrintInCustomTabCallback callback,
base::FilePath file_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (file_path.empty()) {
std::move(callback).Run(mojo::NullRemote());
return;
}
DCHECK(file_path.IsAbsolute()) << file_path;
GURL url = net::FilePathToFileURL(file_path);
aura::Window* arc_window = GetArcWindow(task_id);
if (!arc_window) {
LOG(ERROR) << "No ARC window with the specified task ID " << task_id;
std::move(callback).Run(mojo::NullRemote());
return;
}
auto web_contents = CreateArcCustomTabWebContents(profile_, url);
std::move(callback).Run(PrintSessionImpl::Create(
std::move(web_contents), arc_window, std::move(instance)));
}
// static
void ArcPrintSpoolerBridge::EnsureFactoryBuilt() {
ArcPrintSpoolerBridgeFactory::GetInstance();
}
} // namespace arc
|