File: it2me_confirmation_dialog_linux.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (152 lines) | stat: -rw-r--r-- 4,822 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <gtk/gtk.h>

#include <memory>
#include <string>

#include "base/bind.h"
#include "base/callback.h"
#include "base/callback_helpers.h"
#include "base/i18n/message_formatter.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "remoting/base/string_resources.h"
#include "remoting/host/it2me/it2me_confirmation_dialog.h"
#include "ui/base/glib/glib_signal.h"
#include "ui/base/l10n/l10n_util.h"

namespace remoting {

namespace {

// Time to wait before closing the dialog and cancelling the connection.
constexpr base::TimeDelta kDialogTimeout = base::TimeDelta::FromMinutes(1);

class It2MeConfirmationDialogLinux : public It2MeConfirmationDialog {
 public:
  It2MeConfirmationDialogLinux();
  ~It2MeConfirmationDialogLinux() override;

  // It2MeConfirmationDialog implementation.
  void Show(const std::string& remote_user_email,
            const ResultCallback& callback) override;

 private:
  // Creates a dialog window and makes it visible.
  void CreateWindow(const std::string& remote_user_email);

  // Destroys the dialog window (if created) and stop |dialog_timer_|.
  void Hide();

  // Handles user input from the dialog.
  CHROMEG_CALLBACK_1(It2MeConfirmationDialogLinux, void, OnResponse, GtkDialog*,
                     int);

  GtkWidget* confirmation_window_ = nullptr;

  ResultCallback result_callback_;

  base::OneShotTimer dialog_timer_;

  DISALLOW_COPY_AND_ASSIGN(It2MeConfirmationDialogLinux);
};

It2MeConfirmationDialogLinux::It2MeConfirmationDialogLinux() {}

It2MeConfirmationDialogLinux::~It2MeConfirmationDialogLinux() {
  Hide();
}

void It2MeConfirmationDialogLinux::Show(const std::string& remote_user_email,
                                        const ResultCallback& callback) {
  DCHECK(!remote_user_email.empty());
  DCHECK(callback);
  DCHECK(!result_callback_);

  result_callback_ = callback;

  CreateWindow(remote_user_email);

  dialog_timer_.Start(FROM_HERE, kDialogTimeout,
                      base::Bind(&It2MeConfirmationDialogLinux::OnResponse,
                                 base::Unretained(this),
                                 /*dialog=*/nullptr, GTK_RESPONSE_NONE));
}

void It2MeConfirmationDialogLinux::Hide() {
  dialog_timer_.Stop();

  if (confirmation_window_) {
    gtk_widget_destroy(confirmation_window_);
    confirmation_window_ = nullptr;
  }
}

void It2MeConfirmationDialogLinux::CreateWindow(
    const std::string& remote_user_email) {
  DCHECK(!confirmation_window_);

  confirmation_window_ = gtk_dialog_new_with_buttons(
      l10n_util::GetStringUTF8(IDS_PRODUCT_NAME).c_str(),
      /*parent=*/nullptr,
      static_cast<GtkDialogFlags>(GTK_DIALOG_MODAL),
      l10n_util::GetStringUTF8(IDS_SHARE_CONFIRM_DIALOG_DECLINE).c_str(),
      GTK_RESPONSE_CANCEL,
      l10n_util::GetStringUTF8(IDS_SHARE_CONFIRM_DIALOG_CONFIRM).c_str(),
      GTK_RESPONSE_OK,
      /*next_button=*/nullptr);

  gtk_dialog_set_default_response(GTK_DIALOG(confirmation_window_),
                                  GTK_RESPONSE_CANCEL);

  gtk_window_set_resizable(GTK_WINDOW(confirmation_window_), false);

  gtk_window_set_keep_above(GTK_WINDOW(confirmation_window_), true);

  g_signal_connect(confirmation_window_, "response",
                   G_CALLBACK(OnResponseThunk), this);

  GtkWidget* content_area =
      gtk_dialog_get_content_area(GTK_DIALOG(confirmation_window_));

  base::string16 dialog_text =
      base::i18n::MessageFormatter::FormatWithNumberedArgs(
          l10n_util::GetStringUTF16(
              IDS_SHARE_CONFIRM_DIALOG_MESSAGE_WITH_USERNAME),
          remote_user_email);
  GtkWidget* text_label = gtk_label_new(base::UTF16ToUTF8(dialog_text).c_str());
  gtk_label_set_line_wrap(GTK_LABEL(text_label), true);
  gtk_misc_set_padding(GTK_MISC(text_label), 12, 12);

  gtk_container_add(GTK_CONTAINER(content_area), text_label);
  gtk_widget_show_all(content_area);

  gtk_window_set_urgency_hint(GTK_WINDOW(confirmation_window_), true);
  gtk_window_present(GTK_WINDOW(confirmation_window_));
}

void It2MeConfirmationDialogLinux::OnResponse(GtkDialog* dialog,
                                              int response_id) {
  DCHECK(result_callback_);

  Hide();
  base::ResetAndReturn(&result_callback_).Run(
      (response_id == GTK_RESPONSE_OK) ? Result::OK : Result::CANCEL);
}

}  // namespace

// static
std::unique_ptr<It2MeConfirmationDialog> It2MeConfirmationDialog::Create() {
  return base::MakeUnique<It2MeConfirmationDialogLinux>();
}

}  // namespace remoting