Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / ui / ash / launcher / app_window_launcher_controller.cc
blobee37e6508101060a12f22952b8557a7cf7927877
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/ash/launcher/app_window_launcher_controller.h"
7 #include "ash/shelf/shelf_util.h"
8 #include "ash/shell.h"
9 #include "ash/wm/window_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/ash/launcher/app_window_launcher_item_controller.h"
13 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
14 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
15 #include "chrome/browser/ui/host_desktop.h"
16 #include "extensions/browser/app_window/app_window.h"
17 #include "extensions/common/extension.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/wm/public/activation_client.h"
21 using extensions::AppWindow;
22 using extensions::AppWindowRegistry;
24 namespace {
26 std::string GetAppShelfId(AppWindow* app_window) {
27 if (app_window->window_type_is_panel())
28 return base::StringPrintf("panel:%d", app_window->session_id().id());
29 return app_window->extension_id();
32 bool ControlsWindow(aura::Window* window) {
33 return chrome::GetHostDesktopTypeForNativeWindow(window) ==
34 chrome::HOST_DESKTOP_TYPE_ASH;
37 } // namespace
39 AppWindowLauncherController::AppWindowLauncherController(
40 ChromeLauncherController* owner)
41 : owner_(owner), activation_client_(NULL) {
42 AppWindowRegistry* registry = AppWindowRegistry::Get(owner->profile());
43 registry_.insert(registry);
44 registry->AddObserver(this);
45 if (ash::Shell::HasInstance()) {
46 if (ash::Shell::GetInstance()->GetPrimaryRootWindow()) {
47 activation_client_ = aura::client::GetActivationClient(
48 ash::Shell::GetInstance()->GetPrimaryRootWindow());
49 if (activation_client_)
50 activation_client_->AddObserver(this);
55 AppWindowLauncherController::~AppWindowLauncherController() {
56 for (std::set<AppWindowRegistry*>::iterator it = registry_.begin();
57 it != registry_.end();
58 ++it)
59 (*it)->RemoveObserver(this);
61 if (activation_client_)
62 activation_client_->RemoveObserver(this);
63 for (WindowToAppShelfIdMap::iterator iter =
64 window_to_app_shelf_id_map_.begin();
65 iter != window_to_app_shelf_id_map_.end();
66 ++iter) {
67 iter->first->RemoveObserver(this);
71 void AppWindowLauncherController::AdditionalUserAddedToSession(
72 Profile* profile) {
73 // TODO(skuhne): This was added for the legacy side by side mode in M32. If
74 // this mode gets no longer pursued this special case can be removed.
75 if (chrome::MultiUserWindowManager::GetMultiProfileMode() !=
76 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_MIXED)
77 return;
79 AppWindowRegistry* registry = AppWindowRegistry::Get(profile);
80 if (registry_.find(registry) != registry_.end())
81 return;
83 registry->AddObserver(this);
84 registry_.insert(registry);
87 void AppWindowLauncherController::OnAppWindowIconChanged(
88 AppWindow* app_window) {
89 if (!ControlsWindow(app_window->GetNativeWindow()))
90 return;
92 const std::string app_shelf_id = GetAppShelfId(app_window);
93 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id);
94 if (iter == app_controller_map_.end())
95 return;
96 AppWindowLauncherItemController* controller = iter->second;
97 controller->set_image_set_by_controller(true);
98 owner_->SetLauncherItemImage(controller->shelf_id(),
99 app_window->app_icon().AsImageSkia());
102 void AppWindowLauncherController::OnAppWindowShown(AppWindow* app_window,
103 bool was_hidden) {
104 aura::Window* window = app_window->GetNativeWindow();
105 if (!ControlsWindow(window))
106 return;
108 if (!IsRegisteredApp(window))
109 RegisterApp(app_window);
112 void AppWindowLauncherController::OnAppWindowHidden(AppWindow* app_window) {
113 aura::Window* window = app_window->GetNativeWindow();
114 if (!ControlsWindow(window))
115 return;
117 if (IsRegisteredApp(window))
118 UnregisterApp(window);
121 // Called from aura::Window::~Window(), before delegate_->OnWindowDestroyed()
122 // which destroys AppWindow, so both |window| and the associated AppWindow
123 // are valid here.
124 void AppWindowLauncherController::OnWindowDestroying(aura::Window* window) {
125 if (!ControlsWindow(window))
126 return;
127 UnregisterApp(window);
130 void AppWindowLauncherController::OnWindowActivated(
131 aura::client::ActivationChangeObserver::ActivationReason reason,
132 aura::Window* new_active,
133 aura::Window* old_active) {
134 // Make the newly active window the active (first) entry in the controller.
135 AppWindowLauncherItemController* new_controller =
136 ControllerForWindow(new_active);
137 if (new_controller) {
138 new_controller->SetActiveWindow(new_active);
139 owner_->SetItemStatus(new_controller->shelf_id(), ash::STATUS_ACTIVE);
142 // Mark the old active window's launcher item as running (if different).
143 AppWindowLauncherItemController* old_controller =
144 ControllerForWindow(old_active);
145 if (old_controller && old_controller != new_controller)
146 owner_->SetItemStatus(old_controller->shelf_id(), ash::STATUS_RUNNING);
149 void AppWindowLauncherController::RegisterApp(AppWindow* app_window) {
150 // Windows created by IME extension should be treated the same way as the
151 // virtual keyboard window, which does not register itself in launcher.
152 if (app_window->is_ime_window())
153 return;
155 aura::Window* window = app_window->GetNativeWindow();
156 // Get the app's shelf identifier and add an entry to the map.
157 DCHECK(window_to_app_shelf_id_map_.find(window) ==
158 window_to_app_shelf_id_map_.end());
159 const std::string app_shelf_id = GetAppShelfId(app_window);
160 window_to_app_shelf_id_map_[window] = app_shelf_id;
161 window->AddObserver(this);
163 // Find or create an item controller and launcher item.
164 std::string app_id = app_window->extension_id();
165 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window)
166 ? ash::STATUS_ACTIVE
167 : ash::STATUS_RUNNING;
168 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id);
169 ash::ShelfID shelf_id = 0;
170 if (iter != app_controller_map_.end()) {
171 AppWindowLauncherItemController* controller = iter->second;
172 DCHECK(controller->app_id() == app_id);
173 shelf_id = controller->shelf_id();
174 controller->AddAppWindow(app_window, status);
175 } else {
176 LauncherItemController::Type type =
177 app_window->window_type_is_panel()
178 ? LauncherItemController::TYPE_APP_PANEL
179 : LauncherItemController::TYPE_APP;
180 AppWindowLauncherItemController* controller =
181 new AppWindowLauncherItemController(type, app_shelf_id, app_id, owner_);
182 controller->AddAppWindow(app_window, status);
183 // If the app shelf id is not unique, and there is already a shelf
184 // item for this app id (e.g. pinned), use that shelf item.
185 if (app_shelf_id == app_id)
186 shelf_id = owner_->GetShelfIDForAppID(app_id);
187 if (shelf_id == 0) {
188 shelf_id = owner_->CreateAppLauncherItem(controller, app_id, status);
189 // Restore any existing app icon and flag as set.
190 const gfx::Image& app_icon = app_window->app_icon();
191 if (!app_icon.IsEmpty()) {
192 owner_->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia());
193 controller->set_image_set_by_controller(true);
195 } else {
196 owner_->SetItemController(shelf_id, controller);
198 const std::string app_shelf_id = GetAppShelfId(app_window);
199 app_controller_map_[app_shelf_id] = controller;
201 owner_->SetItemStatus(shelf_id, status);
202 ash::SetShelfIDForWindow(shelf_id, window);
205 void AppWindowLauncherController::UnregisterApp(aura::Window* window) {
206 WindowToAppShelfIdMap::iterator iter1 =
207 window_to_app_shelf_id_map_.find(window);
208 DCHECK(iter1 != window_to_app_shelf_id_map_.end());
209 std::string app_shelf_id = iter1->second;
210 window_to_app_shelf_id_map_.erase(iter1);
211 window->RemoveObserver(this);
213 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id);
214 DCHECK(iter2 != app_controller_map_.end());
215 AppWindowLauncherItemController* controller = iter2->second;
216 controller->RemoveAppWindowForWindow(window);
217 if (controller->app_window_count() == 0) {
218 // If this is the last window associated with the app shelf id, close the
219 // shelf item.
220 ash::ShelfID shelf_id = controller->shelf_id();
221 owner_->CloseLauncherItem(shelf_id);
222 app_controller_map_.erase(iter2);
226 bool AppWindowLauncherController::IsRegisteredApp(aura::Window* window) {
227 return window_to_app_shelf_id_map_.find(window) !=
228 window_to_app_shelf_id_map_.end();
231 // Private Methods
233 AppWindowLauncherItemController*
234 AppWindowLauncherController::ControllerForWindow(aura::Window* window) {
235 WindowToAppShelfIdMap::iterator iter1 =
236 window_to_app_shelf_id_map_.find(window);
237 if (iter1 == window_to_app_shelf_id_map_.end())
238 return NULL;
239 std::string app_shelf_id = iter1->second;
240 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id);
241 if (iter2 == app_controller_map_.end())
242 return NULL;
243 return iter2->second;