| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 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/browser/cld_helper.h" | |
| 6 | |
| 7 #include "chrome/browser/chrome_thread.h" | |
| 8 #include "chrome/browser/tab_contents/tab_contents.h" | |
| 9 #include "chrome/common/notification_service.h" | |
| 10 | |
| 11 #if defined(OS_WIN) | |
| 12 // TODO(port): The compact language detection library works only for Windows. | |
| 13 #include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cl
d_unicodetext.h" | |
| 14 #endif | |
| 15 | |
| 16 CLDHelper::CLDHelper(TabContents* tab_contents, int renderer_process_id, | |
| 17 int page_id, const std::wstring& page_content) | |
| 18 : tab_contents_(tab_contents), | |
| 19 renderer_process_id_(renderer_process_id), | |
| 20 page_id_(page_id), | |
| 21 page_content_(page_content) { | |
| 22 } | |
| 23 | |
| 24 void CLDHelper::DetectLanguage() { | |
| 25 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | |
| 26 | |
| 27 // Balanced in DetectionDone. | |
| 28 AddRef(); | |
| 29 | |
| 30 // Do the actual work on the file thread. | |
| 31 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, | |
| 32 NewRunnableMethod(this, &CLDHelper::DoDetectLanguage)); | |
| 33 } | |
| 34 | |
| 35 void CLDHelper::CancelLanguageDetection() { | |
| 36 tab_contents_ = NULL; | |
| 37 } | |
| 38 | |
| 39 void CLDHelper::DoDetectLanguage() { | |
| 40 if (!tab_contents_) { | |
| 41 // If we have already been canceled, no need to perform the detection. | |
| 42 Release(); // Balance AddRef from DetectLanguage(). | |
| 43 return; | |
| 44 } | |
| 45 | |
| 46 #if defined(OS_WIN) // Only for windows. | |
| 47 int num_languages = 0; | |
| 48 bool is_reliable = false; | |
| 49 | |
| 50 const char* language_iso_code = LanguageCodeISO639_1( | |
| 51 DetectLanguageOfUnicodeText(NULL, page_content_.c_str(), true, | |
| 52 &is_reliable, &num_languages, NULL)); | |
| 53 language_ = language_iso_code; | |
| 54 #else | |
| 55 // TODO(jcampan): port the compact language detection library. | |
| 56 NOTIMPLEMENTED(); | |
| 57 #endif | |
| 58 | |
| 59 // Work is done, notify the RenderViewHost on the UI thread. | |
| 60 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, | |
| 61 NewRunnableMethod(this, &CLDHelper::DetectionDone)); | |
| 62 } | |
| 63 | |
| 64 void CLDHelper::DetectionDone() { | |
| 65 if (tab_contents_) | |
| 66 tab_contents_->PageLanguageDetected(); | |
| 67 | |
| 68 // Remove the ref we added to ourself in DetectLanguage(). | |
| 69 Release(); | |
| 70 } | |
| OLD | NEW |