initialize webui with message handlers
This commit is contained in:
parent
a7ed7068f6
commit
8a2b9c893e
4 changed files with 81 additions and 88 deletions
atom/browser
|
@ -8,6 +8,7 @@
|
|||
#include "atom/browser/web_contents_permission_helper.h"
|
||||
#include "atom/common/platform_util.h"
|
||||
#include "base/strings/utf_string_conversions.h"
|
||||
#include "base/strings/stringprintf.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "content/public/browser/stream_handle.h"
|
||||
#include "content/public/browser/stream_info.h"
|
||||
|
@ -68,11 +69,11 @@ void OnPdfStreamCreated(std::unique_ptr<content::StreamInfo> stream,
|
|||
if (!web_contents)
|
||||
return;
|
||||
|
||||
LOG(WARNING) << stream->handle->GetURL();
|
||||
LOG(WARNING) << stream->original_url;
|
||||
|
||||
content::NavigationController::LoadURLParams params(
|
||||
GURL("chrome://pdf-viewer/index.html"));
|
||||
auto stream_url = stream->handle->GetURL();
|
||||
auto original_url = stream->original_url;
|
||||
content::NavigationController::LoadURLParams params(GURL(base::StringPrintf(
|
||||
"chrome://pdf-viewer/index.html?streamURL=%s&originalURL=%s",
|
||||
stream_url.spec().c_str(), original_url.spec().c_str())));
|
||||
web_contents->GetController().LoadURLWithParams(params);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include "atom/browser/atom_web_ui_controller_factory.h"
|
||||
|
||||
#include "base/strings/string_util.h"
|
||||
#include "base/strings/stringprintf.h"
|
||||
#include "base/strings/string_split.h"
|
||||
#include "content/public/browser/render_view_host.h"
|
||||
#include "content/public/browser/url_data_source.h"
|
||||
#include "content/public/browser/web_contents.h"
|
||||
|
@ -103,14 +103,34 @@ class BundledDataSource : public content::URLDataSource {
|
|||
|
||||
class PdfViewerUI : public content::WebUIController {
|
||||
public:
|
||||
PdfViewerUI(content::BrowserContext* browser_context, content::WebUI* web_ui)
|
||||
: content::WebUIController(web_ui) {
|
||||
PdfViewerUI(content::BrowserContext* browser_context,
|
||||
content::WebUI* web_ui,
|
||||
const std::string& stream_url,
|
||||
const std::string& original_url)
|
||||
: content::WebUIController(web_ui),
|
||||
stream_url_(stream_url),
|
||||
original_url_(original_url) {
|
||||
web_ui->RegisterMessageCallback(
|
||||
"initialize",
|
||||
base::Bind(&PdfViewerUI::OnInitialize, base::Unretained(this)));
|
||||
content::URLDataSource::Add(browser_context, new BundledDataSource);
|
||||
}
|
||||
|
||||
void RenderViewCreated(content::RenderViewHost* rvh) override {
|
||||
rvh->AllowBindings(content::BINDINGS_POLICY_WEB_UI);
|
||||
}
|
||||
|
||||
void OnInitialize(const base::ListValue* args) {
|
||||
web_ui()->CallJavascriptFunctionUnsafe("main",
|
||||
base::StringValue(original_url_),
|
||||
base::StringValue(original_url_));
|
||||
}
|
||||
|
||||
private:
|
||||
std::string stream_url_;
|
||||
std::string original_url_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(PdfViewerUI);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -149,8 +169,18 @@ content::WebUIController*
|
|||
AtomWebUIControllerFactory::CreateWebUIControllerForURL(content::WebUI* web_ui,
|
||||
const GURL& url) const {
|
||||
if (url.host() == kChromeUIPdfViewerHost) {
|
||||
base::StringPairs toplevel_params;
|
||||
base::SplitStringIntoKeyValuePairs(url.query(), '=', '&', &toplevel_params);
|
||||
std::string stream_url, original_url;
|
||||
for (const auto& param : toplevel_params) {
|
||||
if (param.first == "streamURL") {
|
||||
stream_url = param.second;
|
||||
} else if (param.first == "originalURL") {
|
||||
original_url = param.second;
|
||||
}
|
||||
}
|
||||
auto browser_context = web_ui->GetWebContents()->GetBrowserContext();
|
||||
return new PdfViewerUI(browser_context, web_ui);
|
||||
return new PdfViewerUI(browser_context, web_ui, stream_url, original_url);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
|
|
@ -71,13 +71,13 @@ class BrowserApi {
|
|||
* @param {boolean} manageZoom Whether to manage zoom.
|
||||
*/
|
||||
static create(streamInfo, manageZoom) {
|
||||
/*return Promise.all([
|
||||
lookupDefaultZoom(streamInfo),
|
||||
lookupInitialZoom(streamInfo)
|
||||
]).then(function(zoomFactors) {*/
|
||||
return Promise.all([
|
||||
lookupDefaultZoom(streamInfo),
|
||||
lookupInitialZoom(streamInfo)
|
||||
]).then(function(zoomFactors) {
|
||||
return new BrowserApi(
|
||||
streamInfo, 1.0, 1.0, manageZoom);
|
||||
//});
|
||||
streamInfo, zoomFactors[0], zoomFactors[1], manageZoom);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -143,34 +143,12 @@ class BrowserApi {
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a BrowserApi for an extension running as a mime handler.
|
||||
* @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
|
||||
* using the mimeHandlerPrivate API.
|
||||
*/
|
||||
function createBrowserApiForMimeHandlerView() {
|
||||
return new Promise(function(resolve, reject) {
|
||||
chrome.mimeHandlerPrivate.getStreamInfo(resolve);
|
||||
}).then(function(streamInfo) {
|
||||
let manageZoom = !streamInfo.embedded && streamInfo.tabId != -1;
|
||||
return new Promise(function(resolve, reject) {
|
||||
if (!manageZoom) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
chrome.tabs.setZoomSettings(
|
||||
streamInfo.tabId, {mode: 'manual', scope: 'per-tab'}, resolve);
|
||||
}).then(function() { return BrowserApi.create(streamInfo, manageZoom); });
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a BrowserApi instance for an extension not running as a mime handler.
|
||||
* @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
|
||||
* from the URL.
|
||||
*/
|
||||
function createBrowserApi(streamURL, originalURL) {
|
||||
//let url = window.location.search.substring(1);
|
||||
let streamInfo = {
|
||||
streamUrl: streamURL,
|
||||
originalUrl: originalURL,
|
||||
|
@ -189,16 +167,3 @@ function createBrowserApi(streamURL, originalURL) {
|
|||
//});
|
||||
}).then(function() { return BrowserApi.create(streamInfo, false); });
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a promise that will resolve to a BrowserApi instance.
|
||||
* @return {Promise<BrowserApi>} A promise to a BrowserApi instance for the
|
||||
* current environment.
|
||||
|
||||
function createBrowserApi(streamURL, originalURL) {
|
||||
//if (window.location.search)
|
||||
return createBrowserApiForStandaloneExtension();
|
||||
|
||||
//return createBrowserApiForMimeHandlerView();
|
||||
}
|
||||
*/
|
||||
|
|
|
@ -10,46 +10,43 @@
|
|||
*/
|
||||
var viewer;
|
||||
|
||||
/**
|
||||
* Stores any pending messages received which should be passed to the
|
||||
* PDFViewer when it is created.
|
||||
* @type Array
|
||||
*/
|
||||
var pendingMessages = [];
|
||||
|
||||
//(function() {
|
||||
/**
|
||||
* Stores any pending messages received which should be passed to the
|
||||
* PDFViewer when it is created.
|
||||
* @type Array
|
||||
*/
|
||||
var pendingMessages = [];
|
||||
/**
|
||||
* Handles events that are received prior to the PDFViewer being created.
|
||||
* @param {Object} message A message event received.
|
||||
*/
|
||||
function handleScriptingMessage(message) {
|
||||
pendingMessages.push(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles events that are received prior to the PDFViewer being created.
|
||||
* @param {Object} message A message event received.
|
||||
*/
|
||||
function handleScriptingMessage(message) {
|
||||
pendingMessages.push(message);
|
||||
}
|
||||
/**
|
||||
* Initialize the global PDFViewer and pass any outstanding messages to it.
|
||||
* @param {Object} browserApi An object providing an API to the browser.
|
||||
*/
|
||||
function initViewer(browserApi) {
|
||||
// PDFViewer will handle any messages after it is created.
|
||||
window.removeEventListener('message', handleScriptingMessage, false);
|
||||
viewer = new PDFViewer(browserApi);
|
||||
while (pendingMessages.length > 0)
|
||||
viewer.handleScriptingMessage(pendingMessages.shift());
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the global PDFViewer and pass any outstanding messages to it.
|
||||
* @param {Object} browserApi An object providing an API to the browser.
|
||||
*/
|
||||
function initViewer(browserApi) {
|
||||
// PDFViewer will handle any messages after it is created.
|
||||
window.removeEventListener('message', handleScriptingMessage, false);
|
||||
viewer = new PDFViewer(browserApi);
|
||||
while (pendingMessages.length > 0)
|
||||
viewer.handleScriptingMessage(pendingMessages.shift());
|
||||
}
|
||||
/**
|
||||
* Entrypoint for starting the PDF viewer. This function obtains the browser
|
||||
* API for the PDF and constructs a PDFViewer object with it.
|
||||
*/
|
||||
function main(streamURL, originalURL) {
|
||||
// Set up an event listener to catch scripting messages which are sent prior
|
||||
// to the PDFViewer being created.
|
||||
window.addEventListener('message', handleScriptingMessage, false);
|
||||
|
||||
/**
|
||||
* Entrypoint for starting the PDF viewer. This function obtains the browser
|
||||
* API for the PDF and constructs a PDFViewer object with it.
|
||||
*/
|
||||
function main(streamURL, originalURL) {
|
||||
// Set up an event listener to catch scripting messages which are sent prior
|
||||
// to the PDFViewer being created.
|
||||
window.addEventListener('message', handleScriptingMessage, false);
|
||||
createBrowserApi(streamURL, originalURL).then(initViewer);
|
||||
};
|
||||
|
||||
createBrowserApi(streamURL, originalURL).then(initViewer);
|
||||
};
|
||||
|
||||
//main();
|
||||
//})();
|
||||
chrome.send('initialize');
|
||||
|
|
Loading…
Add table
Reference in a new issue