check if target stream is intercepted for correct request
authordeepak1556 <hop2deep@gmail.com>
Mon, 13 Feb 2017 22:41:04 +0000 (04:11 +0530)
committerdeepak1556 <hop2deep@gmail.com>
Mon, 13 Mar 2017 18:56:27 +0000 (00:26 +0530)
atom/browser/atom_resource_dispatcher_host_delegate.cc
atom/browser/atom_resource_dispatcher_host_delegate.h

index 571208e..12abd8e 100644 (file)
@@ -65,6 +65,7 @@ void HandleExternalProtocolInUI(
 
 void OnPdfStreamCreated(
     std::unique_ptr<content::StreamInfo> stream,
+    const std::string& stream_id,
     const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter,
     int render_process_id,
     int render_frame_id) {
@@ -75,7 +76,6 @@ void OnPdfStreamCreated(
   auto browser_context =
       static_cast<AtomBrowserContext*>(web_contents->GetBrowserContext());
   auto stream_manager = browser_context->stream_manager();
-  std::string stream_id = base::GenerateGUID();
   GURL original_url = stream->original_url;
   stream_manager->AddStream(std::move(stream), stream_id, render_process_id,
                             render_frame_id);
@@ -136,6 +136,7 @@ bool AtomResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream(
     std::string* payload) {
   if (mime_type == "application/pdf") {
     *origin = GURL(kPdfViewerUIOrigin);
+    stream_info_[request] = base::GenerateGUID();
     return true;
   }
   return false;
@@ -144,13 +145,17 @@ bool AtomResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream(
 void AtomResourceDispatcherHostDelegate::OnStreamCreated(
     net::URLRequest* request,
     std::unique_ptr<content::StreamInfo> stream) {
+  auto it = stream_info_.find(request);
+  if (it == stream_info_.end())
+    return;
   const content::ResourceRequestInfo* info =
       content::ResourceRequestInfo::ForRequest(request);
   content::BrowserThread::PostTask(
       BrowserThread::UI, FROM_HERE,
-      base::Bind(&OnPdfStreamCreated, base::Passed(&stream),
+      base::Bind(&OnPdfStreamCreated, base::Passed(&stream), it->second,
                  info->GetWebContentsGetterForRequest(), info->GetChildID(),
                  info->GetRenderFrameID()));
+  stream_info_.erase(it);
 }
 
 }  // namespace atom
index 65735f1..e1bb803 100644 (file)
@@ -5,6 +5,7 @@
 #ifndef ATOM_BROWSER_ATOM_RESOURCE_DISPATCHER_HOST_DELEGATE_H_
 #define ATOM_BROWSER_ATOM_RESOURCE_DISPATCHER_HOST_DELEGATE_H_
 
+#include <map>
 #include <string>
 
 #include "content/public/browser/resource_dispatcher_host_delegate.h"
@@ -31,6 +32,12 @@ class AtomResourceDispatcherHostDelegate
                                        std::string* payload) override;
   void OnStreamCreated(net::URLRequest* request,
                        std::unique_ptr<content::StreamInfo> stream) override;
+
+ private:
+  // Map between intercepted request and its generated stream id.
+  std::map<net::URLRequest*, std::string> stream_info_;
+
+  DISALLOW_COPY_AND_ASSIGN(AtomResourceDispatcherHostDelegate);
 };
 
 }  // namespace atom