1 // Copyright 2014 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.
5 #include "chrome/browser/ui/app_list/app_list_shower_views.h"
8 #include "base/location.h"
9 #include "base/profiler/scoped_tracker.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "chrome/browser/apps/scoped_keep_alive.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/app_list/app_list_shower_delegate.h"
15 #include "chrome/browser/ui/app_list/app_list_view_delegate.h"
16 #include "ui/app_list/views/app_list_view.h"
17 #include "ui/gfx/geometry/point.h"
18 #include "ui/gfx/screen.h"
20 AppListShower::AppListShower(AppListShowerDelegate
* delegate
)
21 : delegate_(delegate
),
24 window_icon_updated_(false) {
27 AppListShower::~AppListShower() {
30 void AppListShower::ShowForCurrentProfile() {
32 keep_alive_
.reset(new ScopedKeepAlive
);
34 // If the app list is already displaying |profile| just activate it (in case
35 // we have lost focus).
36 if (!IsAppListVisible())
37 delegate_
->MoveNearCursor(app_list_
);
42 gfx::NativeWindow
AppListShower::GetWindow() {
43 if (!IsAppListVisible())
45 return app_list_
->GetWidget()->GetNativeWindow();
48 void AppListShower::CreateViewForProfile(Profile
* requested_profile
) {
49 DCHECK(requested_profile
);
50 if (HasView() && requested_profile
->IsSameProfile(profile_
))
53 profile_
= requested_profile
->GetOriginalProfile();
55 UpdateViewForNewProfile();
58 app_list_
= MakeViewForCurrentProfile();
60 // TODO(tapted): Remove ScopedTracker below once crbug.com/431326 is fixed.
61 tracked_objects::ScopedTracker
tracking_profile(
62 FROM_HERE_WITH_EXPLICIT_FUNCTION(
63 "431326 AppListShowerDelegate::OnViewCreated()"));
65 delegate_
->OnViewCreated();
68 void AppListShower::DismissAppList() {
71 delegate_
->OnViewDismissed();
72 // This can be reached by pressing the dismiss accelerator. To prevent
73 // events from being processed with a destroyed dispatcher, delay the reset
79 void AppListShower::HandleViewBeingDestroyed() {
83 // We may end up here as the result of the OS deleting the AppList's
84 // widget (WidgetObserver::OnWidgetDestroyed). If this happens and there
85 // are no browsers around then deleting the keep alive will result in
86 // deleting the Widget again (by way of CloseAllSecondaryWidgets). When
87 // the stack unravels we end up back in the Widget that was deleted and
88 // crash. By delaying deletion of the keep alive we ensure the Widget has
89 // correctly been destroyed before ending the keep alive so that
90 // CloseAllSecondaryWidgets() won't attempt to delete the AppList's Widget
95 bool AppListShower::IsAppListVisible() const {
96 return app_list_
&& app_list_
->GetWidget()->IsVisible();
99 void AppListShower::WarmupForProfile(Profile
* profile
) {
101 CreateViewForProfile(profile
);
102 app_list_
->Prerender();
105 bool AppListShower::HasView() const {
109 app_list::AppListView
* AppListShower::MakeViewForCurrentProfile() {
110 app_list::AppListView
* view
;
112 // TODO(tapted): Remove ScopedTracker below once crbug.com/431326 is fixed.
113 tracked_objects::ScopedTracker
tracking_profile1(
114 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 AppListView()"));
116 // The app list view manages its own lifetime.
117 view
= new app_list::AppListView(delegate_
->GetViewDelegateForCreate());
120 gfx::Point cursor
= gfx::Screen::GetNativeScreen()->GetCursorScreenPoint();
121 view
->InitAsBubbleAtFixedLocation(NULL
,
124 views::BubbleBorder::FLOAT
,
125 false /* border_accepts_events */);
129 void AppListShower::UpdateViewForNewProfile() {
130 app_list_
->SetProfileByPath(profile_
->GetPath());
133 void AppListShower::Show() {
134 app_list_
->GetWidget()->Show();
135 if (!window_icon_updated_
) {
136 app_list_
->GetWidget()->GetTopLevelWidget()->UpdateWindowIcon();
137 window_icon_updated_
= true;
139 app_list_
->GetWidget()->Activate();
142 void AppListShower::Hide() {
143 app_list_
->GetWidget()->Hide();
146 void AppListShower::ResetKeepAliveSoon() {
147 if (base::ThreadTaskRunnerHandle::IsSet()) { // Not set in tests.
148 base::ThreadTaskRunnerHandle::Get()->PostTask(
150 base::Bind(&AppListShower::ResetKeepAlive
, base::Unretained(this)));
156 void AppListShower::ResetKeepAlive() {