OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/memory/memory_pressure_controller.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/memory_pressure_listener.h" | |
9 #include "content/browser/memory/memory_message_filter.h" | |
10 #include "content/public/browser/browser_thread.h" | |
11 | |
12 namespace content { | |
13 | |
14 MemoryPressureController::MemoryPressureController() {} | |
15 | |
16 MemoryPressureController::~MemoryPressureController() {} | |
17 | |
18 void MemoryPressureController::OnMemoryMessageFilterAdded( | |
19 MemoryMessageFilter* filter) { | |
20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
Primiano Tucci (use gerrit)
2015/09/17 18:23:49
You can use just DCHECK_CURRENTLY_ON macro
petrcermak
2015/09/18 10:19:12
Done.
| |
21 DCHECK(memory_message_filters_.find(filter) == memory_message_filters_.end()); | |
Primiano Tucci (use gerrit)
2015/09/17 18:23:49
You could make this a bit faster on debug builds b
petrcermak
2015/09/18 10:19:12
Done.
| |
22 | |
23 memory_message_filters_.insert(filter); | |
24 | |
25 // There's no need to send a message to the child process if memory pressure | |
26 // notifications are not suppressed. | |
27 if (base::MemoryPressureListener::AreNotificationsSuppressed()) | |
28 filter->SendSetPressureNotificationsSuppressed(true); | |
29 } | |
30 | |
31 void MemoryPressureController::OnMemoryMessageFilterRemoved( | |
32 MemoryMessageFilter* filter) { | |
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
34 DCHECK(memory_message_filters_.find(filter) != memory_message_filters_.end()); | |
Primiano Tucci (use gerrit)
2015/09/17 18:23:49
just use count() here, should be slighly faster
petrcermak
2015/09/18 10:19:12
I changed this to use the return value of std::set
| |
35 | |
36 memory_message_filters_.erase(filter); | |
37 } | |
38 | |
39 // static | |
40 MemoryPressureController* MemoryPressureController::GetInstance() { | |
41 return base::Singleton< | |
42 MemoryPressureController, | |
43 base::LeakySingletonTraits<MemoryPressureController>>::get(); | |
44 } | |
45 | |
46 void MemoryPressureController::SetPressureNotificationsSuppressedInAllProcesses( | |
47 bool suppressed) { | |
48 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
49 // Note that passing base::Unretained(this) is safe here because the | |
50 // controller is a leaky singleton (i.e. it is never deleted). | |
51 BrowserThread::PostTask( | |
52 BrowserThread::IO, FROM_HERE, | |
53 base::Bind(&MemoryPressureController:: | |
54 SetPressureNotificationsSuppressedInAllProcesses, | |
55 base::Unretained(this), suppressed)); | |
56 return; | |
57 } | |
58 | |
59 // Enable/disable suppressing memory notifications in the browser process. | |
60 base::MemoryPressureListener::SetNotificationsSuppressed(suppressed); | |
61 | |
62 // Enable/disable suppressing memory notifications in all child processes. | |
63 for (MemoryMessageFilterSet::iterator it = memory_message_filters_.begin(); | |
64 it != memory_message_filters_.end(); ++it) { | |
65 it->get()->SendSetPressureNotificationsSuppressed(suppressed); | |
66 } | |
67 } | |
68 | |
69 } // namespace content | |
OLD | NEW |