| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/manifest/manifest_manager_host.h" | 5 #include "content/browser/manifest/manifest_manager_host.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "content/common/manifest_manager_messages.h" | 10 #include "content/common/manifest_manager_messages.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 } | 27 } |
| 28 | 28 |
| 29 } // anonymous namespace | 29 } // anonymous namespace |
| 30 | 30 |
| 31 ManifestManagerHost::ManifestManagerHost(WebContents* web_contents) | 31 ManifestManagerHost::ManifestManagerHost(WebContents* web_contents) |
| 32 : WebContentsObserver(web_contents) { | 32 : WebContentsObserver(web_contents) { |
| 33 } | 33 } |
| 34 | 34 |
| 35 ManifestManagerHost::~ManifestManagerHost() { | 35 ManifestManagerHost::~ManifestManagerHost() { |
| 36 STLDeleteValues(&pending_get_callbacks_); | 36 STLDeleteValues(&pending_get_callbacks_); |
| 37 STLDeleteValues(&pending_has_callbacks_); | |
| 38 } | 37 } |
| 39 | 38 |
| 40 ManifestManagerHost::GetCallbackMap* | 39 ManifestManagerHost::GetCallbackMap* |
| 41 ManifestManagerHost::GetCallbackMapForFrame( | 40 ManifestManagerHost::GetCallbackMapForFrame( |
| 42 RenderFrameHost* render_frame_host) { | 41 RenderFrameHost* render_frame_host) { |
| 43 FrameGetCallbackMap::iterator it = | 42 FrameGetCallbackMap::iterator it = |
| 44 pending_get_callbacks_.find(render_frame_host); | 43 pending_get_callbacks_.find(render_frame_host); |
| 45 return it != pending_get_callbacks_.end() ? it->second : nullptr; | 44 return it != pending_get_callbacks_.end() ? it->second : nullptr; |
| 46 } | 45 } |
| 47 | 46 |
| 48 ManifestManagerHost::HasCallbackMap* | |
| 49 ManifestManagerHost::HasCallbackMapForFrame( | |
| 50 RenderFrameHost* render_frame_host) { | |
| 51 FrameHasCallbackMap::iterator it = | |
| 52 pending_has_callbacks_.find(render_frame_host); | |
| 53 return it != pending_has_callbacks_.end() ? it->second : nullptr; | |
| 54 } | |
| 55 | |
| 56 void ManifestManagerHost::RenderFrameDeleted( | 47 void ManifestManagerHost::RenderFrameDeleted( |
| 57 RenderFrameHost* render_frame_host) { | 48 RenderFrameHost* render_frame_host) { |
| 49 GetCallbackMap* callbacks = GetCallbackMapForFrame(render_frame_host); |
| 50 if (!callbacks) |
| 51 return; |
| 52 |
| 53 // Call the callbacks with a failure state before deleting them. Do this in |
| 54 // a block so the iterator is destroyed before |callbacks|. |
| 58 { | 55 { |
| 59 GetCallbackMap* callbacks = GetCallbackMapForFrame(render_frame_host); | 56 GetCallbackMap::const_iterator it(callbacks); |
| 60 if (!callbacks) | 57 for (; !it.IsAtEnd(); it.Advance()) |
| 61 return; | 58 it.GetCurrentValue()->Run(GURL(), Manifest()); |
| 62 | |
| 63 // Call the callbacks with a failure state before deleting them. Do this in | |
| 64 // a block so the iterator is destroyed before |callbacks|. | |
| 65 { | |
| 66 GetCallbackMap::const_iterator it(callbacks); | |
| 67 for (; !it.IsAtEnd(); it.Advance()) | |
| 68 it.GetCurrentValue()->Run(GURL(), Manifest()); | |
| 69 } | |
| 70 | |
| 71 delete callbacks; | |
| 72 pending_get_callbacks_.erase(render_frame_host); | |
| 73 } | 59 } |
| 74 | 60 |
| 75 { | 61 delete callbacks; |
| 76 HasCallbackMap* callbacks = HasCallbackMapForFrame(render_frame_host); | 62 pending_get_callbacks_.erase(render_frame_host); |
| 77 if (!callbacks) | |
| 78 return; | |
| 79 | |
| 80 // Call the callbacks with a failure state before deleting them. Do this in | |
| 81 // a block so the iterator is destroyed before |callbacks|. | |
| 82 { | |
| 83 HasCallbackMap::const_iterator it(callbacks); | |
| 84 for (; !it.IsAtEnd(); it.Advance()) | |
| 85 it.GetCurrentValue()->Run(false); | |
| 86 } | |
| 87 | |
| 88 delete callbacks; | |
| 89 pending_has_callbacks_.erase(render_frame_host); | |
| 90 } | |
| 91 } | 63 } |
| 92 | 64 |
| 93 void ManifestManagerHost::GetManifest(RenderFrameHost* render_frame_host, | 65 void ManifestManagerHost::GetManifest(RenderFrameHost* render_frame_host, |
| 94 const GetManifestCallback& callback) { | 66 const GetManifestCallback& callback) { |
| 95 GetCallbackMap* callbacks = GetCallbackMapForFrame(render_frame_host); | 67 GetCallbackMap* callbacks = GetCallbackMapForFrame(render_frame_host); |
| 96 if (!callbacks) { | 68 if (!callbacks) { |
| 97 callbacks = new GetCallbackMap(); | 69 callbacks = new GetCallbackMap(); |
| 98 pending_get_callbacks_[render_frame_host] = callbacks; | 70 pending_get_callbacks_[render_frame_host] = callbacks; |
| 99 } | 71 } |
| 100 | 72 |
| 101 int request_id = callbacks->Add(new GetManifestCallback(callback)); | 73 int request_id = callbacks->Add(new GetManifestCallback(callback)); |
| 102 | 74 |
| 103 render_frame_host->Send(new ManifestManagerMsg_RequestManifest( | 75 render_frame_host->Send(new ManifestManagerMsg_RequestManifest( |
| 104 render_frame_host->GetRoutingID(), request_id)); | 76 render_frame_host->GetRoutingID(), request_id)); |
| 105 } | 77 } |
| 106 | 78 |
| 107 void ManifestManagerHost::HasManifest(RenderFrameHost* render_frame_host, | |
| 108 const HasManifestCallback& callback) { | |
| 109 HasCallbackMap* callbacks = HasCallbackMapForFrame(render_frame_host); | |
| 110 if (!callbacks) { | |
| 111 callbacks = new HasCallbackMap(); | |
| 112 pending_has_callbacks_[render_frame_host] = callbacks; | |
| 113 } | |
| 114 | |
| 115 int request_id = callbacks->Add(new HasManifestCallback(callback)); | |
| 116 | |
| 117 render_frame_host->Send(new ManifestManagerMsg_HasManifest( | |
| 118 render_frame_host->GetRoutingID(), request_id)); | |
| 119 } | |
| 120 | |
| 121 bool ManifestManagerHost::OnMessageReceived( | 79 bool ManifestManagerHost::OnMessageReceived( |
| 122 const IPC::Message& message, RenderFrameHost* render_frame_host) { | 80 const IPC::Message& message, RenderFrameHost* render_frame_host) { |
| 123 bool handled = true; | 81 bool handled = true; |
| 124 | 82 |
| 125 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(ManifestManagerHost, message, | 83 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(ManifestManagerHost, message, |
| 126 render_frame_host) | 84 render_frame_host) |
| 127 IPC_MESSAGE_HANDLER(ManifestManagerHostMsg_RequestManifestResponse, | 85 IPC_MESSAGE_HANDLER(ManifestManagerHostMsg_RequestManifestResponse, |
| 128 OnRequestManifestResponse) | 86 OnRequestManifestResponse) |
| 129 IPC_MESSAGE_HANDLER(ManifestManagerHostMsg_HasManifestResponse, | |
| 130 OnHasManifestResponse) | |
| 131 IPC_MESSAGE_UNHANDLED(handled = false) | 87 IPC_MESSAGE_UNHANDLED(handled = false) |
| 132 IPC_END_MESSAGE_MAP() | 88 IPC_END_MESSAGE_MAP() |
| 133 | 89 |
| 134 return handled; | 90 return handled; |
| 135 } | 91 } |
| 136 | 92 |
| 137 void ManifestManagerHost::OnRequestManifestResponse( | 93 void ManifestManagerHost::OnRequestManifestResponse( |
| 138 RenderFrameHost* render_frame_host, | 94 RenderFrameHost* render_frame_host, |
| 139 int request_id, | 95 int request_id, |
| 140 const GURL& manifest_url, | 96 const GURL& manifest_url, |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 manifest.background_color = Manifest::kInvalidOrMissingColor; | 152 manifest.background_color = Manifest::kInvalidOrMissingColor; |
| 197 | 153 |
| 198 callback->Run(manifest_url, manifest); | 154 callback->Run(manifest_url, manifest); |
| 199 callbacks->Remove(request_id); | 155 callbacks->Remove(request_id); |
| 200 if (callbacks->IsEmpty()) { | 156 if (callbacks->IsEmpty()) { |
| 201 delete callbacks; | 157 delete callbacks; |
| 202 pending_get_callbacks_.erase(render_frame_host); | 158 pending_get_callbacks_.erase(render_frame_host); |
| 203 } | 159 } |
| 204 } | 160 } |
| 205 | 161 |
| 206 void ManifestManagerHost::OnHasManifestResponse( | |
| 207 RenderFrameHost* render_frame_host, | |
| 208 int request_id, | |
| 209 bool has_manifest) { | |
| 210 HasCallbackMap* callbacks = HasCallbackMapForFrame(render_frame_host); | |
| 211 if (!callbacks) { | |
| 212 DVLOG(1) << "Unexpected HasManifestResponse from renderer. " | |
| 213 "Killing renderer."; | |
| 214 KillRenderer(render_frame_host); | |
| 215 return; | |
| 216 } | |
| 217 | |
| 218 HasManifestCallback* callback = callbacks->Lookup(request_id); | |
| 219 if (!callback) { | |
| 220 DVLOG(1) << "Received a request_id (" << request_id << ") from renderer " | |
| 221 "with no associated callback. Killing renderer."; | |
| 222 KillRenderer(render_frame_host); | |
| 223 return; | |
| 224 } | |
| 225 | |
| 226 callback->Run(has_manifest); | |
| 227 callbacks->Remove(request_id); | |
| 228 if (callbacks->IsEmpty()) { | |
| 229 delete callbacks; | |
| 230 pending_has_callbacks_.erase(render_frame_host); | |
| 231 } | |
| 232 } | |
| 233 | |
| 234 } // namespace content | 162 } // namespace content |
| OLD | NEW |