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") {
"//components/sync",
"//components/sync_preferences",
"//components/sync_sessions",
"//components/translate/core/browser",
"//components/translate/content/browser",
"//components/undo",
"//components/unified_consent",
"//components/update_client",
......
......@@ -26,7 +26,6 @@
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.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_util.h"
#include "chrome/browser/extensions/api/tabs/windows_util.h"
......@@ -68,8 +67,6 @@
#include "components/zoom/zoom_controller.h"
#include "content/public/browser/navigation_controller.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_widget_host_view.h"
#include "content/public/browser/web_contents.h"
......@@ -1785,7 +1782,7 @@ ExtensionFunction::ResponseAction TabsDetectLanguageFunction::Run() {
Error(tabs_constants::kCannotDetermineLanguageOfUnloadedTab));
}
AddRef(); // Balanced in GotLanguage().
AddRef(); // Balanced in RespondWithLanguage().
ChromeTranslateClient* chrome_translate_client =
ChromeTranslateClient::FromWebContents(contents);
......@@ -1797,45 +1794,52 @@ ExtensionFunction::ResponseAction TabsDetectLanguageFunction::Run() {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(
&TabsDetectLanguageFunction::GotLanguage, this,
&TabsDetectLanguageFunction::RespondWithLanguage, this,
chrome_translate_client->GetLanguageState().original_language()));
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.
registrar_.Add(this, chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
content::Source<WebContents>(contents));
registrar_.Add(
this, chrome::NOTIFICATION_TAB_CLOSING,
content::Source<NavigationController>(&(contents->GetController())));
registrar_.Add(
this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<NavigationController>(&(contents->GetController())));
// Observe the WebContents' lifetime and navigations.
Observe(contents);
// Wait until the language is determined.
chrome_translate_client->translate_driver().AddObserver(this);
is_observing_ = true;
return RespondLater();
}
void TabsDetectLanguageFunction::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
std::string language;
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;
}
void TabsDetectLanguageFunction::NavigationEntryCommitted(
const content::LoadCommittedDetails& load_details) {
// 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());
}
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
// called for every API call the extension made.
GotLanguage(language);
void TabsDetectLanguageFunction::OnLanguageDetermined(
const translate::LanguageDetectionDetails& details) {
RespondWithLanguage(details.adopted_language);
}
void TabsDetectLanguageFunction::GotLanguage(const std::string& language) {
Respond(OneArgument(std::make_unique<base::Value>(language)));
void TabsDetectLanguageFunction::RespondWithLanguage(
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()
}
......
......@@ -11,9 +11,9 @@
#include "base/compiler_specific.h"
#include "chrome/browser/extensions/chrome_extension_function_details.h"
#include "chrome/common/extensions/api/tabs.h"
#include "components/translate/content/browser/content_translate_driver.h"
#include "components/zoom/zoom_controller.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/web_contents_observer.h"
#include "extensions/browser/api/execute_code_function.h"
#include "extensions/browser/api/web_contents_capture_client.h"
#include "extensions/browser/extension_function.h"
......@@ -180,17 +180,30 @@ class TabsRemoveFunction : public UIThreadExtensionFunction {
bool RemoveTab(int tab_id, std::string* error);
DECLARE_EXTENSION_FUNCTION("tabs.remove", TABS_REMOVE)
};
class TabsDetectLanguageFunction : public UIThreadExtensionFunction,
public content::NotificationObserver {
class TabsDetectLanguageFunction
: public UIThreadExtensionFunction,
public content::WebContentsObserver,
public translate::ContentTranslateDriver::Observer {
private:
~TabsDetectLanguageFunction() override {}
ResponseAction Run() override;
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
void GotLanguage(const std::string& language);
content::NotificationRegistrar registrar_;
// content::WebContentsObserver:
void NavigationEntryCommitted(
const content::LoadCommittedDetails& load_details) override;
void WebContentsDestroyed() override;
// 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)
};
......
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