File: first_app_run_toast_manager.cc

package info (click to toggle)
chromium 90.0.4430.212-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,450,632 kB
  • sloc: cpp: 19,832,434; javascript: 2,948,838; ansic: 2,312,399; python: 1,464,622; xml: 584,121; java: 514,189; asm: 470,557; objc: 83,463; perl: 77,861; sh: 77,030; cs: 70,789; fortran: 24,137; tcl: 18,916; php: 18,872; makefile: 16,848; ruby: 16,721; pascal: 13,150; sql: 10,199; yacc: 7,507; lex: 1,313; lisp: 840; awk: 329; jsp: 39; sed: 19
file content (179 lines) | stat: -rw-r--r-- 5,704 bytes parent folder | download | duplicates (2)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2017 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 "chrome/browser/chromeos/lock_screen_apps/first_app_run_toast_manager.h"

#include "base/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/values.h"
#include "chrome/browser/chromeos/lock_screen_apps/toast_dialog_view.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/common/extension.h"
#include "ui/aura/window.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"

namespace lock_screen_apps {

namespace {

// Toast dialog's vertical offset from the app window bottom.
constexpr int kToastDialogVerticalOffset = 20;

}  // namespace

// Observes the note taking app widget so bounds changes can update the toast
// position.
class FirstAppRunToastManager::AppWidgetObserver
    : public views::WidgetObserver {
 public:
  AppWidgetObserver(FirstAppRunToastManager* manager, views::Widget* widget)
      : manager_(manager), widget_(widget) {
    widget_->AddObserver(this);
  }

  ~AppWidgetObserver() override {
    // This is a no-op of the observer was previously removed.
    widget_->RemoveObserver(this);
    CHECK(!IsInObserverList());
  }

  // views::WidgetObserver:
  void OnWidgetBoundsChanged(views::Widget* widget,
                             const gfx::Rect& new_bounds) override {
    manager_->AdjustToastWidgetBounds();
  }

  void OnWidgetDestroying(views::Widget* widget) override {
    widget_->RemoveObserver(this);
  }

 private:
  FirstAppRunToastManager* manager_;
  views::Widget* widget_;

  DISALLOW_COPY_AND_ASSIGN(AppWidgetObserver);
};

FirstAppRunToastManager::FirstAppRunToastManager(Profile* profile)
    : profile_(profile) {}

FirstAppRunToastManager::~FirstAppRunToastManager() {
  Reset();
}

void FirstAppRunToastManager::RunForAppWindow(
    extensions::AppWindow* app_window) {
  if (app_window_)
    return;

  DCHECK(app_window->GetNativeWindow());

  const extensions::Extension* app = app_window->GetExtension();
  const base::DictionaryValue* toast_shown =
      profile_->GetPrefs()->GetDictionary(
          prefs::kNoteTakingAppsLockScreenToastShown);
  bool already_shown_for_app = false;
  if (toast_shown->GetBoolean(app->id(), &already_shown_for_app) &&
      already_shown_for_app) {
    return;
  }

  app_window_ = app_window;
  views::Widget* app_widget =
      views::Widget::GetWidgetForNativeWindow(app_window_->GetNativeWindow());
  DCHECK(app_widget);
  app_widget_observer_ = std::make_unique<AppWidgetObserver>(this, app_widget);

  if (app_window_->GetNativeWindow()->HasFocus()) {
    CreateAndShowToastDialog();
  } else {
    app_window_observer_.Add(
        extensions::AppWindowRegistry::Get(app_window_->browser_context()));
  }
}

void FirstAppRunToastManager::Reset() {
  app_widget_observer_.reset();
  app_window_observer_.RemoveAll();
  toast_widget_observer_.RemoveAll();

  app_window_ = nullptr;

  weak_ptr_factory_.InvalidateWeakPtrs();

  if (toast_widget_ && !toast_widget_->IsClosed())
    toast_widget_->Close();
  toast_widget_ = nullptr;
}

void FirstAppRunToastManager::OnWidgetDestroyed(views::Widget* widget) {
  Reset();
}

void FirstAppRunToastManager::OnAppWindowActivated(
    extensions::AppWindow* app_window) {
  if (app_window == app_window_) {
    app_window_observer_.RemoveAll();

    // Start toast dialog creation asynchronously so it happens after app window
    // activation completes.
    base::ThreadTaskRunnerHandle::Get()->PostTask(
        FROM_HERE,
        base::BindOnce(&FirstAppRunToastManager::CreateAndShowToastDialog,
                       weak_ptr_factory_.GetWeakPtr()));
  }
}

void FirstAppRunToastManager::CreateAndShowToastDialog() {
  auto* toast_dialog = new ToastDialogView(
      base::UTF8ToUTF16(app_window_->GetExtension()->short_name()),
      base::BindOnce(&FirstAppRunToastManager::ToastDialogDismissed,
                     weak_ptr_factory_.GetWeakPtr()));
  toast_widget_ = views::BubbleDialogDelegateView::CreateBubble(toast_dialog);
  toast_widget_->Show();
  AdjustToastWidgetBounds();
  toast_widget_observer_.Add(toast_widget_);
}

void FirstAppRunToastManager::ToastDialogDismissed() {
  {
    const extensions::Extension* app = app_window_->GetExtension();
    DictionaryPrefUpdate dict_update(
        profile_->GetPrefs(), prefs::kNoteTakingAppsLockScreenToastShown);
    dict_update->SetBoolean(app->id(), true);
  }
  Reset();
}

void FirstAppRunToastManager::AdjustToastWidgetBounds() {
  if (!toast_widget_)
    return;

  DCHECK(app_window_);

  const gfx::Rect app_window_bounds =
      app_window_->GetNativeWindow()->GetBoundsInScreen();
  const gfx::Rect original_bounds = toast_widget_->GetWindowBoundsInScreen();

  gfx::Point intended_origin = gfx::Point(
      // Center toast widget horizontally relative to app_window bounds.
      app_window_bounds.x() +
          (app_window_bounds.width() - original_bounds.width()) / 2,
      // Position toast widget dialog at the bottom of app window, with an
      // additional offset (so poirtion of the dialog is painted outside the
      // app window bounds).
      app_window_bounds.bottom() - original_bounds.height() +
          kToastDialogVerticalOffset);

  toast_widget_->SetBounds(gfx::Rect(intended_origin, original_bounds.size()));
}

}  // namespace lock_screen_apps