Skip to content

Commit

Permalink
Bug 793105 - Add a "backgroundPerceivable" class for audio-channel-co…
Browse files Browse the repository at this point in the history
…ntent. r=jlebar,roc
  • Loading branch information
kanru committed Jan 5, 2013
1 parent 2ad944e commit f34bb3f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 11 deletions.
2 changes: 2 additions & 0 deletions b2g/app/b2g.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ pref("hal.processPriorityManager.gonk.masterOomScoreAdjust", 0);
pref("hal.processPriorityManager.gonk.masterKillUnderMB", 1);
pref("hal.processPriorityManager.gonk.foregroundOomScoreAdjust", 67);
pref("hal.processPriorityManager.gonk.foregroundKillUnderMB", 4);
pref("hal.processPriorityManager.gonk.backgroundPerceivableOomScoreAdjust", 134);
pref("hal.processPriorityManager.gonk.backgroundPerceivebleKillUnderMB", 5);
pref("hal.processPriorityManager.gonk.backgroundHomescreenOomScoreAdjust", 200);
pref("hal.processPriorityManager.gonk.backgroundHomescreenKillUnderMB", 5);
pref("hal.processPriorityManager.gonk.backgroundOomScoreAdjust", 400);
Expand Down
6 changes: 6 additions & 0 deletions dom/audiochannel/AudioChannelService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ AudioChannelService::GetMuted(AudioChannelType aType, bool aElementHidden)
return muted;
}

bool
AudioChannelService::ContentChannelIsActive()
{
return mChannelCounters[AUDIO_CHANNEL_CONTENT].Length() > 0;
}

static PLDHashOperator
NotifyEnumerator(AudioChannelAgent* aAgent,
AudioChannelType aType, void* aData)
Expand Down
6 changes: 6 additions & 0 deletions dom/audiochannel/AudioChannelService.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class AudioChannelService : public nsIObserver
*/
virtual bool GetMuted(AudioChannelType aType, bool aElementHidden);

/**
* Return true if there is a content channel active in this process
* or one of its subprocesses.
*/
virtual bool ContentChannelIsActive();

protected:
void Notify();

Expand Down
16 changes: 11 additions & 5 deletions dom/audiochannel/AudioChannelServiceChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
#include "mozilla/StaticPtr.h"
#include "mozilla/unused.h"
#include "mozilla/Util.h"

#include "mozilla/dom/ContentChild.h"

#include "base/basictypes.h"

#include "nsIObserverService.h"
#include "nsThreadUtils.h"

using namespace mozilla;
Expand Down Expand Up @@ -82,6 +79,11 @@ AudioChannelServiceChild::RegisterAudioChannelAgent(AudioChannelAgent* aAgent,
if (cc) {
cc->SendAudioChannelRegisterType(aType);
}

nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->NotifyObservers(nullptr, "audio-channel-agent-changed", nullptr);
}
}

void
Expand All @@ -98,5 +100,9 @@ AudioChannelServiceChild::UnregisterAudioChannelAgent(AudioChannelAgent* aAgent)
if (cc) {
cc->SendAudioChannelUnregisterType(type);
}
}

nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->NotifyObservers(nullptr, "audio-channel-agent-changed", nullptr);
}
}
35 changes: 31 additions & 4 deletions dom/ipc/ProcessPriorityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "mozilla/Services.h"
#include "mozilla/HalTypes.h"
#include "mozilla/TimeStamp.h"
#include "AudioChannelService.h"
#include "prlog.h"
#include "nsWeakPtr.h"
#include "nsXULAppAPI.h"
Expand Down Expand Up @@ -80,6 +81,11 @@ GetPPMLog()
ProcessPriority
GetBackgroundPriority()
{
AudioChannelService* service = AudioChannelService::GetAudioChannelService();
if (service->ContentChannelIsActive()) {
return PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE;
}

bool isHomescreen = false;

ContentChild* contentChild = ContentChild::GetSingleton();
Expand All @@ -101,6 +107,17 @@ GetBackgroundPriority()
PROCESS_PRIORITY_BACKGROUND;
}

/**
* Determine if the priority is a backround priority.
*/
bool
IsBackgroundPriority(ProcessPriority aPriority)
{
return (aPriority == PROCESS_PRIORITY_BACKGROUND ||
aPriority == PROCESS_PRIORITY_BACKGROUND_HOMESCREEN ||
aPriority == PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE);
}

