Commit 5bc9087d authored by noyau's avatar noyau Committed by Commit bot

Pass more information by argument to the dom_distiller script.

BUG=None

Review URL: https://codereview.chromium.org/1004223002

Cr-Commit-Position: refs/heads/master@{#320899}
parent 5c9e7095
...@@ -71,6 +71,14 @@ DistillerPageWebContents::~DistillerPageWebContents() { ...@@ -71,6 +71,14 @@ DistillerPageWebContents::~DistillerPageWebContents() {
web_contents_->SetDelegate(NULL); web_contents_->SetDelegate(NULL);
} }
bool DistillerPageWebContents::StringifyOutput() {
return false;
}
bool DistillerPageWebContents::CreateNewContext() {
return true;
}
void DistillerPageWebContents::DistillPageImpl(const GURL& url, void DistillerPageWebContents::DistillPageImpl(const GURL& url,
const std::string& script) { const std::string& script) {
DCHECK(browser_context_); DCHECK(browser_context_);
......
...@@ -69,6 +69,8 @@ class DistillerPageWebContents : public DistillerPage, ...@@ -69,6 +69,8 @@ class DistillerPageWebContents : public DistillerPage,
const base::string16& error_description) override; const base::string16& error_description) override;
protected: protected:
bool StringifyOutput() override;
bool CreateNewContext() override;
void DistillPageImpl(const GURL& url, const std::string& script) override; void DistillPageImpl(const GURL& url, const std::string& script) override;
private: private:
......
...@@ -23,9 +23,13 @@ namespace dom_distiller { ...@@ -23,9 +23,13 @@ namespace dom_distiller {
namespace { namespace {
const char* kOptionsPlaceholder = "$$OPTIONS"; const char* kOptionsPlaceholder = "$$OPTIONS";
const char* kStringifyPlaceholder = "$$STRINGIFY";
const char* kNewContextPlaceholder = "$$NEW_CONTEXT";
std::string GetDistillerScriptWithOptions( std::string GetDistillerScriptWithOptions(
const dom_distiller::proto::DomDistillerOptions& options) { const dom_distiller::proto::DomDistillerOptions& options,
bool stringify_output,
bool create_new_context) {
std::string script = ResourceBundle::GetSharedInstance() std::string script = ResourceBundle::GetSharedInstance()
.GetRawDataResource(IDR_DISTILLER_JS) .GetRawDataResource(IDR_DISTILLER_JS)
.as_string(); .as_string();
...@@ -45,6 +49,25 @@ std::string GetDistillerScriptWithOptions( ...@@ -45,6 +49,25 @@ std::string GetDistillerScriptWithOptions(
script.find(kOptionsPlaceholder, options_offset + 1)); script.find(kOptionsPlaceholder, options_offset + 1));
script = script =
script.replace(options_offset, strlen(kOptionsPlaceholder), options_json); script.replace(options_offset, strlen(kOptionsPlaceholder), options_json);
std::string stringify = stringify_output ? "true" : "false";
size_t stringify_offset = script.find(kStringifyPlaceholder);
DCHECK_NE(std::string::npos, stringify_offset);
DCHECK_EQ(std::string::npos,
script.find(kStringifyPlaceholder, stringify_offset + 1));
script = script.replace(stringify_offset,
strlen(kStringifyPlaceholder),
stringify);
std::string new_context = create_new_context ? "true" : "false";
size_t new_context_offset = script.find(kNewContextPlaceholder);
DCHECK_NE(std::string::npos, new_context_offset);
DCHECK_EQ(std::string::npos,
script.find(kNewContextPlaceholder, new_context_offset + 1));
script = script.replace(new_context_offset,
strlen(kNewContextPlaceholder),
new_context);
return script; return script;
} }
...@@ -65,7 +88,9 @@ void DistillerPage::DistillPage( ...@@ -65,7 +88,9 @@ void DistillerPage::DistillPage(
// the callback to OnDistillationDone happens. // the callback to OnDistillationDone happens.
ready_ = false; ready_ = false;
distiller_page_callback_ = callback; distiller_page_callback_ = callback;
DistillPageImpl(gurl, GetDistillerScriptWithOptions(options)); DistillPageImpl(gurl, GetDistillerScriptWithOptions(options,
StringifyOutput(),
CreateNewContext()));
} }
void DistillerPage::OnDistillationDone(const GURL& page_url, void DistillerPage::OnDistillationDone(const GURL& page_url,
......
...@@ -55,6 +55,14 @@ class DistillerPage { ...@@ -55,6 +55,14 @@ class DistillerPage {
// should be the same regardless of the DistillerPage implementation. // should be the same regardless of the DistillerPage implementation.
virtual void DistillPageImpl(const GURL& url, const std::string& script) = 0; virtual void DistillPageImpl(const GURL& url, const std::string& script) = 0;
// The value returned between the JavaScript and the DistillerPage can be
// either a dictionary with all the content, or a stringified version.
virtual bool StringifyOutput() = 0;
// If true, forces the creation of a new window context to evaluate the
// JavaScript.
virtual bool CreateNewContext() = 0;
private: private:
bool ready_; bool ready_;
DistillerPageCallback distiller_page_callback_; DistillerPageCallback distiller_page_callback_;
......
...@@ -30,6 +30,8 @@ class MockDistillerPage : public DistillerPage { ...@@ -30,6 +30,8 @@ class MockDistillerPage : public DistillerPage {
public: public:
MockDistillerPage(); MockDistillerPage();
virtual ~MockDistillerPage(); virtual ~MockDistillerPage();
bool StringifyOutput() override { return false; };
bool CreateNewContext() override { return false; };
MOCK_METHOD2(DistillPageImpl, MOCK_METHOD2(DistillPageImpl,
void(const GURL& gurl, const std::string& script)); void(const GURL& gurl, const std::string& script));
}; };
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
// Applies DomDistillerJs to the content of the page and returns a // Applies DomDistillerJs to the content of the page and returns a
// DomDistillerResults (as a javascript object/dict). // DomDistillerResults (as a javascript object/dict).
(function() { (function(options, stringify_output, use_new_context) {
try { try {
// The generated domdistiller.js accesses the window object only explicitly // The generated domdistiller.js accesses the window object only explicitly
// via the window name. So, we create a new object with the normal window // via the window name. So, we create a new object with the normal window
...@@ -14,16 +14,7 @@ ...@@ -14,16 +14,7 @@
// This include will be processed at build time by grit. // This include will be processed at build time by grit.
<include src="../../../../third_party/dom_distiller_js/package/js/domdistiller.js"/> <include src="../../../../third_party/dom_distiller_js/package/js/domdistiller.js"/>
} }
<if expr="is_ios"> var context = use_new_context ? Object.create(window) : window
// UIWebView's JavaScript engine has a bug that causes crashes when
// creating a separate window object, so allow the script to run directly
// in the window until a better solution is created.
// TODO(kkhorimoto): investigate whether this is necessary for WKWebView.
var context = window;
</if>
<if expr="not is_ios">
var context = Object.create(window);
</if>
context.setTimeout = function() {}; context.setTimeout = function() {};
context.clearTimeout = function() {}; context.clearTimeout = function() {};
initialize(context); initialize(context);
...@@ -31,17 +22,17 @@ ...@@ -31,17 +22,17 @@
// The OPTIONS placeholder will be replaced with the DomDistillerOptions at // The OPTIONS placeholder will be replaced with the DomDistillerOptions at
// runtime. // runtime.
var distiller = context.org.chromium.distiller.DomDistiller; var distiller = context.org.chromium.distiller.DomDistiller;
var res = distiller.applyWithOptions($$OPTIONS); var res = distiller.applyWithOptions(options);
<if expr="is_ios">
// UIWebView requires javascript to return a single string value. if (stringify_output) {
return JSON.stringify(res); return JSON.stringify(res);
</if> }
<if expr="not is_ios">
return res; return res;
</if>
} catch (e) { } catch (e) {
window.console.error("Error during distillation: " + e); window.console.error("Error during distillation: " + e);
if (e.stack != undefined) window.console.error(e.stack); if (e.stack != undefined) window.console.error(e.stack);
} }
return undefined; return undefined;
})() })(options = $$OPTIONS,
stringify_output = $$STRINGIFY,
use_new_context = $$NEW_CONTEXT)
...@@ -33,12 +33,14 @@ class DistillerPageIOS : public DistillerPage { ...@@ -33,12 +33,14 @@ class DistillerPageIOS : public DistillerPage {
explicit DistillerPageIOS(web::BrowserState* browser_state); explicit DistillerPageIOS(web::BrowserState* browser_state);
~DistillerPageIOS() override; ~DistillerPageIOS() override;
protected:
bool StringifyOutput() override;
bool CreateNewContext() override;
void DistillPageImpl(const GURL& url, const std::string& script) override;
private: private:
friend class DistillerWebStateObserver; friend class DistillerWebStateObserver;
// DistillerPage implementation:
void DistillPageImpl(const GURL& url, const std::string& script) override;
// Called by |web_state_observer_| once the page has finished loading. // Called by |web_state_observer_| once the page has finished loading.
void OnLoadURLDone(web::PageLoadCompletionStatus load_completion_status); void OnLoadURLDone(web::PageLoadCompletionStatus load_completion_status);
......
...@@ -51,6 +51,19 @@ DistillerPageIOS::DistillerPageIOS(web::BrowserState* browser_state) ...@@ -51,6 +51,19 @@ DistillerPageIOS::DistillerPageIOS(web::BrowserState* browser_state)
DistillerPageIOS::~DistillerPageIOS() { DistillerPageIOS::~DistillerPageIOS() {
} }
bool DistillerPageIOS::StringifyOutput() {
// UIWebView requires JavaScript to return a single string value.
return true;
}
bool DistillerPageIOS::CreateNewContext() {
// UIWebView's JavaScript engine has a bug that causes crashes when
// creating a separate window object, so allow the script to run directly
// in the window until a better solution is created.
// TODO(kkhorimoto): investigate whether this is necessary for WKWebView.
return false;
}
void DistillerPageIOS::DistillPageImpl(const GURL& url, void DistillerPageIOS::DistillPageImpl(const GURL& url,
const std::string& script) { const std::string& script) {
if (!url.is_valid() || !script.length()) if (!url.is_valid() || !script.length())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment