Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <gtk/gtk.h> | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "base/callback_helpers.h" | |
| 13 #include "base/i18n/message_formatter.h" | |
| 14 #include "base/location.h" | |
| 15 #include "base/logging.h" | |
| 16 #include "base/macros.h" | |
| 17 #include "base/memory/ptr_util.h" | |
| 18 #include "base/strings/utf_string_conversions.h" | |
| 19 #include "base/time/time.h" | |
| 20 #include "base/timer/timer.h" | |
| 21 #include "remoting/base/string_resources.h" | |
| 5 #include "remoting/host/it2me/it2me_confirmation_dialog.h" | 22 #include "remoting/host/it2me/it2me_confirmation_dialog.h" |
| 23 #include "ui/base/glib/glib_signal.h" | |
| 24 #include "ui/base/l10n/l10n_util.h" | |
| 6 | 25 |
| 7 namespace remoting { | 26 namespace remoting { |
| 8 | 27 |
| 28 namespace { | |
| 29 | |
| 30 // Time to wait before closing the dialog and cancelling the connection. | |
| 31 constexpr base::TimeDelta kDialogTimeout = base::TimeDelta::FromMinutes(1); | |
| 32 | |
| 33 class It2MeConfirmationDialogLinux : public It2MeConfirmationDialog { | |
| 34 public: | |
| 35 It2MeConfirmationDialogLinux(); | |
| 36 ~It2MeConfirmationDialogLinux() override; | |
| 37 | |
| 38 // It2MeConfirmationDialog implementation. | |
| 39 void Show(const std::string& remote_user_email, | |
| 40 const ResultCallback& callback) override; | |
| 41 | |
| 42 private: | |
| 43 // Creates a dialog window and makes it visible. | |
| 44 void CreateWindow(const std::string& remote_user_email); | |
| 45 | |
| 46 // Destroys the dialog window (if created) and stop |dialog_timer_|. | |
| 47 void Hide(); | |
| 48 | |
| 49 // Handles user input from the dialog. | |
| 50 CHROMEG_CALLBACK_1(It2MeConfirmationDialogLinux, void, OnResponse, GtkDialog*, | |
| 51 int); | |
| 52 | |
| 53 GtkWidget* confirmation_window_ = nullptr; | |
| 54 | |
| 55 ResultCallback result_callback_; | |
| 56 | |
| 57 base::OneShotTimer dialog_timer_; | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(It2MeConfirmationDialogLinux); | |
| 60 }; | |
| 61 | |
| 62 It2MeConfirmationDialogLinux::It2MeConfirmationDialogLinux() {} | |
| 63 | |
| 64 It2MeConfirmationDialogLinux::~It2MeConfirmationDialogLinux() { | |
| 65 Hide(); | |
| 66 } | |
| 67 | |
| 68 void It2MeConfirmationDialogLinux::Show(const std::string& remote_user_email, | |
| 69 const ResultCallback& callback) { | |
| 70 DCHECK(!remote_user_email.empty()); | |
| 71 DCHECK(!callback.is_null()); | |
| 72 DCHECK(result_callback_.is_null()); | |
|
Sergey Ulanov
2016/09/14 00:25:11
nit: don't need .is_null(). Callbacks can be impli
joedow
2016/09/14 13:28:51
Done.
| |
| 73 | |
| 74 result_callback_ = callback; | |
| 75 | |
| 76 CreateWindow(remote_user_email); | |
| 77 | |
| 78 dialog_timer_.Start(FROM_HERE, kDialogTimeout, | |
| 79 base::Bind(&It2MeConfirmationDialogLinux::OnResponse, | |
| 80 base::Unretained(this), | |
| 81 /*dialog=*/nullptr, GTK_RESPONSE_NONE)); | |
| 82 } | |
| 83 | |
| 84 void It2MeConfirmationDialogLinux::Hide() { | |
| 85 dialog_timer_.Stop(); | |
| 86 | |
| 87 if (confirmation_window_) { | |
| 88 gtk_widget_destroy(confirmation_window_); | |
| 89 confirmation_window_ = nullptr; | |
| 90 } | |
| 91 | |
| 92 if (!result_callback_.is_null()) { | |
|
Sergey Ulanov
2016/09/14 00:25:10
nit: don't need is_null()
joedow
2016/09/14 13:28:51
Done.
| |
| 93 base::ResetAndReturn(&result_callback_).Run(Result::CANCEL); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void It2MeConfirmationDialogLinux::CreateWindow( | |
| 98 const std::string& remote_user_email) { | |
| 99 DCHECK(!confirmation_window_); | |
| 100 | |
| 101 confirmation_window_ = gtk_dialog_new_with_buttons( | |
| 102 l10n_util::GetStringUTF8(IDS_PRODUCT_NAME).c_str(), | |
| 103 /*parent=*/nullptr, | |
| 104 static_cast<GtkDialogFlags>(GTK_DIALOG_MODAL), | |
| 105 l10n_util::GetStringUTF8(IDS_SHARE_CONFIRM_DIALOG_DECLINE).c_str(), | |
| 106 GTK_RESPONSE_CANCEL, | |
| 107 l10n_util::GetStringUTF8(IDS_SHARE_CONFIRM_DIALOG_CONFIRM).c_str(), | |
| 108 GTK_RESPONSE_OK, | |
| 109 /*next_button=*/nullptr); | |
| 110 | |
| 111 gtk_dialog_set_default_response(GTK_DIALOG(confirmation_window_), | |
| 112 GTK_RESPONSE_CANCEL); | |
| 113 | |
| 114 gtk_window_set_resizable(GTK_WINDOW(confirmation_window_), false); | |
| 115 | |
| 116 gtk_window_set_keep_above(GTK_WINDOW(confirmation_window_), true); | |
| 117 | |
| 118 g_signal_connect(confirmation_window_, "response", | |
| 119 G_CALLBACK(OnResponseThunk), this); | |
| 120 | |
| 121 GtkWidget* content_area = | |
| 122 gtk_dialog_get_content_area(GTK_DIALOG(confirmation_window_)); | |
| 123 | |
| 124 base::string16 dialog_text = | |
| 125 base::i18n::MessageFormatter::FormatWithNumberedArgs( | |
| 126 l10n_util::GetStringUTF16( | |
| 127 IDS_SHARE_CONFIRM_DIALOG_MESSAGE_WITH_USERNAME), | |
| 128 base::UTF8ToUTF16(remote_user_email)); | |
|
Sergey Ulanov
2016/09/14 00:25:11
I don't think you need base::UTF8ToUTF16(). There
joedow
2016/09/14 13:28:51
Done.
| |
| 129 GtkWidget* text_label = gtk_label_new(base::UTF16ToUTF8(dialog_text).c_str()); | |
| 130 gtk_label_set_line_wrap(GTK_LABEL(text_label), true); | |
| 131 gtk_misc_set_padding(GTK_MISC(text_label), 12, 12); | |
| 132 | |
| 133 gtk_container_add(GTK_CONTAINER(content_area), text_label); | |
| 134 gtk_widget_show_all(content_area); | |
| 135 | |
| 136 gtk_window_set_urgency_hint(GTK_WINDOW(confirmation_window_), true); | |
| 137 gtk_window_present(GTK_WINDOW(confirmation_window_)); | |
| 138 } | |
| 139 | |
| 140 void It2MeConfirmationDialogLinux::OnResponse(GtkDialog* dialog, | |
| 141 int response_id) { | |
| 142 DCHECK(!result_callback_.is_null()); | |
| 143 | |
| 144 if (response_id == GTK_RESPONSE_OK) { | |
|
Sergey Ulanov
2016/09/14 00:25:11
Can write as a single line:
base::ResetAndReturn
joedow
2016/09/14 13:28:51
Done.
| |
| 145 base::ResetAndReturn(&result_callback_).Run(Result::OK); | |
| 146 } else { | |
| 147 base::ResetAndReturn(&result_callback_).Run(Result::CANCEL); | |
| 148 } | |
| 149 | |
| 150 Hide(); | |
|
Sergey Ulanov
2016/09/14 00:25:11
It's best to call Hide() before calling the result
joedow
2016/09/14 13:28:51
Done.
| |
| 151 } | |
| 152 | |
| 153 } // namespace | |
| 154 | |
| 9 std::unique_ptr<It2MeConfirmationDialog> | 155 std::unique_ptr<It2MeConfirmationDialog> |
| 10 It2MeConfirmationDialogFactory::Create() { | 156 It2MeConfirmationDialogFactory::Create() { |
| 11 return nullptr; | 157 return base::MakeUnique<It2MeConfirmationDialogLinux>(); |
| 12 } | 158 } |
| 13 | 159 |
| 14 } // namespace remoting | 160 } // namespace remoting |
| OLD | NEW |