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