diff options
Diffstat (limited to 'chromium/content/browser/webui/web_ui_impl.cc')
-rw-r--r-- | chromium/content/browser/webui/web_ui_impl.cc | 69 |
1 files changed, 51 insertions, 18 deletions
diff --git a/chromium/content/browser/webui/web_ui_impl.cc b/chromium/content/browser/webui/web_ui_impl.cc index 41fc037852c..880029a3ee1 100644 --- a/chromium/content/browser/webui/web_ui_impl.cc +++ b/chromium/content/browser/webui/web_ui_impl.cc @@ -18,8 +18,10 @@ #include "content/browser/webui/web_ui_controller_factory_registry.h" #include "content/common/view_messages.h" #include "content/public/browser/content_browser_client.h" +#include "content/public/browser/navigation_handle.h" #include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_view_host.h" +#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_ui_controller.h" #include "content/public/browser/web_ui_message_handler.h" #include "content/public/common/bindings_policy.h" @@ -27,6 +29,26 @@ namespace content { +class WebUIImpl::MainFrameNavigationObserver : public WebContentsObserver { + public: + MainFrameNavigationObserver(WebUIImpl* web_ui, WebContents* contents) + : WebContentsObserver(contents), web_ui_(web_ui) {} + ~MainFrameNavigationObserver() override {} + + private: + void DidFinishNavigation(NavigationHandle* navigation_handle) override { + // Only disallow JavaScript on cross-document navigations in the main frame. + if (!navigation_handle->IsInMainFrame() || + !navigation_handle->HasCommitted() || navigation_handle->IsSamePage()) { + return; + } + + web_ui_->DisallowJavascriptOnAllHandlers(); + } + + WebUIImpl* web_ui_; +}; + const WebUI::TypeID WebUI::kNoWebUI = NULL; // static @@ -50,6 +72,7 @@ WebUIImpl::WebUIImpl(WebContents* contents, const std::string& frame_name) : link_transition_type_(ui::PAGE_TRANSITION_LINK), bindings_(BINDINGS_POLICY_WEB_UI), web_contents_(contents), + web_contents_observer_(new MainFrameNavigationObserver(this, contents)), frame_name_(frame_name) { DCHECK(contents); } @@ -95,9 +118,10 @@ void WebUIImpl::RenderViewReused(RenderViewHost* render_view_host, GURL site_url = render_view_host->GetSiteInstance()->GetSiteURL(); GetContentClient()->browser()->LogWebUIUrl(site_url); } +} - for (WebUIMessageHandler* handler : handlers_) - handler->RenderViewReused(); +void WebUIImpl::RenderFrameHostSwappingOut() { + DisallowJavascriptOnAllHandlers(); } WebContents* WebUIImpl::GetWebContents() const { @@ -154,23 +178,23 @@ bool WebUIImpl::CanCallJavascript() { target_frame->GetLastCommittedURL().spec() == url::kAboutBlankURL); } -void WebUIImpl::CallJavascriptFunction(const std::string& function_name) { +void WebUIImpl::CallJavascriptFunctionUnsafe(const std::string& function_name) { DCHECK(base::IsStringASCII(function_name)); base::string16 javascript = base::ASCIIToUTF16(function_name + "();"); ExecuteJavascript(javascript); } -void WebUIImpl::CallJavascriptFunction(const std::string& function_name, - const base::Value& arg) { +void WebUIImpl::CallJavascriptFunctionUnsafe(const std::string& function_name, + const base::Value& arg) { DCHECK(base::IsStringASCII(function_name)); std::vector<const base::Value*> args; args.push_back(&arg); ExecuteJavascript(GetJavascriptCall(function_name, args)); } -void WebUIImpl::CallJavascriptFunction( - const std::string& function_name, - const base::Value& arg1, const base::Value& arg2) { +void WebUIImpl::CallJavascriptFunctionUnsafe(const std::string& function_name, + const base::Value& arg1, + const base::Value& arg2) { DCHECK(base::IsStringASCII(function_name)); std::vector<const base::Value*> args; args.push_back(&arg1); @@ -178,9 +202,10 @@ void WebUIImpl::CallJavascriptFunction( ExecuteJavascript(GetJavascriptCall(function_name, args)); } -void WebUIImpl::CallJavascriptFunction( - const std::string& function_name, - const base::Value& arg1, const base::Value& arg2, const base::Value& arg3) { +void WebUIImpl::CallJavascriptFunctionUnsafe(const std::string& function_name, + const base::Value& arg1, + const base::Value& arg2, + const base::Value& arg3) { DCHECK(base::IsStringASCII(function_name)); std::vector<const base::Value*> args; args.push_back(&arg1); @@ -189,12 +214,11 @@ void WebUIImpl::CallJavascriptFunction( ExecuteJavascript(GetJavascriptCall(function_name, args)); } -void WebUIImpl::CallJavascriptFunction( - const std::string& function_name, - const base::Value& arg1, - const base::Value& arg2, - const base::Value& arg3, - const base::Value& arg4) { +void WebUIImpl::CallJavascriptFunctionUnsafe(const std::string& function_name, + const base::Value& arg1, + const base::Value& arg2, + const base::Value& arg3, + const base::Value& arg4) { DCHECK(base::IsStringASCII(function_name)); std::vector<const base::Value*> args; args.push_back(&arg1); @@ -204,7 +228,7 @@ void WebUIImpl::CallJavascriptFunction( ExecuteJavascript(GetJavascriptCall(function_name, args)); } -void WebUIImpl::CallJavascriptFunction( +void WebUIImpl::CallJavascriptFunctionUnsafe( const std::string& function_name, const std::vector<const base::Value*>& args) { DCHECK(base::IsStringASCII(function_name)); @@ -233,6 +257,10 @@ void WebUIImpl::ProcessWebUIMessage(const GURL& source_url, } } +ScopedVector<WebUIMessageHandler>* WebUIImpl::GetHandlersForTesting() { + return &handlers_; +} + // WebUIImpl, protected: ------------------------------------------------------- void WebUIImpl::AddMessageHandler(WebUIMessageHandler* handler) { @@ -275,4 +303,9 @@ void WebUIImpl::AddToSetIfFrameNameMatches( frame_set->insert(host); } +void WebUIImpl::DisallowJavascriptOnAllHandlers() { + for (WebUIMessageHandler* handler : handlers_) + handler->DisallowJavascript(); +} + } // namespace content |