Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / gtk / process_singleton_dialog.cc
1 // Copyright (c) 2011 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 "chrome/browser/ui/process_singleton_dialog_linux.h"
6
7 #include <gtk/gtk.h>
8
9 #include <string>
10
11 #include "base/basictypes.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/ui/gtk/gtk_util.h"
15 #include "grit/chromium_strings.h"
16 #include "ui/base/gtk/gtk_signal.h"
17 #include "ui/base/l10n/l10n_util.h"
18
19 namespace {
20
21 class ProcessSingletonDialog {
22  public:
23   ProcessSingletonDialog(const std::string& message,
24                          const std::string& relaunch_text);
25
26   int GetResponseId() const { return response_id_; }
27
28  private:
29   CHROMEGTK_CALLBACK_1(ProcessSingletonDialog, void, OnResponse, int);
30
31   GtkWidget* dialog_;
32   int response_id_;
33
34   DISALLOW_COPY_AND_ASSIGN(ProcessSingletonDialog);
35 };
36
37 ProcessSingletonDialog::ProcessSingletonDialog(
38     const std::string& message,
39     const std::string& relaunch_text) {
40   dialog_ = gtk_message_dialog_new(
41       NULL,
42       static_cast<GtkDialogFlags>(0),
43       GTK_MESSAGE_ERROR,
44       GTK_BUTTONS_NONE,
45       "%s",
46       message.c_str());
47   gtk_util::ApplyMessageDialogQuirks(dialog_);
48   gtk_window_set_title(GTK_WINDOW(dialog_),
49                        l10n_util::GetStringUTF8(IDS_PRODUCT_NAME).c_str());
50   gtk_dialog_add_button(GTK_DIALOG(dialog_), GTK_STOCK_QUIT,
51                         GTK_RESPONSE_REJECT);
52   gtk_dialog_add_button(GTK_DIALOG(dialog_), relaunch_text.c_str(),
53                         GTK_RESPONSE_ACCEPT);
54   gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_ACCEPT);
55
56   g_signal_connect(dialog_, "response", G_CALLBACK(OnResponseThunk), this);
57
58   gtk_widget_show_all(dialog_);
59   base::MessageLoop::current()->Run();
60 }
61
62 void ProcessSingletonDialog::OnResponse(GtkWidget* dialog, int response_id) {
63   response_id_ = response_id;
64   gtk_widget_destroy(dialog_);
65   base::MessageLoop::current()->Quit();
66 }
67
68 }  // namespace
69
70 bool ShowProcessSingletonDialog(const base::string16& message,
71                                 const base::string16& relaunch_text) {
72   ProcessSingletonDialog dialog(base::UTF16ToUTF8(message),
73                                 base::UTF16ToUTF8(relaunch_text));
74   return dialog.GetResponseId() == GTK_RESPONSE_ACCEPT;
75 }