| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/web_dialogs/constrained_web_dialog_ui.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/bind_helpers.h" | |
| 12 #include "base/lazy_instance.h" | |
| 13 #include "base/values.h" | |
| 14 #include "content/public/browser/notification_service.h" | |
| 15 #include "content/public/browser/render_view_host.h" | |
| 16 #include "content/public/browser/web_contents.h" | |
| 17 #include "content/public/browser/web_ui.h" | |
| 18 #include "ui/web_dialogs/web_dialog_delegate.h" | |
| 19 #include "ui/web_dialogs/web_dialog_ui.h" | |
| 20 | |
| 21 using content::RenderViewHost; | |
| 22 using content::WebContents; | |
| 23 using content::WebUIMessageHandler; | |
| 24 | |
| 25 namespace ui { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 const char kConstrainedWebDialogDelegateUserDataKey[] = | |
| 30 "ConstrainedWebDialogDelegateUserData"; | |
| 31 | |
| 32 class ConstrainedWebDialogDelegateUserData | |
| 33 : public base::SupportsUserData::Data { | |
| 34 public: | |
| 35 explicit ConstrainedWebDialogDelegateUserData( | |
| 36 ConstrainedWebDialogDelegate* delegate) : delegate_(delegate) {} | |
| 37 virtual ~ConstrainedWebDialogDelegateUserData() {} | |
| 38 | |
| 39 ConstrainedWebDialogDelegate* delegate() { return delegate_; } | |
| 40 | |
| 41 private: | |
| 42 ConstrainedWebDialogDelegate* delegate_; // unowned | |
| 43 | |
| 44 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateUserData); | |
| 45 }; | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 ConstrainedWebDialogUI::ConstrainedWebDialogUI(content::WebUI* web_ui) | |
| 50 : WebUIController(web_ui) { | |
| 51 } | |
| 52 | |
| 53 ConstrainedWebDialogUI::~ConstrainedWebDialogUI() { | |
| 54 } | |
| 55 | |
| 56 void ConstrainedWebDialogUI::RenderViewCreated( | |
| 57 RenderViewHost* render_view_host) { | |
| 58 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate(); | |
| 59 if (!delegate) | |
| 60 return; | |
| 61 | |
| 62 WebDialogDelegate* dialog_delegate = delegate->GetWebDialogDelegate(); | |
| 63 std::vector<WebUIMessageHandler*> handlers; | |
| 64 dialog_delegate->GetWebUIMessageHandlers(&handlers); | |
| 65 render_view_host->SetWebUIProperty("dialogArguments", | |
| 66 dialog_delegate->GetDialogArgs()); | |
| 67 for (std::vector<WebUIMessageHandler*>::iterator it = handlers.begin(); | |
| 68 it != handlers.end(); ++it) { | |
| 69 web_ui()->AddMessageHandler(*it); | |
| 70 } | |
| 71 | |
| 72 // Add a "DialogClose" callback which matches WebDialogUI behavior. | |
| 73 web_ui()->RegisterMessageCallback("DialogClose", | |
| 74 base::Bind(&ConstrainedWebDialogUI::OnDialogCloseMessage, | |
| 75 base::Unretained(this))); | |
| 76 | |
| 77 dialog_delegate->OnDialogShown(web_ui(), render_view_host); | |
| 78 } | |
| 79 | |
| 80 void ConstrainedWebDialogUI::OnDialogCloseMessage(const ListValue* args) { | |
| 81 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate(); | |
| 82 if (!delegate) | |
| 83 return; | |
| 84 | |
| 85 std::string json_retval; | |
| 86 if (!args->empty() && !args->GetString(0, &json_retval)) | |
| 87 NOTREACHED() << "Could not read JSON argument"; | |
| 88 delegate->GetWebDialogDelegate()->OnDialogClosed(json_retval); | |
| 89 delegate->OnDialogCloseFromWebUI(); | |
| 90 } | |
| 91 | |
| 92 // static | |
| 93 void ConstrainedWebDialogUI::SetConstrainedDelegate( | |
| 94 content::WebContents* web_contents, | |
| 95 ConstrainedWebDialogDelegate* delegate) { | |
| 96 web_contents->SetUserData(&kConstrainedWebDialogDelegateUserDataKey, | |
| 97 new ConstrainedWebDialogDelegateUserData(delegate)); | |
| 98 } | |
| 99 | |
| 100 ConstrainedWebDialogDelegate* ConstrainedWebDialogUI::GetConstrainedDelegate() { | |
| 101 ConstrainedWebDialogDelegateUserData* user_data = | |
| 102 static_cast<ConstrainedWebDialogDelegateUserData*>( | |
| 103 web_ui()->GetWebContents()-> | |
| 104 GetUserData(&kConstrainedWebDialogDelegateUserDataKey)); | |
| 105 | |
| 106 return user_data ? user_data->delegate() : NULL; | |
| 107 } | |
| 108 | |
| 109 } // namespace ui | |
| OLD | NEW |