diff options
17 files changed, 147 insertions, 64 deletions
diff --git a/chrome/browser/extensions/extension_file_util.cc b/chrome/browser/extensions/extension_file_util.cc index fffa85d..6e84e81 100644 --- a/chrome/browser/extensions/extension_file_util.cc +++ b/chrome/browser/extensions/extension_file_util.cc @@ -395,7 +395,7 @@ void GarbageCollectExtensions( ExtensionMessageBundle* LoadExtensionMessageBundle( const FilePath& extension_path, - const DictionaryValue& manifest, + const std::string& default_locale, std::string* error) { error->clear(); // Load locale information if available. @@ -407,8 +407,6 @@ ExtensionMessageBundle* LoadExtensionMessageBundle( if (!extension_l10n_util::GetValidLocales(locale_path, &locales, error)) return NULL; - std::string default_locale = - extension_l10n_util::GetDefaultLocaleFromManifest(manifest, error); if (default_locale.empty() || locales.find(default_locale) == locales.end()) { *error = extension_manifest_errors::kLocalesNoDefaultLocaleSpecified; diff --git a/chrome/browser/extensions/extension_file_util.h b/chrome/browser/extensions/extension_file_util.h index 95f01e5..e6c88ff 100644 --- a/chrome/browser/extensions/extension_file_util.h +++ b/chrome/browser/extensions/extension_file_util.h @@ -90,7 +90,7 @@ void GarbageCollectExtensions( // Returns NULL on error, or if extension is not localized. ExtensionMessageBundle* LoadExtensionMessageBundle( const FilePath& extension_path, - const DictionaryValue& manifest, + const std::string& default_locale, std::string* error); // We need to reserve the namespace of entries that start with "_" for future diff --git a/chrome/browser/extensions/extension_file_util_unittest.cc b/chrome/browser/extensions/extension_file_util_unittest.cc index 4184272..5b6cd71 100644 --- a/chrome/browser/extensions/extension_file_util_unittest.cc +++ b/chrome/browser/extensions/extension_file_util_unittest.cc @@ -140,7 +140,6 @@ TEST(ExtensionFileUtil, LoadExtensionWithoutLocalesFolder) { scoped_ptr<Extension> extension( extension_file_util::LoadExtension(install_dir, false, &error)); ASSERT_FALSE(extension == NULL); - EXPECT_TRUE(NULL == extension->message_bundle()); EXPECT_TRUE(error.empty()); } diff --git a/chrome/browser/extensions/extension_prefs.h b/chrome/browser/extensions/extension_prefs.h index 0b2e4d9..0488545 100644 --- a/chrome/browser/extensions/extension_prefs.h +++ b/chrome/browser/extensions/extension_prefs.h @@ -9,6 +9,7 @@ #include <string> #include <vector> +#include "base/linked_ptr.h" #include "base/task.h" #include "chrome/common/extensions/extension.h" #include "chrome/common/pref_service.h" diff --git a/chrome/browser/extensions/extension_process_manager.cc b/chrome/browser/extensions/extension_process_manager.cc index 4a2b6d1..76e2a6e 100644 --- a/chrome/browser/extensions/extension_process_manager.cc +++ b/chrome/browser/extensions/extension_process_manager.cc @@ -167,12 +167,6 @@ void ExtensionProcessManager::RegisterExtensionProcess( RenderProcessHost* rph = RenderProcessHost::FromID(process_id); rph->Send(new ViewMsg_Extension_UpdatePageActions(extension_id, page_action_ids)); - - // Send l10n messages to the renderer - if there are any. - if (extension->message_bundle()) { - rph->Send(new ViewMsg_Extension_SetL10nMessages( - extension_id, *extension->message_bundle()->dictionary())); - } } void ExtensionProcessManager::UnregisterExtensionProcess(int process_id) { diff --git a/chrome/browser/extensions/extensions_service.cc b/chrome/browser/extensions/extensions_service.cc index 2920ea6..2e26261 100644 --- a/chrome/browser/extensions/extensions_service.cc +++ b/chrome/browser/extensions/extensions_service.cc @@ -454,7 +454,8 @@ void ExtensionsService::NotifyExtensionLoaded(Extension* extension) { context_getter, &ChromeURLRequestContextGetter::OnNewExtensions, extension->id(), - extension->path())); + extension->path(), + extension->default_locale())); } } diff --git a/chrome/browser/net/chrome_url_request_context.cc b/chrome/browser/net/chrome_url_request_context.cc index 3873748..998afc7 100644 --- a/chrome/browser/net/chrome_url_request_context.cc +++ b/chrome/browser/net/chrome_url_request_context.cc @@ -526,9 +526,11 @@ void ChromeURLRequestContextGetter::CleanupOnUIThread() { } } -void ChromeURLRequestContextGetter::OnNewExtensions(const std::string& id, - const FilePath& path) { - GetIOContext()->OnNewExtensions(id, path); +void ChromeURLRequestContextGetter::OnNewExtensions( + const std::string& id, + const FilePath& path, + const std::string default_locale) { + GetIOContext()->OnNewExtensions(id, path, default_locale); } void ChromeURLRequestContextGetter::OnUnloadedExtension( @@ -673,6 +675,16 @@ FilePath ChromeURLRequestContext::GetPathForExtension(const std::string& id) { } } +std::string ChromeURLRequestContext::GetDefaultLocaleForExtension( + const std::string& id) { + ExtensionDefaultLocales::iterator iter = extension_default_locales_.find(id); + std::string result; + if (iter != extension_default_locales_.end()) + result = iter->second; + + return result; +} + const std::string& ChromeURLRequestContext::GetUserAgent( const GURL& url) const { return webkit_glue::GetUserAgent(url); @@ -725,10 +737,15 @@ const Blacklist* ChromeURLRequestContext::GetBlacklist() const { return blacklist_manager_->GetCompiledBlacklist(); } -void ChromeURLRequestContext::OnNewExtensions(const std::string& id, - const FilePath& path) { - if (!is_off_the_record_) +void ChromeURLRequestContext::OnNewExtensions( + const std::string& id, + const FilePath& path, + const std::string& default_locale) { + if (!is_off_the_record_) { extension_paths_[id] = path; + if (!default_locale.empty()) + extension_default_locales_[id] = default_locale; + } } void ChromeURLRequestContext::OnUnloadedExtension(const std::string& id) { @@ -738,6 +755,8 @@ void ChromeURLRequestContext::OnUnloadedExtension(const std::string& id) { ExtensionPaths::iterator iter = extension_paths_.find(id); DCHECK(iter != extension_paths_.end()); extension_paths_.erase(iter); + + extension_default_locales_.erase(id); } ChromeURLRequestContext::ChromeURLRequestContext( @@ -851,6 +870,8 @@ ChromeURLRequestContextFactory::ChromeURLRequestContextFactory(Profile* profile) for (ExtensionList::const_iterator iter = extensions->begin(); iter != extensions->end(); ++iter) { extension_paths_[(*iter)->id()] = (*iter)->path(); + if (!(*iter)->default_locale().empty()) + extension_default_locales_[(*iter)->id()] = (*iter)->default_locale(); } } @@ -877,6 +898,7 @@ void ChromeURLRequestContextFactory::ApplyProfileParametersToContext( context->set_referrer_charset(referrer_charset_); context->set_cookie_policy_type(cookie_policy_type_); context->set_extension_paths(extension_paths_); + context->set_extension_default_locales(extension_default_locales_); context->set_user_script_dir_path(user_script_dir_path_); context->set_host_zoom_map(host_zoom_map_); context->set_blacklist_manager(blacklist_manager_.get()); diff --git a/chrome/browser/net/chrome_url_request_context.h b/chrome/browser/net/chrome_url_request_context.h index 5d697fc..060626e 100644 --- a/chrome/browser/net/chrome_url_request_context.h +++ b/chrome/browser/net/chrome_url_request_context.h @@ -88,9 +88,11 @@ class ChromeURLRequestContextGetter : public URLRequestContextGetter, // thread before the instance is deleted on the IO thread. void CleanupOnUIThread(); - // These methods simply forward to the corresponding method on + // These methods simply forward to the corresponding methods on // ChromeURLRequestContext. - void OnNewExtensions(const std::string& id, const FilePath& path); + void OnNewExtensions(const std::string& id, + const FilePath& path, + const std::string default_locale); void OnUnloadedExtension(const std::string& id); // NotificationObserver implementation. @@ -146,12 +148,17 @@ class ChromeURLRequestContextGetter : public URLRequestContextGetter, class ChromeURLRequestContext : public URLRequestContext { public: typedef std::map<std::string, FilePath> ExtensionPaths; + typedef std::map<std::string, std::string> ExtensionDefaultLocales; ChromeURLRequestContext(); // Gets the path to the directory for the specified extension. FilePath GetPathForExtension(const std::string& id); + // Returns an empty string if the extension with |id| doesn't have a default + // locale. + std::string GetDefaultLocaleForExtension(const std::string& id); + // Gets the path to the directory user scripts are stored in. FilePath user_script_dir_path() const { return user_script_dir_path_; @@ -173,6 +180,10 @@ class ChromeURLRequestContext : public URLRequestContext { return extension_paths_; } + const ExtensionDefaultLocales& extension_default_locales() const { + return extension_default_locales_; + } + virtual const std::string& GetUserAgent(const GURL& url) const; virtual bool InterceptCookie(const URLRequest* request, std::string* cookie); @@ -185,7 +196,9 @@ class ChromeURLRequestContext : public URLRequestContext { const Blacklist* GetBlacklist() const; // Callback for when new extensions are loaded. - void OnNewExtensions(const std::string& id, const FilePath& path); + void OnNewExtensions(const std::string& id, + const FilePath& path, + const std::string& default_locale); // Callback for when an extension is unloaded. void OnUnloadedExtension(const std::string& id); @@ -246,6 +259,9 @@ class ChromeURLRequestContext : public URLRequestContext { void set_extension_paths(const ExtensionPaths& paths) { extension_paths_ = paths; } + void set_extension_default_locales(const ExtensionDefaultLocales& locales) { + extension_default_locales_ = locales; + } void set_host_zoom_map(HostZoomMap* host_zoom_map) { host_zoom_map_ = host_zoom_map; } @@ -265,9 +281,14 @@ class ChromeURLRequestContext : public URLRequestContext { protected: // Maps extension IDs to paths on disk. This is initialized in the - // construtor and updated when extensions changed. + // constructor and updated when extensions changed. ExtensionPaths extension_paths_; + // Maps extension IDs to default locales. This is initialized in the + // constructor and updated when extensions change. Only extensions that + // have default_locale set are inserted. + ExtensionDefaultLocales extension_default_locales_; + // Path to the directory user scripts are stored in. FilePath user_script_dir_path_; @@ -318,6 +339,7 @@ class ChromeURLRequestContextFactory { std::string referrer_charset_; net::CookiePolicy::Type cookie_policy_type_; ChromeURLRequestContext::ExtensionPaths extension_paths_; + ChromeURLRequestContext::ExtensionDefaultLocales extension_default_locales_; FilePath user_script_dir_path_; scoped_refptr<HostZoomMap> host_zoom_map_; scoped_refptr<BlacklistManager> blacklist_manager_; diff --git a/chrome/browser/renderer_host/resource_message_filter.cc b/chrome/browser/renderer_host/resource_message_filter.cc index 1f623a3..2bcccb2 100644 --- a/chrome/browser/renderer_host/resource_message_filter.cc +++ b/chrome/browser/renderer_host/resource_message_filter.cc @@ -15,6 +15,7 @@ #include "chrome/browser/child_process_security_policy.h" #include "chrome/browser/chrome_plugin_browsing_context.h" #include "chrome/browser/chrome_thread.h" +#include "chrome/browser/extensions/extension_file_util.h" #include "chrome/browser/extensions/extension_message_service.h" #include "chrome/browser/host_zoom_map.h" #include "chrome/browser/in_process_webkit/dom_storage_dispatcher_host.h" @@ -39,6 +40,8 @@ #include "chrome/common/chrome_plugin_lib.h" #include "chrome/common/chrome_plugin_util.h" #include "chrome/common/chrome_switches.h" +#include "chrome/common/extensions/extension_constants.h" +#include "chrome/common/extensions/extension_message_bundle.h" #include "chrome/common/histogram_synchronizer.h" #include "chrome/common/notification_service.h" #include "chrome/common/pref_names.h" @@ -388,10 +391,11 @@ bool ResourceMessageFilter::OnMessageReceived(const IPC::Message& msg) { IPC_MESSAGE_HANDLER(ViewHostMsg_SetCacheMode, OnSetCacheMode) IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_GetFileSize, OnGetFileSize) IPC_MESSAGE_HANDLER(ViewHostMsg_Keygen, OnKeygen) + IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_GetExtensionMessageBundle, + OnGetExtensionMessageBundle) #if defined(USE_TCMALLOC) IPC_MESSAGE_HANDLER(ViewHostMsg_RendererTcmalloc, OnRendererTcmalloc) #endif - IPC_MESSAGE_UNHANDLED( handled = false) IPC_END_MESSAGE_MAP_EX() @@ -1179,3 +1183,42 @@ void ResourceMessageFilter::OnRendererTcmalloc(base::ProcessId pid, NewRunnableFunction(AboutTcmallocRendererCallback, pid, output)); } #endif + +void ResourceMessageFilter::OnGetExtensionMessageBundle( + const std::string& extension_id, IPC::Message* reply_msg) { + ChromeURLRequestContext* context = static_cast<ChromeURLRequestContext*>( + request_context_->GetURLRequestContext()); + + FilePath extension_path = context->GetPathForExtension(extension_id); + std::string default_locale = + context->GetDefaultLocaleForExtension(extension_id); + + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, + NewRunnableMethod( + this, &ResourceMessageFilter::OnGetExtensionMessageBundleOnFileThread, + extension_path, default_locale, reply_msg)); +} + +void ResourceMessageFilter::OnGetExtensionMessageBundleOnFileThread( + const FilePath& extension_path, + const std::string& default_locale, + IPC::Message* reply_msg) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); + + std::string error; + ExtensionMessageBundle* bundle = + extension_file_util::LoadExtensionMessageBundle( + extension_path, default_locale, &error); + + std::map<std::string, std::string> dictionary_map; + if (bundle) + dictionary_map = *bundle->dictionary(); + + ViewHostMsg_GetExtensionMessageBundle::WriteReplyParams( + reply_msg, dictionary_map); + + ChromeThread::PostTask( + ChromeThread::IO, FROM_HERE, + NewRunnableMethod(this, &ResourceMessageFilter::Send, reply_msg)); +} diff --git a/chrome/browser/renderer_host/resource_message_filter.h b/chrome/browser/renderer_host/resource_message_filter.h index b40dcb3..89db5d7 100644 --- a/chrome/browser/renderer_host/resource_message_filter.h +++ b/chrome/browser/renderer_host/resource_message_filter.h @@ -294,6 +294,13 @@ class ResourceMessageFilter : public IPC::ChannelProxy::MessageFilter, void OnGetFileSizeOnFileThread(const FilePath& path, IPC::Message* reply_msg); void OnKeygen(uint32 key_size_index, const std::string& challenge_string, const GURL& url, std::string* signed_public_key); + void OnGetExtensionMessageBundle(const std::string& extension_id, + IPC::Message* reply_msg); + void OnGetExtensionMessageBundleOnFileThread( + const FilePath& extension_path, + const std::string& default_locale, + IPC::Message* reply_msg); + #if defined(OS_LINUX) void SendDelayedReply(IPC::Message* reply_msg); void DoOnGetScreenInfo(gfx::NativeViewId view, IPC::Message* reply_msg); diff --git a/chrome/common/extensions/extension.h b/chrome/common/extensions/extension.h index 2fc25e5..52e3363 100644 --- a/chrome/common/extensions/extension.h +++ b/chrome/common/extensions/extension.h @@ -16,7 +16,6 @@ #include "base/version.h" #include "chrome/browser/extensions/user_script_master.h" #include "chrome/common/extensions/extension_action.h" -#include "chrome/common/extensions/extension_message_bundle.h" #include "chrome/common/extensions/extension_resource.h" #include "chrome/common/extensions/user_script.h" #include "chrome/common/extensions/url_pattern.h" @@ -276,14 +275,6 @@ class Extension { return manifest_value_.get(); } - // Getter/setter for l10n message bundle. - const ExtensionMessageBundle* message_bundle() const { - return message_bundle_.get(); - } - void set_message_bundle(ExtensionMessageBundle* message_bundle) { - message_bundle_.reset(message_bundle); - } - const std::string default_locale() const { return default_locale_; } // Chrome URL overrides (see ExtensionOverrideUI). @@ -415,9 +406,6 @@ class Extension { // A copy of the manifest that this extension was created from. scoped_ptr<DictionaryValue> manifest_value_; - // Handles the l10n messages replacement and parsing. - scoped_ptr<ExtensionMessageBundle> message_bundle_; - // Default locale for fall back. Can be empty if extension is not localized. std::string default_locale_; diff --git a/chrome/common/extensions/extension_l10n_util.cc b/chrome/common/extensions/extension_l10n_util.cc index 9d0510e..f315d92 100644 --- a/chrome/common/extensions/extension_l10n_util.cc +++ b/chrome/common/extensions/extension_l10n_util.cc @@ -37,12 +37,12 @@ void SetProcessLocale(const std::string& locale) { std::string GetDefaultLocaleFromManifest(const DictionaryValue& manifest, std::string* error) { std::string default_locale; - if (!manifest.GetString(keys::kDefaultLocale, &default_locale)) { - *error = errors::kInvalidDefaultLocale; - return ""; - } + if (manifest.GetString(keys::kDefaultLocale, &default_locale)) + return default_locale; + + *error = errors::kInvalidDefaultLocale; + return ""; - return default_locale; } bool ShouldRelocalizeManifest(const ExtensionInfo& info) { @@ -118,17 +118,18 @@ bool LocalizeManifest(const ExtensionMessageBundle& messages, bool LocalizeExtension(Extension* extension, DictionaryValue* manifest, std::string* error) { + DCHECK(manifest); + + std::string default_locale = GetDefaultLocaleFromManifest(*manifest, error); + ExtensionMessageBundle* message_bundle = extension_file_util::LoadExtensionMessageBundle(extension->path(), - *manifest, + default_locale, error); + if (!message_bundle && !error->empty()) return false; - // TODO(cira): remove ExtensionMessageBundle object from Extension class - // after we implement IPC that requests message bundles on demand. - extension->set_message_bundle(message_bundle); - if (message_bundle && !LocalizeManifest(*message_bundle, manifest, error)) return false; diff --git a/chrome/common/extensions/extension_l10n_util_unittest.cc b/chrome/common/extensions/extension_l10n_util_unittest.cc index 84e8a47..4da8b38 100644 --- a/chrome/common/extensions/extension_l10n_util_unittest.cc +++ b/chrome/common/extensions/extension_l10n_util_unittest.cc @@ -14,6 +14,7 @@ #include "chrome/common/extensions/extension.h" #include "chrome/common/extensions/extension_constants.h" #include "chrome/common/extensions/extension_l10n_util.h" +#include "chrome/common/extensions/extension_message_bundle.h" #include "testing/gtest/include/gtest/gtest.h" namespace errors = extension_manifest_errors; diff --git a/chrome/common/render_messages_internal.h b/chrome/common/render_messages_internal.h index 0051389..5772159 100644 --- a/chrome/common/render_messages_internal.h +++ b/chrome/common/render_messages_internal.h @@ -681,12 +681,6 @@ IPC_BEGIN_MESSAGES(View) std::string /* extension_id */, std::vector<std::string> /* page_action_ids */) - // Tell the renderer process all known localized messages for a particular - // extension. - IPC_MESSAGE_CONTROL2(ViewMsg_Extension_SetL10nMessages, - std::string /* extension_id */, - SubstitutionMap /* l10n messages */) - // Changes the text direction of the currently selected input field (if any). IPC_MESSAGE_ROUTED1(ViewMsg_SetTextDirection, WebKit::WebTextDirection /* direction */) @@ -1143,6 +1137,11 @@ IPC_BEGIN_MESSAGES(ViewHost) int32 /* page id */, std::wstring /*page contents */) + // Used to get the extension message bundle. + IPC_SYNC_MESSAGE_CONTROL1_1(ViewHostMsg_GetExtensionMessageBundle, + std::string /* extension id */, + SubstitutionMap /* message bundle */) + // Specifies the URL as the first parameter (a wstring) and thumbnail as // binary data as the second parameter. IPC_MESSAGE_ROUTED3(ViewHostMsg_Thumbnail, diff --git a/chrome/renderer/extensions/extension_process_bindings.cc b/chrome/renderer/extensions/extension_process_bindings.cc index e7aef4b..540469f 100644 --- a/chrome/renderer/extensions/extension_process_bindings.cc +++ b/chrome/renderer/extensions/extension_process_bindings.cc @@ -423,8 +423,26 @@ class ExtensionImpl : public ExtensionBase { return v8::Undefined(); L10nMessagesMap* l10n_messages = GetL10nMessagesMap(extension_id); - if (!l10n_messages) - return v8::Undefined(); + if (!l10n_messages) { + // Get the current RenderView so that we can send a routed IPC message + // from the correct source. + RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); + if (!renderview) + return v8::Undefined(); + + L10nMessagesMap messages; + // A sync call to load message catalogs for current extension. + renderview->Send(new ViewHostMsg_GetExtensionMessageBundle( + extension_id, &messages)); + + if (messages.empty()) + return v8::Undefined(); + + ExtensionProcessBindings::SetL10nMessages(extension_id, messages); + l10n_messages = GetL10nMessagesMap(extension_id); + if (!l10n_messages) + return v8::Undefined(); + } std::string message_name = *v8::String::AsciiValue(args[0]); std::string message = diff --git a/chrome/renderer/render_thread.cc b/chrome/renderer/render_thread.cc index df6dc42..d1ce79d 100644 --- a/chrome/renderer/render_thread.cc +++ b/chrome/renderer/render_thread.cc @@ -329,12 +329,6 @@ void RenderThread::OnDOMStorageEvent( params.storage_type_ == DOM_STORAGE_LOCAL); } -void RenderThread::OnExtensionSetL10nMessages( - const std::string& extension_id, - const std::map<std::string, std::string>& l10n_messages) { - ExtensionProcessBindings::SetL10nMessages(extension_id, l10n_messages); -} - void RenderThread::OnControlMessageReceived(const IPC::Message& msg) { // App cache messages are handled by a delegate. if (appcache_dispatcher_->OnMessageReceived(msg)) @@ -380,8 +374,6 @@ void RenderThread::OnControlMessageReceived(const IPC::Message& msg) { OnExtensionSetHostPermissions) IPC_MESSAGE_HANDLER(ViewMsg_DOMStorageEvent, OnDOMStorageEvent) - IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetL10nMessages, - OnExtensionSetL10nMessages) #if defined(IPC_MESSAGE_LOG_ENABLED) IPC_MESSAGE_HANDLER(ViewMsg_SetIPCLoggingEnabled, OnSetIPCLoggingEnabled) diff --git a/chrome/renderer/render_thread.h b/chrome/renderer/render_thread.h index 2b5693a..6769b06 100644 --- a/chrome/renderer/render_thread.h +++ b/chrome/renderer/render_thread.h @@ -169,9 +169,6 @@ class RenderThread : public RenderThreadBase, void OnExtensionSetHostPermissions( const GURL& extension_url, const std::vector<URLPattern>& permissions); - void OnExtensionSetL10nMessages( - const std::string& extension_id, - const std::map<std::string, std::string>& l10n_messages); void OnSetNextPageID(int32 next_page_id); void OnSetCSSColors(const std::vector<CSSColors::CSSColorMapping>& colors); void OnCreateNewView(gfx::NativeViewId parent_hwnd, |