| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/i18n_custom_bindings.h" | 5 #include "extensions/renderer/i18n_custom_bindings.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "content/public/renderer/render_thread.h" | 8 #include "content/public/renderer/render_thread.h" |
| 9 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
| 10 #include "extensions/common/extension_messages.h" | 10 #include "extensions/common/extension_messages.h" |
| 11 #include "extensions/common/message_bundle.h" | 11 #include "extensions/common/message_bundle.h" |
| 12 #include "extensions/renderer/script_context.h" | 12 #include "extensions/renderer/script_context.h" |
| 13 #include "extensions/renderer/v8_maybe_helpers.h" |
| 13 | 14 |
| 14 namespace extensions { | 15 namespace extensions { |
| 15 | 16 |
| 16 I18NCustomBindings::I18NCustomBindings(ScriptContext* context) | 17 I18NCustomBindings::I18NCustomBindings(ScriptContext* context) |
| 17 : ObjectBackedNativeHandler(context) { | 18 : ObjectBackedNativeHandler(context) { |
| 18 RouteFunction( | 19 RouteFunction( |
| 19 "GetL10nMessage", | 20 "GetL10nMessage", |
| 20 base::Bind(&I18NCustomBindings::GetL10nMessage, base::Unretained(this))); | 21 base::Bind(&I18NCustomBindings::GetL10nMessage, base::Unretained(this))); |
| 21 RouteFunction("GetL10nUILanguage", | 22 RouteFunction("GetL10nUILanguage", |
| 22 base::Bind(&I18NCustomBindings::GetL10nUILanguage, | 23 base::Bind(&I18NCustomBindings::GetL10nUILanguage, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 | 67 |
| 67 v8::Isolate* isolate = args.GetIsolate(); | 68 v8::Isolate* isolate = args.GetIsolate(); |
| 68 std::vector<std::string> substitutions; | 69 std::vector<std::string> substitutions; |
| 69 if (args[1]->IsArray()) { | 70 if (args[1]->IsArray()) { |
| 70 // chrome.i18n.getMessage("message_name", ["more", "params"]); | 71 // chrome.i18n.getMessage("message_name", ["more", "params"]); |
| 71 v8::Local<v8::Array> placeholders = v8::Local<v8::Array>::Cast(args[1]); | 72 v8::Local<v8::Array> placeholders = v8::Local<v8::Array>::Cast(args[1]); |
| 72 uint32_t count = placeholders->Length(); | 73 uint32_t count = placeholders->Length(); |
| 73 if (count > 9) | 74 if (count > 9) |
| 74 return; | 75 return; |
| 75 for (uint32_t i = 0; i < count; ++i) { | 76 for (uint32_t i = 0; i < count; ++i) { |
| 76 substitutions.push_back(*v8::String::Utf8Value(placeholders->Get( | 77 v8::Local<v8::Value> param; |
| 77 v8::Integer::New(isolate, i)))); | 78 if (!placeholders->Get(isolate->GetCurrentContext(), |
| 79 v8::Integer::New(isolate, i)) |
| 80 .ToLocal(¶m)) |
| 81 return; |
| 82 substitutions.push_back(*v8::String::Utf8Value(param)); |
| 78 } | 83 } |
| 79 } else if (args[1]->IsString()) { | 84 } else if (args[1]->IsString()) { |
| 80 // chrome.i18n.getMessage("message_name", "one param"); | 85 // chrome.i18n.getMessage("message_name", "one param"); |
| 81 substitutions.push_back(*v8::String::Utf8Value(args[1])); | 86 substitutions.push_back(*v8::String::Utf8Value(args[1])); |
| 82 } | 87 } |
| 83 | 88 |
| 84 args.GetReturnValue().Set(v8::String::NewFromUtf8( | 89 std::string ret_value = |
| 85 isolate, | 90 ReplaceStringPlaceholders(message, substitutions, NULL); |
| 86 ReplaceStringPlaceholders(message, substitutions, NULL).c_str())); | 91 if (ret_value.size() >= v8::String::kMaxLength) |
| 92 return; |
| 93 args.GetReturnValue().Set(ToV8String(isolate, ret_value.c_str())); |
| 87 } | 94 } |
| 88 | 95 |
| 89 void I18NCustomBindings::GetL10nUILanguage( | 96 void I18NCustomBindings::GetL10nUILanguage( |
| 90 const v8::FunctionCallbackInfo<v8::Value>& args) { | 97 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 91 args.GetReturnValue().Set(v8::String::NewFromUtf8( | 98 args.GetReturnValue().Set(ToV8String( |
| 92 args.GetIsolate(), content::RenderThread::Get()->GetLocale().c_str())); | 99 args.GetIsolate(), content::RenderThread::Get()->GetLocale().c_str())); |
| 93 } | 100 } |
| 94 | 101 |
| 95 } // namespace extensions | 102 } // namespace extensions |
| OLD | NEW |