Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/webui/web_ui_impl.h" | 5 #include "content/browser/webui/web_ui_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/debug/dump_without_crashing.h" | 9 #include "base/debug/dump_without_crashing.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "content/browser/child_process_security_policy_impl.h" | 13 #include "content/browser/child_process_security_policy_impl.h" |
| 14 #include "content/browser/renderer_host/dip_util.h" | 14 #include "content/browser/renderer_host/dip_util.h" |
| 15 #include "content/browser/renderer_host/render_process_host_impl.h" | 15 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 16 #include "content/browser/web_contents/web_contents_impl.h" | 16 #include "content/browser/web_contents/web_contents_impl.h" |
| 17 #include "content/browser/web_contents/web_contents_view.h" | 17 #include "content/browser/web_contents/web_contents_view.h" |
| 18 #include "content/browser/webui/web_ui_controller_factory_registry.h" | 18 #include "content/browser/webui/web_ui_controller_factory_registry.h" |
| 19 #include "content/common/view_messages.h" | 19 #include "content/common/view_messages.h" |
| 20 #include "content/public/browser/content_browser_client.h" | 20 #include "content/public/browser/content_browser_client.h" |
| 21 #include "content/public/browser/navigation_handle.h" | |
| 21 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
| 22 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
| 23 #include "content/public/browser/web_ui_controller.h" | 24 #include "content/public/browser/web_ui_controller.h" |
|
Charlie Reis
2016/06/24 23:15:00
IWYU nit: web_contents_observer.h
tommycli
2016/06/24 23:55:36
Done.
| |
| 24 #include "content/public/browser/web_ui_message_handler.h" | 25 #include "content/public/browser/web_ui_message_handler.h" |
| 25 #include "content/public/common/bindings_policy.h" | 26 #include "content/public/common/bindings_policy.h" |
| 26 #include "content/public/common/content_client.h" | 27 #include "content/public/common/content_client.h" |
| 27 | 28 |
| 28 namespace content { | 29 namespace content { |
| 29 | 30 |
| 31 class WebUIImpl::MainFrameNavigationObserver : public WebContentsObserver { | |
| 32 public: | |
| 33 MainFrameNavigationObserver(WebUIImpl* parent, WebContents* contents) | |
| 34 : WebContentsObserver(contents), parent_(parent) {} | |
| 35 ~MainFrameNavigationObserver() override {} | |
| 36 | |
| 37 private: | |
| 38 void DidFinishNavigation(NavigationHandle* navigation_handle) override { | |
| 39 if (!navigation_handle->IsInMainFrame() || | |
|
Charlie Reis
2016/06/24 23:15:00
// Only disallow Javascript on cross-document navi
tommycli
2016/06/24 23:55:36
Done.
| |
| 40 !navigation_handle->HasCommitted() || navigation_handle->IsSamePage()) { | |
| 41 return; | |
| 42 } | |
| 43 | |
| 44 parent_->DisallowJavascriptOnAllHandlers(); | |
| 45 } | |
| 46 | |
| 47 WebUIImpl* parent_; | |
|
Charlie Reis
2016/06/24 23:15:00
web_ui_, perhaps? (Parent is a confusing term, si
tommycli
2016/06/24 23:55:36
Done.
| |
| 48 }; | |
| 49 | |
| 30 const WebUI::TypeID WebUI::kNoWebUI = NULL; | 50 const WebUI::TypeID WebUI::kNoWebUI = NULL; |
| 31 | 51 |
| 32 // static | 52 // static |
| 33 base::string16 WebUI::GetJavascriptCall( | 53 base::string16 WebUI::GetJavascriptCall( |
| 34 const std::string& function_name, | 54 const std::string& function_name, |
| 35 const std::vector<const base::Value*>& arg_list) { | 55 const std::vector<const base::Value*>& arg_list) { |
| 36 base::string16 parameters; | 56 base::string16 parameters; |
| 37 std::string json; | 57 std::string json; |
| 38 for (size_t i = 0; i < arg_list.size(); ++i) { | 58 for (size_t i = 0; i < arg_list.size(); ++i) { |
| 39 if (i > 0) | 59 if (i > 0) |
| 40 parameters += base::char16(','); | 60 parameters += base::char16(','); |
| 41 | 61 |
| 42 base::JSONWriter::Write(*arg_list[i], &json); | 62 base::JSONWriter::Write(*arg_list[i], &json); |
| 43 parameters += base::UTF8ToUTF16(json); | 63 parameters += base::UTF8ToUTF16(json); |
| 44 } | 64 } |
| 45 return base::ASCIIToUTF16(function_name) + | 65 return base::ASCIIToUTF16(function_name) + |
| 46 base::char16('(') + parameters + base::char16(')') + base::char16(';'); | 66 base::char16('(') + parameters + base::char16(')') + base::char16(';'); |
| 47 } | 67 } |
| 48 | 68 |
| 49 WebUIImpl::WebUIImpl(WebContents* contents, const std::string& frame_name) | 69 WebUIImpl::WebUIImpl(WebContents* contents, const std::string& frame_name) |
| 50 : link_transition_type_(ui::PAGE_TRANSITION_LINK), | 70 : link_transition_type_(ui::PAGE_TRANSITION_LINK), |
| 51 bindings_(BINDINGS_POLICY_WEB_UI), | 71 bindings_(BINDINGS_POLICY_WEB_UI), |
| 52 web_contents_(contents), | 72 web_contents_(contents), |
| 73 web_contents_observer_(new MainFrameNavigationObserver(this, contents)), | |
| 53 frame_name_(frame_name) { | 74 frame_name_(frame_name) { |
| 54 DCHECK(contents); | 75 DCHECK(contents); |
| 55 } | 76 } |
| 56 | 77 |
| 57 WebUIImpl::~WebUIImpl() { | 78 WebUIImpl::~WebUIImpl() { |
| 58 // Delete the controller first, since it may also be keeping a pointer to some | 79 // Delete the controller first, since it may also be keeping a pointer to some |
| 59 // of the handlers and can call them at destruction. | 80 // of the handlers and can call them at destruction. |
| 60 controller_.reset(); | 81 controller_.reset(); |
| 61 } | 82 } |
| 62 | 83 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 86 } | 107 } |
| 87 | 108 |
| 88 void WebUIImpl::RenderViewCreated(RenderViewHost* render_view_host) { | 109 void WebUIImpl::RenderViewCreated(RenderViewHost* render_view_host) { |
| 89 controller_->RenderViewCreated(render_view_host); | 110 controller_->RenderViewCreated(render_view_host); |
| 90 } | 111 } |
| 91 | 112 |
| 92 void WebUIImpl::RenderViewReused(RenderViewHost* render_view_host, | 113 void WebUIImpl::RenderViewReused(RenderViewHost* render_view_host, |
| 93 bool was_main_frame) { | 114 bool was_main_frame) { |
| 94 if (was_main_frame) { | 115 if (was_main_frame) { |
| 95 GURL site_url = render_view_host->GetSiteInstance()->GetSiteURL(); | 116 GURL site_url = render_view_host->GetSiteInstance()->GetSiteURL(); |
| 96 GetContentClient()->browser()->LogWebUIUrl(site_url); | 117 GetContentClient()->browser()->LogWebUIUrl(site_url); |
|
Charlie Reis
2016/06/24 23:15:01
Ooh, I wonder if we can get rid of RenderViewReuse
tommycli
2016/06/24 23:55:36
Yeah I think that would be eminently doable. But y
| |
| 97 } | 118 } |
| 98 | |
| 99 for (WebUIMessageHandler* handler : handlers_) | |
| 100 handler->RenderViewReused(); | |
| 101 } | 119 } |
| 102 | 120 |
| 103 void WebUIImpl::RenderFrameHostSwappingOut() { | 121 void WebUIImpl::RenderFrameHostSwappingOut() { |
| 104 for (WebUIMessageHandler* handler : handlers_) | 122 DisallowJavascriptOnAllHandlers(); |
| 105 handler->DisallowJavascript(); | |
| 106 } | 123 } |
| 107 | 124 |
| 108 WebContents* WebUIImpl::GetWebContents() const { | 125 WebContents* WebUIImpl::GetWebContents() const { |
| 109 return web_contents_; | 126 return web_contents_; |
| 110 } | 127 } |
| 111 | 128 |
| 112 float WebUIImpl::GetDeviceScaleFactor() const { | 129 float WebUIImpl::GetDeviceScaleFactor() const { |
| 113 return GetScaleFactorForView(web_contents_->GetRenderWidgetHostView()); | 130 return GetScaleFactorForView(web_contents_->GetRenderWidgetHostView()); |
| 114 } | 131 } |
| 115 | 132 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 return *frame_set.begin(); | 294 return *frame_set.begin(); |
| 278 } | 295 } |
| 279 | 296 |
| 280 void WebUIImpl::AddToSetIfFrameNameMatches( | 297 void WebUIImpl::AddToSetIfFrameNameMatches( |
| 281 std::set<RenderFrameHost*>* frame_set, | 298 std::set<RenderFrameHost*>* frame_set, |
| 282 RenderFrameHost* host) { | 299 RenderFrameHost* host) { |
| 283 if (host->GetFrameName() == frame_name_) | 300 if (host->GetFrameName() == frame_name_) |
| 284 frame_set->insert(host); | 301 frame_set->insert(host); |
| 285 } | 302 } |
| 286 | 303 |
| 304 void WebUIImpl::DisallowJavascriptOnAllHandlers() { | |
| 305 for (WebUIMessageHandler* handler : handlers_) | |
| 306 handler->DisallowJavascript(); | |
| 307 } | |
| 308 | |
| 287 } // namespace content | 309 } // namespace content |
| OLD | NEW |