Commit 8fdb106e authored by Patrick Monette's avatar Patrick Monette Committed by Commit Bot

Remove usage of the NotificationService in TabsDetectLanguageFunction

Bug: 170921
Change-Id: If5a365004c7b9daac05132c6f2149e2abf49160e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1648708Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Commit-Queue: Patrick Monette <pmonette@chromium.org>
Cr-Commit-Position: refs/heads/master@{#668601}
parent cb1885d8
...@@ -839,7 +839,7 @@ jumbo_static_library("extensions") { ...@@ -839,7 +839,7 @@ jumbo_static_library("extensions") {
"//components/sync", "//components/sync",
"//components/sync_preferences", "//components/sync_preferences",
"//components/sync_sessions", "//components/sync_sessions",
"//components/translate/core/browser", "//components/translate/content/browser",
"//components/undo", "//components/undo",
"//components/unified_consent", "//components/unified_consent",
"//components/update_client", "//components/update_client",
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/tabs/tabs_constants.h" #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
#include "chrome/browser/extensions/api/tabs/tabs_util.h" #include "chrome/browser/extensions/api/tabs/tabs_util.h"
#include "chrome/browser/extensions/api/tabs/windows_util.h" #include "chrome/browser/extensions/api/tabs/windows_util.h"
...@@ -68,8 +67,6 @@ ...@@ -68,8 +67,6 @@
#include "components/zoom/zoom_controller.h" #include "components/zoom/zoom_controller.h"
#include "content/public/browser/navigation_controller.h" #include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h" #include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_widget_host_view.h" #include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
...@@ -1785,7 +1782,7 @@ ExtensionFunction::ResponseAction TabsDetectLanguageFunction::Run() { ...@@ -1785,7 +1782,7 @@ ExtensionFunction::ResponseAction TabsDetectLanguageFunction::Run() {
Error(tabs_constants::kCannotDetermineLanguageOfUnloadedTab)); Error(tabs_constants::kCannotDetermineLanguageOfUnloadedTab));
} }
AddRef(); // Balanced in GotLanguage(). AddRef(); // Balanced in RespondWithLanguage().
ChromeTranslateClient* chrome_translate_client = ChromeTranslateClient* chrome_translate_client =
ChromeTranslateClient::FromWebContents(contents); ChromeTranslateClient::FromWebContents(contents);
...@@ -1797,45 +1794,52 @@ ExtensionFunction::ResponseAction TabsDetectLanguageFunction::Run() { ...@@ -1797,45 +1794,52 @@ ExtensionFunction::ResponseAction TabsDetectLanguageFunction::Run() {
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, FROM_HERE,
base::BindOnce( base::BindOnce(
&TabsDetectLanguageFunction::GotLanguage, this, &TabsDetectLanguageFunction::RespondWithLanguage, this,
chrome_translate_client->GetLanguageState().original_language())); chrome_translate_client->GetLanguageState().original_language()));
return RespondLater(); return RespondLater();
} }
// The tab contents does not know its language yet. Let's wait until it
// The tab contents does not know its language yet. Let's wait until it
// receives it, or until the tab is closed/navigates to some other page. // receives it, or until the tab is closed/navigates to some other page.
registrar_.Add(this, chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
content::Source<WebContents>(contents)); // Observe the WebContents' lifetime and navigations.
registrar_.Add( Observe(contents);
this, chrome::NOTIFICATION_TAB_CLOSING, // Wait until the language is determined.
content::Source<NavigationController>(&(contents->GetController()))); chrome_translate_client->translate_driver().AddObserver(this);
registrar_.Add( is_observing_ = true;
this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<NavigationController>(&(contents->GetController())));
return RespondLater(); return RespondLater();
} }
void TabsDetectLanguageFunction::Observe( void TabsDetectLanguageFunction::NavigationEntryCommitted(
int type, const content::LoadCommittedDetails& load_details) {
const content::NotificationSource& source, // Call RespondWithLanguage() with an empty string as we want to guarantee the
const content::NotificationDetails& details) { // callback is called for every API call the extension made.
std::string language; RespondWithLanguage(std::string());
if (type == chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED) { }
const translate::LanguageDetectionDetails* lang_det_details =
content::Details<const translate::LanguageDetectionDetails>(details)
.ptr();
language = lang_det_details->adopted_language;
}
registrar_.RemoveAll(); void TabsDetectLanguageFunction::WebContentsDestroyed() {
// Call RespondWithLanguage() with an empty string as we want to guarantee the
// callback is called for every API call the extension made.
RespondWithLanguage(std::string());
}
// Call GotLanguage in all cases as we want to guarantee the callback is void TabsDetectLanguageFunction::OnLanguageDetermined(
// called for every API call the extension made. const translate::LanguageDetectionDetails& details) {
GotLanguage(language); RespondWithLanguage(details.adopted_language);
} }
void TabsDetectLanguageFunction::GotLanguage(const std::string& language) { void TabsDetectLanguageFunction::RespondWithLanguage(
Respond(OneArgument(std::make_unique<base::Value>(language))); const std::string& language) {
// Stop observing.
if (is_observing_) {
ChromeTranslateClient::FromWebContents(web_contents())
->translate_driver()
.RemoveObserver(this);
Observe(nullptr);
}
Respond(OneArgument(std::make_unique<base::Value>(language)));
Release(); // Balanced in Run() Release(); // Balanced in Run()
} }
......
...@@ -11,9 +11,9 @@ ...@@ -11,9 +11,9 @@
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "chrome/browser/extensions/chrome_extension_function_details.h" #include "chrome/browser/extensions/chrome_extension_function_details.h"
#include "chrome/common/extensions/api/tabs.h" #include "chrome/common/extensions/api/tabs.h"
#include "components/translate/content/browser/content_translate_driver.h"
#include "components/zoom/zoom_controller.h" #include "components/zoom/zoom_controller.h"
#include "content/public/browser/notification_observer.h" #include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "extensions/browser/api/execute_code_function.h" #include "extensions/browser/api/execute_code_function.h"
#include "extensions/browser/api/web_contents_capture_client.h" #include "extensions/browser/api/web_contents_capture_client.h"
#include "extensions/browser/extension_function.h" #include "extensions/browser/extension_function.h"
...@@ -180,17 +180,30 @@ class TabsRemoveFunction : public UIThreadExtensionFunction { ...@@ -180,17 +180,30 @@ class TabsRemoveFunction : public UIThreadExtensionFunction {
bool RemoveTab(int tab_id, std::string* error); bool RemoveTab(int tab_id, std::string* error);
DECLARE_EXTENSION_FUNCTION("tabs.remove", TABS_REMOVE) DECLARE_EXTENSION_FUNCTION("tabs.remove", TABS_REMOVE)
}; };
class TabsDetectLanguageFunction : public UIThreadExtensionFunction, class TabsDetectLanguageFunction
public content::NotificationObserver { : public UIThreadExtensionFunction,
public content::WebContentsObserver,
public translate::ContentTranslateDriver::Observer {
private: private:
~TabsDetectLanguageFunction() override {} ~TabsDetectLanguageFunction() override {}
ResponseAction Run() override; ResponseAction Run() override;
void Observe(int type, // content::WebContentsObserver:
const content::NotificationSource& source, void NavigationEntryCommitted(
const content::NotificationDetails& details) override; const content::LoadCommittedDetails& load_details) override;
void GotLanguage(const std::string& language); void WebContentsDestroyed() override;
content::NotificationRegistrar registrar_;
// translate::ContentTranslateDriver::Observer:
void OnLanguageDetermined(
const translate::LanguageDetectionDetails& details) override;
// Resolves the API call with the detected |language|.
void RespondWithLanguage(const std::string& language);
// Indicates if this instance is observing the tabs' WebContents and the
// ContentTranslateDriver, in which case the observers must be unregistered.
bool is_observing_ = false;
DECLARE_EXTENSION_FUNCTION("tabs.detectLanguage", TABS_DETECTLANGUAGE) DECLARE_EXTENSION_FUNCTION("tabs.detectLanguage", TABS_DETECTLANGUAGE)
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment