summaryrefslogtreecommitdiffstats
path: root/chrome/browser/dom_ui/dom_ui.cc
diff options
context:
space:
mode:
authorglen@chromium.org <glen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-07 23:48:03 +0000
committerglen@chromium.org <glen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-07 23:48:03 +0000
commit76a010b47593d41d1d443dacc51bc906689c2e53 (patch)
tree28f61efd20f447d21bb5ca3e8582b29994c79eba /chrome/browser/dom_ui/dom_ui.cc
parent85ad84eba6540630a33ab7bfa37c2d37c4a9554d (diff)
downloadchromium_src-76a010b47593d41d1d443dacc51bc906689c2e53.zip
chromium_src-76a010b47593d41d1d443dacc51bc906689c2e53.tar.gz
chromium_src-76a010b47593d41d1d443dacc51bc906689c2e53.tar.bz2
Reupload of 12418
Review URL: http://codereview.chromium.org/13183 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6497 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/dom_ui/dom_ui.cc')
-rw-r--r--chrome/browser/dom_ui/dom_ui.cc124
1 files changed, 124 insertions, 0 deletions
diff --git a/chrome/browser/dom_ui/dom_ui.cc b/chrome/browser/dom_ui/dom_ui.cc
new file mode 100644
index 0000000..4fe9070
--- /dev/null
+++ b/chrome/browser/dom_ui/dom_ui.cc
@@ -0,0 +1,124 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/dom_ui/dom_ui.h"
+
+#include "base/json_reader.h"
+#include "base/json_writer.h"
+#include "chrome/common/l10n_util.h"
+
+///////////////////////////////////////////////////////////////////////////////
+// DOMMessageHandler
+
+DOMUI::DOMUI(DOMUIContents* contents) : contents_(contents) {
+}
+
+DOMUI::~DOMUI() {
+ STLDeleteContainerPairSecondPointers(message_callbacks_.begin(),
+ message_callbacks_.end());
+ STLDeleteContainerPointers(handlers_.begin(), handlers_.end());
+}
+
+// DOMUI, public: -------------------------------------------------------------
+
+void DOMUI::ProcessDOMUIMessage(const std::string& message,
+ const std::string& content) {
+ // Look up the callback for this message.
+ MessageCallbackMap::const_iterator callback =
+ message_callbacks_.find(message);
+ if (callback == message_callbacks_.end())
+ return;
+
+ // Convert the content JSON into a Value.
+ Value* value = NULL;
+ if (!content.empty()) {
+ if (!JSONReader::Read(content, &value, false)) {
+ // The page sent us something that we didn't understand.
+ // This probably indicates a programming error.
+ NOTREACHED();
+ return;
+ }
+ }
+
+ // Forward this message and content on.
+ callback->second->Run(value);
+ delete value;
+}
+
+void DOMUI::CallJavascriptFunction(const std::wstring& function_name,
+ const Value& arg) {
+ std::string json;
+ JSONWriter::Write(&arg, false, &json);
+ std::wstring javascript = function_name + L"(" + UTF8ToWide(json) + L");";
+
+ ExecuteJavascript(javascript);
+}
+
+void DOMUI::CallJavascriptFunction(
+ const std::wstring& function_name,
+ const Value& arg1, const Value& arg2) {
+ std::string json;
+ JSONWriter::Write(&arg1, false, &json);
+ std::wstring javascript = function_name + L"(" + UTF8ToWide(json);
+ JSONWriter::Write(&arg2, false, &json);
+ javascript += L"," + UTF8ToWide(json) + L");";
+
+ ExecuteJavascript(javascript);
+}
+
+void DOMUI::RegisterMessageCallback(const std::string &message,
+ MessageCallback *callback) {
+ message_callbacks_.insert(std::make_pair(message, callback));
+}
+
+// DOMUI, protected: ----------------------------------------------------------
+
+void DOMUI::AddMessageHandler(DOMMessageHandler* handler) {
+ handlers_.push_back(handler);
+}
+
+// DOMUI, private: ------------------------------------------------------------
+
+void DOMUI::ExecuteJavascript(const std::wstring& javascript) {
+ DCHECK(contents_);
+ contents_->render_view_host()->ExecuteJavascriptInWebFrame(std::wstring(),
+ javascript);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// DOMMessageHandler
+
+DOMMessageHandler::DOMMessageHandler(DOMUI *dom_ui) : dom_ui_(dom_ui) {
+}
+
+// DOMMessageHandler, protected: ----------------------------------------------
+
+void DOMMessageHandler::SetURLAndTitle(DictionaryValue* dictionary,
+ std::wstring title,
+ const GURL& gurl) {
+ std::wstring wstring_url = UTF8ToWide(gurl.spec());
+ dictionary->SetString(L"url", wstring_url);
+
+ bool using_url_as_the_title = false;
+ if (title.empty()) {
+ using_url_as_the_title = true;
+ title = wstring_url;
+ }
+
+ // Since the title can contain BiDi text, we need to mark the text as either
+ // RTL or LTR, depending on the characters in the string. If we use the URL
+ // as the title, we mark the title as LTR since URLs are always treated as
+ // left to right strings.
+ std::wstring title_to_set(title);
+ if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) {
+ if (using_url_as_the_title) {
+ l10n_util::WrapStringWithLTRFormatting(&title_to_set);
+ } else {
+ bool success =
+ l10n_util::AdjustStringForLocaleDirection(title, &title_to_set);
+ DCHECK(success ? (title != title_to_set) : (title == title_to_set));
+ }
+ }
+ dictionary->SetString(L"title", title_to_set);
+} \ No newline at end of file