Skip to content

Instantly share code, notes, and snippets.

@allstarschh
Created May 19, 2016 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allstarschh/74a2f436f690a7349311317171d10f54 to your computer and use it in GitHub Desktop.
Save allstarschh/74a2f436f690a7349311317171d10f54 to your computer and use it in GitHub Desktop.
From befbe4372a3265ed06f83587f730b436720a4e1e Mon Sep 17 00:00:00 2001
From: Yoshi Huang <allstars.chh@mozilla.com>
Date: Thu, 19 May 2016 17:47:38 +0800
Subject: add setOriginAttributes in nsIXMLHttpRequest
---
dom/base/nsIXMLHttpRequest.idl | 6 ++++++
dom/base/nsXMLHttpRequest.cpp | 17 +++++++++++++++++
.../components/search/SearchSuggestionController.jsm | 9 +++++++++
3 files changed, 32 insertions(+)
diff --git a/dom/base/nsIXMLHttpRequest.idl b/dom/base/nsIXMLHttpRequest.idl
index 9eb6aa8..fb4548b 100644
--- a/dom/base/nsIXMLHttpRequest.idl
+++ b/dom/base/nsIXMLHttpRequest.idl
@@ -179,16 +179,22 @@ interface nsIXMLHttpRequest : nsISupports
* The default value is the empty string
*/
[optional_argc] void open(in ACString method, in AUTF8String url,
[optional] in boolean async,
[optional,Undefined(Empty)] in DOMString user,
[optional,Undefined(Empty)] in DOMString password);
/**
+ * Set Origin Attributes on this XHR.
+ */
+ [implicit_jscontext]
+ void setOriginAttributes(in jsval originAttributes);
+
+ /**
* Sends the request. If the request is asynchronous, returns
* immediately after sending the request. If it is synchronous
* returns only after the response has been received.
*
* All event listeners must be set before calling send().
*
* After the initial response, all event listeners will be cleared.
* // XXXbz what does that mean, exactly?
diff --git a/dom/base/nsXMLHttpRequest.cpp b/dom/base/nsXMLHttpRequest.cpp
index b0bec82e..2f55e01 100644
--- a/dom/base/nsXMLHttpRequest.cpp
+++ b/dom/base/nsXMLHttpRequest.cpp
@@ -14,16 +14,17 @@
#include "mozilla/dom/BlobSet.h"
#include "mozilla/dom/File.h"
#include "mozilla/dom/FetchUtil.h"
#include "mozilla/dom/FormData.h"
#include "mozilla/dom/XMLHttpRequestUploadBinding.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/EventListenerManager.h"
#include "mozilla/LoadInfo.h"
+#include "mozilla/LoadContext.h"
#include "mozilla/MemoryReporting.h"
#include "nsIDOMDocument.h"
#include "mozilla/dom/ProgressEvent.h"
#include "nsIJARChannel.h"
#include "nsIJARURI.h"
#include "nsLayoutCID.h"
#include "nsReadableUtils.h"
@@ -1665,16 +1666,32 @@ nsXMLHttpRequest::Open(const nsACString& inMethod, const nsACString& url,
}
}
ChangeState(XML_HTTP_REQUEST_OPENED);
return NS_OK;
}
+NS_IMETHODIMP
+nsXMLHttpRequest::SetOriginAttributes(JS::Handle<JS::Value> aAttrs,
+ JSContext* aCx)
+{
+ DocShellOriginAttributes attrs;
+ if (!aAttrs.isObject() || !attrs.Init(aCx, aAttrs)) {
+ return NS_ERROR_INVALID_ARG;
+ }
+
+ nsCOMPtr<nsIInterfaceRequestor> loadContext = new LoadContext(attrs);
+ nsresult rv = mChannel->SetNotificationCallbacks(loadContext);
+ NS_ENSURE_SUCCESS(rv, rv);
+
+ return NS_OK;
+}
+
void
nsXMLHttpRequest::PopulateNetworkInterfaceId()
{
if (mNetworkInterfaceId.IsEmpty()) {
return;
}
nsCOMPtr<nsIHttpChannelInternal> channel(do_QueryInterface(mChannel));
if (!channel) {
diff --git a/toolkit/components/search/SearchSuggestionController.jsm b/toolkit/components/search/SearchSuggestionController.jsm
index 0858974..ae45f29 100644
--- a/toolkit/components/search/SearchSuggestionController.jsm
+++ b/toolkit/components/search/SearchSuggestionController.jsm
@@ -226,26 +226,35 @@ this.SearchSuggestionController.prototype = {
acSearchObserver);
return deferredFormHistory;
},
/**
* Fetch suggestions from the search engine over the network.
*/
_fetchRemote: function(searchTerm, engine, privateMode) {
+ dump("XXX _fetchRemote searchTerm="+searchTerm+"\n");
let deferredResponse = Promise.defer();
this._request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].
createInstance(Ci.nsIXMLHttpRequest);
let submission = engine.getSubmission(searchTerm,
SEARCH_RESPONSE_SUGGESTION_JSON);
let method = (submission.postData ? "POST" : "GET");
this._request.open(method, submission.uri.spec, true);
if (this._request.channel instanceof Ci.nsIPrivateBrowsingChannel) {
this._request.channel.setPrivate(privateMode);
}
+ dump("XXX xhr="+this._request+"\n");
+ dump("XXX xhr.send="+this._request.send+"\n");
+ dump("XXX xhr.setOriginAttributes="+this._request.setOriginAttributes+"\n");
+ if (this._request instanceof Ci.nsIXMLHttpRequest ) {
+ dump("XXX xhr is instanceof nsIXMLHttpRequest \n");
+ }
+
+ this._request.setOriginAttributes({userContextId: 1});
this._request.mozBackgroundRequest = true; // suppress dialogs and fail silently
this._request.addEventListener("load", this._onRemoteLoaded.bind(this, deferredResponse));
this._request.addEventListener("error", (evt) => deferredResponse.resolve("HTTP error"));
// Reject for an abort assuming it's always from .stop() in which case we shouldn't return local
// or remote results for existing searches.
this._request.addEventListener("abort", (evt) => deferredResponse.reject("HTTP request aborted"));
--
2.5.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment