dom/speakermanager/SpeakerManagerServiceChild.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
     2 /* vim: set ts=2 et sw=2 tw=80: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #include "SpeakerManagerServiceChild.h"
     8 #include "mozilla/Services.h"
     9 #include "mozilla/StaticPtr.h"
    10 #include "mozilla/unused.h"
    11 #include "mozilla/dom/ContentChild.h"
    12 #include "mozilla/dom/ContentParent.h"
    13 #include "nsIObserverService.h"
    14 #include "nsThreadUtils.h"
    15 #include "AudioChannelService.h"
    16 #include <cutils/properties.h>
    18 using namespace mozilla;
    19 using namespace mozilla::dom;
    21 StaticRefPtr<SpeakerManagerServiceChild> gSpeakerManagerServiceChild;
    23 // static
    24 SpeakerManagerService*
    25 SpeakerManagerServiceChild::GetSpeakerManagerService()
    26 {
    27   MOZ_ASSERT(NS_IsMainThread());
    29   // If we already exist, exit early
    30   if (gSpeakerManagerServiceChild) {
    31     return gSpeakerManagerServiceChild;
    32   }
    34   // Create new instance, register, return
    35   nsRefPtr<SpeakerManagerServiceChild> service = new SpeakerManagerServiceChild();
    36   NS_ENSURE_TRUE(service, nullptr);
    38   gSpeakerManagerServiceChild = service;
    39   return gSpeakerManagerServiceChild;
    40 }
    42 void
    43 SpeakerManagerServiceChild::ForceSpeaker(bool aEnable, bool aVisible)
    44 {
    45   mVisible = aVisible;
    46   mOrgSpeakerStatus = aEnable;
    47   ContentChild *cc = ContentChild::GetSingleton();
    48   if (cc) {
    49     cc->SendSpeakerManagerForceSpeaker(aEnable && aVisible);
    50   }
    51 }
    53 bool
    54 SpeakerManagerServiceChild::GetSpeakerStatus()
    55 {
    56   ContentChild *cc = ContentChild::GetSingleton();
    57   bool status = false;
    58   if (cc) {
    59     cc->SendSpeakerManagerGetSpeakerStatus(&status);
    60   }
    61   char propQemu[PROPERTY_VALUE_MAX];
    62   property_get("ro.kernel.qemu", propQemu, "");
    63   if (!strncmp(propQemu, "1", 1)) {
    64     return mOrgSpeakerStatus;
    65   }
    66   return status;
    67 }
    69 void
    70 SpeakerManagerServiceChild::Shutdown()
    71 {
    72   if (gSpeakerManagerServiceChild) {
    73     gSpeakerManagerServiceChild = nullptr;
    74   }
    75 }
    77 void
    78 SpeakerManagerServiceChild::SetAudioChannelActive(bool aIsActive)
    79 {
    80   // Content process and switch to background with no audio and speaker forced.
    81   // Then disable speaker
    82   for (uint32_t i = 0; i < mRegisteredSpeakerManagers.Length(); i++) {
    83     mRegisteredSpeakerManagers[i]->SetAudioChannelActive(aIsActive);
    84   }
    85 }
    87 SpeakerManagerServiceChild::SpeakerManagerServiceChild()
    88 {
    89   MOZ_ASSERT(NS_IsMainThread());
    90   AudioChannelService* audioChannelService = AudioChannelService::GetAudioChannelService();
    91   if (audioChannelService) {
    92     audioChannelService->RegisterSpeakerManager(this);
    93   }
    94   MOZ_COUNT_CTOR(SpeakerManagerServiceChild);
    95 }
    97 SpeakerManagerServiceChild::~SpeakerManagerServiceChild()
    98 {
    99   AudioChannelService* audioChannelService = AudioChannelService::GetAudioChannelService();
   100   if (audioChannelService) {
   101     audioChannelService->UnregisterSpeakerManager(this);
   102   }
   103   MOZ_COUNT_DTOR(SpeakerManagerServiceChild);
   104 }
   106 void
   107 SpeakerManagerServiceChild::Notify()
   108 {
   109   for (uint32_t i = 0; i < mRegisteredSpeakerManagers.Length(); i++) {
   110     mRegisteredSpeakerManagers[i]->DispatchSimpleEvent(NS_LITERAL_STRING("speakerforcedchange"));
   111   }
   112 }

mercurial