/src/mozilla-central/toolkit/components/extensions/webrequest/WebRequestService.cpp
Line | Count | Source (jump to first uncovered line) |
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 |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "WebRequestService.h" |
8 | | |
9 | | #include "mozilla/Assertions.h" |
10 | | #include "mozilla/ClearOnShutdown.h" |
11 | | #include "nsIChannel.h" |
12 | | |
13 | | using namespace mozilla; |
14 | | using namespace mozilla::dom; |
15 | | using namespace mozilla::extensions; |
16 | | |
17 | | static WebRequestService* sWeakWebRequestService; |
18 | | |
19 | | WebRequestService::~WebRequestService() |
20 | 0 | { |
21 | 0 | sWeakWebRequestService = nullptr; |
22 | 0 | } |
23 | | |
24 | | /* static */ WebRequestService& |
25 | | WebRequestService::GetSingleton() |
26 | 0 | { |
27 | 0 | static RefPtr<WebRequestService> instance; |
28 | 0 | if (!sWeakWebRequestService) { |
29 | 0 | instance = new WebRequestService(); |
30 | 0 | ClearOnShutdown(&instance); |
31 | 0 |
|
32 | 0 | // A separate weak instance that we keep a reference to as long as the |
33 | 0 | // original service is alive, even after our strong reference is cleared to |
34 | 0 | // allow the service to be destroyed. |
35 | 0 | sWeakWebRequestService = instance; |
36 | 0 | } |
37 | 0 | return *sWeakWebRequestService; |
38 | 0 | } |
39 | | |
40 | | |
41 | | UniquePtr<WebRequestChannelEntry> |
42 | | WebRequestService::RegisterChannel(ChannelWrapper* aChannel) |
43 | 0 | { |
44 | 0 | UniquePtr<ChannelEntry> entry(new ChannelEntry(aChannel)); |
45 | 0 |
|
46 | 0 | auto key = mChannelEntries.LookupForAdd(entry->mChannelId); |
47 | 0 | MOZ_DIAGNOSTIC_ASSERT(!key); |
48 | 0 | key.OrInsert([&entry]() { return entry.get(); }); |
49 | 0 |
|
50 | 0 | return entry; |
51 | 0 |
|
52 | 0 | } |
53 | | |
54 | | already_AddRefed<nsITraceableChannel> |
55 | | WebRequestService::GetTraceableChannel(uint64_t aChannelId, |
56 | | nsAtom* aAddonId, |
57 | | nsIContentParent* aContentParent) |
58 | 0 | { |
59 | 0 | if (auto entry = mChannelEntries.Get(aChannelId)) { |
60 | 0 | if (entry->mChannel) { |
61 | 0 | return entry->mChannel->GetTraceableChannel(aAddonId, aContentParent); |
62 | 0 |
|
63 | 0 | } |
64 | 0 | } |
65 | 0 | return nullptr; |
66 | 0 | } |
67 | | |
68 | | WebRequestChannelEntry::WebRequestChannelEntry(ChannelWrapper* aChannel) |
69 | | : mChannelId(aChannel->Id()) |
70 | | , mChannel(aChannel) |
71 | 0 | {} |
72 | | |
73 | | WebRequestChannelEntry::~WebRequestChannelEntry() |
74 | 0 | { |
75 | 0 | if (sWeakWebRequestService) { |
76 | 0 | sWeakWebRequestService->mChannelEntries.Remove(mChannelId); |
77 | 0 | } |
78 | 0 | } |