initialize webui with message handlers
This commit is contained in:
parent
a7ed7068f6
commit
8a2b9c893e
4 changed files with 81 additions and 88 deletions
|
@ -8,6 +8,7 @@
|
||||||
#include "atom/browser/web_contents_permission_helper.h"
|
#include "atom/browser/web_contents_permission_helper.h"
|
||||||
#include "atom/common/platform_util.h"
|
#include "atom/common/platform_util.h"
|
||||||
#include "base/strings/utf_string_conversions.h"
|
#include "base/strings/utf_string_conversions.h"
|
||||||
|
#include "base/strings/stringprintf.h"
|
||||||
#include "content/public/browser/browser_thread.h"
|
#include "content/public/browser/browser_thread.h"
|
||||||
#include "content/public/browser/stream_handle.h"
|
#include "content/public/browser/stream_handle.h"
|
||||||
#include "content/public/browser/stream_info.h"
|
#include "content/public/browser/stream_info.h"
|
||||||
|
@ -68,11 +69,11 @@ void OnPdfStreamCreated(std::unique_ptr<content::StreamInfo> stream,
|
||||||
if (!web_contents)
|
if (!web_contents)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
LOG(WARNING) << stream->handle->GetURL();
|
auto stream_url = stream->handle->GetURL();
|
||||||
LOG(WARNING) << stream->original_url;
|
auto original_url = stream->original_url;
|
||||||
|
content::NavigationController::LoadURLParams params(GURL(base::StringPrintf(
|
||||||
content::NavigationController::LoadURLParams params(
|
"chrome://pdf-viewer/index.html?streamURL=%s&originalURL=%s",
|
||||||
GURL("chrome://pdf-viewer/index.html"));
|
stream_url.spec().c_str(), original_url.spec().c_str())));
|
||||||
web_contents->GetController().LoadURLWithParams(params);
|
web_contents->GetController().LoadURLWithParams(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
#include "atom/browser/atom_web_ui_controller_factory.h"
|
#include "atom/browser/atom_web_ui_controller_factory.h"
|
||||||
|
|
||||||
#include "base/strings/string_util.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/render_view_host.h"
|
||||||
#include "content/public/browser/url_data_source.h"
|
#include "content/public/browser/url_data_source.h"
|
||||||
#include "content/public/browser/web_contents.h"
|
#include "content/public/browser/web_contents.h"
|
||||||
|
@ -103,14 +103,34 @@ class BundledDataSource : public content::URLDataSource {
|
||||||
|
|
||||||
class PdfViewerUI : public content::WebUIController {
|
class PdfViewerUI : public content::WebUIController {
|
||||||
public:
|
public:
|
||||||
PdfViewerUI(content::BrowserContext* browser_context, content::WebUI* web_ui)
|
PdfViewerUI(content::BrowserContext* browser_context,
|
||||||
: content::WebUIController(web_ui) {
|
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);
|
content::URLDataSource::Add(browser_context, new BundledDataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RenderViewCreated(content::RenderViewHost* rvh) override {
|
void RenderViewCreated(content::RenderViewHost* rvh) override {
|
||||||
rvh->AllowBindings(content::BINDINGS_POLICY_WEB_UI);
|
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,
|
AtomWebUIControllerFactory::CreateWebUIControllerForURL(content::WebUI* web_ui,
|
||||||
const GURL& url) const {
|
const GURL& url) const {
|
||||||
if (url.host() == kChromeUIPdfViewerHost) {
|
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();
|
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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,13 +71,13 @@ class BrowserApi {
|
||||||
* @param {boolean} manageZoom Whether to manage zoom.
|
* @param {boolean} manageZoom Whether to manage zoom.
|
||||||
*/
|
*/
|
||||||
static create(streamInfo, manageZoom) {
|
static create(streamInfo, manageZoom) {
|
||||||
/*return Promise.all([
|
return Promise.all([
|
||||||
lookupDefaultZoom(streamInfo),
|
lookupDefaultZoom(streamInfo),
|
||||||
lookupInitialZoom(streamInfo)
|
lookupInitialZoom(streamInfo)
|
||||||
]).then(function(zoomFactors) {*/
|
]).then(function(zoomFactors) {
|
||||||
return new BrowserApi(
|
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.
|
* Creates a BrowserApi instance for an extension not running as a mime handler.
|
||||||
* @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
|
* @return {Promise<BrowserApi>} A promise to a BrowserApi instance constructed
|
||||||
* from the URL.
|
* from the URL.
|
||||||
*/
|
*/
|
||||||
function createBrowserApi(streamURL, originalURL) {
|
function createBrowserApi(streamURL, originalURL) {
|
||||||
//let url = window.location.search.substring(1);
|
|
||||||
let streamInfo = {
|
let streamInfo = {
|
||||||
streamUrl: streamURL,
|
streamUrl: streamURL,
|
||||||
originalUrl: originalURL,
|
originalUrl: originalURL,
|
||||||
|
@ -189,16 +167,3 @@ function createBrowserApi(streamURL, originalURL) {
|
||||||
//});
|
//});
|
||||||
}).then(function() { return BrowserApi.create(streamInfo, false); });
|
}).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,8 +10,6 @@
|
||||||
*/
|
*/
|
||||||
var viewer;
|
var viewer;
|
||||||
|
|
||||||
|
|
||||||
//(function() {
|
|
||||||
/**
|
/**
|
||||||
* Stores any pending messages received which should be passed to the
|
* Stores any pending messages received which should be passed to the
|
||||||
* PDFViewer when it is created.
|
* PDFViewer when it is created.
|
||||||
|
@ -51,5 +49,4 @@ var viewer;
|
||||||
createBrowserApi(streamURL, originalURL).then(initViewer);
|
createBrowserApi(streamURL, originalURL).then(initViewer);
|
||||||
};
|
};
|
||||||
|
|
||||||
//main();
|
chrome.send('initialize');
|
||||||
//})();
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue