Merge pull request #8085 from deepak1556/protocol_http_session_patch
protocol: support custom session with interceptHttpProtocol
This commit is contained in:
		
				commit
				
					
						f2aabeefd5
					
				
			
		
					 2 changed files with 57 additions and 20 deletions
				
			
		| 
						 | 
					@ -7,6 +7,8 @@
 | 
				
			||||||
#include <algorithm>
 | 
					#include <algorithm>
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "atom/browser/api/atom_api_session.h"
 | 
				
			||||||
 | 
					#include "atom/browser/atom_browser_context.h"
 | 
				
			||||||
#include "base/memory/ptr_util.h"
 | 
					#include "base/memory/ptr_util.h"
 | 
				
			||||||
#include "base/strings/string_util.h"
 | 
					#include "base/strings/string_util.h"
 | 
				
			||||||
#include "native_mate/dictionary.h"
 | 
					#include "native_mate/dictionary.h"
 | 
				
			||||||
| 
						 | 
					@ -89,15 +91,23 @@ void URLRequestFetchJob::BeforeStartInUI(
 | 
				
			||||||
    return;
 | 
					    return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // When |session| is set to |null| we use a new request context for fetch job.
 | 
					  // When |session| is set to |null| we use a new request context for fetch job.
 | 
				
			||||||
  // TODO(zcbenz): Handle the case when it is not null.
 | 
					  v8::Local<v8::Value> val;
 | 
				
			||||||
  v8::Local<v8::Value> session;
 | 
					  if (options.Get("session", &val)) {
 | 
				
			||||||
  if (options.Get("session", &session) && session->IsNull()) {
 | 
					    if (val->IsNull()) {
 | 
				
			||||||
      // We have to create the URLRequestContextGetter on UI thread.
 | 
					      // We have to create the URLRequestContextGetter on UI thread.
 | 
				
			||||||
      url_request_context_getter_ = new brightray::URLRequestContextGetter(
 | 
					      url_request_context_getter_ = new brightray::URLRequestContextGetter(
 | 
				
			||||||
          this, nullptr, nullptr, base::FilePath(), true,
 | 
					          this, nullptr, nullptr, base::FilePath(), true,
 | 
				
			||||||
          BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
 | 
					          BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
 | 
				
			||||||
          BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
 | 
					          BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
 | 
				
			||||||
          nullptr, content::URLRequestInterceptorScopedVector());
 | 
					          nullptr, content::URLRequestInterceptorScopedVector());
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      mate::Handle<api::Session> session;
 | 
				
			||||||
 | 
					      if (mate::ConvertFromV8(isolate, val, &session) && !session.IsEmpty()) {
 | 
				
			||||||
 | 
					        AtomBrowserContext* browser_context = session->browser_context();
 | 
				
			||||||
 | 
					        url_request_context_getter_ =
 | 
				
			||||||
 | 
					            browser_context->url_request_context_getter();
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -237,6 +247,10 @@ int URLRequestFetchJob::GetResponseCode() const {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void URLRequestFetchJob::OnURLFetchComplete(const net::URLFetcher* source) {
 | 
					void URLRequestFetchJob::OnURLFetchComplete(const net::URLFetcher* source) {
 | 
				
			||||||
 | 
					  ClearPendingBuffer();
 | 
				
			||||||
 | 
					  ClearWriteBuffer();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (fetcher_->GetStatus().is_success()) {
 | 
				
			||||||
    if (!response_info_) {
 | 
					    if (!response_info_) {
 | 
				
			||||||
      // Since we notify header completion only after first write there will be
 | 
					      // Since we notify header completion only after first write there will be
 | 
				
			||||||
      // no response object constructed for http respones with no content 204.
 | 
					      // no response object constructed for http respones with no content 204.
 | 
				
			||||||
| 
						 | 
					@ -244,15 +258,11 @@ void URLRequestFetchJob::OnURLFetchComplete(const net::URLFetcher* source) {
 | 
				
			||||||
      HeadersCompleted();
 | 
					      HeadersCompleted();
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
  ClearPendingBuffer();
 | 
					 | 
				
			||||||
  ClearWriteBuffer();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if (fetcher_->GetStatus().is_success())
 | 
					 | 
				
			||||||
    ReadRawDataComplete(0);
 | 
					    ReadRawDataComplete(0);
 | 
				
			||||||
  else
 | 
					  } else {
 | 
				
			||||||
    NotifyStartError(fetcher_->GetStatus());
 | 
					    NotifyStartError(fetcher_->GetStatus());
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int URLRequestFetchJob::BufferCopy(net::IOBuffer* source, int num_bytes,
 | 
					int URLRequestFetchJob::BufferCopy(net::IOBuffer* source, int num_bytes,
 | 
				
			||||||
                                   net::IOBuffer* target, int target_size) {
 | 
					                                   net::IOBuffer* target, int target_size) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,7 +4,7 @@ const path = require('path')
 | 
				
			||||||
const qs = require('querystring')
 | 
					const qs = require('querystring')
 | 
				
			||||||
const {closeWindow} = require('./window-helpers')
 | 
					const {closeWindow} = require('./window-helpers')
 | 
				
			||||||
const remote = require('electron').remote
 | 
					const remote = require('electron').remote
 | 
				
			||||||
const {BrowserWindow, ipcMain, protocol, webContents} = remote
 | 
					const {BrowserWindow, ipcMain, protocol, session, webContents} = remote
 | 
				
			||||||
 | 
					
 | 
				
			||||||
describe('protocol module', function () {
 | 
					describe('protocol module', function () {
 | 
				
			||||||
  var protocolName = 'sp'
 | 
					  var protocolName = 'sp'
 | 
				
			||||||
| 
						 | 
					@ -870,6 +870,33 @@ describe('protocol module', function () {
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it('can use custom session', function (done) {
 | 
				
			||||||
 | 
					      const customSession = session.fromPartition('custom-ses', {
 | 
				
			||||||
 | 
					        cache: false
 | 
				
			||||||
 | 
					      })
 | 
				
			||||||
 | 
					      customSession.webRequest.onBeforeRequest(function (details, callback) {
 | 
				
			||||||
 | 
					        assert.equal(details.url, 'http://fake-host/')
 | 
				
			||||||
 | 
					        callback({cancel: true})
 | 
				
			||||||
 | 
					      })
 | 
				
			||||||
 | 
					      const handler = function (request, callback) {
 | 
				
			||||||
 | 
					        callback({
 | 
				
			||||||
 | 
					          url: request.url,
 | 
				
			||||||
 | 
					          session: customSession
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      protocol.interceptHttpProtocol('http', handler, function (error) {
 | 
				
			||||||
 | 
					        if (error) {
 | 
				
			||||||
 | 
					          return done(error)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        fetch('http://fake-host').then(function () {
 | 
				
			||||||
 | 
					          done('request succeeded but it should not')
 | 
				
			||||||
 | 
					        }).catch(function () {
 | 
				
			||||||
 | 
					          customSession.webRequest.onBeforeRequest(null)
 | 
				
			||||||
 | 
					          done()
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					      })
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  describe('protocol.uninterceptProtocol', function () {
 | 
					  describe('protocol.uninterceptProtocol', function () {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue