Commit 218c3921 authored by Malay Keshav's avatar Malay Keshav Committed by Commit Bot

Implement a URLDataSource to load videos from disk for OOBE

This patch implements a URLDataSource that loads oobe video assets from
disk based on the provided chrome URL. It also installs the same for
the oobe web ui.

Bug: 878594
Change-Id: I443144d4e5ab65d313c0e56d8b988a9cb05e6109
Component: Video source, oobe
Reviewed-on: https://chromium-review.googlesource.com/1196145Reviewed-by: default avatarMichael Giuffrida <michaelpg@chromium.org>
Commit-Queue: Malay Keshav <malaykeshav@chromium.org>
Cr-Commit-Position: refs/heads/master@{#587889}
parent fb380be6
...@@ -2089,6 +2089,8 @@ jumbo_split_static_library("ui") { ...@@ -2089,6 +2089,8 @@ jumbo_split_static_library("ui") {
"webui/chromeos/system_web_dialog_delegate.h", "webui/chromeos/system_web_dialog_delegate.h",
"webui/chromeos/user_image_source.cc", "webui/chromeos/user_image_source.cc",
"webui/chromeos/user_image_source.h", "webui/chromeos/user_image_source.h",
"webui/chromeos/video_source.cc",
"webui/chromeos/video_source.h",
"webui/extensions/chromeos/kiosk_apps_handler.cc", "webui/extensions/chromeos/kiosk_apps_handler.cc",
"webui/extensions/chromeos/kiosk_apps_handler.h", "webui/extensions/chromeos/kiosk_apps_handler.h",
"webui/help/help_utils_chromeos.cc", "webui/help/help_utils_chromeos.cc",
......
...@@ -77,6 +77,7 @@ ...@@ -77,6 +77,7 @@
#include "chrome/browser/ui/webui/chromeos/login/welcome_screen_handler.h" #include "chrome/browser/ui/webui/chromeos/login/welcome_screen_handler.h"
#include "chrome/browser/ui/webui/chromeos/login/wrong_hwid_screen_handler.h" #include "chrome/browser/ui/webui/chromeos/login/wrong_hwid_screen_handler.h"
#include "chrome/browser/ui/webui/chromeos/user_image_source.h" #include "chrome/browser/ui/webui/chromeos/user_image_source.h"
#include "chrome/browser/ui/webui/chromeos/video_source.h"
#include "chrome/browser/ui/webui/test_files_request_filter.h" #include "chrome/browser/ui/webui/test_files_request_filter.h"
#include "chrome/browser/ui/webui/theme_source.h" #include "chrome/browser/ui/webui/theme_source.h"
#include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_constants.h"
...@@ -447,6 +448,9 @@ void OobeUI::ConfigureOobeDisplay() { ...@@ -447,6 +448,9 @@ void OobeUI::ConfigureOobeDisplay() {
content::WebContents* contents = web_ui()->GetWebContents(); content::WebContents* contents = web_ui()->GetWebContents();
extensions::TabHelper::CreateForWebContents(contents); extensions::TabHelper::CreateForWebContents(contents);
// // Handler for the oobe video assets which will be shown if available.
content::URLDataSource::Add(profile, new chromeos::VideoSource());
if (IsRemoraRequisitioned()) if (IsRemoraRequisitioned())
oobe_display_chooser_ = std::make_unique<OobeDisplayChooser>(); oobe_display_chooser_ = std::make_unique<OobeDisplayChooser>();
} }
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/chromeos/video_source.h"
#include <vector>
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/location.h"
#include "base/memory/ref_counted_memory.h"
#include "base/sequenced_task_runner.h"
#include "base/single_thread_task_runner.h"
#include "base/task/post_task.h"
#include "base/task/task_scheduler/task_scheduler.h"
#include "base/task_runner_util.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/common/url_constants.h"
#include "net/base/mime_util.h"
namespace chromeos {
namespace {
const char* kWhitelistedDirectory = "oobe_videos";
bool IsWhitelisted(const std::string& path) {
base::FilePath file_path(path);
if (file_path.ReferencesParent())
return false;
// Check if the path starts with a whitelisted directory.
std::vector<std::string> components;
file_path.GetComponents(&components);
if (components.empty())
return false;
return components[0] == kWhitelistedDirectory;
}
// Callback for user_manager::UserImageLoader.
void VideoLoaded(
const content::URLDataSource::GotDataCallback& got_data_callback,
std::unique_ptr<std::string> video_data,
bool did_load_file) {
if (video_data->size() && did_load_file) {
got_data_callback.Run(new base::RefCountedBytes(
reinterpret_cast<const unsigned char*>(video_data->data()),
video_data->size()));
} else {
got_data_callback.Run(nullptr);
}
}
} // namespace
VideoSource::VideoSource() : weak_factory_(this) {
task_runner_ = base::CreateSequencedTaskRunnerWithTraits(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN});
}
VideoSource::~VideoSource() {}
std::string VideoSource::GetSource() const {
return chrome::kChromeOSAssetHost;
}
void VideoSource::StartDataRequest(
const std::string& path,
const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
const content::URLDataSource::GotDataCallback& got_data_callback) {
if (!IsWhitelisted(path)) {
got_data_callback.Run(nullptr);
return;
}
const base::FilePath asset_dir(chrome::kChromeOSAssetPath);
const base::FilePath video_path = asset_dir.AppendASCII(path);
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
base::BindOnce(&base::PathExists, video_path),
base::BindOnce(&VideoSource::StartDataRequestAfterPathExists,
weak_factory_.GetWeakPtr(), video_path,
got_data_callback));
}
std::string VideoSource::GetMimeType(const std::string& path) const {
std::string mime_type;
std::string ext = base::FilePath(path).Extension();
if (!ext.empty())
net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type);
return mime_type;
}
void VideoSource::StartDataRequestAfterPathExists(
const base::FilePath& video_path,
const content::URLDataSource::GotDataCallback& got_data_callback,
bool path_exists) {
if (path_exists) {
auto video_data = std::make_unique<std::string>();
std::string* data = video_data.get();
base::PostTaskAndReplyWithResult(
task_runner_.get(), FROM_HERE,
base::BindOnce(&base::ReadFileToString, video_path, data),
base::BindOnce(&VideoLoaded, got_data_callback, std::move(video_data)));
} else {
got_data_callback.Run(nullptr);
}
}
} // namespace chromeos
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_WEBUI_CHROMEOS_VIDEO_SOURCE_H_
#define CHROME_BROWSER_UI_WEBUI_CHROMEOS_VIDEO_SOURCE_H_
#include <string>
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/url_data_source.h"
namespace base {
class FilePath;
class SequencedTaskRunner;
} // namespace base
namespace chromeos {
// Data source that reads videos from the file system.
// It provides resources from chrome urls of type:
// chrome://chromeos-asset/<whitelisted directories>/*.webm
class VideoSource : public content::URLDataSource {
public:
VideoSource();
~VideoSource() override;
// content::URLDataSource:
std::string GetSource() const override;
void StartDataRequest(
const std::string& path,
const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
const content::URLDataSource::GotDataCallback& got_data_callback)
override;
std::string GetMimeType(const std::string& path) const override;
private:
// Continuation from StartDataRequest().
void StartDataRequestAfterPathExists(
const base::FilePath& video_path,
const content::URLDataSource::GotDataCallback& got_data_callback,
bool path_exists);
// The background task runner on which file I/O is performed.
scoped_refptr<base::SequencedTaskRunner> task_runner_;
base::WeakPtrFactory<VideoSource> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(VideoSource);
};
} // namespace chromeos
#endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_VIDEO_SOURCE_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment