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 "components/framelet/browser/framelet_memory_tracker.h" |
| 6 |
| 7 #include "components/framelet/browser/framelet_memory_tracker_client.h" |
| 8 #include "components/framelet/common/framelet_constants.h" |
| 9 #include "components/framelet/common/framelet_messages.h" |
| 10 #include "content/public/browser/browser_context.h" |
| 11 #include "content/public/browser/render_view_host.h" |
| 12 #include "content/public/common/child_process_host.h" |
| 13 |
| 14 namespace framelet { |
| 15 |
| 16 namespace { |
| 17 |
| 18 bool SendMessageToClient(const FrameletMemoryTracker::ClientID& client_id, |
| 19 IPC::Message* message) { |
| 20 content::RenderViewHost* rvh = content::RenderViewHost::FromID( |
| 21 client_id.render_process_id(), client_id.routing_id()); |
| 22 if (!rvh) { |
| 23 delete message; |
| 24 return false; |
| 25 } |
| 26 return rvh->Send(message); |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 FrameletMemoryTracker::ClientID::ClientID() |
| 32 : render_process_id_(content::ChildProcessHost::kInvalidUniqueID), |
| 33 routing_id_(MSG_ROUTING_NONE) {} |
| 34 |
| 35 FrameletMemoryTracker::ClientID::ClientID(int render_process_id, int routing_id) |
| 36 : render_process_id_(render_process_id), routing_id_(routing_id) {} |
| 37 |
| 38 FrameletMemoryTracker::ClientID::~ClientID() {} |
| 39 |
| 40 bool FrameletMemoryTracker::ClientID::operator<(const ClientID& other) const { |
| 41 return std::tie(render_process_id_, routing_id_) < |
| 42 std::tie(other.render_process_id_, other.routing_id_); |
| 43 } |
| 44 |
| 45 bool FrameletMemoryTracker::ClientID::operator==(const ClientID& other) const { |
| 46 return (render_process_id_ == other.render_process_id_) && |
| 47 (routing_id_ == other.routing_id_); |
| 48 } |
| 49 |
| 50 // static |
| 51 FrameletMemoryTracker* FrameletMemoryTracker::FromBrowserContext( |
| 52 content::BrowserContext* context) { |
| 53 FrameletMemoryTracker* tracker = static_cast<FrameletMemoryTracker*>( |
| 54 context->GetUserData(kFrameletMemoryTrackerKeyName)); |
| 55 if (!tracker) { |
| 56 tracker = new FrameletMemoryTracker(); |
| 57 context->SetUserData(kFrameletMemoryTrackerKeyName, tracker); |
| 58 } |
| 59 return tracker; |
| 60 } |
| 61 |
| 62 void FrameletMemoryTracker::ReportHeapSize(const ClientID& client_id, |
| 63 int heap_size) { |
| 64 DCHECK_LT(0u, acks_pending_.count(client_id)); |
| 65 // The client might have been removed since we issued the heap size request. |
| 66 if (!clients_.count(client_id)) |
| 67 return; |
| 68 clients_[client_id]->ReportHeapSize(heap_size); |
| 69 acks_pending_.erase(client_id); |
| 70 } |
| 71 |
| 72 void FrameletMemoryTracker::AddClient(const ClientID& client_id, |
| 73 FrameletMemoryTrackerClient* client) { |
| 74 clients_.insert(std::make_pair(client_id, client)); |
| 75 if (!timer_.IsRunning()) { |
| 76 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), this, |
| 77 &FrameletMemoryTracker::OnElapsedTime); |
| 78 } |
| 79 } |
| 80 |
| 81 void FrameletMemoryTracker::RemoveClient(const ClientID& client_id) { |
| 82 clients_.erase(client_id); |
| 83 if (clients_.empty() && timer_.IsRunning()) |
| 84 timer_.Stop(); |
| 85 } |
| 86 |
| 87 FrameletMemoryTracker::FrameletMemoryTracker() {} |
| 88 |
| 89 FrameletMemoryTracker::~FrameletMemoryTracker() {} |
| 90 |
| 91 void FrameletMemoryTracker::OnElapsedTime() { |
| 92 for (auto& kv : clients_) { |
| 93 const ClientID& client_id = kv.first; |
| 94 if (acks_pending_.count(client_id)) |
| 95 continue; |
| 96 if (SendMessageToClient(client_id, new ChromeGuestViewMsg_RequestHeapSize( |
| 97 client_id.routing_id()))) { |
| 98 acks_pending_.insert(client_id); |
| 99 } |
| 100 } |
| 101 } |
| 102 |
| 103 } // namespace framelet |
OLD | NEW |