File: app_list_notifier_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (298 lines) | stat: -rw-r--r-- 10,008 bytes parent folder | download | duplicates (6)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/app_list/app_list_notifier_impl.h"

#include "ash/public/cpp/app_list/app_list_controller.h"
#include "base/check.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "ui/display/types/display_constants.h"

namespace {

// TODO(crbug.com/40128586): Finalize a value for this, and possibly use
// different values for different UI surfaces.
constexpr base::TimeDelta kImpressionTimer = base::Seconds(1);

}  // namespace

AppListNotifierImpl::AppListNotifierImpl(
    ash::AppListController* app_list_controller)
    : app_list_controller_(app_list_controller) {
  DCHECK(app_list_controller_);
  app_list_controller_->AddObserver(this);
  OnAppListVisibilityWillChange(app_list_controller_->IsVisible(),
                                display::kInvalidDisplayId);
}

AppListNotifierImpl::~AppListNotifierImpl() {
  app_list_controller_->RemoveObserver(this);
}

void AppListNotifierImpl::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void AppListNotifierImpl::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

void AppListNotifierImpl::NotifyLaunched(Location location,
                                         const Result& result) {
  launched_result_ = result;

  // The continue and recent apps appear at the same time. If a launch occurs in
  // one, mark the other as 'ignored' rather than abandoned.
  if (location == Location::kContinue) {
    DoStateTransition(Location::kRecentApps, State::kIgnored);
  } else if (location == Location::kRecentApps) {
    DoStateTransition(Location::kContinue, State::kIgnored);
  }

  DoStateTransition(location, State::kLaunched);
}

void AppListNotifierImpl::NotifyResultsUpdated(
    Location location,
    const std::vector<Result>& results) {
  if (location == Location::kList) {
    for (const auto& result : results)
      list_results_[result.id] = result;
  } else if (location == Location::kAnswerCard ||
             location == Location::kImage) {
    if (results.size() > 0) {
      DoStateTransition(location, State::kShown);
    } else {
      DoStateTransition(location, State::kNone);
    }
    results_[location] = results;
  } else {
    results_[location] = results;
  }
}

void AppListNotifierImpl::NotifyContinueSectionVisibilityChanged(
    Location location,
    bool visible) {
  DCHECK(location == Location::kContinue || location == Location::kRecentApps);

  continue_section_visibility_[location] = visible;
  DoStateTransition(location, shown_ && query_.empty() && visible
                                  ? State::kShown
                                  : State::kNone);
}

bool AppListNotifierImpl::GetContinueSectionVisibility(
    Location location) const {
  const auto it = continue_section_visibility_.find(location);
  return it != continue_section_visibility_.cend() && it->second;
}

void AppListNotifierImpl::NotifySearchQueryChanged(
    const std::u16string& query) {
  // In some cases the query can change after the launcher is closed, in
  // particular this happens when abandoning the launcher with a non-empty
  // query. Only do a state transition if the launcher is open.
  if (shown_) {
    if (query.empty()) {
      DoStateTransition(Location::kList, State::kNone);
      DoStateTransition(Location::kAnswerCard, State::kNone);
      DoStateTransition(Location::kContinue,
                        GetContinueSectionVisibility(Location::kContinue)
                            ? State::kShown
                            : State::kNone);
      DoStateTransition(Location::kRecentApps,
                        GetContinueSectionVisibility(Location::kRecentApps)
                            ? State::kShown
                            : State::kNone);
    } else {
      if (!search_session_in_progress_) {
        search_session_in_progress_ = true;
        for (auto& observer : observers_) {
          observer.OnSearchSessionStarted();
        }
      }

      DoStateTransition(Location::kList, State::kShown);
      DoStateTransition(Location::kAnswerCard, State::kNone);
      DoStateTransition(Location::kContinue, State::kNone);
      DoStateTransition(Location::kRecentApps, State::kNone);
    }
  }

  // Update the stored |query_| after performing the state transitions, so that
  // an abandon triggered by the query change correctly uses the pre-abandon
  // query.
  query_ = query;

  results_.clear();
  list_results_.clear();

  for (auto& observer : observers_) {
    observer.OnQueryChanged(query);
  }
}

bool AppListNotifierImpl::FireImpressionTimerForTesting(Location location) {
  auto timer_it = timers_.find(location);
  if (timer_it == timers_.end() || !timer_it->second->IsRunning())
    return false;
  timer_it->second->FireNow();
  return true;
}

