From c1b1348735e969c496cc87adbbc8819b4c98b24b Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Fri, 8 Apr 2016 11:19:36 -0700 Subject: [PATCH 1/4] Add `resourceType` arg to webContents `did-get-response-details` event. Fixes #5074 and follows @zcbenz's recommendation to expose ResourceTypeToString from atom_network_delegate publicly. Also adds testing for other arguments to the `did-get-response-details` events, since there were no existing tests for them. --- atom/browser/api/atom_api_web_contents.cc | 4 ++- atom/browser/net/atom_network_delegate.cc | 5 ++-- atom/browser/net/atom_network_delegate.h | 3 +++ spec/api-browser-window-spec.js | 26 +++++++++++++++++++ .../pages/did-get-response-details.html | 5 ++++ 5 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 spec/fixtures/pages/did-get-response-details.html diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 610d958ca1ea..f8f8add95aac 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -14,6 +14,7 @@ #include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/native_window.h" +#include "atom/browser/net/atom_network_delegate.h" #include "atom/browser/web_contents_permission_helper.h" #include "atom/browser/web_contents_preferences.h" #include "atom/browser/web_view_guest_delegate.h" @@ -587,7 +588,8 @@ void WebContents::DidGetResourceResponseStart( details.http_response_code, details.method, details.referrer, - details.headers.get()); + details.headers.get(), + ResourceTypeToString(details.resource_type)); } void WebContents::DidGetRedirectForResourceRequest( diff --git a/atom/browser/net/atom_network_delegate.cc b/atom/browser/net/atom_network_delegate.cc index 91b63fada359..9050fadbe501 100644 --- a/atom/browser/net/atom_network_delegate.cc +++ b/atom/browser/net/atom_network_delegate.cc @@ -10,15 +10,12 @@ #include "base/stl_util.h" #include "base/strings/string_util.h" #include "content/public/browser/browser_thread.h" -#include "content/public/browser/resource_request_info.h" #include "net/url_request/url_request.h" using content::BrowserThread; namespace atom { -namespace { - const char* ResourceTypeToString(content::ResourceType type) { switch (type) { case content::RESOURCE_TYPE_MAIN_FRAME: @@ -39,6 +36,8 @@ const char* ResourceTypeToString(content::ResourceType type) { return "other"; } } + +namespace { void RunSimpleListener(const AtomNetworkDelegate::SimpleListener& listener, scoped_ptr details) { diff --git a/atom/browser/net/atom_network_delegate.h b/atom/browser/net/atom_network_delegate.h index 4f55f7c09863..ee159df60f13 100644 --- a/atom/browser/net/atom_network_delegate.h +++ b/atom/browser/net/atom_network_delegate.h @@ -15,6 +15,7 @@ #include "net/base/net_errors.h" #include "net/http/http_request_headers.h" #include "net/http/http_response_headers.h" +#include "content/public/browser/resource_request_info.h" namespace extensions { class URLPattern; @@ -24,6 +25,8 @@ namespace atom { using URLPatterns = std::set; +const char* ResourceTypeToString(content::ResourceType type); + class AtomNetworkDelegate : public brightray::NetworkDelegate { public: using ResponseCallback = base::Callback; diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index a9940bde2633..5775a89e2948 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -101,6 +101,32 @@ describe('browser-window module', function () { w.loadURL('about:blank') }) + it('should emit did-get-response-details event', function (done) { + // expected {fileName: resourceType} pairs + var expectedResources = { + 'did-get-response-details.html': 'mainFrame', + 'logo.png': 'image' + } + var responses = 0; + w.webContents.on('did-get-response-details', function (event, status, newUrl, oldUrl, responseCode, method, referrer, headers, resourceType) { + responses++ + var fileName = newUrl.slice(newUrl.lastIndexOf('/') + 1) + var expectedType = expectedResources[fileName] + assert(!!expectedType, `Unexpected response details for ${newUrl}`) + assert(typeof status === 'boolean', 'status should be boolean') + assert.equal(responseCode, 200) + assert.equal(method, 'GET') + assert(typeof referrer === 'string', 'referrer should be string') + assert(!!headers, 'headers should be present') + assert(typeof headers === 'object', 'headers should be object') + assert.equal(resourceType, expectedType, 'Incorrect resourceType') + if (responses === Object.keys(expectedResources).length) { + done() + } + }) + w.loadURL('file://' + path.join(fixtures, 'pages', 'did-get-response-details.html')) + }) + it('should emit did-fail-load event for files that do not exist', function (done) { w.webContents.on('did-fail-load', function (event, code, desc, url, isMainFrame) { assert.equal(code, -6) diff --git a/spec/fixtures/pages/did-get-response-details.html b/spec/fixtures/pages/did-get-response-details.html new file mode 100644 index 000000000000..c98458c8ffaa --- /dev/null +++ b/spec/fixtures/pages/did-get-response-details.html @@ -0,0 +1,5 @@ + + + + + From 15b042b5f6196c917169b9b865cbd1d8114e3c47 Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Fri, 8 Apr 2016 12:55:20 -0700 Subject: [PATCH 2/4] Add support/tests for `did-get-response-details` event on --- lib/renderer/web-view/guest-view-internal.js | 2 +- spec/webview-spec.js | 29 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/renderer/web-view/guest-view-internal.js b/lib/renderer/web-view/guest-view-internal.js index efe462fc9143..4f920707d1be 100644 --- a/lib/renderer/web-view/guest-view-internal.js +++ b/lib/renderer/web-view/guest-view-internal.js @@ -12,7 +12,7 @@ var WEB_VIEW_EVENTS = { 'did-frame-finish-load': ['isMainFrame'], 'did-start-loading': [], 'did-stop-loading': [], - 'did-get-response-details': ['status', 'newURL', 'originalURL', 'httpResponseCode', 'requestMethod', 'referrer', 'headers'], + 'did-get-response-details': ['status', 'newURL', 'originalURL', 'httpResponseCode', 'requestMethod', 'referrer', 'headers', 'resourceType'], 'did-get-redirect-request': ['oldURL', 'newURL', 'isMainFrame'], 'dom-ready': [], 'console-message': ['level', 'message', 'line', 'sourceId'], diff --git a/spec/webview-spec.js b/spec/webview-spec.js index 0d32138906e3..64b73d9d2202 100644 --- a/spec/webview-spec.js +++ b/spec/webview-spec.js @@ -763,4 +763,33 @@ describe(' tag', function () { document.body.appendChild(webview) }) }) + + describe('did-get-response-details event', function () { + it('emits for the page and its resources', function (done) { + // expected {fileName: resourceType} pairs + var expectedResources = { + 'did-get-response-details.html': 'mainFrame', + 'logo.png': 'image' + } + var responses = 0; + webview.addEventListener('did-get-response-details', function (event) { + responses++ + var fileName = event.newURL.slice(event.newURL.lastIndexOf('/') + 1) + var expectedType = expectedResources[fileName] + assert(!!expectedType, `Unexpected response details for ${event.newURL}`) + assert(typeof event.status === 'boolean', 'status should be boolean') + assert.equal(event.httpResponseCode, 200) + assert.equal(event.requestMethod, 'GET') + assert(typeof event.referrer === 'string', 'referrer should be string') + assert(!!event.headers, 'headers should be present') + assert(typeof event.headers === 'object', 'headers should be object') + assert.equal(event.resourceType, expectedType, 'Incorrect resourceType') + if (responses === Object.keys(expectedResources).length) { + done() + } + }) + webview.src = 'file://' + path.join(fixtures, 'pages', 'did-get-response-details.html') + document.body.appendChild(webview) + }) + }) }) From 50f51899de609d75ea7a33dfb531b79f630d0780 Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Fri, 8 Apr 2016 13:03:22 -0700 Subject: [PATCH 3/4] Document `resourceType` arg for `did-get-response-details` event. --- docs-translations/ko-KR/api/web-contents.md | 1 + docs-translations/ko-KR/api/web-view-tag.md | 1 + docs-translations/zh-CN/api/web-contents.md | 1 + docs-translations/zh-CN/api/web-view-tag.md | 1 + docs/api/web-contents.md | 1 + docs/api/web-view-tag.md | 1 + 6 files changed, 6 insertions(+) diff --git a/docs-translations/ko-KR/api/web-contents.md b/docs-translations/ko-KR/api/web-contents.md index 2c066a941105..3e19fcd66bc8 100644 --- a/docs-translations/ko-KR/api/web-contents.md +++ b/docs-translations/ko-KR/api/web-contents.md @@ -67,6 +67,7 @@ Returns: * `requestMethod` String * `referrer` String * `headers` Object +* `resourceType` String 요청한 리소스에 관련된 자세한 정보를 사용할 수 있을 때 발생하는 이벤트입니다. `status`는 리소스를 다운로드하기 위한 소켓 연결을 나타냅니다. diff --git a/docs-translations/ko-KR/api/web-view-tag.md b/docs-translations/ko-KR/api/web-view-tag.md index a40a22070f36..b2d22325840f 100644 --- a/docs-translations/ko-KR/api/web-view-tag.md +++ b/docs-translations/ko-KR/api/web-view-tag.md @@ -495,6 +495,7 @@ Returns: * `requestMethod` String * `referrer` String * `headers` Object +* `resourceType` String 요청한 리소스에 관해 자세한 내용을 알 수 있을 때 발생하는 이벤트입니다. `status`는 리소스를 다운로드할 소켓 커낵션을 나타냅니다. diff --git a/docs-translations/zh-CN/api/web-contents.md b/docs-translations/zh-CN/api/web-contents.md index 89d72f77aa74..54b9c56dcc6c 100644 --- a/docs-translations/zh-CN/api/web-contents.md +++ b/docs-translations/zh-CN/api/web-contents.md @@ -63,6 +63,7 @@ var webContents = win.webContents; * `requestMethod` String * `referrer` String * `headers` Object +* `resourceType` String 当有关请求资源的详细信息可用的时候发出事件. `status` 标识了 socket链接来下载资源. diff --git a/docs-translations/zh-CN/api/web-view-tag.md b/docs-translations/zh-CN/api/web-view-tag.md index e1e31f6f5a02..a1348c4d5882 100644 --- a/docs-translations/zh-CN/api/web-view-tag.md +++ b/docs-translations/zh-CN/api/web-view-tag.md @@ -457,6 +457,7 @@ Returns: * `requestMethod` String * `referrer` String * `headers` Object +* `resourceType` String 当获得返回详情的时候触发. diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index f27055822935..8fc7a959f69d 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -68,6 +68,7 @@ Returns: * `requestMethod` String * `referrer` String * `headers` Object +* `resourceType` String Emitted when details regarding a requested resource are available. `status` indicates the socket connection to download the resource. diff --git a/docs/api/web-view-tag.md b/docs/api/web-view-tag.md index 3bb71936be24..8df8d29347c6 100644 --- a/docs/api/web-view-tag.md +++ b/docs/api/web-view-tag.md @@ -510,6 +510,7 @@ Returns: * `requestMethod` String * `referrer` String * `headers` Object +* `resourceType` String Fired when details regarding a requested resource is available. `status` indicates socket connection to download the resource. From 1a842bf9d50b8a424bcd7a29183c2b795fbb656b Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Fri, 8 Apr 2016 13:15:40 -0700 Subject: [PATCH 4/4] Fix trailing whitespace caught by linter. --- atom/browser/net/atom_network_delegate.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atom/browser/net/atom_network_delegate.cc b/atom/browser/net/atom_network_delegate.cc index 9050fadbe501..1a83174abfd1 100644 --- a/atom/browser/net/atom_network_delegate.cc +++ b/atom/browser/net/atom_network_delegate.cc @@ -36,7 +36,7 @@ const char* ResourceTypeToString(content::ResourceType type) { return "other"; } } - + namespace { void RunSimpleListener(const AtomNetworkDelegate::SimpleListener& listener,