This source file includes following definitions.
- delegate
- CloseDialog
- SetDelegate
- GetDelegate
- RenderViewCreated
- OnDialogClosed
#include "ui/web_dialogs/web_dialog_ui.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/lazy_instance.h"
#include "base/values.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "content/public/common/bindings_policy.h"
#include "ui/web_dialogs/web_dialog_delegate.h"
using content::RenderViewHost;
using content::WebUIMessageHandler;
namespace ui {
namespace {
const char kWebDialogDelegateUserDataKey[] = "WebDialogDelegateUserData";
class WebDialogDelegateUserData : public base::SupportsUserData::Data {
public:
explicit WebDialogDelegateUserData(WebDialogDelegate* delegate)
: delegate_(delegate) {}
virtual ~WebDialogDelegateUserData() {}
WebDialogDelegate* delegate() { return delegate_; }
private:
WebDialogDelegate* delegate_;
};
}
WebDialogUI::WebDialogUI(content::WebUI* web_ui)
: WebUIController(web_ui) {
}
WebDialogUI::~WebDialogUI() {
}
void WebDialogUI::CloseDialog(const base::ListValue* args) {
OnDialogClosed(args);
}
void WebDialogUI::SetDelegate(content::WebContents* web_contents,
WebDialogDelegate* delegate) {
web_contents->SetUserData(&kWebDialogDelegateUserDataKey,
new WebDialogDelegateUserData(delegate));
}
WebDialogDelegate* WebDialogUI::GetDelegate(
content::WebContents* web_contents) {
WebDialogDelegateUserData* user_data =
static_cast<WebDialogDelegateUserData*>(
web_contents->GetUserData(&kWebDialogDelegateUserDataKey));
return user_data ? user_data->delegate() : NULL;
}
void WebDialogUI::RenderViewCreated(RenderViewHost* render_view_host) {
web_ui()->RegisterMessageCallback("DialogClose",
base::Bind(&WebDialogUI::OnDialogClosed, base::Unretained(this)));
std::string dialog_args;
std::vector<WebUIMessageHandler*> handlers;
WebDialogDelegate* delegate = GetDelegate(web_ui()->GetWebContents());
if (delegate) {
dialog_args = delegate->GetDialogArgs();
delegate->GetWebUIMessageHandlers(&handlers);
}
if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI))
render_view_host->SetWebUIProperty("dialogArguments", dialog_args);
for (std::vector<WebUIMessageHandler*>::iterator it = handlers.begin();
it != handlers.end(); ++it) {
web_ui()->AddMessageHandler(*it);
}
if (delegate)
delegate->OnDialogShown(web_ui(), render_view_host);
}
void WebDialogUI::OnDialogClosed(const base::ListValue* args) {
WebDialogDelegate* delegate = GetDelegate(web_ui()->GetWebContents());
if (delegate) {
std::string json_retval;
if (args && !args->empty() && !args->GetString(0, &json_retval))
NOTREACHED() << "Could not read JSON argument";
delegate->OnDialogCloseFromWebUI(json_retval);
}
}
ExternalWebDialogUI::ExternalWebDialogUI(content::WebUI* web_ui)
: WebDialogUI(web_ui) {
web_ui->SetBindings(web_ui->GetBindings() & ~content::BINDINGS_POLICY_WEB_UI);
}
ExternalWebDialogUI::~ExternalWebDialogUI() {
}
}