/**
* This class listens to window creation and visibilitychange events and
* informs the hal back-end when this process transitions between having no
Expand Down Expand Up @@ -135,6 +152,7 @@ class ProcessPriorityManager MOZ_FINAL

private:
void SetPriority(ProcessPriority aPriority);
void OnAudioChannelAgentChanged();
void OnContentDocumentGlobalCreated(nsISupports* aOuterWindow);
void OnInnerWindowDestroyed();
void OnGracePeriodTimerFired();
Expand Down Expand Up @@ -170,6 +188,7 @@ ProcessPriorityManager::Init()
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
os->AddObserver(this, "content-document-global-created", /* ownsWeak = */ false);
os->AddObserver(this, "inner-window-destroyed", /* ownsWeak = */ false);
os->AddObserver(this, "audio-channel-agent-changed", /* ownsWeak = */ false);

SetPriority(PROCESS_PRIORITY_FOREGROUND);
}
Expand All @@ -186,6 +205,8 @@ ProcessPriorityManager::Observe(
OnInnerWindowDestroyed();
} else if (!strcmp(aTopic, "timer-callback")) {
OnGracePeriodTimerFired();
} else if (!strcmp(aTopic, "audio-channel-agent-changed")) {
OnAudioChannelAgentChanged();
} else {
MOZ_ASSERT(false);
}
Expand All @@ -201,6 +222,14 @@ ProcessPriorityManager::HandleEvent(
return NS_OK;
}

void
ProcessPriorityManager::OnAudioChannelAgentChanged()
{
if (IsBackgroundPriority(mProcessPriority)) {
SetPriority(GetBackgroundPriority());
}
}

void
ProcessPriorityManager::OnContentDocumentGlobalCreated(
nsISupports* aOuterWindow)
Expand Down Expand Up @@ -297,8 +326,7 @@ ProcessPriorityManager::SetPriority(ProcessPriority aPriority)
return;
}

if (aPriority == PROCESS_PRIORITY_BACKGROUND ||
aPriority == PROCESS_PRIORITY_BACKGROUND_HOMESCREEN) {
if (IsBackgroundPriority(aPriority)) {
// If this is a foreground --> background transition, give ourselves a
// grace period before informing hal.
uint32_t gracePeriodMS = Preferences::GetUint("dom.ipc.processPriorityManager.gracePeriodMS", 1000);
Expand Down Expand Up @@ -345,8 +373,7 @@ ProcessPriorityManager::OnGracePeriodTimerFired()
// mProcessPriority should already be one of the BACKGROUND values: We set it
// in SetPriority(BACKGROUND), and we canceled this timer if there was an
// intervening SetPriority(FOREGROUND) call.
MOZ_ASSERT(mProcessPriority == PROCESS_PRIORITY_BACKGROUND ||
mProcessPriority == PROCESS_PRIORITY_BACKGROUND_HOMESCREEN);
MOZ_ASSERT(IsBackgroundPriority(mProcessPriority));

mGracePeriodTimer = nullptr;
hal::SetProcessPriority(getpid(), mProcessPriority);
Expand Down
1 change: 1 addition & 0 deletions hal/HalTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ typedef Observer<SwitchEvent> SwitchObserver;
enum ProcessPriority {
PROCESS_PRIORITY_BACKGROUND,
PROCESS_PRIORITY_BACKGROUND_HOMESCREEN,
PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE,
// Any priority greater than or equal to FOREGROUND is considered
// "foreground" for the purposes of priority testing, for example
// CurrentProcessIsForeground().
Expand Down
12 changes: 10 additions & 2 deletions hal/gonk/GonkHal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1046,8 +1046,13 @@ EnsureKernelLowMemKillerParamsSet()
nsAutoCString adjParams;
nsAutoCString minfreeParams;

const char* priorityClasses[] =
{"master", "foreground", "background", "backgroundHomescreen"};
const char* priorityClasses[] = {
"master",
"foreground",
"background",
"backgroundHomescreen",
"backgroundPerceivable"
};
for (size_t i = 0; i < NS_ARRAY_LENGTH(priorityClasses); i++) {
int32_t oomScoreAdj;
if (!NS_SUCCEEDED(Preferences::GetInt(nsPrintfCString(
Expand Down Expand Up @@ -1112,6 +1117,9 @@ SetProcessPriority(int aPid, ProcessPriority aPriority)
case PROCESS_PRIORITY_BACKGROUND_HOMESCREEN:
priorityStr = "backgroundHomescreen";
break;
case PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE:
priorityStr = "backgroundPerceivable";
break;
case PROCESS_PRIORITY_FOREGROUND:
priorityStr = "foreground";
break;
Expand Down

0 comments on commit f34bb3f

Please sign in to comment.