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 "chrome/browser/profiles/profile.h" | |
14 #include "content/browser/child_process_security_policy.h" | 13 #include "content/browser/child_process_security_policy.h" |
15 #include "content/browser/renderer_host/render_process_host.h" | 14 #include "content/browser/renderer_host/render_process_host.h" |
16 #include "content/browser/renderer_host/render_view_host.h" | 15 #include "content/browser/renderer_host/render_view_host.h" |
17 #include "content/browser/tab_contents/tab_contents.h" | 16 #include "content/browser/tab_contents/tab_contents.h" |
18 #include "content/browser/tab_contents/tab_contents_view.h" | 17 #include "content/browser/tab_contents/tab_contents_view.h" |
19 #include "content/browser/webui/generic_handler.h" | 18 #include "content/browser/webui/generic_handler.h" |
20 #include "content/common/bindings_policy.h" | 19 #include "content/common/bindings_policy.h" |
21 #include "content/common/view_messages.h" | 20 #include "content/common/view_messages.h" |
22 #include "ipc/ipc_message.h" | 21 #include "ipc/ipc_message.h" |
23 #include "ipc/ipc_message_macros.h" | 22 #include "ipc/ipc_message_macros.h" |
(...skipping 16 matching lines...) Expand all Loading... |
40 } | 39 } |
41 | 40 |
42 WebUI::WebUI(TabContents* contents) | 41 WebUI::WebUI(TabContents* contents) |
43 : hide_favicon_(false), | 42 : hide_favicon_(false), |
44 focus_location_bar_by_default_(false), | 43 focus_location_bar_by_default_(false), |
45 should_hide_url_(false), | 44 should_hide_url_(false), |
46 link_transition_type_(PageTransition::LINK), | 45 link_transition_type_(PageTransition::LINK), |
47 bindings_(BindingsPolicy::WEB_UI), | 46 bindings_(BindingsPolicy::WEB_UI), |
48 register_callback_overwrites_(false), | 47 register_callback_overwrites_(false), |
49 tab_contents_(contents) { | 48 tab_contents_(contents) { |
| 49 DCHECK(contents); |
50 GenericHandler* handler = new GenericHandler(); | 50 GenericHandler* handler = new GenericHandler(); |
51 AddMessageHandler(handler->Attach(this)); | 51 AddMessageHandler(handler->Attach(this)); |
52 } | 52 } |
53 | 53 |
54 WebUI::~WebUI() { | 54 WebUI::~WebUI() { |
55 STLDeleteContainerPairSecondPointers(message_callbacks_.begin(), | 55 STLDeleteContainerPairSecondPointers(message_callbacks_.begin(), |
56 message_callbacks_.end()); | 56 message_callbacks_.end()); |
57 STLDeleteContainerPointers(handlers_.begin(), handlers_.end()); | 57 STLDeleteContainerPointers(handlers_.begin(), handlers_.end()); |
58 } | 58 } |
59 | 59 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 void WebUI::RegisterMessageCallback(const std::string &message, | 149 void WebUI::RegisterMessageCallback(const std::string &message, |
150 MessageCallback *callback) { | 150 MessageCallback *callback) { |
151 std::pair<MessageCallbackMap::iterator, bool> result = | 151 std::pair<MessageCallbackMap::iterator, bool> result = |
152 message_callbacks_.insert(std::make_pair(message, callback)); | 152 message_callbacks_.insert(std::make_pair(message, callback)); |
153 | 153 |
154 // Overwrite preexisting message callback mappings. | 154 // Overwrite preexisting message callback mappings. |
155 if (register_callback_overwrites() && !result.second) | 155 if (register_callback_overwrites() && !result.second) |
156 result.first->second = callback; | 156 result.first->second = callback; |
157 } | 157 } |
158 | 158 |
159 Profile* WebUI::GetProfile() const { | |
160 DCHECK(tab_contents()); | |
161 return Profile::FromBrowserContext(tab_contents()->browser_context()); | |
162 } | |
163 | |
164 RenderViewHost* WebUI::GetRenderViewHost() const { | |
165 DCHECK(tab_contents()); | |
166 return tab_contents()->render_view_host(); | |
167 } | |
168 | |
169 bool WebUI::IsLoading() const { | 159 bool WebUI::IsLoading() const { |
170 std::vector<WebUIMessageHandler*>::const_iterator iter; | 160 std::vector<WebUIMessageHandler*>::const_iterator iter; |
171 for (iter = handlers_.begin(); iter != handlers_.end(); ++iter) { | 161 for (iter = handlers_.begin(); iter != handlers_.end(); ++iter) { |
172 if ((*iter)->IsLoading()) | 162 if ((*iter)->IsLoading()) |
173 return true; | 163 return true; |
174 } | 164 } |
175 return false; | 165 return false; |
176 } | 166 } |
177 | 167 |
178 // WebUI, protected: ---------------------------------------------------------- | 168 // WebUI, protected: ---------------------------------------------------------- |
179 | 169 |
180 void WebUI::AddMessageHandler(WebUIMessageHandler* handler) { | 170 void WebUI::AddMessageHandler(WebUIMessageHandler* handler) { |
181 handlers_.push_back(handler); | 171 handlers_.push_back(handler); |
182 } | 172 } |
183 | 173 |
184 void WebUI::ExecuteJavascript(const string16& javascript) { | 174 void WebUI::ExecuteJavascript(const string16& javascript) { |
185 GetRenderViewHost()->ExecuteJavascriptInWebFrame(string16(), | 175 tab_contents_->render_view_host()->ExecuteJavascriptInWebFrame(string16(), |
186 javascript); | 176 javascript); |
187 } | 177 } |
188 | 178 |
189 /////////////////////////////////////////////////////////////////////////////// | 179 /////////////////////////////////////////////////////////////////////////////// |
190 // WebUIMessageHandler | 180 // WebUIMessageHandler |
191 WebUIMessageHandler::WebUIMessageHandler() : web_ui_(NULL) { | 181 WebUIMessageHandler::WebUIMessageHandler() : web_ui_(NULL) { |
192 } | 182 } |
193 | 183 |
194 WebUIMessageHandler::~WebUIMessageHandler() { | 184 WebUIMessageHandler::~WebUIMessageHandler() { |
195 } | 185 } |
196 | 186 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 return false; | 231 return false; |
242 } | 232 } |
243 | 233 |
244 string16 WebUIMessageHandler::ExtractStringValue(const ListValue* value) { | 234 string16 WebUIMessageHandler::ExtractStringValue(const ListValue* value) { |
245 string16 string16_value; | 235 string16 string16_value; |
246 if (value->GetString(0, &string16_value)) | 236 if (value->GetString(0, &string16_value)) |
247 return string16_value; | 237 return string16_value; |
248 NOTREACHED(); | 238 NOTREACHED(); |
249 return string16(); | 239 return string16(); |
250 } | 240 } |
OLD | NEW |