diff options
author | sidchat@google.com <sidchat@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-10 16:41:27 +0000 |
---|---|---|
committer | sidchat@google.com <sidchat@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-10 16:41:27 +0000 |
commit | 5c4266928220b850dc96474953f0b3a29739e0d3 (patch) | |
tree | 0981198815b7d83ab1ccdb0fc358db8637618c05 /chrome/browser | |
parent | 479d5bc48969f6de924471de27638409db848229 (diff) | |
download | chromium_src-5c4266928220b850dc96474953f0b3a29739e0d3.zip chromium_src-5c4266928220b850dc96474953f0b3a29739e0d3.tar.gz chromium_src-5c4266928220b850dc96474953f0b3a29739e0d3.tar.bz2 |
Add getLanguage function to tab extension.
BUG=none
TEST=enable extensions using the toolstip.html code (added with this CL) and load pages in different languages. The corresponding language should appear in the bottom left after the page is loadedm or when the button is clicked, or when you navigate back to that tab after visiting some other tab.
Review URL: http://codereview.chromium.org/150062
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20378 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rwxr-xr-x | chrome/browser/DEPS | 1 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_browsertests_misc.cc | 24 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_function.h | 11 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_function_dispatcher.cc | 2 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_tabs_module.cc | 46 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_tabs_module.h | 12 | ||||
-rwxr-xr-x | chrome/browser/extensions/extension_tabs_module_constants.cc | 2 | ||||
-rwxr-xr-x | chrome/browser/extensions/extension_tabs_module_constants.h | 1 | ||||
-rw-r--r-- | chrome/browser/renderer_host/render_view_host.cc | 23 | ||||
-rw-r--r-- | chrome/browser/renderer_host/render_view_host.h | 7 | ||||
-rw-r--r-- | chrome/browser/tab_contents/tab_contents.cc | 4 | ||||
-rw-r--r-- | chrome/browser/tab_contents/tab_contents.h | 3 |
12 files changed, 129 insertions, 7 deletions
diff --git a/chrome/browser/DEPS b/chrome/browser/DEPS index a18a7a56..d5be2b8 100755 --- a/chrome/browser/DEPS +++ b/chrome/browser/DEPS @@ -17,6 +17,7 @@ include_rules = [ "+media/audio", # Chrome's lightweight audio library. "+third_party/sqlite", "+third_party/libevent", # For the remote V8 debugging server + "+third_party/cld", "+v8/include", # Browser uses V8 to get the version and run the debugger. # FIXME: this should probably not be here, we need to find a better diff --git a/chrome/browser/extensions/extension_browsertests_misc.cc b/chrome/browser/extensions/extension_browsertests_misc.cc index f17e5d5..4b8fb8f 100644 --- a/chrome/browser/extensions/extension_browsertests_misc.cc +++ b/chrome/browser/extensions/extension_browsertests_misc.cc @@ -43,10 +43,10 @@ static ExtensionHost* FindHostWithPath(ExtensionProcessManager* manager, // Tests that toolstrips initializes properly and can run basic extension js. IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, Toolstrip) { - ASSERT_TRUE(LoadExtension( - test_data_dir_.AppendASCII("good").AppendASCII("Extensions") - .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") - .AppendASCII("1.0.0.0"))); + FilePath extension_test_data_dir = test_data_dir_.AppendASCII("good"). + AppendASCII("Extensions").AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj"). + AppendASCII("1.0.0.0"); + ASSERT_TRUE(LoadExtension(extension_test_data_dir)); // At this point, there should be two ExtensionHosts loaded because this // extension has two toolstrips. Find the one that is hosting toolstrip1.html. @@ -59,6 +59,22 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, Toolstrip) { ui_test_utils::ExecuteJavaScriptAndExtractBool( host->render_view_host(), L"", L"testTabsAPI()", &result); EXPECT_TRUE(result); + +#if defined(OS_WIN) + // Test for compact language detection API. First navigate to a (static) html + // file with a French sentence. Then, run the test API in toolstrip1.html to + // actually call the language detection API through the existing extension, + // and verify that the language returned is indeed French. + FilePath language_url = extension_test_data_dir.AppendASCII( + "french_sentence.html"); + ui_test_utils::NavigateToURL( + browser(), + GURL(language_url.ToWStringHack())); + + ui_test_utils::ExecuteJavaScriptAndExtractBool( + host->render_view_host(), L"", L"testTabsLanguageAPI()", &result); + EXPECT_TRUE(result); +#endif } // Tests that the ExtensionShelf initializes properly, notices that diff --git a/chrome/browser/extensions/extension_function.h b/chrome/browser/extensions/extension_function.h index 7798a19..ed2f9d0 100644 --- a/chrome/browser/extensions/extension_function.h +++ b/chrome/browser/extensions/extension_function.h @@ -93,7 +93,14 @@ class AsyncExtensionFunction : public ExtensionFunction { virtual void SetArgs(const std::string& args); virtual const std::string GetResult(); virtual const std::string GetError() { return error_; } - virtual void Run() = 0; + virtual void Run() { + if (!RunImpl()) + SendResponse(false); + } + + // Derived classes should implement this method to do their work and return + // success/failure. + virtual bool RunImpl() = 0; protected: void SendResponse(bool success); @@ -107,7 +114,7 @@ class AsyncExtensionFunction : public ExtensionFunction { Value* args_; // The result of the API. This should be populated by the derived class before - // Run() returns. + // SendResponse() is called. scoped_ptr<Value> result_; // Any detailed error from the API. This should be populated by the derived diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index 70cb59e..8238595 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -98,6 +98,8 @@ void FactoryRegistry::ResetFunctions() { &NewExtensionFunction<MoveTabFunction>; factories_[tabs::kRemoveTabFunction] = &NewExtensionFunction<RemoveTabFunction>; + factories_[tabs::kGetTabLanguageFunction] = + &NewExtensionFunction<GetTabLanguageFunction>; // Page Actions. factories_[page_actions::kEnablePageActionFunction] = diff --git a/chrome/browser/extensions/extension_tabs_module.cc b/chrome/browser/extensions/extension_tabs_module.cc index b92677d..57248d7 100644 --- a/chrome/browser/extensions/extension_tabs_module.cc +++ b/chrome/browser/extensions/extension_tabs_module.cc @@ -615,6 +615,52 @@ bool RemoveTabFunction::RunImpl() { return true; } +bool GetTabLanguageFunction::RunImpl() { + int tab_id = 0; + Browser* browser = NULL; + TabContents* contents = NULL; + + // If |tab_id| is specified, look for it. Otherwise default to selected tab + // in the current window. + if (!args_->IsType(Value::TYPE_NULL)) { + EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id)); + if (!GetTabById(tab_id, profile(), &browser, NULL, &contents, NULL, + &error_)) { + return false; + } + if (!browser || !contents) + return false; + } else { + browser = dispatcher()->GetBrowser(); + if (!browser) + return false; + contents = browser->tabstrip_model()->GetSelectedTabContents(); + if (!contents) + return false; + } + + // Figure out what language |contents| contains. This sends an async call via + // the browser to the renderer to determine the language of the tab the + // renderer has. The renderer sends back the language of the tab after the + // tab loads (it may be delayed) to the browser, which in turn notifies this + // object that the language has been received. + contents->GetPageLanguage(); + registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, + NotificationService::AllSources()); + AddRef(); // balanced in Observe() + return true; +} + +void GetTabLanguageFunction::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + DCHECK(type == NotificationType::TAB_LANGUAGE_DETERMINED); + std::string language(*Details<std::string>(details).ptr()); + result_.reset(Value::CreateStringValue(language.c_str())); + SendResponse(true); + Release(); // balanced in Run() +} + // static helpers // if |populate| is true, each window gets a list property |tabs| which contains diff --git a/chrome/browser/extensions/extension_tabs_module.h b/chrome/browser/extensions/extension_tabs_module.h index cb8b3d3..1b7c307 100644 --- a/chrome/browser/extensions/extension_tabs_module.h +++ b/chrome/browser/extensions/extension_tabs_module.h @@ -8,6 +8,8 @@ #include <string> #include "chrome/browser/extensions/extension_function.h" +#include "chrome/common/notification_service.h" +#include "chrome/common/notification_registrar.h" class Browser; class DictionaryValue; @@ -86,5 +88,15 @@ class MoveTabFunction : public SyncExtensionFunction { class RemoveTabFunction : public SyncExtensionFunction { virtual bool RunImpl(); }; +class GetTabLanguageFunction : public AsyncExtensionFunction, + public NotificationObserver { + virtual bool RunImpl(); + + private: + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); + NotificationRegistrar registrar_; +}; #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TABS_MODULE_H__ diff --git a/chrome/browser/extensions/extension_tabs_module_constants.cc b/chrome/browser/extensions/extension_tabs_module_constants.cc index 049f36a..69676e7 100755 --- a/chrome/browser/extensions/extension_tabs_module_constants.cc +++ b/chrome/browser/extensions/extension_tabs_module_constants.cc @@ -56,6 +56,6 @@ const char kCreateTabFunction[] = "CreateTab"; const char kUpdateTabFunction[] = "UpdateTab"; const char kMoveTabFunction[] = "MoveTab"; const char kRemoveTabFunction[] = "RemoveTab"; - +const char kGetTabLanguageFunction[] = "GetTabLanguage"; } // namespace extension_tabs_module_constants diff --git a/chrome/browser/extensions/extension_tabs_module_constants.h b/chrome/browser/extensions/extension_tabs_module_constants.h index 10abcf7..4df84c8 100755 --- a/chrome/browser/extensions/extension_tabs_module_constants.h +++ b/chrome/browser/extensions/extension_tabs_module_constants.h @@ -64,6 +64,7 @@ extern const char kCreateTabFunction[]; extern const char kUpdateTabFunction[]; extern const char kMoveTabFunction[]; extern const char kRemoveTabFunction[]; +extern const char kGetTabLanguageFunction[]; }; // namespace extension_tabs_module_constants diff --git a/chrome/browser/renderer_host/render_view_host.cc b/chrome/browser/renderer_host/render_view_host.cc index 8e8f161..b41c04c 100644 --- a/chrome/browser/renderer_host/render_view_host.cc +++ b/chrome/browser/renderer_host/render_view_host.cc @@ -41,6 +41,8 @@ #if defined(OS_WIN) // TODO(port): accessibility not yet implemented. See http://crbug.com/8288. #include "chrome/browser/browser_accessibility_manager.h" +// TODO(port): The compact language detection library works only for Windows. +#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_unicodetext.h" #endif using base::TimeDelta; @@ -410,6 +412,10 @@ void RenderViewHost::StopFinding(bool clear_selection) { Send(new ViewMsg_StopFinding(routing_id(), clear_selection)); } +void RenderViewHost::GetPageLanguage() { + Send(new ViewMsg_DeterminePageText(routing_id())); +} + void RenderViewHost::Zoom(PageZoom::Function function) { Send(new ViewMsg_Zoom(routing_id(), function)); } @@ -726,6 +732,8 @@ void RenderViewHost::OnMessageReceived(const IPC::Message& msg) { IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError, OnMsgDidFailProvisionalLoadWithError) IPC_MESSAGE_HANDLER(ViewHostMsg_Find_Reply, OnMsgFindReply) + IPC_MESSAGE_HANDLER(ViewMsg_DeterminePageText_Reply, + OnDeterminePageTextReply) IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateFavIconURL, OnMsgUpdateFavIconURL) IPC_MESSAGE_HANDLER(ViewHostMsg_DidDownloadFavIcon, OnMsgDidDownloadFavIcon) IPC_MESSAGE_HANDLER(ViewHostMsg_ContextMenu, OnMsgContextMenu) @@ -1058,6 +1066,21 @@ void RenderViewHost::OnMsgFindReply(int request_id, Send(new ViewMsg_FindReplyACK(routing_id())); } +void RenderViewHost::OnDeterminePageTextReply( + const std::wstring& page_text) { +#if defined(OS_WIN) // Only for windows. + int num_languages = 0; + bool is_reliable = false; + const char* language_char = LanguageName(DetectLanguageOfUnicodeText( + page_text.c_str(), true, &is_reliable, &num_languages, NULL)); + std::string language(language_char); + NotificationService::current()->Notify( + NotificationType::TAB_LANGUAGE_DETERMINED, + Source<RenderViewHost>(this), + Details<std::string>(&language)); +#endif +} + void RenderViewHost::OnMsgUpdateFavIconURL(int32 page_id, const GURL& icon_url) { RenderViewHostDelegate::FavIcon* favicon_delegate = diff --git a/chrome/browser/renderer_host/render_view_host.h b/chrome/browser/renderer_host/render_view_host.h index de4c70c..a43c5a2 100644 --- a/chrome/browser/renderer_host/render_view_host.h +++ b/chrome/browser/renderer_host/render_view_host.h @@ -206,6 +206,12 @@ class RenderViewHost : public RenderWidgetHost, // clear the selection on the focused frame. void StopFinding(bool clear_selection); + // Get the most probable language of the text content in the tab. This sends + // a message to the render view to get the content of the page as text. The + // caller gets the language via the NotificationService by registering to the + // NotificationType TAB_LANGUAGE_DETERMINED. + void GetPageLanguage(); + // Change the zoom level of a page. void Zoom(PageZoom::Function function); @@ -460,6 +466,7 @@ class RenderViewHost : public RenderWidgetHost, const gfx::Rect& selection_rect, int active_match_ordinal, bool final_update); + void OnDeterminePageTextReply(const std::wstring& tab_text); void OnMsgUpdateFavIconURL(int32 page_id, const GURL& icon_url); void OnMsgDidDownloadFavIcon(int id, const GURL& image_url, diff --git a/chrome/browser/tab_contents/tab_contents.cc b/chrome/browser/tab_contents/tab_contents.cc index 001be58..9832fa4 100644 --- a/chrome/browser/tab_contents/tab_contents.cc +++ b/chrome/browser/tab_contents/tab_contents.cc @@ -976,6 +976,10 @@ void TabContents::StopFinding(bool clear_selection) { render_view_host()->StopFinding(clear_selection); } +void TabContents::GetPageLanguage() { + render_view_host()->GetPageLanguage(); +} + void TabContents::OnJavaScriptMessageBoxClosed(IPC::Message* reply_msg, bool success, const std::wstring& prompt) { diff --git a/chrome/browser/tab_contents/tab_contents.h b/chrome/browser/tab_contents/tab_contents.h index 99e99a1..e974f8e 100644 --- a/chrome/browser/tab_contents/tab_contents.h +++ b/chrome/browser/tab_contents/tab_contents.h @@ -493,6 +493,9 @@ class TabContents : public PageNavigator, return last_search_result_; } + // Get the most probable language of the text content in the tab. + void GetPageLanguage(); + // Misc state & callbacks ---------------------------------------------------- // Set whether the contents should block javascript message boxes or not. |