Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / simple_message_box_views.cc
index 3e85e68..3812aee 100644 (file)
@@ -6,79 +6,66 @@
 
 #include "base/basictypes.h"
 #include "base/compiler_specific.h"
-#include "base/memory/ref_counted.h"
 #include "base/message_loop/message_loop.h"
-#include "base/message_loop/message_pump_dispatcher.h"
 #include "base/run_loop.h"
-#include "chrome/browser/browser_process.h"
-#include "chrome/browser/ui/views/constrained_window_views.h"
-#include "grit/generated_resources.h"
-#include "ui/aura/client/dispatcher_client.h"
-#include "ui/aura/env.h"
-#include "ui/aura/root_window.h"
+#include "chrome/grit/generated_resources.h"
+#include "components/constrained_window/constrained_window_views.h"
+#include "ui/aura/window.h"
 #include "ui/base/l10n/l10n_util.h"
+#include "ui/base/resource/resource_bundle.h"
 #include "ui/gfx/native_widget_types.h"
 #include "ui/views/controls/message_box_view.h"
 #include "ui/views/widget/widget.h"
 #include "ui/views/window/dialog_delegate.h"
 
 #if defined(OS_WIN)
-#include "chrome/browser/ui/views/simple_message_box_win.h"
+#include "ui/base/win/message_box_win.h"
+#include "ui/views/win/hwnd_util.h"
 #endif
 
 namespace chrome {
 
 namespace {
 
-// Multiple SimpleMessageBoxViews can show up at the same time. Each of these
-// start a nested message-loop. However, these SimpleMessageBoxViews can be
-// deleted in any order. This creates problems if a box in an inner-loop gets
-// destroyed before a box in an outer-loop. So to avoid this, ref-counting is
-// used so that the SimpleMessageBoxViews gets deleted at the right time.
-class SimpleMessageBoxViews : public views::DialogDelegate,
-                              public base::MessagePumpDispatcher,
-                              public base::RefCounted<SimpleMessageBoxViews> {
+class SimpleMessageBoxViews : public views::DialogDelegate {
  public:
   SimpleMessageBoxViews(const base::string16& title,
                         const base::string16& message,
                         MessageBoxType type,
                         const base::string16& yes_text,
-                        const base::string16& no_text);
+                        const base::string16& no_text,
+                        bool is_system_modal);
+  ~SimpleMessageBoxViews() override;
 
-  MessageBoxResult result() const { return result_; }
+  MessageBoxResult RunDialogAndGetResult();
 
   // Overridden from views::DialogDelegate:
-  virtual int GetDialogButtons() const OVERRIDE;
-  virtual base::string16 GetDialogButtonLabel(
-      ui::DialogButton button) const OVERRIDE;
-  virtual bool Cancel() OVERRIDE;
-  virtual bool Accept() OVERRIDE;
+  int GetDialogButtons() const override;
+  base::string16 GetDialogButtonLabel(ui::DialogButton button) const override;
+  bool Cancel() override;
+  bool Accept() override;
 
   // Overridden from views::WidgetDelegate:
-  virtual base::string16 GetWindowTitle() const OVERRIDE;
-  virtual void DeleteDelegate() OVERRIDE;
-  virtual ui::ModalType GetModalType() const OVERRIDE;
-  virtual views::View* GetContentsView() OVERRIDE;
-  virtual views::Widget* GetWidget() OVERRIDE;
-  virtual const views::Widget* GetWidget() const OVERRIDE;
-
-  // Overridden from MessagePumpDispatcher:
-  virtual uint32_t Dispatch(const base::NativeEvent& event) OVERRIDE;
+  base::string16 GetWindowTitle() const override;
+  void DeleteDelegate() override;
+  ui::ModalType GetModalType() const override;
+  views::View* GetContentsView() override;
+  views::Widget* GetWidget() override;
+  const views::Widget* GetWidget() const override;
 
  private:
-  friend class base::RefCounted<SimpleMessageBoxViews>;
-  virtual ~SimpleMessageBoxViews();
+
+  // This terminates the nested message-loop.
+  void Done();
 
   const base::string16 window_title_;
   const MessageBoxType type_;
   base::string16 yes_text_;
   base::string16 no_text_;
-  MessageBoxResult result_;
+  MessageBoxResult* result_;
+  bool is_system_modal_;
   views::MessageBoxView* message_box_view_;
-
-  // Set to false as soon as the user clicks a dialog button; this tells the
-  // dispatcher we're done.
-  bool should_show_dialog_;
+  base::Closure quit_runloop_;
 
   DISALLOW_COPY_AND_ASSIGN(SimpleMessageBoxViews);
 };
@@ -90,17 +77,16 @@ SimpleMessageBoxViews::SimpleMessageBoxViews(const base::string16& title,
                                              const base::string16& message,
                                              MessageBoxType type,
                                              const base::string16& yes_text,
-                                             const base::string16& no_text)
+                                             const base::string16& no_text,
+                                             bool is_system_modal)
     : window_title_(title),
       type_(type),
       yes_text_(yes_text),
       no_text_(no_text),
-      result_(MESSAGE_BOX_RESULT_NO),
+      result_(NULL),
+      is_system_modal_(is_system_modal),
       message_box_view_(new views::MessageBoxView(
-          views::MessageBoxView::InitParams(message))),
-      should_show_dialog_(true) {
-  AddRef();
-
+          views::MessageBoxView::InitParams(message))) {
   if (yes_text_.empty()) {
     if (type_ == MESSAGE_BOX_TYPE_QUESTION)
       yes_text_ =
@@ -120,6 +106,22 @@ SimpleMessageBoxViews::SimpleMessageBoxViews(const base::string16& title,
   }
 }
 
+SimpleMessageBoxViews::~SimpleMessageBoxViews() {
+}
+
+MessageBoxResult SimpleMessageBoxViews::RunDialogAndGetResult() {
+  MessageBoxResult result = MESSAGE_BOX_RESULT_NO;
+  result_ = &result;
+  // TODO(pkotwicz): Exit message loop when the dialog is closed by some other
+  // means than |Cancel| or |Accept|. crbug.com/404385
+  base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
+  base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop);
+  base::RunLoop run_loop;
+  quit_runloop_ = run_loop.QuitClosure();
+  run_loop.Run();
+  return result;
+}
+
 int SimpleMessageBoxViews::GetDialogButtons() const {
   if (type_ == MESSAGE_BOX_TYPE_QUESTION ||
       type_ == MESSAGE_BOX_TYPE_OK_CANCEL) {
@@ -137,14 +139,14 @@ base::string16 SimpleMessageBoxViews::GetDialogButtonLabel(
 }
 
 bool SimpleMessageBoxViews::Cancel() {
-  should_show_dialog_= false;
-  result_ = MESSAGE_BOX_RESULT_NO;
+  *result_ = MESSAGE_BOX_RESULT_NO;
+  Done();
   return true;
 }
 
 bool SimpleMessageBoxViews::Accept() {
-  should_show_dialog_ = false;
-  result_ = MESSAGE_BOX_RESULT_YES;
+  *result_ = MESSAGE_BOX_RESULT_YES;
+  Done();
   return true;
 }
 
@@ -153,11 +155,11 @@ base::string16 SimpleMessageBoxViews::GetWindowTitle() const {
 }
 
 void SimpleMessageBoxViews::DeleteDelegate() {
-  Release();
+  delete this;
 }
 
 ui::ModalType SimpleMessageBoxViews::GetModalType() const {
-  return ui::MODAL_TYPE_WINDOW;
+  return is_system_modal_ ? ui::MODAL_TYPE_SYSTEM : ui::MODAL_TYPE_WINDOW;
 }
 
 views::View* SimpleMessageBoxViews::GetContentsView() {
@@ -172,18 +174,31 @@ const views::Widget* SimpleMessageBoxViews::GetWidget() const {
   return message_box_view_->GetWidget();
 }
 
-uint32_t SimpleMessageBoxViews::Dispatch(const base::NativeEvent& event) {
-  uint32_t action = POST_DISPATCH_PERFORM_DEFAULT;
-  if (!should_show_dialog_)
-    action |= POST_DISPATCH_QUIT_LOOP;
-  return action;
-}
-
 ////////////////////////////////////////////////////////////////////////////////
 // SimpleMessageBoxViews, private:
 
-SimpleMessageBoxViews::~SimpleMessageBoxViews() {
+void SimpleMessageBoxViews::Done() {
+  CHECK(!quit_runloop_.is_null());
+  quit_runloop_.Run();
+}
+
+#if defined(OS_WIN)
+UINT GetMessageBoxFlagsFromType(MessageBoxType type) {
+  UINT flags = MB_SETFOREGROUND;
+  switch (type) {
+    case MESSAGE_BOX_TYPE_INFORMATION:
+      return flags | MB_OK | MB_ICONINFORMATION;
+    case MESSAGE_BOX_TYPE_WARNING:
+      return flags | MB_OK | MB_ICONWARNING;
+    case MESSAGE_BOX_TYPE_QUESTION:
+      return flags | MB_YESNO | MB_ICONQUESTION;
+    case MESSAGE_BOX_TYPE_OK_CANCEL:
+      return flags | MB_OKCANCEL | MB_ICONWARNING;
+  }
+  NOTREACHED();
+  return flags | MB_OK | MB_ICONWARNING;
 }
+#endif
 
 MessageBoxResult ShowMessageBoxImpl(gfx::NativeWindow parent,
                                     const base::string16& title,
@@ -191,30 +206,40 @@ MessageBoxResult ShowMessageBoxImpl(gfx::NativeWindow parent,
                                     MessageBoxType type,
                                     const base::string16& yes_text,
                                     const base::string16& no_text) {
-
+  // Views dialogs cannot be shown outside the UI thread message loop or if the
+  // ResourceBundle is not initialized yet.
+  // Fallback to logging with a default response or a Windows MessageBox.
 #if defined(OS_WIN)
-  // If we're very early, we can't show a GPU-based dialog, so fallback to
-  // plain Windows MessageBox.
-  if (!ui::ContextFactory::GetInstance())
-    return NativeShowMessageBox(NULL, title, message, type);
+  if (!base::MessageLoopForUI::IsCurrent() ||
+      !base::MessageLoopForUI::current()->is_running() ||
+      !ResourceBundle::HasSharedInstance()) {
+    int result = ui::MessageBox(views::HWNDForNativeWindow(parent), message,
+                                title, GetMessageBoxFlagsFromType(type));
+    return (result == IDYES || result == IDOK) ?
+        MESSAGE_BOX_RESULT_YES : MESSAGE_BOX_RESULT_NO;
+  }
+#else
+  if (!base::MessageLoopForUI::IsCurrent() ||
+      !ResourceBundle::HasSharedInstance()) {
+    LOG(ERROR) << "Unable to show a dialog outside the UI thread message loop: "
+               << title << " - " << message;
+    return MESSAGE_BOX_RESULT_NO;
+  }
 #endif
 
-  scoped_refptr<SimpleMessageBoxViews> dialog(
-      new SimpleMessageBoxViews(title, message, type, yes_text, no_text));
-  CreateBrowserModalDialogViews(dialog.get(), parent)->Show();
-
-  aura::Window* anchor = parent;
-  aura::client::DispatcherClient* client = anchor ?
-      aura::client::GetDispatcherClient(anchor->GetRootWindow()) : NULL;
-  if (!client) {
-    // Use the widget's window itself so that the message loop
-    // exists when the dialog is closed by some other means than
-    // |Cancel| or |Accept|.
-    anchor = dialog->GetWidget()->GetNativeWindow();
-    client = aura::client::GetDispatcherClient(anchor->GetRootWindow());
-  }
-  client->RunWithDispatcher(dialog.get(), anchor);
-  return dialog->result();
+  SimpleMessageBoxViews* dialog =
+      new SimpleMessageBoxViews(title,
+                                message,
+                                type,
+                                yes_text,
+                                no_text,
+                                parent == NULL  // is_system_modal
+                                );
+  CreateBrowserModalDialogViews(dialog, parent)->Show();
+
+  // NOTE: |dialog| may have been deleted by the time |RunDialogAndGetResult()|
+  // returns.
+  return dialog->RunDialogAndGetResult();
 }
 
 }  // namespace