2015-06-05 05:49:12 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/common_web_contents_delegate.h"
|
|
|
|
|
2015-06-05 09:01:17 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-06-05 06:55:07 +00:00
|
|
|
#include "atom/browser/atom_javascript_dialog_manager.h"
|
|
|
|
#include "atom/browser/native_window.h"
|
|
|
|
#include "atom/browser/ui/file_dialog.h"
|
2015-06-05 07:12:38 +00:00
|
|
|
#include "atom/browser/web_dialog_helper.h"
|
2015-06-08 05:19:56 +00:00
|
|
|
#include "base/files/file_util.h"
|
2015-06-13 08:02:16 +00:00
|
|
|
#include "chrome/browser/printing/print_preview_message_handler.h"
|
2015-06-13 13:23:45 +00:00
|
|
|
#include "chrome/browser/printing/print_view_manager_basic.h"
|
2015-06-05 07:12:38 +00:00
|
|
|
#include "chrome/browser/ui/browser_dialogs.h"
|
2015-06-22 02:00:33 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2015-06-05 06:55:07 +00:00
|
|
|
#include "content/public/browser/child_process_security_policy.h"
|
|
|
|
#include "content/public/browser/render_process_host.h"
|
|
|
|
#include "content/public/browser/render_view_host.h"
|
|
|
|
#include "storage/browser/fileapi/isolated_context.h"
|
|
|
|
|
2015-06-22 02:00:33 +00:00
|
|
|
using content::BrowserThread;
|
|
|
|
|
2015-06-05 05:49:12 +00:00
|
|
|
namespace atom {
|
|
|
|
|
2015-06-05 06:55:07 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct FileSystem {
|
|
|
|
FileSystem() {
|
|
|
|
}
|
|
|
|
FileSystem(const std::string& file_system_name,
|
|
|
|
const std::string& root_url,
|
|
|
|
const std::string& file_system_path)
|
|
|
|
: file_system_name(file_system_name),
|
|
|
|
root_url(root_url),
|
|
|
|
file_system_path(file_system_path) {
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string file_system_name;
|
|
|
|
std::string root_url;
|
|
|
|
std::string file_system_path;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string RegisterFileSystem(content::WebContents* web_contents,
|
|
|
|
const base::FilePath& path,
|
|
|
|
std::string* registered_name) {
|
|
|
|
auto isolated_context = storage::IsolatedContext::GetInstance();
|
|
|
|
std::string file_system_id = isolated_context->RegisterFileSystemForPath(
|
|
|
|
storage::kFileSystemTypeNativeLocal,
|
|
|
|
std::string(),
|
|
|
|
path,
|
|
|
|
registered_name);
|
|
|
|
|
|
|
|
content::ChildProcessSecurityPolicy* policy =
|
|
|
|
content::ChildProcessSecurityPolicy::GetInstance();
|
|
|
|
content::RenderViewHost* render_view_host = web_contents->GetRenderViewHost();
|
|
|
|
int renderer_id = render_view_host->GetProcess()->GetID();
|
|
|
|
policy->GrantReadFileSystem(renderer_id, file_system_id);
|
|
|
|
policy->GrantWriteFileSystem(renderer_id, file_system_id);
|
|
|
|
policy->GrantCreateFileForFileSystem(renderer_id, file_system_id);
|
|
|
|
policy->GrantDeleteFromFileSystem(renderer_id, file_system_id);
|
|
|
|
|
|
|
|
if (!policy->CanReadFile(renderer_id, path))
|
|
|
|
policy->GrantReadFile(renderer_id, path);
|
|
|
|
|
|
|
|
return file_system_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSystem CreateFileSystemStruct(
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
const std::string& file_system_id,
|
|
|
|
const std::string& registered_name,
|
|
|
|
const std::string& file_system_path) {
|
|
|
|
const GURL origin = web_contents->GetURL().GetOrigin();
|
|
|
|
std::string file_system_name =
|
|
|
|
storage::GetIsolatedFileSystemName(origin, file_system_id);
|
|
|
|
std::string root_url = storage::GetIsolatedFileSystemRootURIString(
|
|
|
|
origin, file_system_id, registered_name);
|
|
|
|
return FileSystem(file_system_name, root_url, file_system_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
base::DictionaryValue* CreateFileSystemValue(const FileSystem& file_system) {
|
|
|
|
base::DictionaryValue* file_system_value = new base::DictionaryValue();
|
|
|
|
file_system_value->SetString("fileSystemName", file_system.file_system_name);
|
|
|
|
file_system_value->SetString("rootURL", file_system.root_url);
|
|
|
|
file_system_value->SetString("fileSystemPath", file_system.file_system_path);
|
|
|
|
return file_system_value;
|
|
|
|
}
|
|
|
|
|
2015-06-22 02:00:33 +00:00
|
|
|
void WriteToFile(const base::FilePath& path,
|
|
|
|
const std::string& content) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
|
|
|
|
DCHECK(!path.empty());
|
|
|
|
|
|
|
|
base::WriteFile(path, content.data(), content.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendToFile(const base::FilePath& path,
|
|
|
|
const std::string& content) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
|
|
|
|
DCHECK(!path.empty());
|
|
|
|
|
|
|
|
base::AppendToFile(path, content.data(), content.size());
|
|
|
|
}
|
|
|
|
|
2015-06-05 06:55:07 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-06-05 05:49:12 +00:00
|
|
|
CommonWebContentsDelegate::CommonWebContentsDelegate(bool is_guest)
|
2015-06-05 06:55:07 +00:00
|
|
|
: is_guest_(is_guest),
|
2015-06-05 09:27:24 +00:00
|
|
|
owner_window_(nullptr),
|
|
|
|
html_fullscreen_(false),
|
|
|
|
native_fullscreen_(false) {
|
2015-06-05 05:49:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CommonWebContentsDelegate::~CommonWebContentsDelegate() {
|
|
|
|
}
|
|
|
|
|
2015-06-05 06:55:07 +00:00
|
|
|
void CommonWebContentsDelegate::InitWithWebContents(
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
NativeWindow* owner_window) {
|
|
|
|
owner_window_ = owner_window;
|
|
|
|
web_contents->SetDelegate(this);
|
|
|
|
|
2015-06-13 13:23:45 +00:00
|
|
|
printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
|
2015-06-13 08:02:16 +00:00
|
|
|
printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
|
|
|
|
|
2015-06-05 06:55:07 +00:00
|
|
|
// Create InspectableWebContents.
|
|
|
|
web_contents_.reset(brightray::InspectableWebContents::Create(web_contents));
|
|
|
|
web_contents_->SetDelegate(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommonWebContentsDelegate::DestroyWebContents() {
|
|
|
|
web_contents_.reset();
|
|
|
|
}
|
|
|
|
|
2015-06-05 09:27:24 +00:00
|
|
|
content::WebContents* CommonWebContentsDelegate::GetWebContents() const {
|
|
|
|
if (!web_contents_)
|
|
|
|
return nullptr;
|
|
|
|
return web_contents_->GetWebContents();
|
|
|
|
}
|
|
|
|
|
|
|
|
content::WebContents*
|
|
|
|
CommonWebContentsDelegate::GetDevToolsWebContents() const {
|
|
|
|
if (!web_contents_)
|
|
|
|
return nullptr;
|
|
|
|
return web_contents_->GetDevToolsWebContents();
|
|
|
|
}
|
|
|
|
|
2015-06-05 09:45:17 +00:00
|
|
|
content::WebContents* CommonWebContentsDelegate::OpenURLFromTab(
|
|
|
|
content::WebContents* source,
|
|
|
|
const content::OpenURLParams& params) {
|
|
|
|
content::NavigationController::LoadURLParams load_url_params(params.url);
|
|
|
|
load_url_params.referrer = params.referrer;
|
|
|
|
load_url_params.transition_type = params.transition;
|
|
|
|
load_url_params.extra_headers = params.extra_headers;
|
|
|
|
load_url_params.should_replace_current_entry =
|
|
|
|
params.should_replace_current_entry;
|
|
|
|
load_url_params.is_renderer_initiated = params.is_renderer_initiated;
|
|
|
|
load_url_params.transferred_global_request_id =
|
|
|
|
params.transferred_global_request_id;
|
|
|
|
load_url_params.should_clear_history_list = true;
|
|
|
|
|
|
|
|
source->GetController().LoadURLWithParams(load_url_params);
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
2015-06-05 07:12:38 +00:00
|
|
|
void CommonWebContentsDelegate::RequestToLockMouse(
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
bool user_gesture,
|
|
|
|
bool last_unlocked_by_target) {
|
|
|
|
GetWebContents()->GotResponseToLockMouseRequest(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CommonWebContentsDelegate::CanOverscrollContent() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CommonWebContentsDelegate::IsPopupOrPanel(
|
|
|
|
const content::WebContents* source) const {
|
|
|
|
return !is_guest_;
|
|
|
|
}
|
|
|
|
|
2015-06-05 06:55:07 +00:00
|
|
|
content::JavaScriptDialogManager*
|
|
|
|
CommonWebContentsDelegate::GetJavaScriptDialogManager(
|
|
|
|
content::WebContents* source) {
|
|
|
|
if (!dialog_manager_)
|
|
|
|
dialog_manager_.reset(new AtomJavaScriptDialogManager);
|
|
|
|
|
|
|
|
return dialog_manager_.get();
|
|
|
|
}
|
|
|
|
|
2015-06-05 07:12:38 +00:00
|
|
|
content::ColorChooser* CommonWebContentsDelegate::OpenColorChooser(
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
SkColor color,
|
|
|
|
const std::vector<content::ColorSuggestion>& suggestions) {
|
|
|
|
return chrome::ShowColorChooser(web_contents, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommonWebContentsDelegate::RunFileChooser(
|
|
|
|
content::WebContents* guest,
|
|
|
|
const content::FileChooserParams& params) {
|
|
|
|
if (!web_dialog_helper_)
|
|
|
|
web_dialog_helper_.reset(new WebDialogHelper(owner_window_));
|
|
|
|
web_dialog_helper_->RunFileChooser(guest, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommonWebContentsDelegate::EnumerateDirectory(content::WebContents* guest,
|
|
|
|
int request_id,
|
|
|
|
const base::FilePath& path) {
|
|
|
|
if (!web_dialog_helper_)
|
|
|
|
web_dialog_helper_.reset(new WebDialogHelper(owner_window_));
|
|
|
|
web_dialog_helper_->EnumerateDirectory(guest, request_id, path);
|
|
|
|
}
|
|
|
|
|
2015-06-05 09:27:24 +00:00
|
|
|
void CommonWebContentsDelegate::EnterFullscreenModeForTab(
|
|
|
|
content::WebContents* source, const GURL& origin) {
|
|
|
|
if (!owner_window_)
|
|
|
|
return;
|
|
|
|
SetHtmlApiFullscreen(true);
|
|
|
|
owner_window_->NotifyWindowEnterHtmlFullScreen();
|
|
|
|
source->GetRenderViewHost()->WasResized();
|
2015-06-05 06:55:07 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 09:27:24 +00:00
|
|
|
void CommonWebContentsDelegate::ExitFullscreenModeForTab(
|
|
|
|
content::WebContents* source) {
|
|
|
|
if (!owner_window_)
|
|
|
|
return;
|
|
|
|
SetHtmlApiFullscreen(false);
|
|
|
|
owner_window_->NotifyWindowLeaveHtmlFullScreen();
|
|
|
|
source->GetRenderViewHost()->WasResized();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CommonWebContentsDelegate::IsFullscreenForTabOrPending(
|
|
|
|
const content::WebContents* source) const {
|
|
|
|
return html_fullscreen_;
|
2015-06-05 06:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommonWebContentsDelegate::DevToolsSaveToFile(
|
|
|
|
const std::string& url, const std::string& content, bool save_as) {
|
|
|
|
base::FilePath path;
|
|
|
|
PathsMap::iterator it = saved_files_.find(url);
|
|
|
|
if (it != saved_files_.end() && !save_as) {
|
|
|
|
path = it->second;
|
|
|
|
} else {
|
|
|
|
file_dialog::Filters filters;
|
|
|
|
base::FilePath default_path(base::FilePath::FromUTF8Unsafe(url));
|
|
|
|
if (!file_dialog::ShowSaveDialog(owner_window_, url, default_path,
|
|
|
|
filters, &path)) {
|
|
|
|
base::StringValue url_value(url);
|
|
|
|
web_contents_->CallClientFunction(
|
|
|
|
"DevToolsAPI.canceledSaveURL", &url_value, nullptr, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
saved_files_[url] = path;
|
2015-06-22 02:00:33 +00:00
|
|
|
BrowserThread::PostTaskAndReply(
|
|
|
|
BrowserThread::FILE, FROM_HERE,
|
|
|
|
base::Bind(&WriteToFile, path, content),
|
|
|
|
base::Bind(&CommonWebContentsDelegate::OnDevToolsSaveToFile,
|
|
|
|
base::Unretained(this), url));
|
2015-06-05 06:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommonWebContentsDelegate::DevToolsAppendToFile(
|
|
|
|
const std::string& url, const std::string& content) {
|
|
|
|
PathsMap::iterator it = saved_files_.find(url);
|
|
|
|
if (it == saved_files_.end())
|
|
|
|
return;
|
|
|
|
|
2015-06-22 02:00:33 +00:00
|
|
|
BrowserThread::PostTaskAndReply(
|
|
|
|
BrowserThread::FILE, FROM_HERE,
|
|
|
|
base::Bind(&AppendToFile, it->second, content),
|
|
|
|
base::Bind(&CommonWebContentsDelegate::OnDevToolsAppendToFile,
|
|
|
|
base::Unretained(this), url));
|
2015-06-05 06:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommonWebContentsDelegate::DevToolsAddFileSystem() {
|
|
|
|
file_dialog::Filters filters;
|
|
|
|
base::FilePath default_path;
|
|
|
|
std::vector<base::FilePath> paths;
|
|
|
|
int flag = file_dialog::FILE_DIALOG_OPEN_DIRECTORY;
|
|
|
|
if (!file_dialog::ShowOpenDialog(owner_window_, "", default_path,
|
|
|
|
filters, flag, &paths))
|
|
|
|
return;
|
|
|
|
|
|
|
|
base::FilePath path = paths[0];
|
|
|
|
std::string registered_name;
|
|
|
|
std::string file_system_id = RegisterFileSystem(GetDevToolsWebContents(),
|
|
|
|
path,
|
|
|
|
®istered_name);
|
|
|
|
|
|
|
|
WorkspaceMap::iterator it = saved_paths_.find(file_system_id);
|
|
|
|
if (it != saved_paths_.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
saved_paths_[file_system_id] = path;
|
|
|
|
|
|
|
|
FileSystem file_system = CreateFileSystemStruct(GetDevToolsWebContents(),
|
|
|
|
file_system_id,
|
|
|
|
registered_name,
|
|
|
|
path.AsUTF8Unsafe());
|
|
|
|
|
|
|
|
scoped_ptr<base::StringValue> error_string_value(
|
|
|
|
new base::StringValue(std::string()));
|
|
|
|
scoped_ptr<base::DictionaryValue> file_system_value;
|
|
|
|
if (!file_system.file_system_path.empty())
|
|
|
|
file_system_value.reset(CreateFileSystemValue(file_system));
|
|
|
|
web_contents_->CallClientFunction(
|
|
|
|
"DevToolsAPI.fileSystemAdded",
|
|
|
|
error_string_value.get(),
|
|
|
|
file_system_value.get(),
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommonWebContentsDelegate::DevToolsRemoveFileSystem(
|
|
|
|
const std::string& file_system_path) {
|
|
|
|
if (!web_contents_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system_path);
|
|
|
|
storage::IsolatedContext::GetInstance()->RevokeFileSystemByPath(path);
|
|
|
|
|
|
|
|
for (auto it = saved_paths_.begin(); it != saved_paths_.end(); ++it)
|
|
|
|
if (it->second == path) {
|
|
|
|
saved_paths_.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::StringValue file_system_path_value(file_system_path);
|
|
|
|
web_contents_->CallClientFunction(
|
|
|
|
"DevToolsAPI.fileSystemRemoved",
|
|
|
|
&file_system_path_value,
|
|
|
|
nullptr,
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
|
2015-06-22 02:00:33 +00:00
|
|
|
void CommonWebContentsDelegate::OnDevToolsSaveToFile(
|
|
|
|
const std::string& url) {
|
|
|
|
// Notify DevTools.
|
|
|
|
base::StringValue url_value(url);
|
|
|
|
web_contents_->CallClientFunction(
|
|
|
|
"DevToolsAPI.savedURL", &url_value, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommonWebContentsDelegate::OnDevToolsAppendToFile(
|
|
|
|
const std::string& url) {
|
|
|
|
// Notify DevTools.
|
|
|
|
base::StringValue url_value(url);
|
|
|
|
web_contents_->CallClientFunction(
|
|
|
|
"DevToolsAPI.appendedToURL", &url_value, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
2015-06-05 09:27:24 +00:00
|
|
|
void CommonWebContentsDelegate::SetHtmlApiFullscreen(bool enter_fullscreen) {
|
|
|
|
// Window is already in fullscreen mode, save the state.
|
|
|
|
if (enter_fullscreen && owner_window_->IsFullscreen()) {
|
|
|
|
native_fullscreen_ = true;
|
|
|
|
html_fullscreen_ = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit html fullscreen state but not window's fullscreen mode.
|
|
|
|
if (!enter_fullscreen && native_fullscreen_) {
|
|
|
|
html_fullscreen_ = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
owner_window_->SetFullScreen(enter_fullscreen);
|
|
|
|
html_fullscreen_ = enter_fullscreen;
|
|
|
|
native_fullscreen_ = false;
|
|
|
|
}
|
|
|
|
|
2015-06-05 05:49:12 +00:00
|
|
|
} // namespace atom
|