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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/webapps/browser/launch_queue/launch_queue.h"
#include <memory>
#include <utility>
#include <vector>
#include "base/check_is_test.h"
#include "base/files/file_path.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "components/webapps/browser/launch_queue/launch_queue_delegate.h"
#include "content/public/browser/file_system_access_entry_factory.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/file_system_access/file_system_access_directory_handle.mojom.h"
#include "third_party/blink/public/mojom/web_launch/web_launch.mojom.h"
#include "url/origin.h"
namespace webapps {
namespace {
class EntriesBuilder {
public:
EntriesBuilder(content::WebContents* web_contents,
const GURL& launch_url,
size_t expected_number_of_entries)
: entry_factory_(web_contents->GetPrimaryMainFrame()
->GetProcess()
->GetStoragePartition()
->GetFileSystemAccessEntryFactory()),
context_(blink::StorageKey::CreateFirstParty(
url::Origin::Create(launch_url)),
launch_url,
content::GlobalRenderFrameHostId(
web_contents->GetPrimaryMainFrame()
->GetProcess()
->GetDeprecatedID(),
web_contents->GetPrimaryMainFrame()->GetRoutingID())) {
entries_.reserve(expected_number_of_entries);
}
void AddFileEntry(const content::PathInfo& path_info) {
entries_.push_back(entry_factory_->CreateFileEntryFromPath(
context_, path_info,
content::FileSystemAccessEntryFactory::UserAction::kSave));
}
void AddDirectoryEntry(const content::PathInfo& path_info) {
entries_.push_back(entry_factory_->CreateDirectoryEntryFromPath(
context_, path_info,
content::FileSystemAccessEntryFactory::UserAction::kOpen));
}
std::vector<blink::mojom::FileSystemAccessEntryPtr> Build() {
return std::move(entries_);
}
private:
std::vector<blink::mojom::FileSystemAccessEntryPtr> entries_;
scoped_refptr<content::FileSystemAccessEntryFactory> entry_factory_;
content::FileSystemAccessEntryFactory::BindingContext context_;
};
} // namespace
LaunchQueue::LaunchQueue(content::WebContents* web_contents,
std::unique_ptr<LaunchQueueDelegate> delegate)
: content::WebContentsObserver(web_contents),
delegate_(std::move(delegate)) {}
LaunchQueue::~LaunchQueue() = default;
void LaunchQueue::Enqueue(LaunchParams launch_params) {
DCHECK(delegate_->IsInScope(launch_params, launch_params.target_url))
<< launch_params.target_url.spec();
DCHECK(delegate_->IsValidLaunchParams(launch_params));
// Drop the existing queue state if a new launch navigation was started.
if (launch_params.started_new_navigation) {
Reset();
queue_.push_back(std::move(launch_params));
pending_navigation_ = true;
return;
}
if (!queue_.empty()) {
DCHECK_EQ(launch_params.app_id, queue_.front().app_id);
}
queue_.push_back(std::move(launch_params));
if (!pending_navigation_) {
SendQueuedLaunchParams(web_contents()->GetLastCommittedURL());
}
}
void LaunchQueue::FlushForTesting() const {
CHECK_IS_TEST();
mojo::AssociatedRemote<blink::mojom::WebLaunchService> launch_service;
web_contents()
->GetPrimaryMainFrame()
->GetRemoteAssociatedInterfaces()
->GetInterface(&launch_service);
launch_service.FlushForTesting(); // IN-TEST
}
void LaunchQueue::Reset() {
queue_.clear();
pending_navigation_ = false;
last_sent_queued_launch_params_.reset();
}
const webapps::AppId* LaunchQueue::GetPendingLaunchAppId() const {
if (queue_.empty()) {
return nullptr;
}
return &(queue_.front().app_id);
}
void LaunchQueue::DidFinishNavigation(content::NavigationHandle* handle) {
// Currently, launch data is only sent the primary main frame.
if (!handle->IsInPrimaryMainFrame()) {
return;
}
if (pending_navigation_) {
if (!delegate_->IsInScope(queue_.front(), handle->GetURL())) {
Reset();
return;
}
pending_navigation_ = false;
SendQueuedLaunchParams(handle->GetURL());
return;
}
// Reloads have the last sent launch params re-sent as they may contain live
// file handles that should persist across reloads.
if (last_sent_queued_launch_params_ &&
handle->GetReloadType() != content::ReloadType::NONE) {
if (!delegate_->IsInScope(*last_sent_queued_launch_params_,
handle->GetURL())) {
Reset();
return;
}
// LaunchParams with the `time_navigation_started_for_enqueue` set will be
// resent, but the latency metrics should not be measured, so the time is
// cleared.
last_sent_queued_launch_params_->time_navigation_started_for_enqueue =
base::TimeTicks();
SendLaunchParams(*last_sent_queued_launch_params_, handle->GetURL());
return;
}
// Leaving the document resets all queue state.
if (!handle->IsSameDocument()) {
Reset();
}
}
void LaunchQueue::SendQueuedLaunchParams(const GURL& current_url) {
for (LaunchParams& launch_params : queue_) {
if (&launch_params == &queue_.back()) {
last_sent_queued_launch_params_ = launch_params;
}
SendLaunchParams(std::move(launch_params), current_url);
}
queue_.clear();
}
void LaunchQueue::SendLaunchParams(LaunchParams launch_params,
const GURL& current_url) {
// TODO(dmurph): Figure out why this is failing.
// https://crbug.com/2546057
DCHECK(delegate_->IsInScope(launch_params, current_url))
<< current_url.spec();
mojo::AssociatedRemote<blink::mojom::WebLaunchService> launch_service;
web_contents()
->GetPrimaryMainFrame()
->GetRemoteAssociatedInterfaces()
->GetInterface(&launch_service);
DCHECK(launch_service);
if (!launch_params.paths.empty() || !launch_params.dir.empty()) {
EntriesBuilder entries_builder(web_contents(), launch_params.target_url,
launch_params.paths.size() + 1);
if (!launch_params.dir.empty()) {
entries_builder.AddDirectoryEntry(
delegate_->GetPathInfo(launch_params.dir));
}
for (const auto& path : launch_params.paths) {
entries_builder.AddFileEntry(delegate_->GetPathInfo(path));
}
launch_service->SetLaunchFiles(entries_builder.Build());
} else {
launch_service->EnqueueLaunchParams(
launch_params.target_url,
launch_params.time_navigation_started_for_enqueue,
launch_params.started_new_navigation);
}
}
} // namespace webapps
|