Commit c9d02005 authored by Tommy C. Li's avatar Tommy C. Li Committed by Commit Bot

[omnibox] Combine AutocompleteController observer classes

This CL combines AutocompleteControllerDelegate and
OmniboxControllerEmitter::Observer classes into a single
AutocompleteController::Observer class.

This is because these two classes are essentially the same, both
observing the AutocompleteController.

In the future, we can also make OmniboxControllerEmitter itself an
instance of AutocompleteController::Observer, because it too, is
essentially an observer class that's a KeyedService.

The ultimate goal is to hook up the Android omnibox and NTP realbox
to the chrome://omnibox debug surfaces.

This refactor is to pave the way for that. See the bug for the full
engineering plan on how to get there.

Bug: 1058486
Change-Id: Ieb65cc2570a5af1ba417be6a87f4779bbe4710ce
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2088223Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarJustin Donnelly <jdonnelly@chromium.org>
Commit-Queue: Tommy Li <tommycli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#751822}
parent 2432631b
...@@ -36,7 +36,6 @@ ...@@ -36,7 +36,6 @@
#include "components/bookmarks/browser/bookmark_model.h" #include "components/bookmarks/browser/bookmark_model.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/omnibox/browser/autocomplete_classifier.h" #include "components/omnibox/browser/autocomplete_classifier.h"
#include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/browser/autocomplete_input.h" #include "components/omnibox/browser/autocomplete_input.h"
#include "components/omnibox/browser/autocomplete_match.h" #include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/autocomplete_match_type.h" #include "components/omnibox/browser/autocomplete_match_type.h"
...@@ -105,17 +104,13 @@ void RecordClipboardMetrics(AutocompleteMatchType::Type match_type) { ...@@ -105,17 +104,13 @@ void RecordClipboardMetrics(AutocompleteMatchType::Type match_type) {
* The prefetch occurs as a side-effect of calling OnOmniboxFocused() on * The prefetch occurs as a side-effect of calling OnOmniboxFocused() on
* the AutocompleteController object. * the AutocompleteController object.
*/ */
class ZeroSuggestPrefetcher : public AutocompleteControllerDelegate { class ZeroSuggestPrefetcher {
public: public:
explicit ZeroSuggestPrefetcher(Profile* profile); explicit ZeroSuggestPrefetcher(Profile* profile);
private: private:
~ZeroSuggestPrefetcher() override = default;
void SelfDestruct(); void SelfDestruct();
// AutocompleteControllerDelegate:
void OnResultChanged(bool default_match_changed) override;
std::unique_ptr<AutocompleteController> controller_; std::unique_ptr<AutocompleteController> controller_;
base::OneShotTimer expire_timer_; base::OneShotTimer expire_timer_;
}; };
...@@ -123,7 +118,7 @@ class ZeroSuggestPrefetcher : public AutocompleteControllerDelegate { ...@@ -123,7 +118,7 @@ class ZeroSuggestPrefetcher : public AutocompleteControllerDelegate {
ZeroSuggestPrefetcher::ZeroSuggestPrefetcher(Profile* profile) ZeroSuggestPrefetcher::ZeroSuggestPrefetcher(Profile* profile)
: controller_(new AutocompleteController( : controller_(new AutocompleteController(
std::make_unique<ChromeAutocompleteProviderClient>(profile), std::make_unique<ChromeAutocompleteProviderClient>(profile),
this, nullptr, // We only want to warm up the cache, don't need the result.
AutocompleteProvider::TYPE_ZERO_SUGGEST)) { AutocompleteProvider::TYPE_ZERO_SUGGEST)) {
AutocompleteInput input(base::string16(), metrics::OmniboxEventProto::NTP, AutocompleteInput input(base::string16(), metrics::OmniboxEventProto::NTP,
ChromeAutocompleteSchemeClassifier(profile)); ChromeAutocompleteSchemeClassifier(profile));
...@@ -140,12 +135,6 @@ void ZeroSuggestPrefetcher::SelfDestruct() { ...@@ -140,12 +135,6 @@ void ZeroSuggestPrefetcher::SelfDestruct() {
delete this; delete this;
} }
void ZeroSuggestPrefetcher::OnResultChanged(bool default_match_changed) {
// Nothing to do here, the results have been cached.
// We don't want to trigger deletion here because this is being called by the
// AutocompleteController object.
}
} // namespace } // namespace
AutocompleteControllerAndroid::AutocompleteControllerAndroid(Profile* profile) AutocompleteControllerAndroid::AutocompleteControllerAndroid(Profile* profile)
...@@ -400,7 +389,9 @@ void AutocompleteControllerAndroid::InitJNI(JNIEnv* env, jobject obj) { ...@@ -400,7 +389,9 @@ void AutocompleteControllerAndroid::InitJNI(JNIEnv* env, jobject obj) {
} }
void AutocompleteControllerAndroid::OnResultChanged( void AutocompleteControllerAndroid::OnResultChanged(
AutocompleteController* controller,
bool default_match_changed) { bool default_match_changed) {
DCHECK(controller == autocomplete_controller_.get());
if (autocomplete_controller_ && !inside_synchronous_start_) if (autocomplete_controller_ && !inside_synchronous_start_)
NotifySuggestionsReceived(autocomplete_controller_->result()); NotifySuggestionsReceived(autocomplete_controller_->result());
} }
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h" #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h" #include "components/keyed_service/core/keyed_service.h"
#include "components/omnibox/browser/autocomplete_controller_delegate.h" #include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/browser/autocomplete_input.h" #include "components/omnibox/browser/autocomplete_input.h"
#include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h" #include "content/public/browser/notification_registrar.h"
...@@ -26,7 +26,7 @@ class AutocompleteResult; ...@@ -26,7 +26,7 @@ class AutocompleteResult;
class Profile; class Profile;
// The native part of the Java AutocompleteController class. // The native part of the Java AutocompleteController class.
class AutocompleteControllerAndroid : public AutocompleteControllerDelegate, class AutocompleteControllerAndroid : public AutocompleteController::Observer,
public KeyedService { public KeyedService {
public: public:
explicit AutocompleteControllerAndroid(Profile* profile); explicit AutocompleteControllerAndroid(Profile* profile);
...@@ -112,8 +112,9 @@ class AutocompleteControllerAndroid : public AutocompleteControllerDelegate, ...@@ -112,8 +112,9 @@ class AutocompleteControllerAndroid : public AutocompleteControllerDelegate,
~AutocompleteControllerAndroid() override; ~AutocompleteControllerAndroid() override;
void InitJNI(JNIEnv* env, jobject obj); void InitJNI(JNIEnv* env, jobject obj);
// AutocompleteControllerDelegate implementation. // AutocompleteController::Observer implementation.
void OnResultChanged(bool default_match_changed) override; void OnResultChanged(AutocompleteController* controller,
bool default_match_changed) override;
// Notifies the Java AutocompleteController that suggestions were received // Notifies the Java AutocompleteController that suggestions were received
// based on the text the user typed in last. // based on the text the user typed in last.
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/vr/model/omnibox_suggestions.h" #include "chrome/browser/vr/model/omnibox_suggestions.h"
#include "components/omnibox/browser/autocomplete_classifier.h" #include "components/omnibox/browser/autocomplete_classifier.h"
#include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/browser/autocomplete_input.h" #include "components/omnibox/browser/autocomplete_input.h"
#include "components/search_engines/util.h" #include "components/search_engines/util.h"
...@@ -71,7 +70,11 @@ std::tuple<GURL, bool> AutocompleteController::GetUrlFromVoiceInput( ...@@ -71,7 +70,11 @@ std::tuple<GURL, bool> AutocompleteController::GetUrlFromVoiceInput(
false}; false};
} }
void AutocompleteController::OnResultChanged(bool default_match_changed) { void AutocompleteController::OnResultChanged(
::AutocompleteController* controller,
bool default_match_changed) {
DCHECK(controller == autocomplete_controller_.get());
std::vector<OmniboxSuggestion> suggestions; std::vector<OmniboxSuggestion> suggestions;
for (const auto& match : autocomplete_controller_->result()) { for (const auto& match : autocomplete_controller_->result()) {
const gfx::VectorIcon* icon = &match.GetVectorIcon(false); const gfx::VectorIcon* icon = &match.GetVectorIcon(false);
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/values.h" #include "base/values.h"
#include "chrome/browser/vr/model/omnibox_suggestions.h" #include "chrome/browser/vr/model/omnibox_suggestions.h"
#include "components/omnibox/browser/autocomplete_controller_delegate.h" #include "components/omnibox/browser/autocomplete_controller.h"
#include "url/gurl.h" #include "url/gurl.h"
class AutocompleteController; class AutocompleteController;
...@@ -21,7 +21,7 @@ class Profile; ...@@ -21,7 +21,7 @@ class Profile;
namespace vr { namespace vr {
class AutocompleteController : public AutocompleteControllerDelegate { class AutocompleteController : public ::AutocompleteController::Observer {
public: public:
using SuggestionCallback = using SuggestionCallback =
base::RepeatingCallback<void(std::vector<OmniboxSuggestion>)>; base::RepeatingCallback<void(std::vector<OmniboxSuggestion>)>;
...@@ -41,7 +41,9 @@ class AutocompleteController : public AutocompleteControllerDelegate { ...@@ -41,7 +41,9 @@ class AutocompleteController : public AutocompleteControllerDelegate {
std::tuple<GURL, bool> GetUrlFromVoiceInput(const base::string16& input); std::tuple<GURL, bool> GetUrlFromVoiceInput(const base::string16& input);
private: private:
void OnResultChanged(bool default_match_changed) override; // ::AutocompleteController::Observer:
void OnResultChanged(::AutocompleteController* controller,
bool default_match_changed) override;
Profile* profile_; Profile* profile_;
ChromeAutocompleteProviderClient* client_; ChromeAutocompleteProviderClient* client_;
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
#include "chrome/browser/ui/app_list/app_list_controller_delegate.h" #include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
#include "chrome/browser/ui/app_list/search/omnibox_result.h" #include "chrome/browser/ui/app_list/search/omnibox_result.h"
#include "components/omnibox/browser/autocomplete_classifier.h" #include "components/omnibox/browser/autocomplete_classifier.h"
#include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/browser/autocomplete_input.h" #include "components/omnibox/browser/autocomplete_input.h"
#include "third_party/metrics_proto/omnibox_event.pb.h" #include "third_party/metrics_proto/omnibox_event.pb.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -76,7 +75,10 @@ void OmniboxProvider::PopulateFromACResult(const AutocompleteResult& result) { ...@@ -76,7 +75,10 @@ void OmniboxProvider::PopulateFromACResult(const AutocompleteResult& result) {
SwapResults(&new_results); SwapResults(&new_results);
} }
void OmniboxProvider::OnResultChanged(bool default_match_changed) { void OmniboxProvider::OnResultChanged(AutocompleteController* controller,
bool default_match_changed) {
DCHECK(controller == controller_.get());
// Record the query latency. // Record the query latency.
RecordQueryLatencyHistogram(); RecordQueryLatencyHistogram();
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "chrome/browser/ui/app_list/search/search_provider.h" #include "chrome/browser/ui/app_list/search/search_provider.h"
#include "components/omnibox/browser/autocomplete_controller_delegate.h" #include "components/omnibox/browser/autocomplete_controller.h"
class AppListControllerDelegate; class AppListControllerDelegate;
class AutocompleteController; class AutocompleteController;
...@@ -20,7 +20,7 @@ namespace app_list { ...@@ -20,7 +20,7 @@ namespace app_list {
// OmniboxProvider wraps AutocompleteController to provide omnibox results. // OmniboxProvider wraps AutocompleteController to provide omnibox results.
class OmniboxProvider : public SearchProvider, class OmniboxProvider : public SearchProvider,
public AutocompleteControllerDelegate { public AutocompleteController::Observer {
public: public:
explicit OmniboxProvider(Profile* profile, explicit OmniboxProvider(Profile* profile,
AppListControllerDelegate* list_controller); AppListControllerDelegate* list_controller);
...@@ -33,8 +33,9 @@ class OmniboxProvider : public SearchProvider, ...@@ -33,8 +33,9 @@ class OmniboxProvider : public SearchProvider,
// Populates result list from AutocompleteResult. // Populates result list from AutocompleteResult.
void PopulateFromACResult(const AutocompleteResult& result); void PopulateFromACResult(const AutocompleteResult& result);
// AutocompleteControllerDelegate overrides: // AutocompleteController::Observer overrides:
void OnResultChanged(bool default_match_changed) override; void OnResultChanged(AutocompleteController* controller,
bool default_match_changed) override;
void RecordQueryLatencyHistogram(); void RecordQueryLatencyHistogram();
......
...@@ -449,7 +449,10 @@ void SearchTabHelper::FileSelectionCanceled(void* params) { ...@@ -449,7 +449,10 @@ void SearchTabHelper::FileSelectionCanceled(void* params) {
->LogEvent(NTP_BACKGROUND_UPLOAD_CANCEL, base::TimeDelta::FromSeconds(0)); ->LogEvent(NTP_BACKGROUND_UPLOAD_CANCEL, base::TimeDelta::FromSeconds(0));
} }
void SearchTabHelper::OnResultChanged(bool default_result_changed) { void SearchTabHelper::OnResultChanged(AutocompleteController* controller,
bool default_result_changed) {
DCHECK(controller == autocomplete_controller_.get());
if (!autocomplete_controller_) { if (!autocomplete_controller_) {
NOTREACHED(); NOTREACHED();
return; return;
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
#include "chrome/common/search/instant_types.h" #include "chrome/common/search/instant_types.h"
#include "chrome/common/search/ntp_logging_events.h" #include "chrome/common/search/ntp_logging_events.h"
#include "components/ntp_tiles/ntp_tile_impression.h" #include "components/ntp_tiles/ntp_tile_impression.h"
#include "components/omnibox/browser/autocomplete_controller_delegate.h" #include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/common/omnibox_focus_state.h" #include "components/omnibox/common/omnibox_focus_state.h"
#include "content/public/browser/reload_type.h" #include "content/public/browser/reload_type.h"
#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_observer.h"
...@@ -54,7 +54,7 @@ class SearchTabHelper : public content::WebContentsObserver, ...@@ -54,7 +54,7 @@ class SearchTabHelper : public content::WebContentsObserver,
public InstantServiceObserver, public InstantServiceObserver,
public SearchIPCRouter::Delegate, public SearchIPCRouter::Delegate,
public ui::SelectFileDialog::Listener, public ui::SelectFileDialog::Listener,
public AutocompleteControllerDelegate, public AutocompleteController::Observer,
public OmniboxTabHelper::Observer { public OmniboxTabHelper::Observer {
public: public:
~SearchTabHelper() override; ~SearchTabHelper() override;
...@@ -164,8 +164,9 @@ class SearchTabHelper : public content::WebContentsObserver, ...@@ -164,8 +164,9 @@ class SearchTabHelper : public content::WebContentsObserver,
void* params) override; void* params) override;
void FileSelectionCanceled(void* params) override; void FileSelectionCanceled(void* params) override;
// Overridden from AutocompleteControllerDelegate: // Overridden from AutocompleteController::Observer:
void OnResultChanged(bool default_match_changed) override; void OnResultChanged(AutocompleteController* controller,
bool default_match_changed) override;
// Overridden from OmniboxTabHelper::Observer: // Overridden from OmniboxTabHelper::Observer:
void OnOmniboxInputStateChanged() override; void OnOmniboxInputStateChanged() override;
......
...@@ -29,7 +29,6 @@ ...@@ -29,7 +29,6 @@
#include "components/history/core/browser/history_service.h" #include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/url_database.h" #include "components/history/core/browser/url_database.h"
#include "components/omnibox/browser/autocomplete_classifier.h" #include "components/omnibox/browser/autocomplete_classifier.h"
#include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/browser/autocomplete_match.h" #include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/autocomplete_provider.h" #include "components/omnibox/browser/autocomplete_provider.h"
#include "components/omnibox/browser/omnibox_controller_emitter.h" #include "components/omnibox/browser/omnibox_controller_emitter.h"
...@@ -205,23 +204,18 @@ OmniboxPageHandler::OmniboxPageHandler( ...@@ -205,23 +204,18 @@ OmniboxPageHandler::OmniboxPageHandler(
ResetController(); ResetController();
} }
OmniboxPageHandler::~OmniboxPageHandler() {} OmniboxPageHandler::~OmniboxPageHandler() = default;
void OmniboxPageHandler::OnResultChanged(bool default_match_changed) { void OmniboxPageHandler::OnStart(AutocompleteController* controller,
OnOmniboxResultChanged(default_match_changed, controller_.get()); const AutocompleteInput& input) {
}
void OmniboxPageHandler::OnOmniboxQuery(AutocompleteController* controller,
const AutocompleteInput& input) {
time_omnibox_started_ = base::Time::Now(); time_omnibox_started_ = base::Time::Now();
input_ = input; input_ = input;
page_->HandleNewAutocompleteQuery(controller == controller_.get(), page_->HandleNewAutocompleteQuery(controller == controller_.get(),
base::UTF16ToUTF8(input.text())); base::UTF16ToUTF8(input.text()));
} }
void OmniboxPageHandler::OnOmniboxResultChanged( void OmniboxPageHandler::OnResultChanged(AutocompleteController* controller,
bool default_match_changed, bool default_match_changed) {
AutocompleteController* controller) {
mojom::OmniboxResponsePtr response(mojom::OmniboxResponse::New()); mojom::OmniboxResponsePtr response(mojom::OmniboxResponse::New());
response->cursor_position = input_.cursor_position(); response->cursor_position = input_.cursor_position();
response->time_since_omnibox_started_ms = response->time_since_omnibox_started_ms =
...@@ -355,8 +349,7 @@ void OmniboxPageHandler::StartOmniboxQuery(const std::string& input_string, ...@@ -355,8 +349,7 @@ void OmniboxPageHandler::StartOmniboxQuery(const std::string& input_string,
input.set_keyword_mode_entry_method(metrics::OmniboxEventProto::TAB); input.set_keyword_mode_entry_method(metrics::OmniboxEventProto::TAB);
input.set_from_omnibox_focus(zero_suggest); input.set_from_omnibox_focus(zero_suggest);
OnOmniboxQuery(controller_.get(), input); controller_->Start(input);
controller_->Start(input_);
} }
void OmniboxPageHandler::ResetController() { void OmniboxPageHandler::ResetController() {
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/ui/webui/omnibox/omnibox.mojom.h" #include "chrome/browser/ui/webui/omnibox/omnibox.mojom.h"
#include "components/omnibox/browser/autocomplete_controller_delegate.h" #include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/browser/autocomplete_input.h" #include "components/omnibox/browser/autocomplete_input.h"
#include "components/omnibox/browser/autocomplete_match.h" #include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/omnibox_controller_emitter.h" #include "components/omnibox/browser/omnibox_controller_emitter.h"
...@@ -30,23 +30,19 @@ class Profile; ...@@ -30,23 +30,19 @@ class Profile;
// private AutocompleteController. It also listens for updates from the // private AutocompleteController. It also listens for updates from the
// AutocompleteController to OnResultChanged() and passes those results to // AutocompleteController to OnResultChanged() and passes those results to
// the OmniboxPage. // the OmniboxPage.
class OmniboxPageHandler : public AutocompleteControllerDelegate, class OmniboxPageHandler : public AutocompleteController::Observer,
public mojom::OmniboxPageHandler, public mojom::OmniboxPageHandler {
public OmniboxControllerEmitter::Observer {
public: public:
// OmniboxPageHandler is deleted when the supplied pipe is destroyed. // OmniboxPageHandler is deleted when the supplied pipe is destroyed.
OmniboxPageHandler(Profile* profile, OmniboxPageHandler(Profile* profile,
mojo::PendingReceiver<mojom::OmniboxPageHandler> receiver); mojo::PendingReceiver<mojom::OmniboxPageHandler> receiver);
~OmniboxPageHandler() override; ~OmniboxPageHandler() override;
// AutocompleteControllerDelegate overrides: // AutocompleteController::Observer overrides:
void OnResultChanged(bool default_match_changed) override; void OnStart(AutocompleteController* controller,
const AutocompleteInput& input) override;
// OmniboxControllerEmitter::Observer overrides: void OnResultChanged(AutocompleteController* controller,
void OnOmniboxQuery(AutocompleteController* controller, bool default_match_changed) override;
const AutocompleteInput& input) override;
void OnOmniboxResultChanged(bool default_match_changed,
AutocompleteController* controller) override;
// mojom::OmniboxPageHandler overrides: // mojom::OmniboxPageHandler overrides:
void SetClientPage(mojo::PendingRemote<mojom::OmniboxPage> page) override; void SetClientPage(mojo::PendingRemote<mojom::OmniboxPage> page) override;
...@@ -93,7 +89,7 @@ class OmniboxPageHandler : public AutocompleteControllerDelegate, ...@@ -93,7 +89,7 @@ class OmniboxPageHandler : public AutocompleteControllerDelegate,
mojo::Receiver<mojom::OmniboxPageHandler> receiver_; mojo::Receiver<mojom::OmniboxPageHandler> receiver_;
ScopedObserver<OmniboxControllerEmitter, OmniboxControllerEmitter::Observer> ScopedObserver<OmniboxControllerEmitter, AutocompleteController::Observer>
observer_; observer_;
base::WeakPtrFactory<OmniboxPageHandler> weak_factory_{this}; base::WeakPtrFactory<OmniboxPageHandler> weak_factory_{this};
......
...@@ -206,7 +206,7 @@ class BrowserChangeObserver : public BrowserListObserver { ...@@ -206,7 +206,7 @@ class BrowserChangeObserver : public BrowserListObserver {
DISALLOW_COPY_AND_ASSIGN(BrowserChangeObserver); DISALLOW_COPY_AND_ASSIGN(BrowserChangeObserver);
}; };
class AutocompleteChangeObserver : public OmniboxControllerEmitter::Observer { class AutocompleteChangeObserver : public AutocompleteController::Observer {
public: public:
explicit AutocompleteChangeObserver(Profile* profile) { explicit AutocompleteChangeObserver(Profile* profile) {
scoped_observer_.Add( scoped_observer_.Add(
...@@ -217,18 +217,16 @@ class AutocompleteChangeObserver : public OmniboxControllerEmitter::Observer { ...@@ -217,18 +217,16 @@ class AutocompleteChangeObserver : public OmniboxControllerEmitter::Observer {
void Wait() { run_loop_.Run(); } void Wait() { run_loop_.Run(); }
// OmniboxControllerEmitter::Observer: // AutocompleteController::Observer:
void OnOmniboxQuery(AutocompleteController* controller, void OnResultChanged(AutocompleteController* controller,
const AutocompleteInput& input) override {} bool default_match_changed) override {
void OnOmniboxResultChanged(bool default_match_changed,
AutocompleteController* controller) override {
if (run_loop_.running()) if (run_loop_.running())
run_loop_.Quit(); run_loop_.Quit();
} }
private: private:
base::RunLoop run_loop_; base::RunLoop run_loop_;
ScopedObserver<OmniboxControllerEmitter, OmniboxControllerEmitter::Observer> ScopedObserver<OmniboxControllerEmitter, AutocompleteController::Observer>
scoped_observer_{this}; scoped_observer_{this};
DISALLOW_COPY_AND_ASSIGN(AutocompleteChangeObserver); DISALLOW_COPY_AND_ASSIGN(AutocompleteChangeObserver);
......
...@@ -82,7 +82,6 @@ jumbo_static_library("browser") { ...@@ -82,7 +82,6 @@ jumbo_static_library("browser") {
"autocomplete_classifier.h", "autocomplete_classifier.h",
"autocomplete_controller.cc", "autocomplete_controller.cc",
"autocomplete_controller.h", "autocomplete_controller.h",
"autocomplete_controller_delegate.h",
"autocomplete_input.cc", "autocomplete_input.cc",
"autocomplete_input.h", "autocomplete_input.h",
"autocomplete_match.cc", "autocomplete_match.cc",
......
...@@ -30,7 +30,6 @@ ...@@ -30,7 +30,6 @@
#include "base/trace_event/memory_usage_estimator.h" #include "base/trace_event/memory_usage_estimator.h"
#include "base/trace_event/trace_event.h" #include "base/trace_event/trace_event.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "components/omnibox/browser/autocomplete_controller_delegate.h"
#include "components/omnibox/browser/bookmark_provider.h" #include "components/omnibox/browser/bookmark_provider.h"
#include "components/omnibox/browser/builtin_provider.h" #include "components/omnibox/browser/builtin_provider.h"
#include "components/omnibox/browser/clipboard_provider.h" #include "components/omnibox/browser/clipboard_provider.h"
...@@ -208,10 +207,9 @@ bool AutocompleteMatchHasCustomDescription(const AutocompleteMatch& match) { ...@@ -208,10 +207,9 @@ bool AutocompleteMatchHasCustomDescription(const AutocompleteMatch& match) {
AutocompleteController::AutocompleteController( AutocompleteController::AutocompleteController(
std::unique_ptr<AutocompleteProviderClient> provider_client, std::unique_ptr<AutocompleteProviderClient> provider_client,
AutocompleteControllerDelegate* delegate, Observer* observer,
int provider_types) int provider_types)
: delegate_(delegate), : provider_client_(std::move(provider_client)),
provider_client_(std::move(provider_client)),
document_provider_(nullptr), document_provider_(nullptr),
history_url_provider_(nullptr), history_url_provider_(nullptr),
keyword_provider_(nullptr), keyword_provider_(nullptr),
...@@ -224,6 +222,11 @@ AutocompleteController::AutocompleteController( ...@@ -224,6 +222,11 @@ AutocompleteController::AutocompleteController(
first_query_(true), first_query_(true),
search_service_worker_signal_sent_(false), search_service_worker_signal_sent_(false),
template_url_service_(provider_client_->GetTemplateURLService()) { template_url_service_(provider_client_->GetTemplateURLService()) {
// TODO(tommycli): We should make a separate AddObserver method on this class
// and take |observer| out of the constructor. Tests pass nullptr anyways.
if (observer)
observers_.AddObserver(observer);
provider_types &= ~OmniboxFieldTrial::GetDisabledProviderTypes(); provider_types &= ~OmniboxFieldTrial::GetDisabledProviderTypes();
if (provider_types & AutocompleteProvider::TYPE_BOOKMARK) if (provider_types & AutocompleteProvider::TYPE_BOOKMARK)
providers_.push_back(new BookmarkProvider(provider_client_.get())); providers_.push_back(new BookmarkProvider(provider_client_.get()));
...@@ -327,6 +330,14 @@ AutocompleteController::~AutocompleteController() { ...@@ -327,6 +330,14 @@ AutocompleteController::~AutocompleteController() {
void AutocompleteController::Start(const AutocompleteInput& input) { void AutocompleteController::Start(const AutocompleteInput& input) {
TRACE_EVENT1("omnibox", "AutocompleteController::Start", TRACE_EVENT1("omnibox", "AutocompleteController::Start",
"text", base::UTF16ToUTF8(input.text())); "text", base::UTF16ToUTF8(input.text()));
// When input.want_asynchronous_matches() is false, the AutocompleteController
// is being used for text classification, which should not notify observers.
if (input.want_asynchronous_matches()) {
for (Observer& obs : observers_)
obs.OnStart(this, input);
}
const base::string16 old_input_text(input_.text()); const base::string16 old_input_text(input_.text());
const bool old_allow_exact_keyword_match = input_.allow_exact_keyword_match(); const bool old_allow_exact_keyword_match = input_.allow_exact_keyword_match();
const bool old_want_asynchronous_matches = input_.want_asynchronous_matches(); const bool old_want_asynchronous_matches = input_.want_asynchronous_matches();
...@@ -855,8 +866,8 @@ void AutocompleteController::UpdateAssistedQueryStats( ...@@ -855,8 +866,8 @@ void AutocompleteController::UpdateAssistedQueryStats(
} }
void AutocompleteController::NotifyChanged(bool notify_default_match) { void AutocompleteController::NotifyChanged(bool notify_default_match) {
if (delegate_) for (Observer& obs : observers_)
delegate_->OnResultChanged(notify_default_match); obs.OnResultChanged(this, notify_default_match);
if (done_) if (done_)
provider_client_->OnAutocompleteControllerResultReady(this); provider_client_->OnAutocompleteControllerResultReady(this);
} }
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
#include "base/gtest_prod_util.h" #include "base/gtest_prod_util.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
...@@ -23,7 +25,6 @@ ...@@ -23,7 +25,6 @@
#include "components/omnibox/browser/autocomplete_provider_listener.h" #include "components/omnibox/browser/autocomplete_provider_listener.h"
#include "components/omnibox/browser/autocomplete_result.h" #include "components/omnibox/browser/autocomplete_result.h"
class AutocompleteControllerDelegate;
class DocumentProvider; class DocumentProvider;
class HistoryURLProvider; class HistoryURLProvider;
class KeywordProvider; class KeywordProvider;
...@@ -57,15 +58,32 @@ class AutocompleteController : public AutocompleteProviderListener, ...@@ -57,15 +58,32 @@ class AutocompleteController : public AutocompleteProviderListener,
public: public:
typedef std::vector<scoped_refptr<AutocompleteProvider> > Providers; typedef std::vector<scoped_refptr<AutocompleteProvider> > Providers;
class Observer : public base::CheckedObserver {
public:
// Invoked when the |controller| Start() is called with an |input| that
// wants asynchronous matches. This is meant to exclude text classification
// requests. The |controller| parameter is only useful for observers that
// are observing multiple AutocompleteController instances.
virtual void OnStart(AutocompleteController* controller,
const AutocompleteInput& input) {}
// Invoked when the result set of |controller| changes. If
// |default_match_changed| is true, the default match of the result set has
// changed. The |controller| parameter is only useful for observers that
// are observing multiple AutocompleteController instances.
virtual void OnResultChanged(AutocompleteController* controller,
bool default_match_changed) {}
};
// |provider_types| is a bitmap containing AutocompleteProvider::Type values // |provider_types| is a bitmap containing AutocompleteProvider::Type values
// that will (potentially, depending on platform, flags, etc.) be // that will (potentially, depending on platform, flags, etc.) be
// instantiated. |provider_client| is passed to all those providers, and // instantiated. |provider_client| is passed to all those providers, and
// is used to get access to the template URL service. |delegate| is a // is used to get access to the template URL service. |observer| is a
// proxy for UI elements which need to be notified when the results get // proxy for UI elements which need to be notified when the results get
// updated. // updated.
AutocompleteController( AutocompleteController(
std::unique_ptr<AutocompleteProviderClient> provider_client, std::unique_ptr<AutocompleteProviderClient> provider_client,
AutocompleteControllerDelegate* delegate, Observer* observer,
int provider_types); int provider_types);
~AutocompleteController() override; ~AutocompleteController() override;
...@@ -76,9 +94,9 @@ class AutocompleteController : public AutocompleteProviderListener, ...@@ -76,9 +94,9 @@ class AutocompleteController : public AutocompleteProviderListener,
// See AutocompleteInput::AutocompleteInput(...) for more details regarding // See AutocompleteInput::AutocompleteInput(...) for more details regarding
// |input| params. // |input| params.
// //
// The controller calls AutocompleteControllerDelegate::OnResultChanged() from // The controller calls AutocompleteController::Observer::OnResultChanged()
// inside this call at least once. If matches are available later on that // from inside this call at least once. If matches are available later on that
// result in changing the result set the delegate is notified again. When the // result in changing the result set the observers is notified again. When the
// controller is done the notification AUTOCOMPLETE_CONTROLLER_RESULT_READY is // controller is done the notification AUTOCOMPLETE_CONTROLLER_RESULT_READY is
// sent. // sent.
void Start(const AutocompleteInput& input); void Start(const AutocompleteInput& input);
...@@ -214,7 +232,7 @@ class AutocompleteController : public AutocompleteProviderListener, ...@@ -214,7 +232,7 @@ class AutocompleteController : public AutocompleteProviderListener,
// stats. // stats.
void UpdateAssistedQueryStats(AutocompleteResult* result); void UpdateAssistedQueryStats(AutocompleteResult* result);
// Calls AutocompleteControllerDelegate::OnResultChanged() and if done sends // Calls AutocompleteController::Observer::OnResultChanged() and if done sends
// AUTOCOMPLETE_CONTROLLER_RESULT_READY. // AUTOCOMPLETE_CONTROLLER_RESULT_READY.
void NotifyChanged(bool notify_default_match); void NotifyChanged(bool notify_default_match);
...@@ -242,7 +260,7 @@ class AutocompleteController : public AutocompleteProviderListener, ...@@ -242,7 +260,7 @@ class AutocompleteController : public AutocompleteProviderListener,
const base::trace_event::MemoryDumpArgs& args, const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* process_memory_dump) override; base::trace_event::ProcessMemoryDump* process_memory_dump) override;
AutocompleteControllerDelegate* delegate_; base::ObserverList<Observer> observers_;
// The client passed to the providers. // The client passed to the providers.
std::unique_ptr<AutocompleteProviderClient> provider_client_; std::unique_ptr<AutocompleteProviderClient> provider_client_;
......
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_CONTROLLER_DELEGATE_H_
#define COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_CONTROLLER_DELEGATE_H_
class AutocompleteControllerDelegate {
public:
// Invoked when the result set of the AutocompleteController changes. If
// |default_match_changed| is true, the default match of the result set has
// changed.
virtual void OnResultChanged(bool default_match_changed) = 0;
protected:
virtual ~AutocompleteControllerDelegate() {}
};
#endif // COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_CONTROLLER_DELEGATE_H_
...@@ -43,7 +43,10 @@ void OmniboxController::StartAutocomplete( ...@@ -43,7 +43,10 @@ void OmniboxController::StartAutocomplete(
autocomplete_controller_->Start(input); autocomplete_controller_->Start(input);
} }
void OmniboxController::OnResultChanged(bool default_match_changed) { void OmniboxController::OnResultChanged(AutocompleteController* controller,
bool default_match_changed) {
DCHECK(controller == autocomplete_controller_.get());
if (client_->GetOmniboxControllerEmitter()) if (client_->GetOmniboxControllerEmitter())
client_->GetOmniboxControllerEmitter()->NotifyOmniboxResultChanged( client_->GetOmniboxControllerEmitter()->NotifyOmniboxResultChanged(
default_match_changed, autocomplete_controller_.get()); default_match_changed, autocomplete_controller_.get());
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "components/omnibox/browser/autocomplete_controller.h" #include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/browser/autocomplete_controller_delegate.h"
#include "components/omnibox/browser/autocomplete_match.h" #include "components/omnibox/browser/autocomplete_match.h"
struct AutocompleteMatch; struct AutocompleteMatch;
...@@ -29,7 +28,7 @@ class OmniboxPopupModel; ...@@ -29,7 +28,7 @@ class OmniboxPopupModel;
// this the point of contact between InstantController and OmniboxEditModel. // this the point of contact between InstantController and OmniboxEditModel.
// As the refactor progresses, keep the class comment up to date to // As the refactor progresses, keep the class comment up to date to
// precisely explain what this class is doing. // precisely explain what this class is doing.
class OmniboxController : public AutocompleteControllerDelegate { class OmniboxController : public AutocompleteController::Observer {
public: public:
OmniboxController(OmniboxEditModel* omnibox_edit_model, OmniboxController(OmniboxEditModel* omnibox_edit_model,
OmniboxClient* client); OmniboxClient* client);
...@@ -38,8 +37,9 @@ class OmniboxController : public AutocompleteControllerDelegate { ...@@ -38,8 +37,9 @@ class OmniboxController : public AutocompleteControllerDelegate {
// The |current_url| field of input is only set for mobile ports. // The |current_url| field of input is only set for mobile ports.
void StartAutocomplete(const AutocompleteInput& input) const; void StartAutocomplete(const AutocompleteInput& input) const;
// AutocompleteControllerDelegate: // AutocompleteController::Observer:
void OnResultChanged(bool default_match_changed) override; void OnResultChanged(AutocompleteController* controller,
bool default_match_changed) override;
AutocompleteController* autocomplete_controller() { AutocompleteController* autocomplete_controller() {
return autocomplete_controller_.get(); return autocomplete_controller_.get();
......
...@@ -59,24 +59,26 @@ OmniboxControllerEmitter* OmniboxControllerEmitter::GetForBrowserContext( ...@@ -59,24 +59,26 @@ OmniboxControllerEmitter* OmniboxControllerEmitter::GetForBrowserContext(
} }
#endif // !defined(OS_IOS) #endif // !defined(OS_IOS)
void OmniboxControllerEmitter::AddObserver(Observer* observer) { void OmniboxControllerEmitter::AddObserver(
AutocompleteController::Observer* observer) {
observers_.AddObserver(observer); observers_.AddObserver(observer);
} }
void OmniboxControllerEmitter::RemoveObserver(Observer* observer) { void OmniboxControllerEmitter::RemoveObserver(
AutocompleteController::Observer* observer) {
observers_.RemoveObserver(observer); observers_.RemoveObserver(observer);
} }
void OmniboxControllerEmitter::NotifyOmniboxQuery( void OmniboxControllerEmitter::NotifyOmniboxQuery(
AutocompleteController* controller, AutocompleteController* controller,
const AutocompleteInput& input) { const AutocompleteInput& input) {
for (Observer& observer : observers_) for (auto& observer : observers_)
observer.OnOmniboxQuery(controller, input); observer.OnStart(controller, input);
} }
void OmniboxControllerEmitter::NotifyOmniboxResultChanged( void OmniboxControllerEmitter::NotifyOmniboxResultChanged(
bool default_match_changed, bool default_match_changed,
AutocompleteController* controller) { AutocompleteController* controller) {
for (Observer& observer : observers_) for (auto& observer : observers_)
observer.OnOmniboxResultChanged(default_match_changed, controller); observer.OnResultChanged(controller, default_match_changed);
} }
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include "build/build_config.h" #include "build/build_config.h"
#include "components/keyed_service/core/keyed_service.h" #include "components/keyed_service/core/keyed_service.h"
#include "components/omnibox/browser/autocomplete_controller.h" #include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/browser/autocomplete_controller_delegate.h"
#if !defined(OS_IOS) #if !defined(OS_IOS)
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
...@@ -18,16 +17,6 @@ ...@@ -18,16 +17,6 @@
// and notifies observers (chrome://omnibox debug page). // and notifies observers (chrome://omnibox debug page).
class OmniboxControllerEmitter : public KeyedService { class OmniboxControllerEmitter : public KeyedService {
public: public:
class Observer : public base::CheckedObserver {
public:
// Invoked when new autocomplete queries are made from the omnibox
// controller or when those queries' results change.
virtual void OnOmniboxQuery(AutocompleteController* controller,
const AutocompleteInput& input) = 0;
virtual void OnOmniboxResultChanged(bool default_match_changed,
AutocompleteController* controller) = 0;
};
#if !defined(OS_IOS) #if !defined(OS_IOS)
static OmniboxControllerEmitter* GetForBrowserContext( static OmniboxControllerEmitter* GetForBrowserContext(
content::BrowserContext* browser_context); content::BrowserContext* browser_context);
...@@ -37,18 +26,21 @@ class OmniboxControllerEmitter : public KeyedService { ...@@ -37,18 +26,21 @@ class OmniboxControllerEmitter : public KeyedService {
~OmniboxControllerEmitter() override; ~OmniboxControllerEmitter() override;
// Add/remove observer. // Add/remove observer.
void AddObserver(Observer* observer); void AddObserver(AutocompleteController::Observer* observer);
void RemoveObserver(Observer* observer); void RemoveObserver(AutocompleteController::Observer* observer);
// Notifies registered observers when new autocomplete queries are made from // Notifies registered observers when new autocomplete queries are made from
// the omnibox controller or when those queries' results change. // the omnibox controller or when those queries' results change.
//
// TODO(tommycli): These two methods themselves should be overrides of
// AutocompleteController::Observer.
void NotifyOmniboxQuery(AutocompleteController* controller, void NotifyOmniboxQuery(AutocompleteController* controller,
const AutocompleteInput& input); const AutocompleteInput& input);
void NotifyOmniboxResultChanged(bool default_match_changed, void NotifyOmniboxResultChanged(bool default_match_changed,
AutocompleteController* controller); AutocompleteController* controller);
private: private:
base::ObserverList<Observer> observers_; base::ObserverList<AutocompleteController::Observer> observers_;
DISALLOW_COPY_AND_ASSIGN(OmniboxControllerEmitter); DISALLOW_COPY_AND_ASSIGN(OmniboxControllerEmitter);
}; };
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "components/omnibox/browser/autocomplete_controller.h" #include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/browser/autocomplete_controller_delegate.h"
#include "components/omnibox/browser/autocomplete_input.h" #include "components/omnibox/browser/autocomplete_input.h"
#include "components/omnibox/browser/autocomplete_match.h" #include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/omnibox_controller.h" #include "components/omnibox/browser/omnibox_controller.h"
......
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