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

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

Issue 9808073: okashii (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: a Created 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/views/ash/launcher/chrome_launcher_delegate.h" 5 #include "chrome/browser/ui/views/ash/launcher/chrome_launcher_delegate.h"
6 6
7 #include "ash/launcher/launcher_model.h" 7 #include "ash/launcher/launcher_model.h"
8 #include "ash/launcher/launcher_types.h" 8 #include "ash/launcher/launcher_types.h"
9 #include "ash/shell.h" 9 #include "ash/shell.h"
10 #include "ash/wm/window_util.h" 10 #include "ash/wm/window_util.h"
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 62
63 // ChromeLauncherDelegate ------------------------------------------------------ 63 // ChromeLauncherDelegate ------------------------------------------------------
64 64
65 // static 65 // static
66 ChromeLauncherDelegate* ChromeLauncherDelegate::instance_ = NULL; 66 ChromeLauncherDelegate* ChromeLauncherDelegate::instance_ = NULL;
67 67
68 ChromeLauncherDelegate::ChromeLauncherDelegate(Profile* profile, 68 ChromeLauncherDelegate::ChromeLauncherDelegate(Profile* profile,
69 ash::LauncherModel* model) 69 ash::LauncherModel* model)
70 : model_(model), 70 : model_(model),
71 profile_(profile) { 71 profile_(profile) {
72 if (!profile_) {
73 // Use the original profile as on chromeos we may get a temporary off the
74 // record profile.
75 profile_ = ProfileManager::GetDefaultProfile()->GetOriginalProfile();
76 }
77 instance_ = this; 72 instance_ = this;
78 model_->AddObserver(this); 73 model_->AddObserver(this);
79 app_icon_loader_.reset(new LauncherAppIconLoader(profile_, this)); 74
80 registrar_.Add(this, 75 if (profile) {
81 chrome::NOTIFICATION_EXTENSION_UNLOADED, 76 PostProfileInit();
82 content::Source<Profile>(profile_)); 77 } else {
78 registrar_.Add(this,
79 chrome::NOTIFICATION_PROFILE_CREATED,
80 content::NotificationService::AllSources());
81 }
83 } 82 }
84 83
85 ChromeLauncherDelegate::~ChromeLauncherDelegate() { 84 ChromeLauncherDelegate::~ChromeLauncherDelegate() {
86 model_->RemoveObserver(this); 85 model_->RemoveObserver(this);
87 for (IDToItemMap::iterator i = id_to_item_map_.begin(); 86 for (IDToItemMap::iterator i = id_to_item_map_.begin();
88 i != id_to_item_map_.end(); ++i) { 87 i != id_to_item_map_.end(); ++i) {
89 model_->RemoveItemAt(model_->ItemIndexByID(i->first)); 88 model_->RemoveItemAt(model_->ItemIndexByID(i->first));
90 } 89 }
91 if (instance_ == this) 90 if (instance_ == this)
92 instance_ = NULL; 91 instance_ = NULL;
93 } 92 }
94 93
95 void ChromeLauncherDelegate::Init() {
96 const base::ListValue* pinned_apps =
97 profile_->GetPrefs()->GetList(prefs::kPinnedLauncherApps);
98 for (size_t i = 0; i < pinned_apps->GetSize(); ++i) {
99 DictionaryValue* app = NULL;
100 if (pinned_apps->GetDictionary(i, &app)) {
101 std::string app_id, type_string;
102 if (app->GetString(kAppIDPath, &app_id) &&
103 app->GetString(kAppTypePath, &type_string) &&
104 app_icon_loader_->IsValidID(app_id)) {
105 AppType app_type;
106 if (type_string == kAppTypeWindow)
107 app_type = APP_TYPE_WINDOW;
108 else if (type_string == kAppTypePanel)
109 app_type = APP_TYPE_APP_PANEL;
110 else
111 app_type = APP_TYPE_TAB;
112 CreateAppLauncherItem(NULL, app_id, app_type, ash::STATUS_CLOSED);
113 }
114 }
115 }
116 }
117
118 // static 94 // static
119 void ChromeLauncherDelegate::RegisterUserPrefs(PrefService* user_prefs) { 95 void ChromeLauncherDelegate::RegisterUserPrefs(PrefService* user_prefs) {
120 // TODO: If we want to support multiple profiles this will likely need to be 96 // TODO: If we want to support multiple profiles this will likely need to be
121 // pushed to local state and we'll need to track profile per item. 97 // pushed to local state and we'll need to track profile per item.
122 user_prefs->RegisterListPref(prefs::kPinnedLauncherApps, 98 user_prefs->RegisterListPref(prefs::kPinnedLauncherApps,
123 PrefService::SYNCABLE_PREF); 99 PrefService::SYNCABLE_PREF);
124 } 100 }
125 101
126 ash::LauncherID ChromeLauncherDelegate::CreateTabbedLauncherItem( 102 ash::LauncherID ChromeLauncherDelegate::CreateTabbedLauncherItem(
127 LauncherUpdater* updater, 103 LauncherUpdater* updater,
(...skipping 18 matching lines...) Expand all
146 id_to_item_map_[id].item_type = TYPE_TABBED_BROWSER; 122 id_to_item_map_[id].item_type = TYPE_TABBED_BROWSER;
147 id_to_item_map_[id].updater = updater; 123 id_to_item_map_[id].updater = updater;
148 return id; 124 return id;
149 } 125 }
150 126
151 ash::LauncherID ChromeLauncherDelegate::CreateAppLauncherItem( 127 ash::LauncherID ChromeLauncherDelegate::CreateAppLauncherItem(
152 LauncherUpdater* updater, 128 LauncherUpdater* updater,
153 const std::string& app_id, 129 const std::string& app_id,
154 AppType app_type, 130 AppType app_type,
155 ash::LauncherItemStatus status) { 131 ash::LauncherItemStatus status) {
132 DCHECK(profile_);
156 // See if we have a closed item that matches the app. 133 // See if we have a closed item that matches the app.
157 if (updater) { 134 if (updater) {
158 for (IDToItemMap::iterator i = id_to_item_map_.begin(); 135 for (IDToItemMap::iterator i = id_to_item_map_.begin();
159 i != id_to_item_map_.end(); ++i) { 136 i != id_to_item_map_.end(); ++i) {
160 if (i->second.updater == NULL && i->second.app_id == app_id && 137 if (i->second.updater == NULL && i->second.app_id == app_id &&
161 i->second.app_type == app_type) { 138 i->second.app_type == app_type) {
162 i->second.updater = updater; 139 i->second.updater = updater;
163 SetItemStatus(i->first, status); 140 SetItemStatus(i->first, status);
164 return i->first; 141 return i->first;
165 } 142 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 id_to_item_map_[id].item_type = TYPE_TABBED_BROWSER; 199 id_to_item_map_[id].item_type = TYPE_TABBED_BROWSER;
223 id_to_item_map_[id].app_id.clear(); 200 id_to_item_map_[id].app_id.clear();
224 } 201 }
225 202
226 void ChromeLauncherDelegate::ConvertTabbedToApp(ash::LauncherID id, 203 void ChromeLauncherDelegate::ConvertTabbedToApp(ash::LauncherID id,
227 const std::string& app_id, 204 const std::string& app_id,
228 AppType app_type) { 205 AppType app_type) {
229 DCHECK(id_to_item_map_.find(id) != id_to_item_map_.end()); 206 DCHECK(id_to_item_map_.find(id) != id_to_item_map_.end());
230 DCHECK_EQ(TYPE_TABBED_BROWSER, id_to_item_map_[id].item_type); 207 DCHECK_EQ(TYPE_TABBED_BROWSER, id_to_item_map_[id].item_type);
231 DCHECK(!id_to_item_map_[id].pinned); 208 DCHECK(!id_to_item_map_[id].pinned);
209 DCHECK(profile_);
232 id_to_item_map_[id].item_type = TYPE_APP; 210 id_to_item_map_[id].item_type = TYPE_APP;
233 id_to_item_map_[id].app_type = app_type; 211 id_to_item_map_[id].app_type = app_type;
234 id_to_item_map_[id].app_id = app_id; 212 id_to_item_map_[id].app_id = app_id;
235 213
236 ash::LauncherItem item; 214 ash::LauncherItem item;
237 item.type = ash::TYPE_APP; 215 item.type = ash::TYPE_APP;
238 item.is_incognito = false; 216 item.is_incognito = false;
239 item.id = id; 217 item.id = id;
240 model_->Set(model_->ItemIndexByID(id), item); 218 model_->Set(model_->ItemIndexByID(id), item);
241 219
242 app_icon_loader_->FetchImage(app_id); 220 app_icon_loader_->FetchImage(app_id);
243 } 221 }
244 222
245 void ChromeLauncherDelegate::LauncherItemClosed(ash::LauncherID id) { 223 void ChromeLauncherDelegate::LauncherItemClosed(ash::LauncherID id) {
246 DCHECK(id_to_item_map_.find(id) != id_to_item_map_.end()); 224 DCHECK(id_to_item_map_.find(id) != id_to_item_map_.end());
247 if (id_to_item_map_[id].pinned) { 225 if (id_to_item_map_[id].pinned) {
248 // The item is pinned, leave it in the launcher. 226 // The item is pinned, leave it in the launcher.
249 id_to_item_map_[id].updater = NULL; 227 id_to_item_map_[id].updater = NULL;
250 SetItemStatus(id, ash::STATUS_CLOSED); 228 SetItemStatus(id, ash::STATUS_CLOSED);
251 } else { 229 } else {
252 id_to_item_map_.erase(id); 230 id_to_item_map_.erase(id);
253 model_->RemoveItemAt(model_->ItemIndexByID(id)); 231 model_->RemoveItemAt(model_->ItemIndexByID(id));
254 } 232 }
255 } 233 }
256 234
257 void ChromeLauncherDelegate::AppIDChanged(ash::LauncherID id, 235 void ChromeLauncherDelegate::AppIDChanged(ash::LauncherID id,
258 const std::string& app_id) { 236 const std::string& app_id) {
259 DCHECK(id_to_item_map_.find(id) != id_to_item_map_.end()); 237 DCHECK(id_to_item_map_.find(id) != id_to_item_map_.end());
238 DCHECK(profile_);
260 id_to_item_map_[id].app_id = app_id; 239 id_to_item_map_[id].app_id = app_id;
261 PersistPinnedState(); 240 PersistPinnedState();
262 241
263 app_icon_loader_->FetchImage(app_id); 242 app_icon_loader_->FetchImage(app_id);
264 } 243 }
265 244
266 bool ChromeLauncherDelegate::HasClosedAppItem(const std::string& app_id, 245 bool ChromeLauncherDelegate::HasClosedAppItem(const std::string& app_id,
267 AppType app_type) { 246 AppType app_type) {
268 for (IDToItemMap::const_iterator i = id_to_item_map_.begin(); 247 for (IDToItemMap::const_iterator i = id_to_item_map_.begin();
269 i != id_to_item_map_.end(); ++i) { 248 i != id_to_item_map_.end(); ++i) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 284
306 bool ChromeLauncherDelegate::IsPinnable(ash::LauncherID id) { 285 bool ChromeLauncherDelegate::IsPinnable(ash::LauncherID id) {
307 // Only app windows and app panel windows can properly restore themselves; 286 // Only app windows and app panel windows can properly restore themselves;
308 // exptension panels can not so should not be pinnable. 287 // exptension panels can not so should not be pinnable.
309 return id_to_item_map_.find(id) != id_to_item_map_.end() && 288 return id_to_item_map_.find(id) != id_to_item_map_.end() &&
310 id_to_item_map_[id].item_type == TYPE_APP && 289 id_to_item_map_[id].item_type == TYPE_APP &&
311 id_to_item_map_[id].app_type != APP_TYPE_EXTENSION_PANEL; 290 id_to_item_map_[id].app_type != APP_TYPE_EXTENSION_PANEL;
312 } 291 }
313 292
314 void ChromeLauncherDelegate::Open(ash::LauncherID id) { 293 void ChromeLauncherDelegate::Open(ash::LauncherID id) {
294 DCHECK(profile_);
295
315 if (id_to_item_map_.find(id) == id_to_item_map_.end()) 296 if (id_to_item_map_.find(id) == id_to_item_map_.end())
316 return; // In case invoked from menu and item closed while menu up. 297 return; // In case invoked from menu and item closed while menu up.
317 298
318 LauncherUpdater* updater = id_to_item_map_[id].updater; 299 LauncherUpdater* updater = id_to_item_map_[id].updater;
319 if (updater) { 300 if (updater) {
320 updater->window()->Show(); 301 updater->window()->Show();
321 ash::wm::ActivateWindow(updater->window()); 302 ash::wm::ActivateWindow(updater->window());
322 TabContentsWrapper* tab = updater->GetTab(id); 303 TabContentsWrapper* tab = updater->GetTab(id);
323 if (tab) { 304 if (tab) {
324 updater->tab_model()->ActivateTabAt( 305 updater->tab_model()->ActivateTabAt(
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 id_to_item_map_[id].updater != NULL; 361 id_to_item_map_[id].updater != NULL;
381 } 362 }
382 363
383 ChromeLauncherDelegate::AppType ChromeLauncherDelegate::GetAppType( 364 ChromeLauncherDelegate::AppType ChromeLauncherDelegate::GetAppType(
384 ash::LauncherID id) { 365 ash::LauncherID id) {
385 DCHECK(id_to_item_map_.find(id) != id_to_item_map_.end()); 366 DCHECK(id_to_item_map_.find(id) != id_to_item_map_.end());
386 return id_to_item_map_[id].app_type; 367 return id_to_item_map_[id].app_type;
387 } 368 }
388 369
389 std::string ChromeLauncherDelegate::GetAppID(TabContentsWrapper* tab) { 370 std::string ChromeLauncherDelegate::GetAppID(TabContentsWrapper* tab) {
371 DCHECK(profile_);
390 return app_icon_loader_->GetAppID(tab); 372 return app_icon_loader_->GetAppID(tab);
391 } 373 }
392 374
393 void ChromeLauncherDelegate::SetAppImage(const std::string& id, 375 void ChromeLauncherDelegate::SetAppImage(const std::string& id,
394 const SkBitmap* image) { 376 const SkBitmap* image) {
395 for (IDToItemMap::const_iterator i = id_to_item_map_.begin(); 377 for (IDToItemMap::const_iterator i = id_to_item_map_.begin();
396 i != id_to_item_map_.end(); ++i) { 378 i != id_to_item_map_.end(); ++i) {
397 if (i->second.app_id != id) 379 if (i->second.app_id != id)
398 continue; 380 continue;
399 // Panel items may share the same app_id as the app that created them, 381 // Panel items may share the same app_id as the app that created them,
(...skipping 17 matching lines...) Expand all
417 DCHECK(id_to_item_map_.find(item.id) != id_to_item_map_.end()); 399 DCHECK(id_to_item_map_.find(item.id) != id_to_item_map_.end());
418 Open(item.id); 400 Open(item.id);
419 } 401 }
420 402
421 int ChromeLauncherDelegate::GetBrowserShortcutResourceId() { 403 int ChromeLauncherDelegate::GetBrowserShortcutResourceId() {
422 return IDR_PRODUCT_LOGO_32; 404 return IDR_PRODUCT_LOGO_32;
423 } 405 }
424 406
425 string16 ChromeLauncherDelegate::GetTitle(const ash::LauncherItem& item) { 407 string16 ChromeLauncherDelegate::GetTitle(const ash::LauncherItem& item) {
426 DCHECK(id_to_item_map_.find(item.id) != id_to_item_map_.end()); 408 DCHECK(id_to_item_map_.find(item.id) != id_to_item_map_.end());
409 DCHECK(profile_);
427 LauncherUpdater* updater = id_to_item_map_[item.id].updater; 410 LauncherUpdater* updater = id_to_item_map_[item.id].updater;
428 if (updater) { 411 if (updater) {
429 if (id_to_item_map_[item.id].item_type == TYPE_TABBED_BROWSER) { 412 if (id_to_item_map_[item.id].item_type == TYPE_TABBED_BROWSER) {
430 return updater->tab_model()->GetActiveTabContents() ? 413 return updater->tab_model()->GetActiveTabContents() ?
431 updater->tab_model()->GetActiveTabContents()->web_contents()-> 414 updater->tab_model()->GetActiveTabContents()->web_contents()->
432 GetTitle() : string16(); 415 GetTitle() : string16();
433 } 416 }
434 // Fall through to get title from extension. 417 // Fall through to get title from extension.
435 } 418 }
436 const Extension* extension = profile_->GetExtensionService()-> 419 const Extension* extension = profile_->GetExtensionService()->
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 } 469 }
487 } 470 }
488 471
489 void ChromeLauncherDelegate::LauncherItemWillChange(int index) { 472 void ChromeLauncherDelegate::LauncherItemWillChange(int index) {
490 } 473 }
491 474
492 void ChromeLauncherDelegate::Observe( 475 void ChromeLauncherDelegate::Observe(
493 int type, 476 int type,
494 const content::NotificationSource& source, 477 const content::NotificationSource& source,
495 const content::NotificationDetails& details) { 478 const content::NotificationDetails& details) {
496 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); 479 switch (type) {
497 const Extension* extension = 480 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
498 content::Details<UnloadedExtensionInfo>(details)->extension; 481 const Extension* extension =
499 UnpinAppsWithID(extension->id()); 482 content::Details<UnloadedExtensionInfo>(details)->extension;
483 UnpinAppsWithID(extension->id());
484 break;
485 }
486 case chrome::NOTIFICATION_PROFILE_CREATED: {
487 profile_ = content::Source<Profile>(source)->GetOriginalProfile();
488 PostProfileInit();
489
490 registrar_.Remove(this,
491 chrome::NOTIFICATION_PROFILE_CREATED,
492 content::NotificationService::AllSources());
493 break;
494 }
495 default:
496 NOTREACHED();
497 }
498 }
499
500 void ChromeLauncherDelegate::PostProfileInit() {
501 DCHECK(profile_);
502 SetAppIconLoader(new LauncherAppIconLoader(profile_, this));
503
504 registrar_.Add(this,
505 chrome::NOTIFICATION_EXTENSION_UNLOADED,
506 content::Source<Profile>(profile_));
500 } 507 }
501 508
502 void ChromeLauncherDelegate::PersistPinnedState() { 509 void ChromeLauncherDelegate::PersistPinnedState() {
503 ListPrefUpdate updater(profile_->GetPrefs(), prefs::kPinnedLauncherApps); 510 ListPrefUpdate updater(profile_->GetPrefs(), prefs::kPinnedLauncherApps);
504 updater.Get()->Clear(); 511 updater.Get()->Clear();
505 for (size_t i = 0; i < model_->items().size(); ++i) { 512 for (size_t i = 0; i < model_->items().size(); ++i) {
506 if (model_->items()[i].type == ash::TYPE_APP) { 513 if (model_->items()[i].type == ash::TYPE_APP) {
507 ash::LauncherID id = model_->items()[i].id; 514 ash::LauncherID id = model_->items()[i].id;
508 if (id_to_item_map_.find(id) != id_to_item_map_.end() && 515 if (id_to_item_map_.find(id) != id_to_item_map_.end() &&
509 id_to_item_map_[id].pinned) { 516 id_to_item_map_[id].pinned) {
(...skipping 21 matching lines...) Expand all
531 void ChromeLauncherDelegate::UnpinAppsWithID(const std::string& app_id) { 538 void ChromeLauncherDelegate::UnpinAppsWithID(const std::string& app_id) {
532 for (IDToItemMap::iterator i = id_to_item_map_.begin(); 539 for (IDToItemMap::iterator i = id_to_item_map_.begin();
533 i != id_to_item_map_.end(); ) { 540 i != id_to_item_map_.end(); ) {
534 IDToItemMap::iterator current(i); 541 IDToItemMap::iterator current(i);
535 ++i; 542 ++i;
536 if (current->second.app_id == app_id && current->second.pinned) 543 if (current->second.app_id == app_id && current->second.pinned)
537 Unpin(current->first); 544 Unpin(current->first);
538 } 545 }
539 } 546 }
540 547
541 void ChromeLauncherDelegate::SetAppIconLoaderForTest(AppIconLoader* loader) { 548 void ChromeLauncherDelegate::SetAppIconLoader(AppIconLoader* loader) {
549 DCHECK(profile_);
542 app_icon_loader_.reset(loader); 550 app_icon_loader_.reset(loader);
551
552 const base::ListValue* pinned_apps =
553 profile_->GetPrefs()->GetList(prefs::kPinnedLauncherApps);
554 for (size_t i = 0; i < pinned_apps->GetSize(); ++i) {
555 DictionaryValue* app = NULL;
556 if (pinned_apps->GetDictionary(i, &app)) {
557 std::string app_id, type_string;
558 if (app->GetString(kAppIDPath, &app_id) &&
559 app->GetString(kAppTypePath, &type_string) &&
560 app_icon_loader_->IsValidID(app_id)) {
561 AppType app_type;
562 if (type_string == kAppTypeWindow)
563 app_type = APP_TYPE_WINDOW;
564 else if (type_string == kAppTypePanel)
565 app_type = APP_TYPE_APP_PANEL;
566 else
567 app_type = APP_TYPE_TAB;
568 CreateAppLauncherItem(NULL, app_id, app_type, ash::STATUS_CLOSED);
569 }
570 }
571 }
543 } 572 }
544 573
545 Profile* ChromeLauncherDelegate::GetProfileForNewWindows() { 574 Profile* ChromeLauncherDelegate::GetProfileForNewWindows() {
546 return ProfileManager::GetDefaultProfileOrOffTheRecord(); 575 return ProfileManager::GetDefaultProfileOrOffTheRecord();
547 } 576 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698