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