diff options
author | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-12 21:31:13 +0000 |
---|---|---|
committer | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-12 21:31:13 +0000 |
commit | 8c4cdd4cdae45e1fb81a8b3eaac9639abd6a2b30 (patch) | |
tree | ae17b06e0be7e6cad93e8f515a9e3d5d08dfa00c | |
parent | 4f0db14eee2abfc9d52707761a65912b76eac392 (diff) | |
download | chromium_src-8c4cdd4cdae45e1fb81a8b3eaac9639abd6a2b30.zip chromium_src-8c4cdd4cdae45e1fb81a8b3eaac9639abd6a2b30.tar.gz chromium_src-8c4cdd4cdae45e1fb81a8b3eaac9639abd6a2b30.tar.bz2 |
Moving the CLD code to the renderer so it runs sanboxed.
BUG=
TEST=Run the unit-tests.
Review URL: http://codereview.chromium.org/541013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@36045 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/extensions/extension_tabs_module.cc | 2 | ||||
-rw-r--r-- | chrome/browser/renderer_host/render_view_host.cc | 28 | ||||
-rw-r--r-- | chrome/browser/renderer_host/render_view_host.h | 8 | ||||
-rwxr-xr-x | chrome/chrome.gyp | 1 | ||||
-rwxr-xr-x | chrome/chrome_renderer.gypi | 4 | ||||
-rw-r--r-- | chrome/common/render_messages_internal.h | 14 | ||||
-rw-r--r-- | chrome/renderer/render_view.cc | 168 | ||||
-rw-r--r-- | chrome/renderer/render_view.h | 22 |
8 files changed, 118 insertions, 129 deletions
diff --git a/chrome/browser/extensions/extension_tabs_module.cc b/chrome/browser/extensions/extension_tabs_module.cc index 1e36294..edfd5f5 100644 --- a/chrome/browser/extensions/extension_tabs_module.cc +++ b/chrome/browser/extensions/extension_tabs_module.cc @@ -905,7 +905,7 @@ bool DetectTabLanguageFunction::RunImpl() { // object that the language has been received. contents->GetPageLanguage(); registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, - NotificationService::AllSources()); + Source<RenderViewHost>(contents->render_view_host())); AddRef(); // balanced in Observe() return true; } diff --git a/chrome/browser/renderer_host/render_view_host.cc b/chrome/browser/renderer_host/render_view_host.cc index fa2c4b2..5d46ccc 100644 --- a/chrome/browser/renderer_host/render_view_host.cc +++ b/chrome/browser/renderer_host/render_view_host.cc @@ -47,8 +47,6 @@ #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; @@ -425,7 +423,7 @@ void RenderViewHost::StopFinding(bool clear_selection) { } void RenderViewHost::GetPageLanguage() { - Send(new ViewMsg_DeterminePageText(routing_id())); + Send(new ViewMsg_DeterminePageLanguage(routing_id())); } void RenderViewHost::Zoom(PageZoom::Function function) { @@ -765,8 +763,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_PageLanguageDetermined, + OnPageLanguageDetermined) IPC_MESSAGE_HANDLER(ViewMsg_ExecuteCodeFinished, OnExecuteCodeFinished) IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateFavIconURL, OnMsgUpdateFavIconURL) @@ -1163,20 +1161,12 @@ 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_iso_code = LanguageCodeISO639_1( - DetectLanguageOfUnicodeText(NULL, page_text.c_str(), true, &is_reliable, - &num_languages, NULL)); - std::string language(language_iso_code); - NotificationService::current()->Notify( - NotificationType::TAB_LANGUAGE_DETERMINED, - Source<RenderViewHost>(this), - Details<std::string>(&language)); -#endif +void RenderViewHost::OnPageLanguageDetermined(const std::string& language) { + std::string lang(language); + NotificationService::current()->Notify( + NotificationType::TAB_LANGUAGE_DETERMINED, + Source<RenderViewHost>(this), + Details<std::string>(&lang)); } void RenderViewHost::OnExecuteCodeFinished(int request_id, bool success) { diff --git a/chrome/browser/renderer_host/render_view_host.h b/chrome/browser/renderer_host/render_view_host.h index 7c3dbb3..cfd1a77 100644 --- a/chrome/browser/renderer_host/render_view_host.h +++ b/chrome/browser/renderer_host/render_view_host.h @@ -212,9 +212,9 @@ 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 + // Gets the most probable language of the text content in the tab. (This sends + // a message to the render view.) The caller gets the language via the + // NotificationService by registering to the // NotificationType TAB_LANGUAGE_DETERMINED. void GetPageLanguage(); @@ -499,7 +499,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 OnPageLanguageDetermined(const std::string& language); void OnExecuteCodeFinished(int request_id, bool success); void OnMsgUpdateFavIconURL(int32 page_id, const GURL& icon_url); void OnMsgDidDownloadFavIcon(int id, diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index 61910b5..49003b4 100755 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -1199,7 +1199,6 @@ '../third_party/bsdiff/bsdiff.gyp:*', '../third_party/bspatch/bspatch.gyp:*', '../third_party/bzip2/bzip2.gyp:*', - '../third_party/cld/cld.gyp:cld', '../third_party/codesighs/codesighs.gyp:*', '../third_party/icu/icu.gyp:*', '../third_party/libjpeg/libjpeg.gyp:*', diff --git a/chrome/chrome_renderer.gypi b/chrome/chrome_renderer.gypi index 40a066a..b6a44b1 100755 --- a/chrome/chrome_renderer.gypi +++ b/chrome/chrome_renderer.gypi @@ -163,8 +163,12 @@ # Windows-specific rules. ['OS=="win"', { 'include_dirs': [ + '../third_party/cld', 'third_party/wtl/include', ], + 'dependencies': [ + '../third_party/cld/cld.gyp:cld', + ], 'conditions': [ ['win_use_allocator_shim==1', { 'dependencies': [ diff --git a/chrome/common/render_messages_internal.h b/chrome/common/render_messages_internal.h index b4e1e92..3ab66e2 100644 --- a/chrome/common/render_messages_internal.h +++ b/chrome/common/render_messages_internal.h @@ -257,13 +257,8 @@ IPC_BEGIN_MESSAGES(View) string16 /* search_text */, WebKit::WebFindOptions) - // Send from the browser to the rendered to get the text content of the page. - IPC_MESSAGE_ROUTED0(ViewMsg_DeterminePageText) - - // Send from the renderer to the browser to return the text content of the - // page. - IPC_MESSAGE_ROUTED1(ViewMsg_DeterminePageText_Reply, - std::wstring /* the language */) + // Asks the renderer for the language of the current page. + IPC_MESSAGE_ROUTED0(ViewMsg_DeterminePageLanguage) // Send from the renderer to the browser to return the script running result. IPC_MESSAGE_ROUTED2(ViewMsg_ExecuteCodeFinished, @@ -1939,6 +1934,11 @@ IPC_BEGIN_MESSAGES(ViewHost) string16 /* origin identifier */, string16 /* database name */) + // Notifies the browser of the language (ISO 639_1 code language, such as fr, + // en, zh...) of the current page. + IPC_MESSAGE_ROUTED1(ViewHostMsg_PageLanguageDetermined, + std::string /* the language */) + //--------------------------------------------------------------------------- // Socket Stream messages: // These are messages from the SocketStreamHandle to the browser. diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc index 2dc6b75..6371372 100644 --- a/chrome/renderer/render_view.cc +++ b/chrome/renderer/render_view.cc @@ -17,12 +17,10 @@ #include "base/command_line.h" #include "base/compiler_specific.h" #include "base/field_trial.h" -#include "base/histogram.h" #include "base/process_util.h" #include "base/singleton.h" #include "base/string_piece.h" #include "base/string_util.h" -#include "base/time.h" #include "build/build_config.h" #include "chrome/common/bindings_policy.h" #include "chrome/common/child_process_logging.h" @@ -66,6 +64,10 @@ #include "net/base/net_errors.h" #include "skia/ext/bitmap_platform_device.h" #include "skia/ext/image_operations.h" +#if defined(OS_WIN) +// 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 #include "third_party/WebKit/WebKit/chromium/public/WebAccessibilityCache.h" #include "third_party/WebKit/WebKit/chromium/public/WebAccessibilityObject.h" #include "third_party/WebKit/WebKit/chromium/public/WebCString.h" @@ -174,6 +176,7 @@ using WebKit::WebWorkerClient; // define to write the time necessary for thumbnail/DOM text retrieval, // respectively, into the system debug log +// #define TIME_BITMAP_RETRIEVAL // #define TIME_TEXT_RETRIEVAL // maximum number of characters in the document to index, any text beyond this @@ -213,6 +216,9 @@ static const char* const kUnreachableWebDataURL = static const char* const kBackForwardNavigationScheme = "history"; +// The string returned in DetectLanguage if we failed to detect the language. +static const char* const kUnknownLanguageCode = "unknown"; + static void GetRedirectChain(WebDataSource* ds, std::vector<GURL>* result) { WebVector<WebURL> urls; ds->redirectChain(urls); @@ -231,35 +237,6 @@ static bool UrlMatchesPermissions( return false; } -static bool PaintViewIntoCanvas(WebView* view, - skia::PlatformCanvas& canvas) { - view->layout(); - const WebSize& size = view->size(); - - if (!canvas.initialize(size.width, size.height, true)) - return false; - - view->paint(webkit_glue::ToWebCanvas(&canvas), - WebRect(0, 0, size.width, size.height)); - // TODO: Add a way to snapshot the whole page, not just the currently - // visible part. - - return true; -} - -// Calculates how "boring" a thumbnail is. The boring score is the -// 0,1 ranged percentage of pixels that are the most common -// luma. Higher boring scores indicate that a higher percentage of a -// bitmap are all the same brightness. -static double CalculateBoringScore(SkBitmap* bitmap) { - int histogram[256] = {0}; - color_utils::BuildLumaHistogram(bitmap, histogram); - - int color_count = *std::max_element(histogram, histogram + 256); - int pixel_count = bitmap->width() * bitmap->height(); - return static_cast<double>(color_count) / pixel_count; -} - /////////////////////////////////////////////////////////////////////////////// int32 RenderView::next_page_id_ = 1; @@ -452,7 +429,6 @@ void RenderView::OnMessageReceived(const IPC::Message& message) { IPC_BEGIN_MESSAGE_MAP(RenderView, message) IPC_MESSAGE_HANDLER(ViewMsg_CaptureThumbnail, SendThumbnail) - IPC_MESSAGE_HANDLER(ViewMsg_CaptureSnapshot, SendSnapshot) IPC_MESSAGE_HANDLER(ViewMsg_PrintPages, OnPrintPages) IPC_MESSAGE_HANDLER(ViewMsg_PrintingDone, OnPrintingDone) IPC_MESSAGE_HANDLER(ViewMsg_Navigate, OnNavigate) @@ -477,7 +453,7 @@ void RenderView::OnMessageReceived(const IPC::Message& message) { IPC_MESSAGE_HANDLER(ViewMsg_CopyImageAt, OnCopyImageAt) IPC_MESSAGE_HANDLER(ViewMsg_ExecuteEditCommand, OnExecuteEditCommand) IPC_MESSAGE_HANDLER(ViewMsg_Find, OnFind) - IPC_MESSAGE_HANDLER(ViewMsg_DeterminePageText, OnDeterminePageText) + IPC_MESSAGE_HANDLER(ViewMsg_DeterminePageLanguage, OnDeterminePageLanguage) IPC_MESSAGE_HANDLER(ViewMsg_Zoom, OnZoom) IPC_MESSAGE_HANDLER(ViewMsg_SetZoomLevelForLoadingHost, OnSetZoomLevelForLoadingHost) @@ -582,24 +558,6 @@ void RenderView::SendThumbnail() { Send(new ViewHostMsg_Thumbnail(routing_id_, url, score, thumbnail)); } -void RenderView::SendSnapshot() { - SkBitmap snapshot; - bool error = false; - - WebFrame* main_frame = webview()->mainFrame(); - if (!main_frame) - error = true; - - if (!error && !CaptureSnapshot(webview(), &snapshot)) - error = true; - - DCHECK(error == snapshot.empty()) << - "Snapshot should be empty on error, non-empty otherwise."; - - // Send the snapshot to the browser process. - Send(new ViewHostMsg_Snapshot(routing_id_, snapshot)); -} - void RenderView::OnPrintPages() { DCHECK(webview()); if (webview()) { @@ -660,10 +618,11 @@ void RenderView::CapturePageInfo(int load_id, bool preliminary_capture) { Send(new ViewHostMsg_PageContents(url, load_id, contents)); } - // Send over text content of this page to the browser. + // Now that we have the contents, we can determine the language if necessary. if (determine_page_text_after_loading_stops_) { determine_page_text_after_loading_stops_ = false; - Send(new ViewMsg_DeterminePageText_Reply(routing_id_, contents)); + Send(new ViewHostMsg_PageLanguageDetermined( + routing_id_, DetermineTextLanguage(contents))); } // thumbnail @@ -715,13 +674,18 @@ bool RenderView::CaptureThumbnail(WebView* view, int h, SkBitmap* thumbnail, ThumbnailScore* score) { - base::TimeTicks beginning_time = base::TimeTicks::Now(); +#ifdef TIME_BITMAP_RETRIEVAL + double begin = time_util::GetHighResolutionTimeNow(); +#endif - skia::PlatformCanvas canvas; + view->layout(); + const WebSize& size = view->size(); - // Paint |view| into |canvas|. - if (!PaintViewIntoCanvas(view, canvas)) + skia::PlatformCanvas canvas; + if (!canvas.initialize(size.width, size.height, true)) return false; + view->paint(webkit_glue::ToWebCanvas(&canvas), + WebRect(0, 0, size.width, size.height)); skia::BitmapPlatformDevice& device = static_cast<skia::BitmapPlatformDevice&>(canvas.getTopPlatformDevice()); @@ -769,28 +733,22 @@ bool RenderView::CaptureThumbnail(WebView* view, score->boring_score = CalculateBoringScore(thumbnail); - HISTOGRAM_TIMES("Renderer4.Thumbnail", - base::TimeTicks::Now() - beginning_time); +#ifdef TIME_BITMAP_RETRIEVAL + double end = time_util::GetHighResolutionTimeNow(); + char buf[128]; + sprintf_s(buf, "thumbnail in %gms\n", (end - begin) * 1000); + OutputDebugStringA(buf); +#endif return true; } -bool RenderView::CaptureSnapshot(WebView* view, SkBitmap* snapshot) { - base::TimeTicks beginning_time = base::TimeTicks::Now(); - - skia::PlatformCanvas canvas; - if (!PaintViewIntoCanvas(view, canvas)) - return false; - - skia::BitmapPlatformDevice& device = - static_cast<skia::BitmapPlatformDevice&>(canvas.getTopPlatformDevice()); - - const SkBitmap& bitmap = device.accessBitmap(false); - if (!bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config)) - return false; +double RenderView::CalculateBoringScore(SkBitmap* bitmap) { + int histogram[256] = {0}; + color_utils::BuildLumaHistogram(bitmap, histogram); - HISTOGRAM_TIMES("Renderer4.Snapshot", - base::TimeTicks::Now() - beginning_time); - return true; + int color_count = *std::max_element(histogram, histogram + 256); + int pixel_count = bitmap->width() * bitmap->height(); + return static_cast<double>(color_count) / pixel_count; } void RenderView::OnNavigate(const ViewMsg_Navigate_Params& params) { @@ -2724,21 +2682,21 @@ webkit_glue::WebPluginDelegate* RenderView::CreatePluginDelegate( } } if (in_process_plugin) { +#if defined(OS_WIN) // In-proc plugins aren't supported on Linux or Mac. if (use_pepper_host) { return WebPluginDelegatePepper::Create( path, *mime_type_to_use, AsWeakPtr(), - 0); + gfx::NativeViewFromId(host_window_)); } else { -#if defined(OS_WIN) // In-proc plugins aren't supported on Linux or Mac. return WebPluginDelegateImpl::Create( path, *mime_type_to_use, gfx::NativeViewFromId(host_window_)); + } #else - NOTIMPLEMENTED(); - return NULL; + NOTIMPLEMENTED(); + return NULL; #endif - } } return new WebPluginDelegateProxy(*mime_type_to_use, AsWeakPtr()); @@ -3032,21 +2990,47 @@ void RenderView::OnFind(int request_id, const string16& search_text, } } -void RenderView::OnDeterminePageText() { - if (!is_loading_) { - if (!webview()) - return; - WebFrame* main_frame = webview()->mainFrame(); - std::wstring contents; - CaptureText(main_frame, &contents); - Send(new ViewMsg_DeterminePageText_Reply(routing_id_, contents)); - determine_page_text_after_loading_stops_ = false; +void RenderView::OnDeterminePageLanguage() { + if (is_loading_) { + // Wait for the page to finish loading before trying to determine the + // language. + determine_page_text_after_loading_stops_ = true; return; } - // We set |determine_page_text_after_loading_stops_| true here so that, - // after page has been loaded completely, the text in the page is captured. - determine_page_text_after_loading_stops_ = true; + Send(new ViewHostMsg_PageLanguageDetermined(routing_id_, DetectLanguage())); +} + +std::string RenderView::DetectLanguage() { + if (!webview() || is_loading_) + return kUnknownLanguageCode; + + std::string language = kUnknownLanguageCode; +#if defined(OS_WIN) // CLD is only available on Windows at this time. + WebFrame* main_frame = webview()->mainFrame(); + std::wstring contents; + CaptureText(main_frame, &contents); + language = DetermineTextLanguage(contents); +#endif + + return language; +} + +// static +std::string RenderView::DetermineTextLanguage(const std::wstring& text) { + std::string language = kUnknownLanguageCode; +#if defined(OS_WIN) // CLD is only available on Windows at this time. + int num_languages = 0; + bool is_reliable = false; + Language cld_language = + DetectLanguageOfUnicodeText(NULL, text.c_str(), true, &is_reliable, + &num_languages, NULL); + if (cld_language != NUM_LANGUAGES && cld_language != UNKNOWN_LANGUAGE && + cld_language != TG_UNKNOWN_LANGUAGE) { + language = LanguageCodeISO639_1(cld_language); + } +#endif + return language; } void RenderView::DnsPrefetch(const std::vector<std::string>& host_names) { diff --git a/chrome/renderer/render_view.h b/chrome/renderer/render_view.h index 012c7cc..1174b3a 100644 --- a/chrome/renderer/render_view.h +++ b/chrome/renderer/render_view.h @@ -430,6 +430,11 @@ class RenderView : public RenderWidget, // UserScript::DOCUMENT_IDLE. void OnUserScriptIdleTriggered(WebKit::WebFrame* frame); + // Returns the ISO 639_1 language code of the current page + // (ex: en, fr, zh...). Returns 'unknown' if the language could not be + // determined. + std::string DetectLanguage(); + protected: // RenderWidget overrides: virtual void Close(); @@ -508,9 +513,11 @@ class RenderView : public RenderWidget, SkBitmap* thumbnail, ThumbnailScore* score); - // Capture a snapshot of a view. This is used to allow an extension - // to get a snapshot of a tab using chrome.tabs.captureVisibleTab(). - bool CaptureSnapshot(WebKit::WebView* view, SkBitmap* snapshot); + // Calculates how "boring" a thumbnail is. The boring score is the + // 0,1 ranged percentage of pixels that are the most common + // luma. Higher boring scores indicate that a higher percentage of a + // bitmap are all the same brightness. + double CalculateBoringScore(SkBitmap* bitmap); bool RunJavaScriptMessage(int type, const std::wstring& message, @@ -524,7 +531,6 @@ class RenderView : public RenderWidget, // RenderView IPC message handlers void SendThumbnail(); - void SendSnapshot(); void OnPrintPages(); void OnPrintingDone(int document_cookie, bool success); void OnNavigate(const ViewMsg_Navigate_Params& params); @@ -556,7 +562,7 @@ class RenderView : public RenderWidget, void OnSetupDevToolsClient(); void OnCancelDownload(int32 download_id); void OnFind(int request_id, const string16&, const WebKit::WebFindOptions&); - void OnDeterminePageText(); + void OnDeterminePageLanguage(); void OnZoom(PageZoom::Function function); void OnSetZoomLevelForLoadingHost(std::string host, int zoom_level); void OnSetPageEncoding(const std::string& encoding_name); @@ -749,6 +755,12 @@ class RenderView : public RenderWidget, // Starts nav_state_sync_timer_ if it isn't already running. void StartNavStateSyncTimerIfNecessary(); + // Returns the ISO 639_1 language code of the specified |text|, or 'unknown' + // if it failed. + // Note this only works on Windows at this time. It always returns 'unknown' + // on other platforms. + static std::string DetermineTextLanguage(const std::wstring& text); + // Bitwise-ORed set of extra bindings that have been enabled. See // BindingsPolicy for details. int enabled_bindings_; |