void AppListNotifierImpl::OnAppListVisibilityWillChange(bool shown,
                                                        int64_t display_id) {
  if (shown_ == shown)
    return;
  shown_ = shown;

  if (shown) {
    if (GetContinueSectionVisibility(Location::kContinue))
      DoStateTransition(Location::kContinue, State::kShown);
    if (GetContinueSectionVisibility(Location::kRecentApps))
      DoStateTransition(Location::kRecentApps, State::kShown);
    // kList is not shown until a search query is entered.
  } else {
    if (search_session_in_progress_) {
      search_session_in_progress_ = false;
      for (auto& observer : observers_) {
        observer.OnSearchSessionEnded(query_);
      }
    }

    DoStateTransition(Location::kList, State::kNone);
    DoStateTransition(Location::kContinue, State::kNone);
    DoStateTransition(Location::kRecentApps, State::kNone);
    DoStateTransition(Location::kAnswerCard, State::kNone);
  }
}

void AppListNotifierImpl::OnViewStateChanged(ash::AppListViewState state) {
  if (state == ash::AppListViewState::kFullscreenSearch && !query_.empty()) {
    search_session_in_progress_ = true;
    for (auto& observer : observers_) {
      observer.OnSearchSessionStarted();
    }
  } else {
    search_session_in_progress_ = false;
    for (auto& observer : observers_) {
      observer.OnSearchSessionEnded(query_);
    }
  }
}

void AppListNotifierImpl::RestartTimer(Location location) {
  if (timers_.find(location) == timers_.end()) {
    timers_[location] = std::make_unique<base::OneShotTimer>();
  }

  auto& timer = timers_[location];
  if (timer->IsRunning()) {
    timer->Stop();
  }
  // base::Unretained is safe here because the timer is a member of |this|, and
  // OneShotTimer cancels its timer on destruction.
  timer->Start(FROM_HERE, kImpressionTimer,
               base::BindOnce(&AppListNotifierImpl::OnTimerFinished,
                              base::Unretained(this), location));
}

void AppListNotifierImpl::StopTimer(Location location) {
  const auto it = timers_.find(location);
  if (it != timers_.end() && it->second->IsRunning()) {
    it->second->Stop();
  }
}

void AppListNotifierImpl::OnTimerFinished(Location location) {
  DoStateTransition(location, State::kSeen);
}

std::vector<AppListNotifierImpl::Result>
AppListNotifierImpl::ResultsForLocation(Location location) {
  // Special case kList, see header comment on |list_results_|.
  if (location == Location::kList) {
    std::vector<Result> results;
    for (const auto& id_result : list_results_) {
      results.push_back(id_result.second.value());
    }
    return results;
  }

  return results_[location];
}

void AppListNotifierImpl::DoStateTransition(Location location,
                                            State new_state) {
  const State old_state = states_[location];

  // Update most recent state. We special-case kLaunched and kIgnored, which are
  // temporary states reflecting a launch either in |location| or another view.
  // They immediately transition to kNone because the launcher closes after a
  // launch.
  if (new_state == State::kLaunched || new_state == State::kIgnored) {
    states_[location] = State::kNone;
  } else {
    states_[location] = new_state;
  }

  // These overlapping cases are equivalent to the explicit cases in the header
  // comment.

  // Restart timer on * -> kShown
  if (new_state == State::kShown) {
    RestartTimer(location);
  }

  // Stop timer on kShown -> {kNone, kLaunch}.
  if (old_state == State::kShown &&
      (new_state == State::kNone || new_state == State::kLaunched)) {
    StopTimer(location);
  }

  // Notify of seen on kShown -> {kSeen} when there is one or more result for
  // `location`.
  if (old_state == State::kShown && new_state == State::kSeen) {
    auto results = ResultsForLocation(location);
    if (results.size() > 0) {
      for (auto& observer : observers_) {
        observer.OnSeen(location, results, query_);
      }
    }
  }

  // Notify of impression on kShown -> {kSeen, kIgnored, kLaunched}.
  if (old_state == State::kShown &&
      (new_state == State::kSeen || new_state == State::kLaunched ||
       new_state == State::kIgnored)) {
    for (auto& observer : observers_) {
      observer.OnImpression(location, ResultsForLocation(location), query_);
    }
  }

  // Notify of launch on * -> kLaunched.
  if (new_state == State::kLaunched && launched_result_.has_value()) {
    for (auto& observer : observers_) {
      observer.OnLaunch(location, launched_result_.value(),
                        ResultsForLocation(location), query_);
    }
  }

  // Notify of ignore on * -> kIgnored.
  if (new_state == State::kIgnored) {
    for (auto& observer : observers_) {
      observer.OnIgnore(location, ResultsForLocation(location), query_);
    }
  }

  // Notify of abandon on kSeen -> {kNone, kShown}.
  if (old_state == State::kSeen &&
      (new_state == State::kNone || new_state == State::kShown)) {
    for (auto& observer : observers_) {
      observer.OnAbandon(location, ResultsForLocation(location), query_);
    }
  }
}