OLD | NEW |
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 <algorithm> | 5 #include <algorithm> |
6 #include <string> | 6 #include <string> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 base::Bind(&BackgroundModeManager::OnBackgroundModeEnabledPrefChanged, | 200 base::Bind(&BackgroundModeManager::OnBackgroundModeEnabledPrefChanged, |
201 base::Unretained(this))); | 201 base::Unretained(this))); |
202 } | 202 } |
203 | 203 |
204 // Keep the browser alive until extensions are done loading - this is needed | 204 // Keep the browser alive until extensions are done loading - this is needed |
205 // by the --no-startup-window flag. We want to stay alive until we load | 205 // by the --no-startup-window flag. We want to stay alive until we load |
206 // extensions, at which point we should either run in background mode (if | 206 // extensions, at which point we should either run in background mode (if |
207 // there are background apps) or exit if there are none. | 207 // there are background apps) or exit if there are none. |
208 if (command_line->HasSwitch(switches::kNoStartupWindow)) { | 208 if (command_line->HasSwitch(switches::kNoStartupWindow)) { |
209 keep_alive_for_startup_ = true; | 209 keep_alive_for_startup_ = true; |
210 chrome::StartKeepAlive(); | 210 chrome::IncrementKeepAliveCount(); |
211 } else { | 211 } else { |
212 // Otherwise, start with background mode suspended in case we're launching | 212 // Otherwise, start with background mode suspended in case we're launching |
213 // in a mode that doesn't open a browser window. It will be resumed when the | 213 // in a mode that doesn't open a browser window. It will be resumed when the |
214 // first browser window is opened. | 214 // first browser window is opened. |
215 SuspendBackgroundMode(); | 215 SuspendBackgroundMode(); |
216 } | 216 } |
217 | 217 |
218 // If the -keep-alive-for-test flag is passed, then always keep chrome running | 218 // If the -keep-alive-for-test flag is passed, then always keep chrome running |
219 // in the background until the user explicitly terminates it. | 219 // in the background until the user explicitly terminates it. |
220 if (command_line->HasSwitch(switches::kKeepAliveForTest)) | 220 if (command_line->HasSwitch(switches::kKeepAliveForTest)) |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 /////////////////////////////////////////////////////////////////////////////// | 314 /////////////////////////////////////////////////////////////////////////////// |
315 // BackgroundModeManager, content::NotificationObserver overrides | 315 // BackgroundModeManager, content::NotificationObserver overrides |
316 void BackgroundModeManager::Observe( | 316 void BackgroundModeManager::Observe( |
317 int type, | 317 int type, |
318 const content::NotificationSource& source, | 318 const content::NotificationSource& source, |
319 const content::NotificationDetails& details) { | 319 const content::NotificationDetails& details) { |
320 switch (type) { | 320 switch (type) { |
321 case chrome::NOTIFICATION_EXTENSIONS_READY: | 321 case chrome::NOTIFICATION_EXTENSIONS_READY: |
322 // Extensions are loaded, so we don't need to manually keep the browser | 322 // Extensions are loaded, so we don't need to manually keep the browser |
323 // process alive any more when running in no-startup-window mode. | 323 // process alive any more when running in no-startup-window mode. |
324 EndKeepAliveForStartup(); | 324 DecrementKeepAliveCountForStartup(); |
325 break; | 325 break; |
326 | 326 |
327 case chrome::NOTIFICATION_EXTENSION_LOADED: { | 327 case chrome::NOTIFICATION_EXTENSION_LOADED: { |
328 Extension* extension = content::Details<Extension>(details).ptr(); | 328 Extension* extension = content::Details<Extension>(details).ptr(); |
329 Profile* profile = content::Source<Profile>(source).ptr(); | 329 Profile* profile = content::Source<Profile>(source).ptr(); |
330 if (BackgroundApplicationListModel::IsBackgroundApp( | 330 if (BackgroundApplicationListModel::IsBackgroundApp( |
331 *extension, profile)) { | 331 *extension, profile)) { |
332 // Extensions loaded after the ExtensionsService is ready should be | 332 // Extensions loaded after the ExtensionsService is ready should be |
333 // treated as new installs. | 333 // treated as new installs. |
334 if (extensions::ExtensionSystem::Get(profile)->extension_service()-> | 334 if (extensions::ExtensionSystem::Get(profile)->extension_service()-> |
(...skipping 15 matching lines...) Expand all Loading... |
350 extensions::APIPermission::kBackground) && | 350 extensions::APIPermission::kBackground) && |
351 info->reason == UpdatedExtensionPermissionsInfo::ADDED) { | 351 info->reason == UpdatedExtensionPermissionsInfo::ADDED) { |
352 // Turned on background permission, so treat this as a new install. | 352 // Turned on background permission, so treat this as a new install. |
353 OnBackgroundAppInstalled(info->extension); | 353 OnBackgroundAppInstalled(info->extension); |
354 } | 354 } |
355 } | 355 } |
356 break; | 356 break; |
357 case chrome::NOTIFICATION_APP_TERMINATING: | 357 case chrome::NOTIFICATION_APP_TERMINATING: |
358 // Make sure we aren't still keeping the app alive (only happens if we | 358 // Make sure we aren't still keeping the app alive (only happens if we |
359 // don't receive an EXTENSIONS_READY notification for some reason). | 359 // don't receive an EXTENSIONS_READY notification for some reason). |
360 EndKeepAliveForStartup(); | 360 DecrementKeepAliveCountForStartup(); |
361 // Performing an explicit shutdown, so exit background mode (does nothing | 361 // Performing an explicit shutdown, so exit background mode (does nothing |
362 // if we aren't in background mode currently). | 362 // if we aren't in background mode currently). |
363 EndBackgroundMode(); | 363 EndBackgroundMode(); |
364 // Shutting down, so don't listen for any more notifications so we don't | 364 // Shutting down, so don't listen for any more notifications so we don't |
365 // try to re-enter/exit background mode again. | 365 // try to re-enter/exit background mode again. |
366 registrar_.RemoveAll(); | 366 registrar_.RemoveAll(); |
367 for (BackgroundModeInfoMap::iterator it = | 367 for (BackgroundModeInfoMap::iterator it = |
368 background_mode_data_.begin(); | 368 background_mode_data_.begin(); |
369 it != background_mode_data_.end(); | 369 it != background_mode_data_.end(); |
370 ++it) { | 370 ++it) { |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 } | 515 } |
516 default: | 516 default: |
517 bmd->ExecuteCommand(command_id, event_flags); | 517 bmd->ExecuteCommand(command_id, event_flags); |
518 break; | 518 break; |
519 } | 519 } |
520 } | 520 } |
521 | 521 |
522 | 522 |
523 /////////////////////////////////////////////////////////////////////////////// | 523 /////////////////////////////////////////////////////////////////////////////// |
524 // BackgroundModeManager, private | 524 // BackgroundModeManager, private |
525 void BackgroundModeManager::EndKeepAliveForStartup() { | 525 void BackgroundModeManager::DecrementKeepAliveCountForStartup() { |
526 if (keep_alive_for_startup_) { | 526 if (keep_alive_for_startup_) { |
527 keep_alive_for_startup_ = false; | 527 keep_alive_for_startup_ = false; |
528 // We call this via the message queue to make sure we don't try to end | 528 // We call this via the message queue to make sure we don't try to end |
529 // keep-alive (which can shutdown Chrome) before the message loop has | 529 // keep-alive (which can shutdown Chrome) before the message loop has |
530 // started. | 530 // started. |
531 base::MessageLoop::current()->PostTask(FROM_HERE, | 531 base::MessageLoop::current()->PostTask( |
532 base::Bind(&chrome::EndKeepAlive)); | 532 FROM_HERE, base::Bind(&chrome::DecrementKeepAliveCount)); |
533 } | 533 } |
534 } | 534 } |
535 | 535 |
536 void BackgroundModeManager::StartBackgroundMode() { | 536 void BackgroundModeManager::StartBackgroundMode() { |
537 DCHECK(ShouldBeInBackgroundMode()); | 537 DCHECK(ShouldBeInBackgroundMode()); |
538 // Don't bother putting ourselves in background mode if we're already there | 538 // Don't bother putting ourselves in background mode if we're already there |
539 // or if background mode is disabled. | 539 // or if background mode is disabled. |
540 if (in_background_mode_) | 540 if (in_background_mode_) |
541 return; | 541 return; |
542 | 542 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 | 589 |
590 void BackgroundModeManager::ResumeBackgroundMode() { | 590 void BackgroundModeManager::ResumeBackgroundMode() { |
591 background_mode_suspended_ = false; | 591 background_mode_suspended_ = false; |
592 UpdateKeepAliveAndTrayIcon(); | 592 UpdateKeepAliveAndTrayIcon(); |
593 } | 593 } |
594 | 594 |
595 void BackgroundModeManager::UpdateKeepAliveAndTrayIcon() { | 595 void BackgroundModeManager::UpdateKeepAliveAndTrayIcon() { |
596 if (in_background_mode_ && !background_mode_suspended_) { | 596 if (in_background_mode_ && !background_mode_suspended_) { |
597 if (!keeping_alive_) { | 597 if (!keeping_alive_) { |
598 keeping_alive_ = true; | 598 keeping_alive_ = true; |
599 chrome::StartKeepAlive(); | 599 chrome::IncrementKeepAliveCount(); |
600 } | 600 } |
601 CreateStatusTrayIcon(); | 601 CreateStatusTrayIcon(); |
602 return; | 602 return; |
603 } | 603 } |
604 | 604 |
605 RemoveStatusTrayIcon(); | 605 RemoveStatusTrayIcon(); |
606 if (keeping_alive_) { | 606 if (keeping_alive_) { |
607 keeping_alive_ = false; | 607 keeping_alive_ = false; |
608 chrome::EndKeepAlive(); | 608 chrome::DecrementKeepAliveCount(); |
609 } | 609 } |
610 } | 610 } |
611 | 611 |
612 void BackgroundModeManager::OnBrowserAdded(Browser* browser) { | 612 void BackgroundModeManager::OnBrowserAdded(Browser* browser) { |
613 ResumeBackgroundMode(); | 613 ResumeBackgroundMode(); |
614 } | 614 } |
615 | 615 |
616 int BackgroundModeManager::GetBackgroundAppCount() const { | 616 int BackgroundModeManager::GetBackgroundAppCount() const { |
617 int count = 0; | 617 int count = 0; |
618 // Walk the BackgroundModeData for all profiles and count the number of apps. | 618 // Walk the BackgroundModeData for all profiles and count the number of apps. |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
815 } | 815 } |
816 } | 816 } |
817 return profile_it; | 817 return profile_it; |
818 } | 818 } |
819 | 819 |
820 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { | 820 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { |
821 PrefService* service = g_browser_process->local_state(); | 821 PrefService* service = g_browser_process->local_state(); |
822 DCHECK(service); | 822 DCHECK(service); |
823 return service->GetBoolean(prefs::kBackgroundModeEnabled); | 823 return service->GetBoolean(prefs::kBackgroundModeEnabled); |
824 } | 824 } |
OLD | NEW |