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";
|
|
|
|
const char kFileSaveMessageName[] = "DialogTest.FileSave";
|
2012-10-16 21:28:07 +02:00
|
|
|
|
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:
|
2014-01-28 00:31:03 +01:00
|
|
|
explicit DialogCallback(
|
|
|
|
CefRefPtr<CefMessageRouterBrowserSide::Callback> router_callback)
|
|
|
|
: router_callback_(router_callback) {
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnFileDialogDismissed(
|
|
|
|
CefRefPtr<CefBrowserHost> browser_host,
|
|
|
|
const std::vector<CefString>& file_paths) OVERRIDE {
|
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;
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-01-28 00:31:03 +01:00
|
|
|
CefRefPtr<CefMessageRouterBrowserSide::Callback> router_callback_;
|
2012-10-16 21:28:07 +02:00
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
IMPLEMENT_REFCOUNTING(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 {
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// Sample file type filter.
|
|
|
|
std::vector<CefString> file_types;
|
|
|
|
file_types.push_back("text/*");
|
|
|
|
file_types.push_back(".log");
|
|
|
|
file_types.push_back(".patch");
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
CefRefPtr<DialogCallback> dialog_callback(new DialogCallback(callback));
|
|
|
|
|
|
|
|
const std::string& message_name = request;
|
2012-10-16 21:28:07 +02:00
|
|
|
if (message_name == kFileOpenMessageName) {
|
|
|
|
browser->GetHost()->RunFileDialog(FILE_DIALOG_OPEN, "My Open Dialog",
|
2014-01-28 00:31:03 +01:00
|
|
|
"test.txt", file_types, dialog_callback.get());
|
2012-10-16 21:28:07 +02:00
|
|
|
} else if (message_name == kFileOpenMultipleMessageName) {
|
|
|
|
browser->GetHost()->RunFileDialog(FILE_DIALOG_OPEN_MULTIPLE,
|
|
|
|
"My Open Multiple Dialog", CefString(), file_types,
|
2014-01-28 00:31:03 +01:00
|
|
|
dialog_callback.get());
|
2012-10-16 21:28:07 +02:00
|
|
|
} else if (message_name == kFileSaveMessageName) {
|
|
|
|
browser->GetHost()->RunFileDialog(FILE_DIALOG_SAVE, "My Save Dialog",
|
2014-01-28 00:31:03 +01:00
|
|
|
"test.txt", file_types, dialog_callback.get());
|
2012-10-16 21:28:07 +02:00
|
|
|
} else {
|
2014-07-11 22:10:05 +02:00
|
|
|
NOTREACHED();
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
return true;
|
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
|