| 
									
										
										
										
											2014-10-31 11:17:05 -07:00
										 |  |  | // Copyright (c) 2013 GitHub, Inc.
 | 
					
						
							| 
									
										
										
										
											2014-04-25 17:49:37 +08:00
										 |  |  | // Use of this source code is governed by the MIT license that can be
 | 
					
						
							| 
									
										
										
										
											2013-04-30 23:56:50 +08:00
										 |  |  | // found in the LICENSE file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-16 08:30:26 +08:00
										 |  |  | #include "atom/browser/atom_javascript_dialog_manager.h"
 | 
					
						
							| 
									
										
										
										
											2013-04-30 23:56:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-16 09:13:06 +08:00
										 |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2017-01-04 16:46:50 -08:00
										 |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2014-03-16 09:13:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 16:46:50 -08:00
										 |  |  | #include "atom/browser/native_window.h"
 | 
					
						
							|  |  |  | #include "atom/browser/ui/message_box.h"
 | 
					
						
							|  |  |  | #include "base/bind.h"
 | 
					
						
							| 
									
										
										
										
											2013-12-11 15:48:19 +08:00
										 |  |  | #include "base/strings/utf_string_conversions.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-04 16:46:50 -08:00
										 |  |  | #include "ui/gfx/image/image_skia.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using content::JavaScriptMessageType; | 
					
						
							| 
									
										
										
										
											2013-04-30 23:56:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace atom { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 19:53:50 +08:00
										 |  |  | void AtomJavaScriptDialogManager::RunJavaScriptDialog( | 
					
						
							|  |  |  |     content::WebContents* web_contents, | 
					
						
							|  |  |  |     const GURL& origin_url, | 
					
						
							| 
									
										
										
										
											2017-01-04 16:46:50 -08:00
										 |  |  |     JavaScriptMessageType message_type, | 
					
						
							| 
									
										
										
										
											2014-06-28 19:36:57 +08:00
										 |  |  |     const base::string16& message_text, | 
					
						
							|  |  |  |     const base::string16& default_prompt_text, | 
					
						
							| 
									
										
										
										
											2014-03-01 19:53:50 +08:00
										 |  |  |     const DialogClosedCallback& callback, | 
					
						
							|  |  |  |     bool* did_suppress_message) { | 
					
						
							| 
									
										
										
										
											2017-01-04 16:46:50 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (message_type != JavaScriptMessageType::JAVASCRIPT_MESSAGE_TYPE_ALERT && | 
					
						
							|  |  |  |       message_type != JavaScriptMessageType::JAVASCRIPT_MESSAGE_TYPE_CONFIRM) { | 
					
						
							|  |  |  |     callback.Run(false, base::string16()); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::vector<std::string> buttons; | 
					
						
							|  |  |  |   if (message_type == JavaScriptMessageType::JAVASCRIPT_MESSAGE_TYPE_CONFIRM) { | 
					
						
							|  |  |  |     buttons.push_back("OK"); | 
					
						
							|  |  |  |     buttons.push_back("Cancel"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   atom::ShowMessageBox(NativeWindow::FromWebContents(web_contents), | 
					
						
							|  |  |  |                        atom::MessageBoxType::MESSAGE_BOX_TYPE_NONE, | 
					
						
							|  |  |  |                        buttons, | 
					
						
							|  |  |  |                        -1, | 
					
						
							|  |  |  |                        0, | 
					
						
							|  |  |  |                        atom::MessageBoxOptions::MESSAGE_BOX_NONE, | 
					
						
							|  |  |  |                        "", | 
					
						
							|  |  |  |                        base::UTF16ToUTF8(message_text), | 
					
						
							|  |  |  |                        "", | 
					
						
							|  |  |  |                        gfx::ImageSkia(), | 
					
						
							|  |  |  |                        base::Bind(&OnMessageBoxCallback, callback)); | 
					
						
							| 
									
										
										
										
											2014-03-01 19:53:50 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-30 23:56:50 +08:00
										 |  |  | void AtomJavaScriptDialogManager::RunBeforeUnloadDialog( | 
					
						
							|  |  |  |     content::WebContents* web_contents, | 
					
						
							|  |  |  |     bool is_reload, | 
					
						
							|  |  |  |     const DialogClosedCallback& callback) { | 
					
						
							| 
									
										
										
										
											2016-05-23 10:59:39 +09:00
										 |  |  |   // FIXME(zcbenz): the |message_text| is removed, figure out what should we do.
 | 
					
						
							| 
									
										
										
										
											2016-05-23 13:28:16 +09:00
										 |  |  |   callback.Run(false, base::ASCIIToUTF16("This should not be displayed")); | 
					
						
							| 
									
										
										
										
											2013-04-30 23:56:50 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 16:46:50 -08:00
										 |  |  | // static
 | 
					
						
							|  |  |  | void AtomJavaScriptDialogManager::OnMessageBoxCallback( | 
					
						
							|  |  |  |     const DialogClosedCallback& callback, int code) { | 
					
						
							|  |  |  |   callback.Run(code == 0, base::string16()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-30 23:56:50 +08:00
										 |  |  | }  // namespace atom
 |