OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/renderer/extensions/i18n_custom_bindings.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "content/public/renderer/render_thread.h" | |
9 #include "content/public/renderer/render_view.h" | |
10 #include "extensions/common/extension_messages.h" | |
11 #include "extensions/common/message_bundle.h" | |
12 #include "grit/renderer_resources.h" | |
13 #include "v8/include/v8.h" | |
14 | |
15 namespace extensions { | |
16 | |
17 I18NCustomBindings::I18NCustomBindings(Dispatcher* dispatcher, | |
18 ChromeV8Context* context) | |
19 : ChromeV8Extension(dispatcher, context) { | |
20 RouteFunction("GetL10nMessage", | |
21 base::Bind(&I18NCustomBindings::GetL10nMessage, base::Unretained(this))); | |
22 RouteFunction("GetL10nUILanguage", | |
23 base::Bind(&I18NCustomBindings::GetL10nUILanguage, | |
24 base::Unretained(this))); | |
25 } | |
26 | |
27 void I18NCustomBindings::GetL10nMessage( | |
28 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
29 if (args.Length() != 3 || !args[0]->IsString()) { | |
30 NOTREACHED() << "Bad arguments"; | |
31 return; | |
32 } | |
33 | |
34 std::string extension_id; | |
35 if (args[2]->IsNull() || !args[2]->IsString()) { | |
36 return; | |
37 } else { | |
38 extension_id = *v8::String::Utf8Value(args[2]->ToString()); | |
39 if (extension_id.empty()) | |
40 return; | |
41 } | |
42 | |
43 L10nMessagesMap* l10n_messages = GetL10nMessagesMap(extension_id); | |
44 if (!l10n_messages) { | |
45 // Get the current RenderView so that we can send a routed IPC message | |
46 // from the correct source. | |
47 content::RenderView* renderview = GetRenderView(); | |
48 if (!renderview) | |
49 return; | |
50 | |
51 L10nMessagesMap messages; | |
52 // A sync call to load message catalogs for current extension. | |
53 renderview->Send(new ExtensionHostMsg_GetMessageBundle( | |
54 extension_id, &messages)); | |
55 | |
56 // Save messages we got. | |
57 ExtensionToL10nMessagesMap& l10n_messages_map = | |
58 *GetExtensionToL10nMessagesMap(); | |
59 l10n_messages_map[extension_id] = messages; | |
60 | |
61 l10n_messages = GetL10nMessagesMap(extension_id); | |
62 } | |
63 | |
64 std::string message_name = *v8::String::Utf8Value(args[0]); | |
65 std::string message = | |
66 MessageBundle::GetL10nMessage(message_name, *l10n_messages); | |
67 | |
68 v8::Isolate* isolate = args.GetIsolate(); | |
69 std::vector<std::string> substitutions; | |
70 if (args[1]->IsArray()) { | |
71 // chrome.i18n.getMessage("message_name", ["more", "params"]); | |
72 v8::Local<v8::Array> placeholders = v8::Local<v8::Array>::Cast(args[1]); | |
73 uint32_t count = placeholders->Length(); | |
74 if (count > 9) | |
75 return; | |
76 for (uint32_t i = 0; i < count; ++i) { | |
77 substitutions.push_back( | |
78 *v8::String::Utf8Value( | |
79 placeholders->Get(v8::Integer::New(isolate, i))->ToString())); | |
80 } | |
81 } else if (args[1]->IsString()) { | |
82 // chrome.i18n.getMessage("message_name", "one param"); | |
83 substitutions.push_back(*v8::String::Utf8Value(args[1]->ToString())); | |
84 } | |
85 | |
86 args.GetReturnValue().Set( | |
87 v8::String::NewFromUtf8(isolate, ReplaceStringPlaceholders( | |
88 message, substitutions, NULL).c_str())); | |
89 } | |
90 | |
91 void I18NCustomBindings::GetL10nUILanguage( | |
92 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
93 args.GetReturnValue().Set(v8::String::NewFromUtf8( | |
94 args.GetIsolate(), content::RenderThread::Get()->GetLocale().c_str())); | |
95 } | |
96 | |
97 } // namespace extensions | |
OLD | NEW |