Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / ash / launcher / app_window_launcher_controller.cc
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/ash/launcher/app_window_launcher_controller.h"
6
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"
20
21 using extensions::AppWindow;
22 using extensions::AppWindowRegistry;
23
24 namespace {
25
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();
30 }
31
32 bool ControlsWindow(aura::Window* window) {
33   return chrome::GetHostDesktopTypeForNativeWindow(window) ==
34          chrome::HOST_DESKTOP_TYPE_ASH;
35 }
36
37 }  // namespace
38
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);
51     }
52   }
53 }
54
55 AppWindowLauncherController::~AppWindowLauncherController() {
56   for (std::set<AppWindowRegistry*>::iterator it = registry_.begin();
57        it != registry_.end();
58        ++it)
59     (*it)->RemoveObserver(this);
60
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);
68   }
69 }
70
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;
78
79   AppWindowRegistry* registry = AppWindowRegistry::Get(profile);
80   if (registry_.find(registry) != registry_.end())
81     return;
82
83   registry->AddObserver(this);
84   registry_.insert(registry);
85 }
86
87 void AppWindowLauncherController::OnAppWindowIconChanged(
88     AppWindow* app_window) {
89   if (!ControlsWindow(app_window->GetNativeWindow()))
90     return;
91
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());
100 }
101
102 void AppWindowLauncherController::OnAppWindowShown(AppWindow* app_window,
103                                                    bool was_hidden) {
104   aura::Window* window = app_window->GetNativeWindow();
105   if (!ControlsWindow(window))
106     return;
107
108   if (!IsRegisteredApp(window))
109     RegisterApp(app_window);
110 }
111
112 void AppWindowLauncherController::OnAppWindowHidden(AppWindow* app_window) {
113   aura::Window* window = app_window->GetNativeWindow();
114   if (!ControlsWindow(window))
115     return;
116
117   if (IsRegisteredApp(window))
118     UnregisterApp(window);
119 }
120
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);
128 }
129
130 void AppWindowLauncherController::OnWindowActivated(aura::Window* new_active,
131                                                     aura::Window* old_active) {
132   // Make the newly active window the active (first) entry in the controller.
133   AppWindowLauncherItemController* new_controller =
134       ControllerForWindow(new_active);
135   if (new_controller) {
136     new_controller->SetActiveWindow(new_active);
137     owner_->SetItemStatus(new_controller->shelf_id(), ash::STATUS_ACTIVE);
138   }
139
140   // Mark the old active window's launcher item as running (if different).
141   AppWindowLauncherItemController* old_controller =
142       ControllerForWindow(old_active);
143   if (old_controller && old_controller != new_controller)
144     owner_->SetItemStatus(old_controller->shelf_id(), ash::STATUS_RUNNING);
145 }
146
147 void AppWindowLauncherController::RegisterApp(AppWindow* app_window) {
148   aura::Window* window = app_window->GetNativeWindow();
149   // Get the app's shelf identifier and add an entry to the map.
150   DCHECK(window_to_app_shelf_id_map_.find(window) ==
151          window_to_app_shelf_id_map_.end());
152   const std::string app_shelf_id = GetAppShelfId(app_window);
153   window_to_app_shelf_id_map_[window] = app_shelf_id;
154   window->AddObserver(this);
155
156   // Find or create an item controller and launcher item.
157   std::string app_id = app_window->extension_id();
158   ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window)
159                                     ? ash::STATUS_ACTIVE
160                                     : ash::STATUS_RUNNING;
161   AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id);
162   ash::ShelfID shelf_id = 0;
163   if (iter != app_controller_map_.end()) {
164     AppWindowLauncherItemController* controller = iter->second;
165     DCHECK(controller->app_id() == app_id);
166     shelf_id = controller->shelf_id();
167     controller->AddAppWindow(app_window, status);
168   } else {
169     LauncherItemController::Type type =
170         app_window->window_type_is_panel()
171             ? LauncherItemController::TYPE_APP_PANEL
172             : LauncherItemController::TYPE_APP;
173     AppWindowLauncherItemController* controller =
174         new AppWindowLauncherItemController(type, app_shelf_id, app_id, owner_);
175     controller->AddAppWindow(app_window, status);
176     // If the app shelf id is not unique, and there is already a shelf
177     // item for this app id (e.g. pinned), use that shelf item.
178     if (app_shelf_id == app_id)
179       shelf_id = owner_->GetShelfIDForAppID(app_id);
180     if (shelf_id == 0) {
181       shelf_id = owner_->CreateAppLauncherItem(controller, app_id, status);
182       // Restore any existing app icon and flag as set.
183       const gfx::Image& app_icon = app_window->app_icon();
184       if (!app_icon.IsEmpty()) {
185         owner_->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia());
186         controller->set_image_set_by_controller(true);
187       }
188     } else {
189       owner_->SetItemController(shelf_id, controller);
190     }
191     const std::string app_shelf_id = GetAppShelfId(app_window);
192     app_controller_map_[app_shelf_id] = controller;
193   }
194   owner_->SetItemStatus(shelf_id, status);
195   ash::SetShelfIDForWindow(shelf_id, window);
196 }
197
198 void AppWindowLauncherController::UnregisterApp(aura::Window* window) {
199   WindowToAppShelfIdMap::iterator iter1 =
200       window_to_app_shelf_id_map_.find(window);
201   DCHECK(iter1 != window_to_app_shelf_id_map_.end());
202   std::string app_shelf_id = iter1->second;
203   window_to_app_shelf_id_map_.erase(iter1);
204   window->RemoveObserver(this);
205
206   AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id);
207   DCHECK(iter2 != app_controller_map_.end());
208   AppWindowLauncherItemController* controller = iter2->second;
209   controller->RemoveAppWindowForWindow(window);
210   if (controller->app_window_count() == 0) {
211     // If this is the last window associated with the app shelf id, close the
212     // shelf item.
213     ash::ShelfID shelf_id = controller->shelf_id();
214     owner_->CloseLauncherItem(shelf_id);
215     app_controller_map_.erase(iter2);
216   }
217 }
218
219 bool AppWindowLauncherController::IsRegisteredApp(aura::Window* window) {
220   return window_to_app_shelf_id_map_.find(window) !=
221          window_to_app_shelf_id_map_.end();
222 }
223
224 // Private Methods
225
226 AppWindowLauncherItemController*
227 AppWindowLauncherController::ControllerForWindow(aura::Window* window) {
228   WindowToAppShelfIdMap::iterator iter1 =
229       window_to_app_shelf_id_map_.find(window);
230   if (iter1 == window_to_app_shelf_id_map_.end())
231     return NULL;
232   std::string app_shelf_id = iter1->second;
233   AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id);
234   if (iter2 == app_controller_map_.end())
235     return NULL;
236   return iter2->second;
237 }