Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / ui / app_list / app_list_shower_views.cc
blobb34e9fc3fe46f49a5cdb930e5637066b938fe529
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"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/profiler/scoped_tracker.h"
10 #include "chrome/browser/apps/scoped_keep_alive.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/app_list/app_list_shower_delegate.h"
13 #include "chrome/browser/ui/app_list/app_list_view_delegate.h"
14 #include "ui/app_list/views/app_list_view.h"
15 #include "ui/gfx/geometry/point.h"
16 #include "ui/gfx/screen.h"
18 AppListShower::AppListShower(AppListShowerDelegate* delegate)
19 : delegate_(delegate),
20 profile_(NULL),
21 app_list_(NULL),
22 window_icon_updated_(false) {
25 AppListShower::~AppListShower() {
28 void AppListShower::ShowForCurrentProfile() {
29 DCHECK(HasView());
30 keep_alive_.reset(new ScopedKeepAlive);
32 // If the app list is already displaying |profile| just activate it (in case
33 // we have lost focus).
34 if (!IsAppListVisible())
35 delegate_->MoveNearCursor(app_list_);
37 Show();
40 gfx::NativeWindow AppListShower::GetWindow() {
41 if (!IsAppListVisible())
42 return NULL;
43 return app_list_->GetWidget()->GetNativeWindow();
46 void AppListShower::CreateViewForProfile(Profile* requested_profile) {
47 DCHECK(requested_profile);
48 if (HasView() && requested_profile->IsSameProfile(profile_))
49 return;
51 profile_ = requested_profile->GetOriginalProfile();
52 if (HasView()) {
53 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
54 tracked_objects::ScopedTracker tracking_profile1(
55 FROM_HERE_WITH_EXPLICIT_FUNCTION(
56 "431326 AppListShower::CreateViewForProfile1"));
58 UpdateViewForNewProfile();
59 return;
61 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
62 tracked_objects::ScopedTracker tracking_profile2(
63 FROM_HERE_WITH_EXPLICIT_FUNCTION(
64 "431326 AppListShower::CreateViewForProfile2"));
66 app_list_ = MakeViewForCurrentProfile();
68 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
69 tracked_objects::ScopedTracker tracking_profile3(
70 FROM_HERE_WITH_EXPLICIT_FUNCTION(
71 "431326 AppListShower::CreateViewForProfile3"));
73 delegate_->OnViewCreated();
76 void AppListShower::DismissAppList() {
77 if (HasView()) {
78 Hide();
79 delegate_->OnViewDismissed();
80 // This can be reached by pressing the dismiss accelerator. To prevent
81 // events from being processed with a destroyed dispatcher, delay the reset
82 // of the keep alive.
83 ResetKeepAliveSoon();
87 void AppListShower::HandleViewBeingDestroyed() {
88 app_list_ = NULL;
89 profile_ = NULL;
91 // We may end up here as the result of the OS deleting the AppList's
92 // widget (WidgetObserver::OnWidgetDestroyed). If this happens and there
93 // are no browsers around then deleting the keep alive will result in
94 // deleting the Widget again (by way of CloseAllSecondaryWidgets). When
95 // the stack unravels we end up back in the Widget that was deleted and
96 // crash. By delaying deletion of the keep alive we ensure the Widget has
97 // correctly been destroyed before ending the keep alive so that
98 // CloseAllSecondaryWidgets() won't attempt to delete the AppList's Widget
99 // again.
100 ResetKeepAliveSoon();
103 bool AppListShower::IsAppListVisible() const {
104 return app_list_ && app_list_->GetWidget()->IsVisible();
107 void AppListShower::WarmupForProfile(Profile* profile) {
108 DCHECK(!profile_);
109 CreateViewForProfile(profile);
110 app_list_->Prerender();
113 bool AppListShower::HasView() const {
114 return !!app_list_;
117 app_list::AppListView* AppListShower::MakeViewForCurrentProfile() {
118 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
119 tracked_objects::ScopedTracker tracking_profile1(
120 FROM_HERE_WITH_EXPLICIT_FUNCTION(
121 "431326 AppListShower::MakeViewForCurrentProfile1"));
123 // The app list view manages its own lifetime.
124 app_list::AppListView* view =
125 new app_list::AppListView(delegate_->GetViewDelegateForCreate());
127 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
128 tracked_objects::ScopedTracker tracking_profile2(
129 FROM_HERE_WITH_EXPLICIT_FUNCTION(
130 "431326 AppListShower::MakeViewForCurrentProfile2"));
132 gfx::Point cursor = gfx::Screen::GetNativeScreen()->GetCursorScreenPoint();
134 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
135 tracked_objects::ScopedTracker tracking_profile3(
136 FROM_HERE_WITH_EXPLICIT_FUNCTION(
137 "431326 AppListShower::MakeViewForCurrentProfile3"));
139 view->InitAsBubbleAtFixedLocation(NULL,
141 cursor,
142 views::BubbleBorder::FLOAT,
143 false /* border_accepts_events */);
144 return view;
147 void AppListShower::UpdateViewForNewProfile() {
148 app_list_->SetProfileByPath(profile_->GetPath());
151 void AppListShower::Show() {
152 app_list_->GetWidget()->Show();
153 if (!window_icon_updated_) {
154 app_list_->GetWidget()->GetTopLevelWidget()->UpdateWindowIcon();
155 window_icon_updated_ = true;
157 app_list_->GetWidget()->Activate();
160 void AppListShower::Hide() {
161 app_list_->GetWidget()->Hide();
164 void AppListShower::ResetKeepAliveSoon() {
165 if (base::MessageLoop::current()) { // NULL in tests.
166 base::MessageLoop::current()->PostTask(
167 FROM_HERE,
168 base::Bind(&AppListShower::ResetKeepAlive, base::Unretained(this)));
169 return;
171 ResetKeepAlive();
174 void AppListShower::ResetKeepAlive() {
175 keep_alive_.reset();