| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "content/browser/webui/web_ui.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "content/browser/child_process_security_policy.h" | 12 #include "content/browser/child_process_security_policy.h" |
| 13 #include "content/browser/renderer_host/render_process_host_impl.h" | 13 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 14 #include "content/browser/renderer_host/render_view_host.h" | 14 #include "content/browser/renderer_host/render_view_host.h" |
| 15 #include "content/browser/tab_contents/tab_contents.h" | 15 #include "content/browser/tab_contents/tab_contents.h" |
| 16 #include "content/browser/tab_contents/tab_contents_view.h" | 16 #include "content/browser/tab_contents/tab_contents_view.h" |
| 17 #include "content/browser/webui/generic_handler.h" | 17 #include "content/browser/webui/generic_handler.h" |
| 18 #include "content/common/view_messages.h" | 18 #include "content/common/view_messages.h" |
| 19 #include "content/public/browser/web_ui_controller.h" |
| 19 #include "content/public/common/bindings_policy.h" | 20 #include "content/public/common/bindings_policy.h" |
| 20 #include "content/public/common/content_switches.h" | 21 #include "content/public/common/content_switches.h" |
| 21 | 22 |
| 22 using content::WebContents; | 23 using content::WebContents; |
| 24 using content::WebUIController; |
| 23 using content::WebUIMessageHandler; | 25 using content::WebUIMessageHandler; |
| 24 | 26 |
| 25 // static | 27 // static |
| 26 string16 WebUI::GetJavascriptCall( | 28 string16 WebUI::GetJavascriptCall( |
| 27 const std::string& function_name, | 29 const std::string& function_name, |
| 28 const std::vector<const Value*>& arg_list) { | 30 const std::vector<const Value*>& arg_list) { |
| 29 string16 parameters; | 31 string16 parameters; |
| 30 std::string json; | 32 std::string json; |
| 31 for (size_t i = 0; i < arg_list.size(); ++i) { | 33 for (size_t i = 0; i < arg_list.size(); ++i) { |
| 32 if (i > 0) | 34 if (i > 0) |
| 33 parameters += char16(','); | 35 parameters += char16(','); |
| 34 | 36 |
| 35 base::JSONWriter::Write(arg_list[i], false, &json); | 37 base::JSONWriter::Write(arg_list[i], false, &json); |
| 36 parameters += UTF8ToUTF16(json); | 38 parameters += UTF8ToUTF16(json); |
| 37 } | 39 } |
| 38 return ASCIIToUTF16(function_name) + | 40 return ASCIIToUTF16(function_name) + |
| 39 char16('(') + parameters + char16(')') + char16(';'); | 41 char16('(') + parameters + char16(')') + char16(';'); |
| 40 } | 42 } |
| 41 | 43 |
| 42 WebUI::WebUI(WebContents* contents) | 44 WebUI::WebUI(WebContents* contents, |
| 45 WebUIController* controller) |
| 43 : hide_favicon_(false), | 46 : hide_favicon_(false), |
| 44 focus_location_bar_by_default_(false), | 47 focus_location_bar_by_default_(false), |
| 45 should_hide_url_(false), | 48 should_hide_url_(false), |
| 46 link_transition_type_(content::PAGE_TRANSITION_LINK), | 49 link_transition_type_(content::PAGE_TRANSITION_LINK), |
| 47 bindings_(content::BINDINGS_POLICY_WEB_UI), | 50 bindings_(content::BINDINGS_POLICY_WEB_UI), |
| 48 register_callback_overwrites_(false), | 51 register_callback_overwrites_(false), |
| 49 web_contents_(contents) { | 52 web_contents_(contents), |
| 53 controller_(controller) { |
| 50 DCHECK(contents); | 54 DCHECK(contents); |
| 51 AddMessageHandler(new GenericHandler()); | 55 AddMessageHandler(new GenericHandler()); |
| 52 } | 56 } |
| 53 | 57 |
| 54 WebUI::~WebUI() { | 58 WebUI::~WebUI() { |
| 55 STLDeleteContainerPointers(handlers_.begin(), handlers_.end()); | 59 STLDeleteContainerPointers(handlers_.begin(), handlers_.end()); |
| 56 } | 60 } |
| 57 | 61 |
| 58 // WebUI, public: ------------------------------------------------------------- | 62 // WebUI, public: ------------------------------------------------------------- |
| 59 | 63 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 70 | 74 |
| 71 void WebUI::OnWebUISend(const GURL& source_url, | 75 void WebUI::OnWebUISend(const GURL& source_url, |
| 72 const std::string& message, | 76 const std::string& message, |
| 73 const ListValue& args) { | 77 const ListValue& args) { |
| 74 if (!ChildProcessSecurityPolicy::GetInstance()-> | 78 if (!ChildProcessSecurityPolicy::GetInstance()-> |
| 75 HasWebUIBindings(web_contents_->GetRenderProcessHost()->GetID())) { | 79 HasWebUIBindings(web_contents_->GetRenderProcessHost()->GetID())) { |
| 76 NOTREACHED() << "Blocked unauthorized use of WebUIBindings."; | 80 NOTREACHED() << "Blocked unauthorized use of WebUIBindings."; |
| 77 return; | 81 return; |
| 78 } | 82 } |
| 79 | 83 |
| 84 if (controller_->OverrideHandleWebUIMessage(source_url, message,args)) |
| 85 return; |
| 86 |
| 80 // Look up the callback for this message. | 87 // Look up the callback for this message. |
| 81 MessageCallbackMap::const_iterator callback = | 88 MessageCallbackMap::const_iterator callback = |
| 82 message_callbacks_.find(message); | 89 message_callbacks_.find(message); |
| 83 if (callback != message_callbacks_.end()) { | 90 if (callback != message_callbacks_.end()) { |
| 84 // Forward this message and content on. | 91 // Forward this message and content on. |
| 85 callback->second.Run(&args); | 92 callback->second.Run(&args); |
| 86 } | 93 } |
| 87 } | 94 } |
| 88 | 95 |
| 89 void WebUI::RenderViewCreated(RenderViewHost* render_view_host) { | 96 void WebUI::RenderViewCreated(RenderViewHost* render_view_host) { |
| 97 controller_->RenderViewCreated(render_view_host); |
| 98 |
| 90 // Do not attempt to set the toolkit property if WebUI is not enabled, e.g., | 99 // Do not attempt to set the toolkit property if WebUI is not enabled, e.g., |
| 91 // the bookmarks manager page. | 100 // the bookmarks manager page. |
| 92 if (!(bindings_ & content::BINDINGS_POLICY_WEB_UI)) | 101 if (!(bindings_ & content::BINDINGS_POLICY_WEB_UI)) |
| 93 return; | 102 return; |
| 94 | 103 |
| 95 #if defined(TOOLKIT_VIEWS) | 104 #if defined(TOOLKIT_VIEWS) |
| 96 render_view_host->SetWebUIProperty("toolkit", "views"); | 105 render_view_host->SetWebUIProperty("toolkit", "views"); |
| 97 #elif defined(TOOLKIT_GTK) | 106 #elif defined(TOOLKIT_GTK) |
| 98 render_view_host->SetWebUIProperty("toolkit", "GTK"); | 107 render_view_host->SetWebUIProperty("toolkit", "GTK"); |
| 99 #endif // defined(TOOLKIT_VIEWS) | 108 #endif // defined(TOOLKIT_VIEWS) |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 DCHECK(!handler->web_ui()); | 188 DCHECK(!handler->web_ui()); |
| 180 handler->set_web_ui(this); | 189 handler->set_web_ui(this); |
| 181 handler->RegisterMessages(); | 190 handler->RegisterMessages(); |
| 182 handlers_.push_back(handler); | 191 handlers_.push_back(handler); |
| 183 } | 192 } |
| 184 | 193 |
| 185 void WebUI::ExecuteJavascript(const string16& javascript) { | 194 void WebUI::ExecuteJavascript(const string16& javascript) { |
| 186 web_contents_->GetRenderViewHost()->ExecuteJavascriptInWebFrame( | 195 web_contents_->GetRenderViewHost()->ExecuteJavascriptInWebFrame( |
| 187 ASCIIToUTF16(frame_xpath_), javascript); | 196 ASCIIToUTF16(frame_xpath_), javascript); |
| 188 } | 197 } |
| OLD | NEW |