File: app_list_shower_views.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (176 lines) | stat: -rw-r--r-- 5,777 bytes parent folder | download
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/app_list/app_list_shower_views.h"

#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/profiler/scoped_tracker.h"
#include "chrome/browser/apps/scoped_keep_alive.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/app_list/app_list_shower_delegate.h"
#include "chrome/browser/ui/app_list/app_list_view_delegate.h"
#include "ui/app_list/views/app_list_view.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/screen.h"

AppListShower::AppListShower(AppListShowerDelegate* delegate)
    : delegate_(delegate),
      profile_(NULL),
      app_list_(NULL),
      window_icon_updated_(false) {
}

AppListShower::~AppListShower() {
}

void AppListShower::ShowForCurrentProfile() {
  DCHECK(HasView());
  keep_alive_.reset(new ScopedKeepAlive);

  // If the app list is already displaying |profile| just activate it (in case
  // we have lost focus).
  if (!IsAppListVisible())
    delegate_->MoveNearCursor(app_list_);

  Show();
}

gfx::NativeWindow AppListShower::GetWindow() {
  if (!IsAppListVisible())
    return NULL;
  return app_list_->GetWidget()->GetNativeWindow();
}

void AppListShower::CreateViewForProfile(Profile* requested_profile) {
  DCHECK(requested_profile);
  if (HasView() && requested_profile->IsSameProfile(profile_))
    return;

  profile_ = requested_profile->GetOriginalProfile();
  if (HasView()) {
    // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
    tracked_objects::ScopedTracker tracking_profile1(
        FROM_HERE_WITH_EXPLICIT_FUNCTION(
            "431326 AppListShower::CreateViewForProfile1"));

    UpdateViewForNewProfile();
    return;
  }
  // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
  tracked_objects::ScopedTracker tracking_profile2(
      FROM_HERE_WITH_EXPLICIT_FUNCTION(
          "431326 AppListShower::CreateViewForProfile2"));

  app_list_ = MakeViewForCurrentProfile();

  // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
  tracked_objects::ScopedTracker tracking_profile3(
      FROM_HERE_WITH_EXPLICIT_FUNCTION(
          "431326 AppListShower::CreateViewForProfile3"));

  delegate_->OnViewCreated();
}

void AppListShower::DismissAppList() {
  if (HasView()) {
    Hide();
    delegate_->OnViewDismissed();
    // This can be reached by pressing the dismiss accelerator. To prevent
    // events from being processed with a destroyed dispatcher, delay the reset
    // of the keep alive.
    ResetKeepAliveSoon();
  }
}

void AppListShower::HandleViewBeingDestroyed() {
  app_list_ = NULL;
  profile_ = NULL;

  // We may end up here as the result of the OS deleting the AppList's
  // widget (WidgetObserver::OnWidgetDestroyed). If this happens and there
  // are no browsers around then deleting the keep alive will result in
  // deleting the Widget again (by way of CloseAllSecondaryWidgets). When
  // the stack unravels we end up back in the Widget that was deleted and
  // crash. By delaying deletion of the keep alive we ensure the Widget has
  // correctly been destroyed before ending the keep alive so that
  // CloseAllSecondaryWidgets() won't attempt to delete the AppList's Widget
  // again.
  ResetKeepAliveSoon();
}

bool AppListShower::IsAppListVisible() const {
  return app_list_ && app_list_->GetWidget()->IsVisible();
}

void AppListShower::WarmupForProfile(Profile* profile) {
  DCHECK(!profile_);
  CreateViewForProfile(profile);
  app_list_->Prerender();
}

bool AppListShower::HasView() const {
  return !!app_list_;
}

app_list::AppListView* AppListShower::MakeViewForCurrentProfile() {
  // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
  tracked_objects::ScopedTracker tracking_profile1(
      FROM_HERE_WITH_EXPLICIT_FUNCTION(
          "431326 AppListShower::MakeViewForCurrentProfile1"));

  // The app list view manages its own lifetime.
  app_list::AppListView* view =
      new app_list::AppListView(delegate_->GetViewDelegateForCreate());

  // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
  tracked_objects::ScopedTracker tracking_profile2(
      FROM_HERE_WITH_EXPLICIT_FUNCTION(
          "431326 AppListShower::MakeViewForCurrentProfile2"));

  gfx::Point cursor = gfx::Screen::GetNativeScreen()->GetCursorScreenPoint();

  // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
  tracked_objects::ScopedTracker tracking_profile3(
      FROM_HERE_WITH_EXPLICIT_FUNCTION(
          "431326 AppListShower::MakeViewForCurrentProfile3"));

  view->InitAsBubbleAtFixedLocation(NULL,
                                    0,
                                    cursor,
                                    views::BubbleBorder::FLOAT,
                                    false /* border_accepts_events */);
  return view;
}

void AppListShower::UpdateViewForNewProfile() {
  app_list_->SetProfileByPath(profile_->GetPath());
}

void AppListShower::Show() {
  app_list_->GetWidget()->Show();
  if (!window_icon_updated_) {
    app_list_->GetWidget()->GetTopLevelWidget()->UpdateWindowIcon();
    window_icon_updated_ = true;
  }
  app_list_->GetWidget()->Activate();
}

void AppListShower::Hide() {
  app_list_->GetWidget()->Hide();
}

void AppListShower::ResetKeepAliveSoon() {
  if (base::MessageLoop::current()) {  // NULL in tests.
    base::MessageLoop::current()->PostTask(
        FROM_HERE,
        base::Bind(&AppListShower::ResetKeepAlive, base::Unretained(this)));
    return;
  }
  ResetKeepAlive();
}

void AppListShower::ResetKeepAlive() {
  keep_alive_.reset();
}