| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/renderer/extensions/i18n_custom_bindings.h" | 5 #include "chrome/renderer/extensions/i18n_custom_bindings.h" |
| 6 | 6 |
| 7 #include "chrome/common/extensions/extension_messages.h" | 7 #include "chrome/common/extensions/extension_messages.h" |
| 8 #include "chrome/common/extensions/message_bundle.h" | 8 #include "chrome/common/extensions/message_bundle.h" |
| 9 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
| 10 #include "grit/renderer_resources.h" | 10 #include "grit/renderer_resources.h" |
| 11 #include "v8/include/v8.h" | 11 #include "v8/include/v8.h" |
| 12 | 12 |
| 13 namespace extensions { | 13 namespace extensions { |
| 14 | 14 |
| 15 I18NCustomBindings::I18NCustomBindings(Dispatcher* dispatcher, | 15 I18NCustomBindings::I18NCustomBindings() |
| 16 v8::Handle<v8::Context> context) | 16 : ChromeV8Extension(NULL) { |
| 17 : ChromeV8Extension(dispatcher, context) { | |
| 18 RouteStaticFunction("GetL10nMessage", &GetL10nMessage); | 17 RouteStaticFunction("GetL10nMessage", &GetL10nMessage); |
| 19 } | 18 } |
| 20 | 19 |
| 21 // static | 20 // static |
| 22 v8::Handle<v8::Value> I18NCustomBindings::GetL10nMessage( | 21 v8::Handle<v8::Value> I18NCustomBindings::GetL10nMessage( |
| 23 const v8::Arguments& args) { | 22 const v8::Arguments& args) { |
| 24 I18NCustomBindings* self = GetFromArguments<I18NCustomBindings>(args); | |
| 25 | |
| 26 if (args.Length() != 3 || !args[0]->IsString()) { | 23 if (args.Length() != 3 || !args[0]->IsString()) { |
| 27 NOTREACHED() << "Bad arguments"; | 24 NOTREACHED() << "Bad arguments"; |
| 28 return v8::Undefined(); | 25 return v8::Undefined(); |
| 29 } | 26 } |
| 30 | 27 |
| 31 std::string extension_id; | 28 std::string extension_id; |
| 32 if (args[2]->IsNull() || !args[2]->IsString()) { | 29 if (args[2]->IsNull() || !args[2]->IsString()) { |
| 33 return v8::Undefined(); | 30 return v8::Undefined(); |
| 34 } else { | 31 } else { |
| 35 extension_id = *v8::String::Utf8Value(args[2]->ToString()); | 32 extension_id = *v8::String::Utf8Value(args[2]->ToString()); |
| 36 if (extension_id.empty()) | 33 if (extension_id.empty()) |
| 37 return v8::Undefined(); | 34 return v8::Undefined(); |
| 38 } | 35 } |
| 39 | 36 |
| 40 L10nMessagesMap* l10n_messages = GetL10nMessagesMap(extension_id); | 37 L10nMessagesMap* l10n_messages = GetL10nMessagesMap(extension_id); |
| 41 if (!l10n_messages) { | 38 if (!l10n_messages) { |
| 42 // Get the current RenderView so that we can send a routed IPC message | 39 // Get the current RenderView so that we can send a routed IPC message |
| 43 // from the correct source. | 40 // from the correct source. |
| 44 content::RenderView* renderview = self->GetRenderView(); | 41 content::RenderView* renderview = GetCurrentRenderView(); |
| 45 if (!renderview) | 42 if (!renderview) |
| 46 return v8::Undefined(); | 43 return v8::Undefined(); |
| 47 | 44 |
| 48 L10nMessagesMap messages; | 45 L10nMessagesMap messages; |
| 49 // A sync call to load message catalogs for current extension. | 46 // A sync call to load message catalogs for current extension. |
| 50 renderview->Send(new ExtensionHostMsg_GetMessageBundle( | 47 renderview->Send(new ExtensionHostMsg_GetMessageBundle( |
| 51 extension_id, &messages)); | 48 extension_id, &messages)); |
| 52 | 49 |
| 53 // Save messages we got. | 50 // Save messages we got. |
| 54 ExtensionToL10nMessagesMap& l10n_messages_map = | 51 ExtensionToL10nMessagesMap& l10n_messages_map = |
| (...skipping 22 matching lines...) Expand all Loading... |
| 77 } else if (args[1]->IsString()) { | 74 } else if (args[1]->IsString()) { |
| 78 // chrome.i18n.getMessage("message_name", "one param"); | 75 // chrome.i18n.getMessage("message_name", "one param"); |
| 79 substitutions.push_back(*v8::String::Utf8Value(args[1]->ToString())); | 76 substitutions.push_back(*v8::String::Utf8Value(args[1]->ToString())); |
| 80 } | 77 } |
| 81 | 78 |
| 82 return v8::String::New(ReplaceStringPlaceholders( | 79 return v8::String::New(ReplaceStringPlaceholders( |
| 83 message, substitutions, NULL).c_str()); | 80 message, substitutions, NULL).c_str()); |
| 84 } | 81 } |
| 85 | 82 |
| 86 } // namespace extension | 83 } // namespace extension |
| OLD | NEW |