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/i18n/rtl.h" | 7 #include "base/i18n/rtl.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/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "content/browser/child_process_security_policy.h" | 13 #include "content/browser/child_process_security_policy.h" |
14 #include "content/browser/renderer_host/render_process_host_impl.h" | 14 #include "content/browser/renderer_host/render_process_host_impl.h" |
15 #include "content/browser/renderer_host/render_view_host.h" | 15 #include "content/browser/renderer_host/render_view_host.h" |
16 #include "content/browser/tab_contents/tab_contents.h" | 16 #include "content/browser/tab_contents/tab_contents.h" |
17 #include "content/browser/tab_contents/tab_contents_view.h" | 17 #include "content/browser/tab_contents/tab_contents_view.h" |
18 #include "content/browser/webui/generic_handler.h" | 18 #include "content/browser/webui/generic_handler.h" |
19 #include "content/common/view_messages.h" | 19 #include "content/common/view_messages.h" |
20 #include "content/public/common/bindings_policy.h" | 20 #include "content/public/common/bindings_policy.h" |
21 #include "ipc/ipc_message.h" | 21 #include "ipc/ipc_message.h" |
22 #include "ipc/ipc_message_macros.h" | 22 #include "ipc/ipc_message_macros.h" |
23 | 23 |
| 24 using content::WebContents; |
| 25 |
24 // static | 26 // static |
25 string16 WebUI::GetJavascriptCall( | 27 string16 WebUI::GetJavascriptCall( |
26 const std::string& function_name, | 28 const std::string& function_name, |
27 const std::vector<const Value*>& arg_list) { | 29 const std::vector<const Value*>& arg_list) { |
28 string16 parameters; | 30 string16 parameters; |
29 std::string json; | 31 std::string json; |
30 for (size_t i = 0; i < arg_list.size(); ++i) { | 32 for (size_t i = 0; i < arg_list.size(); ++i) { |
31 if (i > 0) | 33 if (i > 0) |
32 parameters += char16(','); | 34 parameters += char16(','); |
33 | 35 |
34 base::JSONWriter::Write(arg_list[i], false, &json); | 36 base::JSONWriter::Write(arg_list[i], false, &json); |
35 parameters += UTF8ToUTF16(json); | 37 parameters += UTF8ToUTF16(json); |
36 } | 38 } |
37 return ASCIIToUTF16(function_name) + | 39 return ASCIIToUTF16(function_name) + |
38 char16('(') + parameters + char16(')') + char16(';'); | 40 char16('(') + parameters + char16(')') + char16(';'); |
39 } | 41 } |
40 | 42 |
41 WebUI::WebUI(TabContents* contents) | 43 WebUI::WebUI(WebContents* contents) |
42 : hide_favicon_(false), | 44 : hide_favicon_(false), |
43 focus_location_bar_by_default_(false), | 45 focus_location_bar_by_default_(false), |
44 should_hide_url_(false), | 46 should_hide_url_(false), |
45 link_transition_type_(content::PAGE_TRANSITION_LINK), | 47 link_transition_type_(content::PAGE_TRANSITION_LINK), |
46 bindings_(content::BINDINGS_POLICY_WEB_UI), | 48 bindings_(content::BINDINGS_POLICY_WEB_UI), |
47 register_callback_overwrites_(false), | 49 register_callback_overwrites_(false), |
48 tab_contents_(contents) { | 50 web_contents_(contents) { |
49 DCHECK(contents); | 51 DCHECK(contents); |
50 AddMessageHandler(new GenericHandler()); | 52 AddMessageHandler(new GenericHandler()); |
51 } | 53 } |
52 | 54 |
53 WebUI::~WebUI() { | 55 WebUI::~WebUI() { |
54 STLDeleteContainerPointers(handlers_.begin(), handlers_.end()); | 56 STLDeleteContainerPointers(handlers_.begin(), handlers_.end()); |
55 } | 57 } |
56 | 58 |
57 // WebUI, public: ------------------------------------------------------------- | 59 // WebUI, public: ------------------------------------------------------------- |
58 | 60 |
59 const WebUI::TypeID WebUI::kNoWebUI = NULL; | 61 const WebUI::TypeID WebUI::kNoWebUI = NULL; |
60 | 62 |
61 bool WebUI::OnMessageReceived(const IPC::Message& message) { | 63 bool WebUI::OnMessageReceived(const IPC::Message& message) { |
62 bool handled = true; | 64 bool handled = true; |
63 IPC_BEGIN_MESSAGE_MAP(WebUI, message) | 65 IPC_BEGIN_MESSAGE_MAP(WebUI, message) |
64 IPC_MESSAGE_HANDLER(ViewHostMsg_WebUISend, OnWebUISend) | 66 IPC_MESSAGE_HANDLER(ViewHostMsg_WebUISend, OnWebUISend) |
65 IPC_MESSAGE_UNHANDLED(handled = false) | 67 IPC_MESSAGE_UNHANDLED(handled = false) |
66 IPC_END_MESSAGE_MAP() | 68 IPC_END_MESSAGE_MAP() |
67 return handled; | 69 return handled; |
68 } | 70 } |
69 | 71 |
70 void WebUI::OnWebUISend(const GURL& source_url, | 72 void WebUI::OnWebUISend(const GURL& source_url, |
71 const std::string& message, | 73 const std::string& message, |
72 const ListValue& args) { | 74 const ListValue& args) { |
73 if (!ChildProcessSecurityPolicy::GetInstance()-> | 75 if (!ChildProcessSecurityPolicy::GetInstance()-> |
74 HasWebUIBindings(tab_contents_->GetRenderProcessHost()->GetID())) { | 76 HasWebUIBindings(web_contents_->GetRenderProcessHost()->GetID())) { |
75 NOTREACHED() << "Blocked unauthorized use of WebUIBindings."; | 77 NOTREACHED() << "Blocked unauthorized use of WebUIBindings."; |
76 return; | 78 return; |
77 } | 79 } |
78 | 80 |
79 // Look up the callback for this message. | 81 // Look up the callback for this message. |
80 MessageCallbackMap::const_iterator callback = | 82 MessageCallbackMap::const_iterator callback = |
81 message_callbacks_.find(message); | 83 message_callbacks_.find(message); |
82 if (callback != message_callbacks_.end()) { | 84 if (callback != message_callbacks_.end()) { |
83 // Forward this message and content on. | 85 // Forward this message and content on. |
84 callback->second.Run(&args); | 86 callback->second.Run(&args); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 169 |
168 // WebUI, protected: ---------------------------------------------------------- | 170 // WebUI, protected: ---------------------------------------------------------- |
169 | 171 |
170 void WebUI::AddMessageHandler(WebUIMessageHandler* handler) { | 172 void WebUI::AddMessageHandler(WebUIMessageHandler* handler) { |
171 handler->set_web_ui(this); | 173 handler->set_web_ui(this); |
172 handler->RegisterMessages(); | 174 handler->RegisterMessages(); |
173 handlers_.push_back(handler); | 175 handlers_.push_back(handler); |
174 } | 176 } |
175 | 177 |
176 void WebUI::ExecuteJavascript(const string16& javascript) { | 178 void WebUI::ExecuteJavascript(const string16& javascript) { |
177 tab_contents_->GetRenderViewHost()->ExecuteJavascriptInWebFrame( | 179 web_contents_->GetRenderViewHost()->ExecuteJavascriptInWebFrame( |
178 ASCIIToUTF16(frame_xpath_), javascript); | 180 ASCIIToUTF16(frame_xpath_), javascript); |
179 } | 181 } |
180 | 182 |
181 /////////////////////////////////////////////////////////////////////////////// | 183 /////////////////////////////////////////////////////////////////////////////// |
182 // WebUIMessageHandler | 184 // WebUIMessageHandler |
183 WebUIMessageHandler::WebUIMessageHandler() : web_ui_(NULL) { | 185 WebUIMessageHandler::WebUIMessageHandler() : web_ui_(NULL) { |
184 } | 186 } |
185 | 187 |
186 WebUIMessageHandler::~WebUIMessageHandler() { | 188 WebUIMessageHandler::~WebUIMessageHandler() { |
187 } | 189 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 return false; | 239 return false; |
238 } | 240 } |
239 | 241 |
240 string16 WebUIMessageHandler::ExtractStringValue(const ListValue* value) { | 242 string16 WebUIMessageHandler::ExtractStringValue(const ListValue* value) { |
241 string16 string16_value; | 243 string16 string16_value; |
242 if (value->GetString(0, &string16_value)) | 244 if (value->GetString(0, &string16_value)) |
243 return string16_value; | 245 return string16_value; |
244 NOTREACHED(); | 246 NOTREACHED(); |
245 return string16(); | 247 return string16(); |
246 } | 248 } |
OLD | NEW |