| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/dom_ui/dom_ui.h" |
| 6 |
| 7 #include "base/json_reader.h" |
| 8 #include "base/json_writer.h" |
| 9 #include "chrome/common/l10n_util.h" |
| 10 |
| 11 /////////////////////////////////////////////////////////////////////////////// |
| 12 // DOMMessageHandler |
| 13 |
| 14 DOMUI::DOMUI(DOMUIContents* contents) : contents_(contents) { |
| 15 } |
| 16 |
| 17 DOMUI::~DOMUI() { |
| 18 STLDeleteContainerPairSecondPointers(message_callbacks_.begin(), |
| 19 message_callbacks_.end()); |
| 20 STLDeleteContainerPointers(handlers_.begin(), handlers_.end()); |
| 21 } |
| 22 |
| 23 // DOMUI, public: ------------------------------------------------------------- |
| 24 |
| 25 void DOMUI::ProcessDOMUIMessage(const std::string& message, |
| 26 const std::string& content) { |
| 27 // Look up the callback for this message. |
| 28 MessageCallbackMap::const_iterator callback = |
| 29 message_callbacks_.find(message); |
| 30 if (callback == message_callbacks_.end()) |
| 31 return; |
| 32 |
| 33 // Convert the content JSON into a Value. |
| 34 Value* value = NULL; |
| 35 if (!content.empty()) { |
| 36 if (!JSONReader::Read(content, &value, false)) { |
| 37 // The page sent us something that we didn't understand. |
| 38 // This probably indicates a programming error. |
| 39 NOTREACHED(); |
| 40 return; |
| 41 } |
| 42 } |
| 43 |
| 44 // Forward this message and content on. |
| 45 callback->second->Run(value); |
| 46 delete value; |
| 47 } |
| 48 |
| 49 void DOMUI::CallJavascriptFunction(const std::wstring& function_name, |
| 50 const Value& arg) { |
| 51 std::string json; |
| 52 JSONWriter::Write(&arg, false, &json); |
| 53 std::wstring javascript = function_name + L"(" + UTF8ToWide(json) + L");"; |
| 54 |
| 55 ExecuteJavascript(javascript); |
| 56 } |
| 57 |
| 58 void DOMUI::CallJavascriptFunction( |
| 59 const std::wstring& function_name, |
| 60 const Value& arg1, const Value& arg2) { |
| 61 std::string json; |
| 62 JSONWriter::Write(&arg1, false, &json); |
| 63 std::wstring javascript = function_name + L"(" + UTF8ToWide(json); |
| 64 JSONWriter::Write(&arg2, false, &json); |
| 65 javascript += L"," + UTF8ToWide(json) + L");"; |
| 66 |
| 67 ExecuteJavascript(javascript); |
| 68 } |
| 69 |
| 70 void DOMUI::RegisterMessageCallback(const std::string &message, |
| 71 MessageCallback *callback) { |
| 72 message_callbacks_.insert(std::make_pair(message, callback)); |
| 73 } |
| 74 |
| 75 // DOMUI, protected: ---------------------------------------------------------- |
| 76 |
| 77 void DOMUI::AddMessageHandler(DOMMessageHandler* handler) { |
| 78 handlers_.push_back(handler); |
| 79 } |
| 80 |
| 81 // DOMUI, private: ------------------------------------------------------------ |
| 82 |
| 83 void DOMUI::ExecuteJavascript(const std::wstring& javascript) { |
| 84 DCHECK(contents_); |
| 85 contents_->render_view_host()->ExecuteJavascriptInWebFrame(std::wstring(), |
| 86 javascript); |
| 87 } |
| 88 |
| 89 /////////////////////////////////////////////////////////////////////////////// |
| 90 // DOMMessageHandler |
| 91 |
| 92 DOMMessageHandler::DOMMessageHandler(DOMUI *dom_ui) : dom_ui_(dom_ui) { |
| 93 } |
| 94 |
| 95 // DOMMessageHandler, protected: ---------------------------------------------- |
| 96 |
| 97 void DOMMessageHandler::SetURLAndTitle(DictionaryValue* dictionary, |
| 98 std::wstring title, |
| 99 const GURL& gurl) { |
| 100 std::wstring wstring_url = UTF8ToWide(gurl.spec()); |
| 101 dictionary->SetString(L"url", wstring_url); |
| 102 |
| 103 bool using_url_as_the_title = false; |
| 104 if (title.empty()) { |
| 105 using_url_as_the_title = true; |
| 106 title = wstring_url; |
| 107 } |
| 108 |
| 109 // Since the title can contain BiDi text, we need to mark the text as either |
| 110 // RTL or LTR, depending on the characters in the string. If we use the URL |
| 111 // as the title, we mark the title as LTR since URLs are always treated as |
| 112 // left to right strings. |
| 113 std::wstring title_to_set(title); |
| 114 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) { |
| 115 if (using_url_as_the_title) { |
| 116 l10n_util::WrapStringWithLTRFormatting(&title_to_set); |
| 117 } else { |
| 118 bool success = |
| 119 l10n_util::AdjustStringForLocaleDirection(title, &title_to_set); |
| 120 DCHECK(success ? (title != title_to_set) : (title == title_to_set)); |
| 121 } |
| 122 } |
| 123 dictionary->SetString(L"title", title_to_set); |
| 124 } |
| OLD | NEW |