Add support for pdf in sub frames (https://github.com/electron/electron/issues/9192#issuecomment-335543866)
This commit is contained in:
parent
4ffb6c5f75
commit
1ad95eca4a
1 changed files with 50 additions and 41 deletions
|
@ -15,6 +15,7 @@
|
||||||
#include "content/public/browser/browser_thread.h"
|
#include "content/public/browser/browser_thread.h"
|
||||||
#include "content/public/browser/download_manager.h"
|
#include "content/public/browser/download_manager.h"
|
||||||
#include "content/public/browser/stream_info.h"
|
#include "content/public/browser/stream_info.h"
|
||||||
|
#include "content/public/browser/render_frame_host.h"
|
||||||
#include "net/base/escape.h"
|
#include "net/base/escape.h"
|
||||||
#include "net/ssl/client_cert_store.h"
|
#include "net/ssl/client_cert_store.h"
|
||||||
#include "net/url_request/url_request.h"
|
#include "net/url_request/url_request.h"
|
||||||
|
@ -65,34 +66,33 @@ void HandleExternalProtocolInUI(
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnPdfResourceIntercepted(
|
void OnPdfResourceIntercepted(
|
||||||
const GURL& original_url,
|
const GURL& original_url,
|
||||||
const content::ResourceRequestInfo::WebContentsGetter&
|
int frame_tree_node_id,
|
||||||
web_contents_getter) {
|
const content::ResourceRequestInfo::WebContentsGetter&
|
||||||
content::WebContents* web_contents = web_contents_getter.Run();
|
web_contents_getter) {
|
||||||
if (!web_contents)
|
content::WebContents* web_contents = web_contents_getter.Run();
|
||||||
return;
|
if (!web_contents)
|
||||||
|
return;
|
||||||
|
|
||||||
if (!WebContentsPreferences::IsPluginsEnabled(web_contents)) {
|
if (!WebContentsPreferences::IsPluginsEnabled(web_contents)) {
|
||||||
auto browser_context = web_contents->GetBrowserContext();
|
auto browser_context = web_contents->GetBrowserContext();
|
||||||
auto download_manager =
|
auto download_manager =
|
||||||
content::BrowserContext::GetDownloadManager(browser_context);
|
content::BrowserContext::GetDownloadManager(browser_context);
|
||||||
|
|
||||||
download_manager->DownloadUrl(
|
download_manager->DownloadUrl(
|
||||||
content::DownloadUrlParameters::CreateForWebContentsMainFrame(
|
content::DownloadUrlParameters::CreateForWebContentsMainFrame(
|
||||||
web_contents, original_url));
|
web_contents, original_url));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The URL passes the original pdf resource url, that will be requested
|
// The URL passes the original pdf resource url, that will be requested
|
||||||
// by the webui page.
|
// by the webui page.
|
||||||
// chrome://pdf-viewer/index.html?src=https://somepage/123.pdf
|
// chrome://pdf-viewer/index.html?src=https://somepage/123.pdf
|
||||||
content::NavigationController::LoadURLParams params(
|
content::NavigationController::LoadURLParams params(GURL(base::StringPrintf(
|
||||||
GURL(base::StringPrintf(
|
"%sindex.html?%s=%s", kPdfViewerUIOrigin, kPdfPluginSrc,
|
||||||
"%sindex.html?%s=%s",
|
net::EscapeUrlEncodedData(original_url.spec(), false).c_str())));
|
||||||
kPdfViewerUIOrigin,
|
params.frame_tree_node_id = frame_tree_node_id;
|
||||||
kPdfPluginSrc,
|
web_contents->GetController().LoadURLWithParams(params);
|
||||||
net::EscapeUrlEncodedData(original_url.spec(), false).c_str())));
|
|
||||||
web_contents->GetController().LoadURLWithParams(params);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -134,22 +134,31 @@ AtomResourceDispatcherHostDelegate::CreateClientCertStore(
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AtomResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream(
|
bool AtomResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream(
|
||||||
net::URLRequest* request,
|
net::URLRequest* request,
|
||||||
const base::FilePath& plugin_path,
|
const base::FilePath& plugin_path,
|
||||||
const std::string& mime_type,
|
const std::string& mime_type,
|
||||||
GURL* origin,
|
GURL* origin,
|
||||||
std::string* payload) {
|
std::string* payload) {
|
||||||
const content::ResourceRequestInfo* info =
|
const content::ResourceRequestInfo* info =
|
||||||
content::ResourceRequestInfo::ForRequest(request);
|
content::ResourceRequestInfo::ForRequest(request);
|
||||||
if (mime_type == "application/pdf" && info->IsMainFrame()) {
|
|
||||||
*origin = GURL(kPdfViewerUIOrigin);
|
int render_process_host_id;
|
||||||
content::BrowserThread::PostTask(
|
int render_frame_id;
|
||||||
BrowserThread::UI, FROM_HERE,
|
info->GetAssociatedRenderFrame(&render_process_host_id, &render_frame_id);
|
||||||
base::Bind(&OnPdfResourceIntercepted, request->url(),
|
content::RenderFrameHost* rfh =
|
||||||
info->GetWebContentsGetterForRequest()));
|
content::RenderFrameHost::FromID(render_process_host_id, render_frame_id);
|
||||||
return true;
|
|
||||||
}
|
int frame_tree_node_id = rfh->GetFrameTreeNodeId();
|
||||||
return false;
|
|
||||||
|
if (mime_type == "application/pdf") {
|
||||||
|
*origin = GURL(kPdfViewerUIOrigin);
|
||||||
|
content::BrowserThread::PostTask(
|
||||||
|
BrowserThread::UI, FROM_HERE,
|
||||||
|
base::Bind(&OnPdfResourceIntercepted, request->url(),
|
||||||
|
frame_tree_node_id, info->GetWebContentsGetterForRequest()));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atom
|
||||||
|
|
Loading…
Reference in a new issue