Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / app_list / app_list_shower.cc
blob973827a4508fdd8f9cc882907b3f0d5fe154d386
1 // Copyright 2013 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 "base/bind.h"
6 #include "base/message_loop/message_loop.h"
7 #include "chrome/browser/ui/app_list/app_list_shower.h"
9 AppListShower::AppListShower(scoped_ptr<AppListFactory> factory,
10 scoped_ptr<KeepAliveService> keep_alive,
11 AppListService* service)
12 : factory_(factory.Pass()),
13 keep_alive_service_(keep_alive.Pass()),
14 service_(service),
15 profile_(NULL),
16 can_close_app_list_(true) {
19 AppListShower::~AppListShower() {
22 void AppListShower::ShowAndReacquireFocus(Profile* requested_profile) {
23 ShowForProfile(requested_profile);
24 app_list_->ReactivateOnNextFocusLoss();
27 void AppListShower::ShowForProfile(Profile* requested_profile) {
28 // If the app list is already displaying |profile| just activate it (in case
29 // we have lost focus).
30 if (IsAppListVisible() && (requested_profile == profile_)) {
31 app_list_->Show();
32 return;
35 if (!app_list_) {
36 CreateViewForProfile(requested_profile);
37 } else if (requested_profile != profile_) {
38 profile_ = requested_profile;
39 app_list_->SetProfile(requested_profile);
42 keep_alive_service_->EnsureKeepAlive();
43 if (!IsAppListVisible())
44 app_list_->MoveNearCursor();
45 app_list_->Show();
48 gfx::NativeWindow AppListShower::GetWindow() {
49 if (!IsAppListVisible())
50 return NULL;
51 return app_list_->GetWindow();
54 void AppListShower::CreateViewForProfile(Profile* requested_profile) {
55 profile_ = requested_profile;
56 app_list_.reset(factory_->CreateAppList(
57 profile_,
58 service_,
59 base::Bind(&AppListShower::DismissAppList, base::Unretained(this))));
62 void AppListShower::DismissAppList() {
63 if (app_list_ && can_close_app_list_) {
64 app_list_->Hide();
65 keep_alive_service_->FreeKeepAlive();
69 void AppListShower::CloseAppList() {
70 app_list_.reset();
71 profile_ = NULL;
72 can_close_app_list_ = true;
74 // We may end up here as the result of the OS deleting the AppList's
75 // widget (WidgetObserver::OnWidgetDestroyed). If this happens and there
76 // are no browsers around then deleting the keep alive will result in
77 // deleting the Widget again (by way of CloseAllSecondaryWidgets). When
78 // the stack unravels we end up back in the Widget that was deleted and
79 // crash. By delaying deletion of the keep alive we ensure the Widget has
80 // correctly been destroyed before ending the keep alive so that
81 // CloseAllSecondaryWidgets() won't attempt to delete the AppList's Widget
82 // again.
83 if (base::MessageLoop::current()) { // NULL in tests.
84 base::MessageLoop::current()->PostTask(FROM_HERE,
85 base::Bind(&KeepAliveService::FreeKeepAlive,
86 base::Unretained(keep_alive_service_.get())));
87 return;
89 keep_alive_service_->FreeKeepAlive();
92 bool AppListShower::IsAppListVisible() const {
93 return app_list_ && app_list_->IsVisible();
96 void AppListShower::WarmupForProfile(Profile* profile) {
97 DCHECK(!profile_);
98 CreateViewForProfile(profile);
99 app_list_->Prerender();
102 bool AppListShower::HasView() const {
103 return !!app_list_;