Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: chrome/browser/ui/ash/launcher/extension_app_window_launcher_controller.cc

Issue 1823923002: arc: Support running Arc app in shelf (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nits Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/ash/launcher/app_window_launcher_controller.h" 5 #include "chrome/browser/ui/ash/launcher/extension_app_window_launcher_controlle r.h"
6 6
7 #include "ash/shelf/shelf_util.h" 7 #include "ash/shelf/shelf_util.h"
8 #include "ash/shell.h" 8 #include "ash/shell.h"
9 #include "ash/wm/window_util.h" 9 #include "ash/wm/window_util.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "chrome/browser/profiles/profile.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" 12 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
13 #include "chrome/browser/ui/ash/launcher/extension_app_window_launcher_item_cont roller.h"
14 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" 14 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
15 #include "extensions/browser/app_window/app_window.h" 15 #include "extensions/browser/app_window/app_window.h"
16 #include "extensions/browser/app_window/native_app_window.h"
16 #include "extensions/common/extension.h" 17 #include "extensions/common/extension.h"
17 #include "ui/aura/window_event_dispatcher.h" 18 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/wm/public/activation_client.h" 19 #include "ui/wm/public/activation_client.h"
Mr4D (OOO till 08-26) 2016/03/24 23:03:46 You can remove the include since it isn't used any
khmel 2016/03/24 23:56:53 Done.
19 20
20 using extensions::AppWindow; 21 using extensions::AppWindow;
21 using extensions::AppWindowRegistry; 22 using extensions::AppWindowRegistry;
22 23
23 namespace { 24 namespace {
24 25
25 std::string GetAppShelfId(AppWindow* app_window) { 26 std::string GetAppShelfId(AppWindow* app_window) {
26 if (app_window->window_type_is_panel()) 27 if (app_window->window_type_is_panel())
27 return base::StringPrintf("panel:%d", app_window->session_id().id()); 28 return base::StringPrintf("panel:%d", app_window->session_id().id());
28 return app_window->extension_id(); 29 return app_window->extension_id();
29 } 30 }
30 31
31 } // namespace 32 } // namespace
32 33
33 AppWindowLauncherController::AppWindowLauncherController( 34 ExtensionAppWindowLauncherController::ExtensionAppWindowLauncherController(
34 ChromeLauncherController* owner) 35 ChromeLauncherController* owner)
35 : owner_(owner), activation_client_(NULL) { 36 : AppWindowLauncherController(owner) {
36 AppWindowRegistry* registry = AppWindowRegistry::Get(owner->profile()); 37 AppWindowRegistry* registry = AppWindowRegistry::Get(owner->profile());
37 registry_.insert(registry); 38 registry_.insert(registry);
38 registry->AddObserver(this); 39 registry->AddObserver(this);
39 if (ash::Shell::HasInstance()) {
40 if (ash::Shell::GetInstance()->GetPrimaryRootWindow()) {
41 activation_client_ = aura::client::GetActivationClient(
42 ash::Shell::GetInstance()->GetPrimaryRootWindow());
43 if (activation_client_)
44 activation_client_->AddObserver(this);
45 }
46 }
47 } 40 }
48 41
49 AppWindowLauncherController::~AppWindowLauncherController() { 42 ExtensionAppWindowLauncherController::~ExtensionAppWindowLauncherController() {
50 for (std::set<AppWindowRegistry*>::iterator it = registry_.begin(); 43 for (std::set<AppWindowRegistry*>::iterator it = registry_.begin();
51 it != registry_.end(); 44 it != registry_.end(); ++it)
52 ++it)
53 (*it)->RemoveObserver(this); 45 (*it)->RemoveObserver(this);
54 46
55 if (activation_client_)
56 activation_client_->RemoveObserver(this);
57 for (WindowToAppShelfIdMap::iterator iter = 47 for (WindowToAppShelfIdMap::iterator iter =
58 window_to_app_shelf_id_map_.begin(); 48 window_to_app_shelf_id_map_.begin();
59 iter != window_to_app_shelf_id_map_.end(); 49 iter != window_to_app_shelf_id_map_.end(); ++iter) {
60 ++iter) {
61 iter->first->RemoveObserver(this); 50 iter->first->RemoveObserver(this);
62 } 51 }
63 } 52 }
64 53
65 void AppWindowLauncherController::AdditionalUserAddedToSession( 54 void ExtensionAppWindowLauncherController::AdditionalUserAddedToSession(
66 Profile* profile) { 55 Profile* profile) {
67 // TODO(skuhne): This was added for the legacy side by side mode in M32. If 56 // TODO(skuhne): This was added for the legacy side by side mode in M32. If
68 // this mode gets no longer pursued this special case can be removed. 57 // this mode gets no longer pursued this special case can be removed.
69 if (chrome::MultiUserWindowManager::GetMultiProfileMode() != 58 if (chrome::MultiUserWindowManager::GetMultiProfileMode() !=
70 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_MIXED) 59 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_MIXED)
71 return; 60 return;
72 61
73 AppWindowRegistry* registry = AppWindowRegistry::Get(profile); 62 AppWindowRegistry* registry = AppWindowRegistry::Get(profile);
74 if (registry_.find(registry) != registry_.end()) 63 if (registry_.find(registry) != registry_.end())
75 return; 64 return;
76 65
77 registry->AddObserver(this); 66 registry->AddObserver(this);
78 registry_.insert(registry); 67 registry_.insert(registry);
79 } 68 }
80 69
81 void AppWindowLauncherController::OnAppWindowIconChanged( 70 void ExtensionAppWindowLauncherController::OnAppWindowIconChanged(
82 AppWindow* app_window) { 71 AppWindow* app_window) {
83 const std::string app_shelf_id = GetAppShelfId(app_window); 72 const std::string app_shelf_id = GetAppShelfId(app_window);
84 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id); 73 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id);
85 if (iter == app_controller_map_.end()) 74 if (iter == app_controller_map_.end())
86 return; 75 return;
87 AppWindowLauncherItemController* controller = iter->second; 76 ExtensionAppWindowLauncherItemController* controller = iter->second;
88 controller->set_image_set_by_controller(true); 77 controller->set_image_set_by_controller(true);
89 owner_->SetLauncherItemImage(controller->shelf_id(), 78 owner()->SetLauncherItemImage(controller->shelf_id(),
90 app_window->app_icon().AsImageSkia()); 79 app_window->app_icon().AsImageSkia());
91 } 80 }
92 81
93 void AppWindowLauncherController::OnAppWindowShown(AppWindow* app_window, 82 void ExtensionAppWindowLauncherController::OnAppWindowShown(
94 bool was_hidden) { 83 AppWindow* app_window,
84 bool was_hidden) {
95 aura::Window* window = app_window->GetNativeWindow(); 85 aura::Window* window = app_window->GetNativeWindow();
96 if (!IsRegisteredApp(window)) 86 if (!IsRegisteredApp(window))
97 RegisterApp(app_window); 87 RegisterApp(app_window);
98 } 88 }
99 89
100 void AppWindowLauncherController::OnAppWindowHidden(AppWindow* app_window) { 90 void ExtensionAppWindowLauncherController::OnAppWindowHidden(
91 AppWindow* app_window) {
101 aura::Window* window = app_window->GetNativeWindow(); 92 aura::Window* window = app_window->GetNativeWindow();
102 if (IsRegisteredApp(window)) 93 if (IsRegisteredApp(window))
103 UnregisterApp(window); 94 UnregisterApp(window);
104 } 95 }
105 96
106 // Called from aura::Window::~Window(), before delegate_->OnWindowDestroyed() 97 // Called from aura::Window::~Window(), before delegate_->OnWindowDestroyed()
107 // which destroys AppWindow, so both |window| and the associated AppWindow 98 // which destroys AppWindow, so both |window| and the associated AppWindow
108 // are valid here. 99 // are valid here.
109 void AppWindowLauncherController::OnWindowDestroying(aura::Window* window) { 100 void ExtensionAppWindowLauncherController::OnWindowDestroying(
101 aura::Window* window) {
110 UnregisterApp(window); 102 UnregisterApp(window);
111 } 103 }
112 104
113 void AppWindowLauncherController::OnWindowActivated( 105 void ExtensionAppWindowLauncherController::RegisterApp(AppWindow* app_window) {
114 aura::client::ActivationChangeObserver::ActivationReason reason,
115 aura::Window* new_active,
116 aura::Window* old_active) {
117 // Make the newly active window the active (first) entry in the controller.
118 AppWindowLauncherItemController* new_controller =
119 ControllerForWindow(new_active);
120 if (new_controller) {
121 new_controller->SetActiveWindow(new_active);
122 owner_->SetItemStatus(new_controller->shelf_id(), ash::STATUS_ACTIVE);
123 }
124
125 // Mark the old active window's launcher item as running (if different).
126 AppWindowLauncherItemController* old_controller =
127 ControllerForWindow(old_active);
128 if (old_controller && old_controller != new_controller)
129 owner_->SetItemStatus(old_controller->shelf_id(), ash::STATUS_RUNNING);
130 }
131
132 void AppWindowLauncherController::RegisterApp(AppWindow* app_window) {
133 // Windows created by IME extension should be treated the same way as the 106 // Windows created by IME extension should be treated the same way as the
134 // virtual keyboard window, which does not register itself in launcher. 107 // virtual keyboard window, which does not register itself in launcher.
135 if (app_window->is_ime_window()) 108 if (app_window->is_ime_window())
136 return; 109 return;
137 110
138 aura::Window* window = app_window->GetNativeWindow(); 111 aura::Window* window = app_window->GetNativeWindow();
139 // Get the app's shelf identifier and add an entry to the map. 112 // Get the app's shelf identifier and add an entry to the map.
140 DCHECK(window_to_app_shelf_id_map_.find(window) == 113 DCHECK(window_to_app_shelf_id_map_.find(window) ==
141 window_to_app_shelf_id_map_.end()); 114 window_to_app_shelf_id_map_.end());
142 const std::string app_shelf_id = GetAppShelfId(app_window); 115 const std::string app_shelf_id = GetAppShelfId(app_window);
143 window_to_app_shelf_id_map_[window] = app_shelf_id; 116 window_to_app_shelf_id_map_[window] = app_shelf_id;
144 window->AddObserver(this); 117 window->AddObserver(this);
145 118
146 // Find or create an item controller and launcher item. 119 // Find or create an item controller and launcher item.
147 std::string app_id = app_window->extension_id(); 120 std::string app_id = app_window->extension_id();
148 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window) 121 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window)
149 ? ash::STATUS_ACTIVE 122 ? ash::STATUS_ACTIVE
150 : ash::STATUS_RUNNING; 123 : ash::STATUS_RUNNING;
151 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id); 124 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id);
152 ash::ShelfID shelf_id = 0; 125 ash::ShelfID shelf_id = 0;
153 if (iter != app_controller_map_.end()) { 126 if (iter != app_controller_map_.end()) {
154 AppWindowLauncherItemController* controller = iter->second; 127 ExtensionAppWindowLauncherItemController* controller = iter->second;
155 DCHECK(controller->app_id() == app_id); 128 DCHECK(controller->app_id() == app_id);
156 shelf_id = controller->shelf_id(); 129 shelf_id = controller->shelf_id();
157 controller->AddAppWindow(app_window, status); 130 controller->AddAppWindow(app_window);
158 } else { 131 } else {
159 LauncherItemController::Type type = 132 LauncherItemController::Type type =
160 app_window->window_type_is_panel() 133 app_window->window_type_is_panel()
161 ? LauncherItemController::TYPE_APP_PANEL 134 ? LauncherItemController::TYPE_APP_PANEL
162 : LauncherItemController::TYPE_APP; 135 : LauncherItemController::TYPE_APP;
163 AppWindowLauncherItemController* controller = 136 ExtensionAppWindowLauncherItemController* controller =
164 new AppWindowLauncherItemController(type, app_shelf_id, app_id, owner_); 137 new ExtensionAppWindowLauncherItemController(type, app_shelf_id, app_id,
165 controller->AddAppWindow(app_window, status); 138 owner());
139 controller->AddAppWindow(app_window);
166 // If the app shelf id is not unique, and there is already a shelf 140 // If the app shelf id is not unique, and there is already a shelf
167 // item for this app id (e.g. pinned), use that shelf item. 141 // item for this app id (e.g. pinned), use that shelf item.
168 if (app_shelf_id == app_id) 142 if (app_shelf_id == app_id)
169 shelf_id = owner_->GetShelfIDForAppID(app_id); 143 shelf_id = owner()->GetShelfIDForAppID(app_id);
170 if (shelf_id == 0) { 144 if (shelf_id == 0) {
171 shelf_id = owner_->CreateAppLauncherItem(controller, app_id, status); 145 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status);
172 // Restore any existing app icon and flag as set. 146 // Restore any existing app icon and flag as set.
173 const gfx::Image& app_icon = app_window->app_icon(); 147 const gfx::Image& app_icon = app_window->app_icon();
174 if (!app_icon.IsEmpty()) { 148 if (!app_icon.IsEmpty()) {
175 owner_->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); 149 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia());
176 controller->set_image_set_by_controller(true); 150 controller->set_image_set_by_controller(true);
177 } 151 }
178 } else { 152 } else {
179 owner_->SetItemController(shelf_id, controller); 153 owner()->SetItemController(shelf_id, controller);
180 } 154 }
181 const std::string app_shelf_id = GetAppShelfId(app_window); 155 const std::string app_shelf_id = GetAppShelfId(app_window);
182 app_controller_map_[app_shelf_id] = controller; 156 app_controller_map_[app_shelf_id] = controller;
183 } 157 }
184 owner_->SetItemStatus(shelf_id, status); 158 owner()->SetItemStatus(shelf_id, status);
185 ash::SetShelfIDForWindow(shelf_id, window); 159 ash::SetShelfIDForWindow(shelf_id, window);
186 } 160 }
187 161
188 void AppWindowLauncherController::UnregisterApp(aura::Window* window) { 162 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) {
189 WindowToAppShelfIdMap::iterator iter1 = 163 WindowToAppShelfIdMap::iterator iter1 =
190 window_to_app_shelf_id_map_.find(window); 164 window_to_app_shelf_id_map_.find(window);
191 DCHECK(iter1 != window_to_app_shelf_id_map_.end()); 165 DCHECK(iter1 != window_to_app_shelf_id_map_.end());
192 std::string app_shelf_id = iter1->second; 166 std::string app_shelf_id = iter1->second;
193 window_to_app_shelf_id_map_.erase(iter1); 167 window_to_app_shelf_id_map_.erase(iter1);
194 window->RemoveObserver(this); 168 window->RemoveObserver(this);
195 169
196 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id); 170 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id);
197 DCHECK(iter2 != app_controller_map_.end()); 171 DCHECK(iter2 != app_controller_map_.end());
198 AppWindowLauncherItemController* controller = iter2->second; 172 ExtensionAppWindowLauncherItemController* controller = iter2->second;
199 controller->RemoveAppWindowForWindow(window); 173 controller->RemoveWindowForNativeWindow(window);
200 if (controller->app_window_count() == 0) { 174 if (controller->window_count() == 0) {
201 // If this is the last window associated with the app shelf id, close the 175 // If this is the last window associated with the app shelf id, close the
202 // shelf item. 176 // shelf item.
203 ash::ShelfID shelf_id = controller->shelf_id(); 177 ash::ShelfID shelf_id = controller->shelf_id();
204 owner_->CloseLauncherItem(shelf_id); 178 owner()->CloseLauncherItem(shelf_id);
205 app_controller_map_.erase(iter2); 179 app_controller_map_.erase(iter2);
206 } 180 }
207 } 181 }
208 182
209 bool AppWindowLauncherController::IsRegisteredApp(aura::Window* window) { 183 bool ExtensionAppWindowLauncherController::IsRegisteredApp(
184 aura::Window* window) {
210 return window_to_app_shelf_id_map_.find(window) != 185 return window_to_app_shelf_id_map_.find(window) !=
211 window_to_app_shelf_id_map_.end(); 186 window_to_app_shelf_id_map_.end();
212 } 187 }
213 188
214 // Private Methods 189 // Private Methods
215 190
216 AppWindowLauncherItemController* 191 AppWindowLauncherItemController*
217 AppWindowLauncherController::ControllerForWindow(aura::Window* window) { 192 ExtensionAppWindowLauncherController::ControllerForWindow(
193 aura::Window* window) {
218 WindowToAppShelfIdMap::iterator iter1 = 194 WindowToAppShelfIdMap::iterator iter1 =
219 window_to_app_shelf_id_map_.find(window); 195 window_to_app_shelf_id_map_.find(window);
220 if (iter1 == window_to_app_shelf_id_map_.end()) 196 if (iter1 == window_to_app_shelf_id_map_.end())
221 return NULL; 197 return nullptr;
222 std::string app_shelf_id = iter1->second; 198 std::string app_shelf_id = iter1->second;
223 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id); 199 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id);
224 if (iter2 == app_controller_map_.end()) 200 if (iter2 == app_controller_map_.end())
225 return NULL; 201 return nullptr;
226 return iter2->second; 202 return iter2->second;
227 } 203 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698