2012-10-16 21:28:07 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework 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 "cefclient/dialog_test.h"
|
|
|
|
#include "include/cef_browser.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace dialog_test {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
const char kTestUrl[] = "http://tests/dialogs";
|
|
|
|
const char kFileOpenMessageName[] = "DialogTest.FileOpen";
|
|
|
|
const char kFileOpenMultipleMessageName[] = "DialogTest.FileOpenMultiple";
|
2015-01-20 19:24:54 +01:00
|
|
|
const char kFileOpenFolderMessageName[] = "DialogTest.FileOpenFolder";
|
2014-01-28 00:31:03 +01:00
|
|
|
const char kFileSaveMessageName[] = "DialogTest.FileSave";
|
2012-10-16 21:28:07 +02:00
|
|
|
|
2015-01-20 19:24:54 +01:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#define PATH_SEP '\\'
|
|
|
|
#else
|
|
|
|
#define PATH_SEP '/'
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Store persistent dialog state information.
|
|
|
|
class DialogState : public base::RefCountedThreadSafe<DialogState> {
|
|
|
|
public:
|
|
|
|
DialogState()
|
|
|
|
: mode_(FILE_DIALOG_OPEN),
|
|
|
|
last_selected_filter_(0),
|
|
|
|
pending_(false) {}
|
|
|
|
|
|
|
|
cef_file_dialog_mode_t mode_;
|
|
|
|
int last_selected_filter_;
|
|
|
|
CefString last_file_;
|
|
|
|
bool pending_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(DialogState);
|
|
|
|
};
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
// Callback executed when the file dialog is dismissed.
|
|
|
|
class DialogCallback : public CefRunFileDialogCallback {
|
2012-10-16 21:28:07 +02:00
|
|
|
public:
|
2015-01-20 19:24:54 +01:00
|
|
|
DialogCallback(
|
|
|
|
CefRefPtr<CefMessageRouterBrowserSide::Callback> router_callback,
|
|
|
|
scoped_refptr<DialogState> dialog_state)
|
|
|
|
: router_callback_(router_callback),
|
|
|
|
dialog_state_(dialog_state) {
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnFileDialogDismissed(
|
2015-01-20 19:24:54 +01:00
|
|
|
int last_selected_filter,
|
2012-10-16 21:28:07 +02:00
|
|
|
const std::vector<CefString>& file_paths) OVERRIDE {
|
2015-01-20 19:24:54 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
DCHECK(dialog_state_->pending_);
|
|
|
|
|
|
|
|
if (!file_paths.empty()) {
|
|
|
|
if (dialog_state_->mode_ != FILE_DIALOG_OPEN_FOLDER)
|
|
|
|
dialog_state_->last_selected_filter_ = last_selected_filter;
|
|
|
|
|
|
|
|
dialog_state_->last_file_ = file_paths[0];
|
|
|
|
if (dialog_state_->mode_ == FILE_DIALOG_OPEN_FOLDER) {
|
|
|
|
std::string last_file = dialog_state_->last_file_;
|
|
|
|
if (last_file[last_file.length() - 1] != PATH_SEP) {
|
|
|
|
// Add a trailing slash so we know it's a directory. Otherwise, file
|
|
|
|
// dialogs will think the last path component is a file name.
|
|
|
|
last_file += PATH_SEP;
|
|
|
|
dialog_state_->last_file_ = last_file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
// Send a message back to the render process with the list of file paths.
|
|
|
|
std::string response;
|
|
|
|
for (int i = 0; i < static_cast<int>(file_paths.size()); ++i) {
|
|
|
|
if (!response.empty())
|
|
|
|
response += "|"; // Use a delimiter disallowed in file paths.
|
|
|
|
response += file_paths[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
router_callback_->Success(response);
|
|
|
|
router_callback_ = NULL;
|
2015-01-20 19:24:54 +01:00
|
|
|
|
|
|
|
dialog_state_->pending_ = false;
|
|
|
|
dialog_state_ = NULL;
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-01-28 00:31:03 +01:00
|
|
|
CefRefPtr<CefMessageRouterBrowserSide::Callback> router_callback_;
|
2015-01-20 19:24:54 +01:00
|
|
|
scoped_refptr<DialogState> dialog_state_;
|
2012-10-16 21:28:07 +02:00
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
IMPLEMENT_REFCOUNTING(DialogCallback);
|
2015-01-20 19:24:54 +01:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(DialogCallback);
|
2012-10-16 21:28:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Handle messages in the browser process.
|
2014-01-28 00:31:03 +01:00
|
|
|
class Handler : public CefMessageRouterBrowserSide::Handler {
|
2012-10-16 21:28:07 +02:00
|
|
|
public:
|
2014-01-28 00:31:03 +01:00
|
|
|
Handler() {}
|
|
|
|
|
|
|
|
// Called due to cefQuery execution in dialogs.html.
|
|
|
|
virtual bool OnQuery(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
int64 query_id,
|
|
|
|
const CefString& request,
|
|
|
|
bool persistent,
|
|
|
|
CefRefPtr<Callback> callback) OVERRIDE {
|
2015-01-20 19:24:54 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
// Only handle messages from the test URL.
|
|
|
|
const std::string& url = frame->GetURL();
|
2012-10-16 21:28:07 +02:00
|
|
|
if (url.find(kTestUrl) != 0)
|
|
|
|
return false;
|
|
|
|
|
2015-01-20 19:24:54 +01:00
|
|
|
if (!dialog_state_.get())
|
|
|
|
dialog_state_ = new DialogState;
|
|
|
|
|
|
|
|
// Make sure we're only running one dialog at a time.
|
|
|
|
DCHECK(!dialog_state_->pending_);
|
2012-10-16 21:28:07 +02:00
|
|
|
|
2015-01-20 19:24:54 +01:00
|
|
|
std::vector<CefString> accept_filters;
|
|
|
|
std::string title;
|
2014-01-28 00:31:03 +01:00
|
|
|
|
|
|
|
const std::string& message_name = request;
|
2012-10-16 21:28:07 +02:00
|
|
|
if (message_name == kFileOpenMessageName) {
|
2015-01-20 19:24:54 +01:00
|
|
|
dialog_state_->mode_ = FILE_DIALOG_OPEN;
|
|
|
|
title = "My Open Dialog";
|
2012-10-16 21:28:07 +02:00
|
|
|
} else if (message_name == kFileOpenMultipleMessageName) {
|
2015-01-20 19:24:54 +01:00
|
|
|
dialog_state_->mode_ = FILE_DIALOG_OPEN_MULTIPLE;
|
|
|
|
title = "My Open Multiple Dialog";
|
|
|
|
} else if (message_name == kFileOpenFolderMessageName) {
|
|
|
|
dialog_state_->mode_ = FILE_DIALOG_OPEN_FOLDER;
|
|
|
|
title = "My Open Folder Dialog";
|
2012-10-16 21:28:07 +02:00
|
|
|
} else if (message_name == kFileSaveMessageName) {
|
2015-01-20 19:24:54 +01:00
|
|
|
dialog_state_->mode_ = static_cast<cef_file_dialog_mode_t>(
|
|
|
|
FILE_DIALOG_SAVE | FILE_DIALOG_OVERWRITEPROMPT_FLAG);
|
|
|
|
title = "My Save Dialog";
|
2012-10-16 21:28:07 +02:00
|
|
|
} else {
|
2014-07-11 22:10:05 +02:00
|
|
|
NOTREACHED();
|
2015-01-20 19:24:54 +01:00
|
|
|
return true;
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
2015-01-20 19:24:54 +01:00
|
|
|
if (dialog_state_->mode_ != FILE_DIALOG_OPEN_FOLDER) {
|
|
|
|
// Build filters based on mime time.
|
|
|
|
accept_filters.push_back("text/*");
|
|
|
|
|
|
|
|
// Build filters based on file extension.
|
|
|
|
accept_filters.push_back(".log");
|
|
|
|
accept_filters.push_back(".patch");
|
|
|
|
|
|
|
|
// Add specific filters as-is.
|
|
|
|
accept_filters.push_back("Document Files|.doc;.odt");
|
|
|
|
accept_filters.push_back("Image Files|.png;.jpg;.gif");
|
|
|
|
accept_filters.push_back("PDF Files|.pdf");
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog_state_->pending_ = true;
|
|
|
|
|
|
|
|
browser->GetHost()->RunFileDialog(
|
|
|
|
dialog_state_->mode_,
|
|
|
|
title,
|
|
|
|
dialog_state_->last_file_,
|
|
|
|
accept_filters,
|
|
|
|
dialog_state_->last_selected_filter_,
|
|
|
|
new DialogCallback(callback, dialog_state_));
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
return true;
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
2015-01-20 19:24:54 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
scoped_refptr<DialogState> dialog_state_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(Handler);
|
2012-10-16 21:28:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
void CreateMessageHandlers(ClientHandler::MessageHandlerSet& handlers) {
|
|
|
|
handlers.insert(new Handler());
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dialog_test
|