1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=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 "TemporaryAccessGrantObserver.h"
9 #include "mozilla/AppShutdown.h"
10 #include "mozilla/ClearOnShutdown.h"
11 #include "mozilla/PermissionManager.h"
12 #include "mozilla/Services.h"
13 #include "nsIObserverService.h"
14 #include "nsTHashtable.h"
15 #include "nsXULAppAPI.h"
17 using namespace mozilla
;
19 StaticAutoPtr
<TemporaryAccessGrantObserver::ObserversTable
>
20 TemporaryAccessGrantObserver::sObservers
;
22 TemporaryAccessGrantObserver::TemporaryAccessGrantObserver(
23 PermissionManager
* aPM
, nsIPrincipal
* aPrincipal
, const nsACString
& aType
)
24 : mPM(aPM
), mPrincipal(aPrincipal
), mType(aType
) {
25 MOZ_ASSERT(XRE_IsParentProcess(),
26 "Enforcing temporary access grant lifetimes can only be done in "
27 "the parent process");
30 NS_IMPL_ISUPPORTS(TemporaryAccessGrantObserver
, nsIObserver
, nsINamed
)
33 void TemporaryAccessGrantObserver::Create(PermissionManager
* aPM
,
34 nsIPrincipal
* aPrincipal
,
35 const nsACString
& aType
) {
36 MOZ_ASSERT(XRE_IsParentProcess());
37 MOZ_ASSERT(NS_IsMainThread());
40 // Prevent us from resurrection during shutdown. Note that ClearOnShutdown
41 // would kill us right away in this case, but we want to return with no-op.
42 if (AppShutdown::IsInOrBeyond(ShutdownPhase::XPCOMWillShutdown
)) {
45 sObservers
= new ObserversTable();
46 // PermissionManager shuts down in this phase, so we should avoid to keep
47 // references to it for longer. Clearing will cancel all outstanding timers
48 // and clear the table. This is safe as the timers have been created from
49 // and target to the main thread (so we do not need an explicit Cancel).
50 ClearOnShutdown(&sObservers
, ShutdownPhase::XPCOMWillShutdown
);
52 sObservers
->LookupOrInsertWith(
53 std::make_pair(nsCOMPtr
<nsIPrincipal
>(aPrincipal
), nsCString(aType
)),
54 [&]() -> nsCOMPtr
<nsITimer
> {
55 // Only create a new observer if we don't have a matching
56 // entry in our hashtable.
57 nsCOMPtr
<nsITimer
> timer
;
58 RefPtr
<TemporaryAccessGrantObserver
> observer
=
59 new TemporaryAccessGrantObserver(aPM
, aPrincipal
, aType
);
60 nsresult rv
= NS_NewTimerWithObserver(getter_AddRefs(timer
), observer
,
61 24 * 60 * 60 * 1000, // 24 hours
62 nsITimer::TYPE_ONE_SHOT
);
64 if (NS_SUCCEEDED(rv
)) {
65 observer
->SetTimer(timer
);
73 void TemporaryAccessGrantObserver::SetTimer(nsITimer
* aTimer
) {
78 TemporaryAccessGrantObserver::Observe(nsISupports
* aSubject
, const char* aTopic
,
79 const char16_t
* aData
) {
80 if (strcmp(aTopic
, NS_TIMER_CALLBACK_TOPIC
) == 0) {
81 Unused
<< mPM
->RemoveFromPrincipal(mPrincipal
, mType
);
83 MOZ_ASSERT(sObservers
);
84 sObservers
->Remove(std::make_pair(mPrincipal
, mType
));
91 TemporaryAccessGrantObserver::GetName(nsACString
& aName
) {
92 aName
.AssignLiteral("TemporaryAccessGrantObserver");