OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 25 matching lines...) Expand all Loading... | |
36 #include "ui/base/resource/resource_bundle.h" | 36 #include "ui/base/resource/resource_bundle.h" |
37 | 37 |
38 BackgroundModeManager::BackgroundModeData::BackgroundModeData( | 38 BackgroundModeManager::BackgroundModeData::BackgroundModeData( |
39 int command_id, | 39 int command_id, |
40 Profile* profile, | 40 Profile* profile, |
41 BackgroundModeManager* background_mode_manager) | 41 BackgroundModeManager* background_mode_manager) |
42 : applications_(new BackgroundApplicationListModel(profile)), | 42 : applications_(new BackgroundApplicationListModel(profile)), |
43 command_id_(command_id), | 43 command_id_(command_id), |
44 profile_(profile), | 44 profile_(profile), |
45 background_mode_manager_(background_mode_manager) { | 45 background_mode_manager_(background_mode_manager) { |
46 name_ = UTF8ToUTF16(profile_->GetProfileName()); | 46 ProfileInfoCache* cache = background_mode_manager_->profile_cache(); |
47 size_t index = cache->GetIndexOfProfileWithPath(profile_->GetPath()); | |
48 if (index != std::string::npos) | |
Andrew T Wilson (Slow)
2011/12/06 00:46:03
It seems weird to use std::string::npos here, whic
rpetterson
2011/12/06 05:52:34
I agree that it seems odd. I was originally checki
| |
49 name_ = cache->GetNameOfProfileAtIndex(index); | |
Andrew T Wilson (Slow)
2011/12/06 00:46:03
Also, it's somewhat odd to have this split in resp
rpetterson
2011/12/06 05:52:34
I moved everything to the BMM. I would have prefer
| |
47 if (name_.empty()) | 50 if (name_.empty()) |
48 name_ = l10n_util::GetStringUTF16(IDS_PROFILES_DEFAULT_NAME); | 51 name_ = l10n_util::GetStringUTF16(IDS_PROFILES_DEFAULT_NAME); |
49 } | 52 } |
50 | 53 |
51 BackgroundModeManager::BackgroundModeData::~BackgroundModeData() { | 54 BackgroundModeManager::BackgroundModeData::~BackgroundModeData() { |
52 } | 55 } |
53 | 56 |
54 /////////////////////////////////////////////////////////////////////////////// | 57 /////////////////////////////////////////////////////////////////////////////// |
55 // BackgroundModeManager::BackgroundModeData, ui::SimpleMenuModel overrides | 58 // BackgroundModeManager::BackgroundModeData, ui::SimpleMenuModel overrides |
56 bool BackgroundModeManager::BackgroundModeData::IsCommandIdChecked( | 59 bool BackgroundModeManager::BackgroundModeData::IsCommandIdChecked( |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 const std::string& name = (*cursor)->name(); | 117 const std::string& name = (*cursor)->name(); |
115 menu->AddItem(position, UTF8ToUTF16(name)); | 118 menu->AddItem(position, UTF8ToUTF16(name)); |
116 if (icon) | 119 if (icon) |
117 menu->SetIcon(menu->GetItemCount() - 1, *icon); | 120 menu->SetIcon(menu->GetItemCount() - 1, *icon); |
118 } | 121 } |
119 } | 122 } |
120 if (containing_menu) | 123 if (containing_menu) |
121 containing_menu->AddSubMenu(command_id_, name_, menu); | 124 containing_menu->AddSubMenu(command_id_, name_, menu); |
122 } | 125 } |
123 | 126 |
127 void BackgroundModeManager::BackgroundModeData::SetName( | |
128 const string16& new_profile_name) { | |
129 name_ = new_profile_name; | |
130 } | |
131 | |
132 string16 BackgroundModeManager::BackgroundModeData::name() { | |
133 return name_; | |
134 } | |
135 | |
124 // static | 136 // static |
125 bool BackgroundModeManager::BackgroundModeData::BackgroundModeDataCompare( | 137 bool BackgroundModeManager::BackgroundModeData::BackgroundModeDataCompare( |
126 const BackgroundModeData* bmd1, | 138 const BackgroundModeData* bmd1, |
127 const BackgroundModeData* bmd2) { | 139 const BackgroundModeData* bmd2) { |
128 return bmd1->name_ < bmd2->name_; | 140 return bmd1->name_ < bmd2->name_; |
129 } | 141 } |
130 | 142 |
131 | 143 |
132 /////////////////////////////////////////////////////////////////////////////// | 144 /////////////////////////////////////////////////////////////////////////////// |
133 // BackgroundModeManager, public | 145 // BackgroundModeManager, public |
(...skipping 10 matching lines...) Expand all Loading... | |
144 current_command_id_(0) { | 156 current_command_id_(0) { |
145 // We should never start up if there is no browser process or if we are | 157 // We should never start up if there is no browser process or if we are |
146 // currently quitting. | 158 // currently quitting. |
147 CHECK(g_browser_process != NULL); | 159 CHECK(g_browser_process != NULL); |
148 CHECK(!browser_shutdown::IsTryingToQuit()); | 160 CHECK(!browser_shutdown::IsTryingToQuit()); |
149 // If background mode is currently disabled, just exit - don't listen for any | 161 // If background mode is currently disabled, just exit - don't listen for any |
150 // notifications. | 162 // notifications. |
151 if (IsBackgroundModePermanentlyDisabled(command_line)) | 163 if (IsBackgroundModePermanentlyDisabled(command_line)) |
152 return; | 164 return; |
153 | 165 |
166 // Add self as an observer for the profile info cache so we know when profiles | |
167 // are deleted and their names change. | |
168 profile_cache_->AddObserver(this); | |
169 | |
154 // Listen for the background mode preference changing. | 170 // Listen for the background mode preference changing. |
155 if (g_browser_process->local_state()) { // Skip for unit tests | 171 if (g_browser_process->local_state()) { // Skip for unit tests |
156 pref_registrar_.Init(g_browser_process->local_state()); | 172 pref_registrar_.Init(g_browser_process->local_state()); |
157 pref_registrar_.Add(prefs::kBackgroundModeEnabled, this); | 173 pref_registrar_.Add(prefs::kBackgroundModeEnabled, this); |
158 } | 174 } |
159 | 175 |
160 // Keep the browser alive until extensions are done loading - this is needed | 176 // Keep the browser alive until extensions are done loading - this is needed |
161 // by the --no-startup-window flag. We want to stay alive until we load | 177 // by the --no-startup-window flag. We want to stay alive until we load |
162 // extensions, at which point we should either run in background mode (if | 178 // extensions, at which point we should either run in background mode (if |
163 // there are background apps) or exit if there are none. | 179 // there are background apps) or exit if there are none. |
(...skipping 12 matching lines...) Expand all Loading... | |
176 | 192 |
177 // Listen for the application shutting down so we can decrement our KeepAlive | 193 // Listen for the application shutting down so we can decrement our KeepAlive |
178 // count. | 194 // count. |
179 registrar_.Add(this, content::NOTIFICATION_APP_TERMINATING, | 195 registrar_.Add(this, content::NOTIFICATION_APP_TERMINATING, |
180 content::NotificationService::AllSources()); | 196 content::NotificationService::AllSources()); |
181 } | 197 } |
182 | 198 |
183 BackgroundModeManager::~BackgroundModeManager() { | 199 BackgroundModeManager::~BackgroundModeManager() { |
184 // Remove ourselves from the application observer list (only needed by unit | 200 // Remove ourselves from the application observer list (only needed by unit |
185 // tests since APP_TERMINATING is what does this in a real running system). | 201 // tests since APP_TERMINATING is what does this in a real running system). |
186 for (std::map<Profile*, BackgroundModeInfo>::iterator it = | 202 for (BackgroundModeInfoMap::iterator it = |
187 background_mode_data_.begin(); | 203 background_mode_data_.begin(); |
188 it != background_mode_data_.end(); | 204 it != background_mode_data_.end(); |
189 ++it) { | 205 ++it) { |
190 it->second->applications_->RemoveObserver(this); | 206 it->second->applications_->RemoveObserver(this); |
191 } | 207 } |
192 | 208 |
193 // We're going away, so exit background mode (does nothing if we aren't in | 209 // We're going away, so exit background mode (does nothing if we aren't in |
194 // background mode currently). This is primarily needed for unit tests, | 210 // background mode currently). This is primarily needed for unit tests, |
195 // because in an actual running system we'd get an APP_TERMINATING | 211 // because in an actual running system we'd get an APP_TERMINATING |
196 // notification before being destroyed. | 212 // notification before being destroyed. |
(...skipping 30 matching lines...) Expand all Loading... | |
227 content::Source<Profile>(profile)); | 243 content::Source<Profile>(profile)); |
228 | 244 |
229 bmd->applications_->AddObserver(this); | 245 bmd->applications_->AddObserver(this); |
230 | 246 |
231 // If we're adding a new profile and running in multi-profile mode, this new | 247 // If we're adding a new profile and running in multi-profile mode, this new |
232 // profile should be added to the status icon if one currently exists. | 248 // profile should be added to the status icon if one currently exists. |
233 if (in_background_mode_ && status_icon_) | 249 if (in_background_mode_ && status_icon_) |
234 UpdateStatusTrayIconContextMenu(); | 250 UpdateStatusTrayIconContextMenu(); |
235 } | 251 } |
236 | 252 |
253 ProfileInfoCache* BackgroundModeManager::profile_cache() { | |
254 return profile_cache_; | |
255 } | |
256 | |
237 // static | 257 // static |
238 void BackgroundModeManager::LaunchBackgroundApplication( | 258 void BackgroundModeManager::LaunchBackgroundApplication( |
239 Profile* profile, | 259 Profile* profile, |
240 const Extension* extension) { | 260 const Extension* extension) { |
241 ExtensionService* service = profile->GetExtensionService(); | 261 ExtensionService* service = profile->GetExtensionService(); |
242 extension_misc::LaunchContainer launch_container = | 262 extension_misc::LaunchContainer launch_container = |
243 service->extension_prefs()->GetLaunchContainer( | 263 service->extension_prefs()->GetLaunchContainer( |
244 extension, ExtensionPrefs::LAUNCH_REGULAR); | 264 extension, ExtensionPrefs::LAUNCH_REGULAR); |
245 Browser::OpenApplication(profile, extension, launch_container, GURL(), | 265 Browser::OpenApplication(profile, extension, launch_container, GURL(), |
246 NEW_FOREGROUND_TAB); | 266 NEW_FOREGROUND_TAB); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 case content::NOTIFICATION_APP_TERMINATING: | 312 case content::NOTIFICATION_APP_TERMINATING: |
293 // Make sure we aren't still keeping the app alive (only happens if we | 313 // Make sure we aren't still keeping the app alive (only happens if we |
294 // don't receive an EXTENSIONS_READY notification for some reason). | 314 // don't receive an EXTENSIONS_READY notification for some reason). |
295 EndKeepAliveForStartup(); | 315 EndKeepAliveForStartup(); |
296 // Performing an explicit shutdown, so exit background mode (does nothing | 316 // Performing an explicit shutdown, so exit background mode (does nothing |
297 // if we aren't in background mode currently). | 317 // if we aren't in background mode currently). |
298 EndBackgroundMode(); | 318 EndBackgroundMode(); |
299 // Shutting down, so don't listen for any more notifications so we don't | 319 // Shutting down, so don't listen for any more notifications so we don't |
300 // try to re-enter/exit background mode again. | 320 // try to re-enter/exit background mode again. |
301 registrar_.RemoveAll(); | 321 registrar_.RemoveAll(); |
302 for (std::map<Profile*, BackgroundModeInfo>::iterator it = | 322 for (BackgroundModeInfoMap::iterator it = |
303 background_mode_data_.begin(); | 323 background_mode_data_.begin(); |
304 it != background_mode_data_.end(); | 324 it != background_mode_data_.end(); |
305 ++it) { | 325 ++it) { |
306 it->second->applications_->RemoveObserver(this); | 326 it->second->applications_->RemoveObserver(this); |
307 } | 327 } |
308 break; | 328 break; |
309 default: | 329 default: |
310 NOTREACHED(); | 330 NOTREACHED(); |
311 break; | 331 break; |
312 } | 332 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 #if !defined(OS_MACOSX) | 375 #if !defined(OS_MACOSX) |
356 EnableLaunchOnStartup(true); | 376 EnableLaunchOnStartup(true); |
357 #endif | 377 #endif |
358 | 378 |
359 StartBackgroundMode(); | 379 StartBackgroundMode(); |
360 } | 380 } |
361 // List of applications changed so update the UI. | 381 // List of applications changed so update the UI. |
362 UpdateStatusTrayIconContextMenu(); | 382 UpdateStatusTrayIconContextMenu(); |
363 } | 383 } |
364 } | 384 } |
385 /////////////////////////////////////////////////////////////////////////////// | |
386 // BackgroundModeManager, ProfileInfoCacheObserver overrides | |
387 void BackgroundModeManager::OnProfileAdded(const string16& profile_name, | |
388 const string16& profile_base_dir) { | |
389 // At this point, the profile should be registered with the background mode | |
390 // manager, but when it's actually added to the cache is when its name is | |
391 // set so we need up to update that with the background_mode_data. | |
392 for (BackgroundModeInfoMap::const_iterator it = | |
393 background_mode_data_.begin(); | |
394 it != background_mode_data_.end(); | |
395 ++it) { | |
396 if (it->first->GetPath().BaseName() == FilePath(profile_base_dir)) { | |
397 it->second->SetName(profile_name); | |
398 UpdateStatusTrayIconContextMenu(); | |
399 } | |
400 } | |
401 } | |
402 | |
403 void BackgroundModeManager::OnProfileRemoved(const string16& profile_name) { | |
404 // Remove the profile from our map of profiles | |
405 BackgroundModeInfoMap::const_iterator it = | |
406 GetBackgroundModeIterator(profile_name); | |
407 // If a profile isn't running a background app, it may not be in the map | |
408 if (it != background_mode_data_.end()) { | |
409 background_mode_data_.erase(it); | |
410 UpdateStatusTrayIconContextMenu(); | |
411 } | |
412 } | |
413 | |
414 void BackgroundModeManager::OnProfileNameChanged( | |
415 const string16& old_profile_name, | |
416 const string16& new_profile_name) { | |
417 BackgroundModeInfoMap::const_iterator it = | |
418 GetBackgroundModeIterator(old_profile_name); | |
419 DCHECK(it != background_mode_data_.end()); | |
420 it->second->SetName(new_profile_name); | |
421 UpdateStatusTrayIconContextMenu(); | |
422 } | |
365 | 423 |
366 /////////////////////////////////////////////////////////////////////////////// | 424 /////////////////////////////////////////////////////////////////////////////// |
367 // BackgroundModeManager::BackgroundModeData, ui::SimpleMenuModel overrides | 425 // BackgroundModeManager::BackgroundModeData, ui::SimpleMenuModel overrides |
368 bool BackgroundModeManager::IsCommandIdChecked( | 426 bool BackgroundModeManager::IsCommandIdChecked( |
369 int command_id) const { | 427 int command_id) const { |
370 DCHECK(command_id == IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND); | 428 DCHECK(command_id == IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND); |
371 return true; | 429 return true; |
372 } | 430 } |
373 | 431 |
374 bool BackgroundModeManager::IsCommandIdEnabled( | 432 bool BackgroundModeManager::IsCommandIdEnabled( |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
482 // If background mode is currently enabled, turn it off. | 540 // If background mode is currently enabled, turn it off. |
483 if (in_background_mode_) { | 541 if (in_background_mode_) { |
484 EndBackgroundMode(); | 542 EndBackgroundMode(); |
485 EnableLaunchOnStartup(false); | 543 EnableLaunchOnStartup(false); |
486 } | 544 } |
487 } | 545 } |
488 | 546 |
489 int BackgroundModeManager::GetBackgroundAppCount() const { | 547 int BackgroundModeManager::GetBackgroundAppCount() const { |
490 int count = 0; | 548 int count = 0; |
491 // Walk the BackgroundModeData for all profiles and count the number of apps. | 549 // Walk the BackgroundModeData for all profiles and count the number of apps. |
492 for (std::map<Profile*, BackgroundModeInfo>::const_iterator it = | 550 for (BackgroundModeInfoMap::const_iterator it = |
493 background_mode_data_.begin(); | 551 background_mode_data_.begin(); |
494 it != background_mode_data_.end(); | 552 it != background_mode_data_.end(); |
495 ++it) { | 553 ++it) { |
496 count += it->second->GetBackgroundAppCount(); | 554 count += it->second->GetBackgroundAppCount(); |
497 } | 555 } |
498 DCHECK(count >= 0); | 556 DCHECK(count >= 0); |
499 return count; | 557 return count; |
500 } | 558 } |
501 | 559 |
502 int BackgroundModeManager::GetBackgroundAppCountForProfile( | 560 int BackgroundModeManager::GetBackgroundAppCountForProfile( |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
581 // Create a context menu item for Chrome. | 639 // Create a context menu item for Chrome. |
582 ui::SimpleMenuModel* menu = new ui::SimpleMenuModel(this); | 640 ui::SimpleMenuModel* menu = new ui::SimpleMenuModel(this); |
583 // Add About item | 641 // Add About item |
584 menu->AddItem(IDC_ABOUT, l10n_util::GetStringFUTF16(IDS_ABOUT, | 642 menu->AddItem(IDC_ABOUT, l10n_util::GetStringFUTF16(IDS_ABOUT, |
585 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); | 643 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); |
586 menu->AddItemWithStringId(IDC_TASK_MANAGER, IDS_TASK_MANAGER); | 644 menu->AddItemWithStringId(IDC_TASK_MANAGER, IDS_TASK_MANAGER); |
587 menu->AddSeparator(); | 645 menu->AddSeparator(); |
588 | 646 |
589 if (background_mode_data_.size() > 1) { | 647 if (background_mode_data_.size() > 1) { |
590 std::vector<BackgroundModeData*> bmd_vector; | 648 std::vector<BackgroundModeData*> bmd_vector; |
591 for (std::map<Profile*, BackgroundModeInfo>::iterator it = | 649 for (BackgroundModeInfoMap::iterator it = |
592 background_mode_data_.begin(); | 650 background_mode_data_.begin(); |
593 it != background_mode_data_.end(); | 651 it != background_mode_data_.end(); |
594 ++it) { | 652 ++it) { |
595 bmd_vector.push_back(it->second.get()); | 653 bmd_vector.push_back(it->second.get()); |
596 } | 654 } |
597 std::sort(bmd_vector.begin(), bmd_vector.end(), | 655 std::sort(bmd_vector.begin(), bmd_vector.end(), |
598 &BackgroundModeData::BackgroundModeDataCompare); | 656 &BackgroundModeData::BackgroundModeDataCompare); |
599 for (std::vector<BackgroundModeData*>::const_iterator bmd_it = | 657 for (std::vector<BackgroundModeData*>::const_iterator bmd_it = |
600 bmd_vector.begin(); | 658 bmd_vector.begin(); |
601 bmd_it != bmd_vector.end(); | 659 bmd_it != bmd_vector.end(); |
(...skipping 23 matching lines...) Expand all Loading... | |
625 status_icon_ = NULL; | 683 status_icon_ = NULL; |
626 context_menu_ = NULL; | 684 context_menu_ = NULL; |
627 } | 685 } |
628 | 686 |
629 BackgroundModeManager::BackgroundModeData* | 687 BackgroundModeManager::BackgroundModeData* |
630 BackgroundModeManager::GetBackgroundModeData(Profile* const profile) const { | 688 BackgroundModeManager::GetBackgroundModeData(Profile* const profile) const { |
631 DCHECK(background_mode_data_.find(profile) != background_mode_data_.end()); | 689 DCHECK(background_mode_data_.find(profile) != background_mode_data_.end()); |
632 return background_mode_data_.find(profile)->second.get(); | 690 return background_mode_data_.find(profile)->second.get(); |
633 } | 691 } |
634 | 692 |
693 BackgroundModeManager::BackgroundModeInfoMap::const_iterator | |
694 BackgroundModeManager::GetBackgroundModeIterator( | |
695 const string16& profile_name) const { | |
696 BackgroundModeInfoMap::const_iterator profile_it = | |
697 background_mode_data_.end(); | |
698 for (BackgroundModeInfoMap::const_iterator it = | |
699 background_mode_data_.begin(); | |
700 it != background_mode_data_.end(); | |
701 ++it) { | |
702 if (it->second->name() == profile_name) { | |
703 profile_it = it; | |
704 } | |
705 } | |
706 return profile_it; | |
707 } | |
708 | |
635 // static | 709 // static |
636 bool BackgroundModeManager::IsBackgroundModePermanentlyDisabled( | 710 bool BackgroundModeManager::IsBackgroundModePermanentlyDisabled( |
637 const CommandLine* command_line) { | 711 const CommandLine* command_line) { |
638 | 712 |
639 // Background mode is disabled if the appropriate flag is passed, or if | 713 // Background mode is disabled if the appropriate flag is passed, or if |
640 // extensions are disabled, or if the associated preference is unset. It's | 714 // extensions are disabled, or if the associated preference is unset. It's |
641 // always disabled on chromeos since chrome is always running on that | 715 // always disabled on chromeos since chrome is always running on that |
642 // platform, making it superfluous. | 716 // platform, making it superfluous. |
643 #if defined(OS_CHROMEOS) | 717 #if defined(OS_CHROMEOS) |
644 if (command_line->HasSwitch(switches::kKeepAliveForTest)) | 718 if (command_line->HasSwitch(switches::kKeepAliveForTest)) |
645 return false; | 719 return false; |
646 return true; | 720 return true; |
647 #else | 721 #else |
648 bool background_mode_disabled = | 722 bool background_mode_disabled = |
649 command_line->HasSwitch(switches::kDisableBackgroundMode) || | 723 command_line->HasSwitch(switches::kDisableBackgroundMode) || |
650 command_line->HasSwitch(switches::kDisableExtensions); | 724 command_line->HasSwitch(switches::kDisableExtensions); |
651 return background_mode_disabled; | 725 return background_mode_disabled; |
652 #endif | 726 #endif |
653 } | 727 } |
654 | 728 |
655 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { | 729 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { |
656 PrefService* service = g_browser_process->local_state(); | 730 PrefService* service = g_browser_process->local_state(); |
657 DCHECK(service); | 731 DCHECK(service); |
658 return service->GetBoolean(prefs::kBackgroundModeEnabled); | 732 return service->GetBoolean(prefs::kBackgroundModeEnabled); |
659 } | 733 } |
OLD | NEW |