OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/ui/app_list/app_list_shower_views.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/profiler/scoped_tracker.h" | |
10 #include "base/single_thread_task_runner.h" | |
11 #include "base/threading/thread_task_runner_handle.h" | |
12 #include "chrome/browser/lifetime/keep_alive_types.h" | |
13 #include "chrome/browser/lifetime/scoped_keep_alive.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/browser/ui/app_list/app_list_shower_delegate.h" | |
16 #include "chrome/browser/ui/app_list/app_list_view_delegate.h" | |
17 #include "ui/app_list/views/app_list_view.h" | |
18 #include "ui/display/screen.h" | |
19 #include "ui/gfx/geometry/point.h" | |
20 | |
21 AppListShower::AppListShower(AppListShowerDelegate* delegate) | |
22 : delegate_(delegate), | |
23 profile_(NULL), | |
24 app_list_(NULL), | |
25 window_icon_updated_(false) { | |
26 } | |
27 | |
28 AppListShower::~AppListShower() { | |
29 } | |
30 | |
31 void AppListShower::ShowForCurrentProfile() { | |
32 DCHECK(HasView()); | |
33 keep_alive_.reset(new ScopedKeepAlive(KeepAliveOrigin::APP_LIST_SHOWER, | |
34 KeepAliveRestartOption::DISABLED)); | |
35 | |
36 // If the app list is already displaying |profile| just activate it (in case | |
37 // we have lost focus). | |
38 if (!IsAppListVisible()) | |
39 delegate_->MoveNearCursor(app_list_); | |
40 | |
41 Show(); | |
42 } | |
43 | |
44 gfx::NativeWindow AppListShower::GetWindow() { | |
45 if (!IsAppListVisible()) | |
46 return NULL; | |
47 return app_list_->GetWidget()->GetNativeWindow(); | |
48 } | |
49 | |
50 void AppListShower::CreateViewForProfile(Profile* requested_profile) { | |
51 DCHECK(requested_profile); | |
52 if (HasView() && requested_profile->IsSameProfile(profile_)) | |
53 return; | |
54 | |
55 profile_ = requested_profile->GetOriginalProfile(); | |
56 if (HasView()) { | |
57 UpdateViewForNewProfile(); | |
58 return; | |
59 } | |
60 app_list_ = MakeViewForCurrentProfile(); | |
61 | |
62 // TODO(tapted): Remove ScopedTracker below once crbug.com/431326 is fixed. | |
63 tracked_objects::ScopedTracker tracking_profile( | |
64 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
65 "431326 AppListShowerDelegate::OnViewCreated()")); | |
66 | |
67 delegate_->OnViewCreated(); | |
68 } | |
69 | |
70 void AppListShower::DismissAppList() { | |
71 if (HasView()) { | |
72 Hide(); | |
73 delegate_->OnViewDismissed(); | |
74 // This can be reached by pressing the dismiss accelerator. To prevent | |
75 // events from being processed with a destroyed dispatcher, delay the reset | |
76 // of the keep alive. | |
77 ResetKeepAliveSoon(); | |
78 } | |
79 } | |
80 | |
81 void AppListShower::HandleViewBeingDestroyed() { | |
82 app_list_ = NULL; | |
83 profile_ = NULL; | |
84 | |
85 // We may end up here as the result of the OS deleting the AppList's | |
86 // widget (WidgetObserver::OnWidgetDestroyed). If this happens and there | |
87 // are no browsers around then deleting the keep alive will result in | |
88 // deleting the Widget again (by way of CloseAllSecondaryWidgets). When | |
89 // the stack unravels we end up back in the Widget that was deleted and | |
90 // crash. By delaying deletion of the keep alive we ensure the Widget has | |
91 // correctly been destroyed before ending the keep alive so that | |
92 // CloseAllSecondaryWidgets() won't attempt to delete the AppList's Widget | |
93 // again. | |
94 ResetKeepAliveSoon(); | |
95 } | |
96 | |
97 bool AppListShower::IsAppListVisible() const { | |
98 return app_list_ && app_list_->GetWidget()->IsVisible(); | |
99 } | |
100 | |
101 bool AppListShower::HasView() const { | |
102 return !!app_list_; | |
103 } | |
104 | |
105 app_list::AppListView* AppListShower::MakeViewForCurrentProfile() { | |
106 app_list::AppListView* view; | |
107 { | |
108 // TODO(tapted): Remove ScopedTracker below once crbug.com/431326 is fixed. | |
109 tracked_objects::ScopedTracker tracking_profile1( | |
110 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 AppListView()")); | |
111 | |
112 // The app list view manages its own lifetime. | |
113 view = new app_list::AppListView(delegate_->GetViewDelegateForCreate()); | |
114 } | |
115 | |
116 gfx::Point cursor = display::Screen::GetScreen()->GetCursorScreenPoint(); | |
117 view->InitAsBubbleAtFixedLocation(NULL, | |
118 0, | |
119 cursor, | |
120 views::BubbleBorder::FLOAT, | |
121 false /* border_accepts_events */); | |
122 return view; | |
123 } | |
124 | |
125 void AppListShower::UpdateViewForNewProfile() { | |
126 app_list_->SetProfileByPath(profile_->GetPath()); | |
127 } | |
128 | |
129 void AppListShower::Show() { | |
130 app_list_->GetWidget()->Show(); | |
131 if (!window_icon_updated_) { | |
132 app_list_->GetWidget()->GetTopLevelWidget()->UpdateWindowIcon(); | |
133 window_icon_updated_ = true; | |
134 } | |
135 app_list_->GetWidget()->Activate(); | |
136 } | |
137 | |
138 void AppListShower::Hide() { | |
139 app_list_->GetWidget()->Hide(); | |
140 } | |
141 | |
142 void AppListShower::ResetKeepAliveSoon() { | |
143 if (base::ThreadTaskRunnerHandle::IsSet()) { // Not set in tests. | |
144 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
145 FROM_HERE, | |
146 base::Bind(&AppListShower::ResetKeepAlive, base::Unretained(this))); | |
147 return; | |
148 } | |
149 ResetKeepAlive(); | |
150 } | |
151 | |
152 void AppListShower::ResetKeepAlive() { | |
153 keep_alive_.reset(); | |
154 } | |
OLD | NEW |