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.
5 #include "content/child/web_process_memory_dump_impl.h"
7 #include "base/trace_event/process_memory_dump.h"
8 #include "content/child/web_memory_allocator_dump_impl.h"
12 WebProcessMemoryDumpImpl::WebProcessMemoryDumpImpl()
13 : owned_process_memory_dump_(
14 new base::trace_event::ProcessMemoryDump(nullptr)),
15 process_memory_dump_(owned_process_memory_dump_
.get()) {
18 WebProcessMemoryDumpImpl::WebProcessMemoryDumpImpl(
19 base::trace_event::ProcessMemoryDump
* process_memory_dump
)
20 : process_memory_dump_(process_memory_dump
) {
23 WebProcessMemoryDumpImpl::~WebProcessMemoryDumpImpl() {
26 blink::WebMemoryAllocatorDump
*
27 WebProcessMemoryDumpImpl::createMemoryAllocatorDump(
28 const blink::WebString
& absolute_name
) {
29 // Get a MemoryAllocatorDump from the base/ object.
30 base::trace_event::MemoryAllocatorDump
* mad
=
31 process_memory_dump_
->CreateAllocatorDump(absolute_name
.utf8());
35 // Wrap it and return to blink.
36 WebMemoryAllocatorDumpImpl
* web_mad_impl
=
37 new WebMemoryAllocatorDumpImpl(mad
);
39 // memory_allocator_dumps_ will take ownership of |web_mad_impl|.
40 memory_allocator_dumps_
.push_back(web_mad_impl
);
44 void WebProcessMemoryDumpImpl::clear() {
45 // Clear all the WebMemoryAllocatorDump wrappers.
46 memory_allocator_dumps_
.clear();
48 // Clear the actual MemoryAllocatorDump objects from the underlying PMD.
49 process_memory_dump_
->Clear();
52 void WebProcessMemoryDumpImpl::takeAllDumpsFrom(
53 blink::WebProcessMemoryDump
* other
) {
54 auto other_impl
= static_cast<WebProcessMemoryDumpImpl
*>(other
);
55 // WebProcessMemoryDumpImpl is a container of WebMemoryAllocatorDump(s) which
56 // in turn are wrappers of base::trace_event::MemoryAllocatorDump(s).
57 // In order to expose the move and ownership transfer semantics of the
58 // underlying ProcessMemoryDump, we need to
60 // 1) Move and transfer the ownership of the wrapped
61 // base::trace_event::MemoryAllocatorDump(s) instances.
62 process_memory_dump_
->TakeAllDumpsFrom(other_impl
->process_memory_dump_
);
64 // 2) Move and transfer the ownership of the WebMemoryAllocatorDump wrappers.
65 memory_allocator_dumps_
.insert(memory_allocator_dumps_
.end(),
66 other_impl
->memory_allocator_dumps_
.begin(),
67 other_impl
->memory_allocator_dumps_
.end());
68 other_impl
->memory_allocator_dumps_
.weak_clear();
71 } // namespace content