No chrome dependencies in KeywordProvider

Profile related business is handled by KeywordExtensionsDelegateImpl.

BUG=388515
TEST=unit_tests

Review URL: https://codereview.chromium.org/460313002

Cr-Commit-Position: refs/heads/master@{#289248}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289248 0039d316-1c4b-4281-b951-d872f2087c98
parent c2d97ec1
...@@ -31,6 +31,10 @@ ...@@ -31,6 +31,10 @@
#include "grit/theme_resources.h" #include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#if defined(ENABLE_EXTENSIONS)
#include "chrome/browser/autocomplete/keyword_extensions_delegate_impl.h"
#endif
namespace { namespace {
// Converts the given match to a type (and possibly subtype) based on the AQS // Converts the given match to a type (and possibly subtype) based on the AQS
...@@ -195,7 +199,12 @@ AutocompleteController::AutocompleteController( ...@@ -195,7 +199,12 @@ AutocompleteController::AutocompleteController(
// "Tab to search" can be used on all platforms other than Android. // "Tab to search" can be used on all platforms other than Android.
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
if (provider_types & AutocompleteProvider::TYPE_KEYWORD) { if (provider_types & AutocompleteProvider::TYPE_KEYWORD) {
keyword_provider_ = new KeywordProvider(this, profile); keyword_provider_ = new KeywordProvider(this, template_url_service);
#if defined(ENABLE_EXTENSIONS)
keyword_provider_->set_extensions_delegate(
scoped_ptr<KeywordExtensionsDelegate>(
new KeywordExtensionsDelegateImpl(profile, keyword_provider_)));
#endif
providers_.push_back(keyword_provider_); providers_.push_back(keyword_provider_);
} }
#endif #endif
......
...@@ -15,7 +15,6 @@ void KeywordExtensionsDelegate::IncrementInputId() { ...@@ -15,7 +15,6 @@ void KeywordExtensionsDelegate::IncrementInputId() {
} }
bool KeywordExtensionsDelegate::IsEnabledExtension( bool KeywordExtensionsDelegate::IsEnabledExtension(
Profile* profile,
const std::string& extension_id) { const std::string& extension_id) {
return false; return false;
} }
......
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
class AutocompleteInput; class AutocompleteInput;
class KeywordProvider; class KeywordProvider;
class Profile;
class TemplateURL; class TemplateURL;
class KeywordExtensionsDelegate { class KeywordExtensionsDelegate {
...@@ -30,9 +29,8 @@ class KeywordExtensionsDelegate { ...@@ -30,9 +29,8 @@ class KeywordExtensionsDelegate {
// extension are current. // extension are current.
virtual void IncrementInputId(); virtual void IncrementInputId();
// Returns true if an extension is enabled in a given profile. // Returns true if an extension is enabled.
virtual bool IsEnabledExtension(Profile* profile, virtual bool IsEnabledExtension(const std::string& extension_id);
const std::string& extension_id);
// Handles the extensions portion of KeywordProvider::Start(). // Handles the extensions portion of KeywordProvider::Start().
// Depending on |minimal_changes| and whether |input| wants matches // Depending on |minimal_changes| and whether |input| wants matches
......
...@@ -19,8 +19,10 @@ namespace omnibox_api = extensions::api::omnibox; ...@@ -19,8 +19,10 @@ namespace omnibox_api = extensions::api::omnibox;
int KeywordExtensionsDelegateImpl::global_input_uid_ = 0; int KeywordExtensionsDelegateImpl::global_input_uid_ = 0;
KeywordExtensionsDelegateImpl::KeywordExtensionsDelegateImpl( KeywordExtensionsDelegateImpl::KeywordExtensionsDelegateImpl(
Profile* profile,
KeywordProvider* provider) KeywordProvider* provider)
: KeywordExtensionsDelegate(provider), : KeywordExtensionsDelegate(provider),
profile_(profile),
provider_(provider) { provider_(provider) {
DCHECK(provider_); DCHECK(provider_);
...@@ -30,14 +32,14 @@ KeywordExtensionsDelegateImpl::KeywordExtensionsDelegateImpl( ...@@ -30,14 +32,14 @@ KeywordExtensionsDelegateImpl::KeywordExtensionsDelegateImpl(
// suggestions are meant for us. // suggestions are meant for us.
registrar_.Add(this, registrar_.Add(this,
extensions::NOTIFICATION_EXTENSION_OMNIBOX_SUGGESTIONS_READY, extensions::NOTIFICATION_EXTENSION_OMNIBOX_SUGGESTIONS_READY,
content::Source<Profile>(profile()->GetOriginalProfile())); content::Source<Profile>(profile_->GetOriginalProfile()));
registrar_.Add( registrar_.Add(
this, this,
extensions::NOTIFICATION_EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED, extensions::NOTIFICATION_EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED,
content::Source<Profile>(profile()->GetOriginalProfile())); content::Source<Profile>(profile_->GetOriginalProfile()));
registrar_.Add(this, registrar_.Add(this,
extensions::NOTIFICATION_EXTENSION_OMNIBOX_INPUT_ENTERED, extensions::NOTIFICATION_EXTENSION_OMNIBOX_INPUT_ENTERED,
content::Source<Profile>(profile())); content::Source<Profile>(profile_));
} }
KeywordExtensionsDelegateImpl::~KeywordExtensionsDelegateImpl() { KeywordExtensionsDelegateImpl::~KeywordExtensionsDelegateImpl() {
...@@ -48,15 +50,14 @@ void KeywordExtensionsDelegateImpl::IncrementInputId() { ...@@ -48,15 +50,14 @@ void KeywordExtensionsDelegateImpl::IncrementInputId() {
} }
bool KeywordExtensionsDelegateImpl::IsEnabledExtension( bool KeywordExtensionsDelegateImpl::IsEnabledExtension(
Profile* profile,
const std::string& extension_id) { const std::string& extension_id) {
ExtensionService* extension_service = ExtensionService* extension_service =
extensions::ExtensionSystem::Get(profile)->extension_service(); extensions::ExtensionSystem::Get(profile_)->extension_service();
const extensions::Extension* extension = const extensions::Extension* extension =
extension_service->GetExtensionById(extension_id, false); extension_service->GetExtensionById(extension_id, false);
return extension && return extension &&
(!profile->IsOffTheRecord() || (!profile_->IsOffTheRecord() ||
!extensions::util::IsIncognitoEnabled(extension_id, profile)); !extensions::util::IsIncognitoEnabled(extension_id, profile_));
} }
bool KeywordExtensionsDelegateImpl::Start( bool KeywordExtensionsDelegateImpl::Start(
...@@ -75,7 +76,7 @@ bool KeywordExtensionsDelegateImpl::Start( ...@@ -75,7 +76,7 @@ bool KeywordExtensionsDelegateImpl::Start(
} }
extensions::ApplyDefaultSuggestionForExtensionKeyword( extensions::ApplyDefaultSuggestionForExtensionKeyword(
profile(), template_url, remaining_input, &matches()->front()); profile_, template_url, remaining_input, &matches()->front());
if (minimal_changes) { if (minimal_changes) {
// If the input hasn't significantly changed, we can just use the // If the input hasn't significantly changed, we can just use the
...@@ -92,7 +93,7 @@ bool KeywordExtensionsDelegateImpl::Start( ...@@ -92,7 +93,7 @@ bool KeywordExtensionsDelegateImpl::Start(
// We only have to wait for suggest results if there are actually // We only have to wait for suggest results if there are actually
// extensions listening for input changes. // extensions listening for input changes.
if (extensions::ExtensionOmniboxEventRouter::OnInputChanged( if (extensions::ExtensionOmniboxEventRouter::OnInputChanged(
profile(), template_url->GetExtensionId(), profile_, template_url->GetExtensionId(),
base::UTF16ToUTF8(remaining_input), current_input_id_)) base::UTF16ToUTF8(remaining_input), current_input_id_))
set_done(false); set_done(false);
} }
...@@ -105,13 +106,13 @@ void KeywordExtensionsDelegateImpl::EnterExtensionKeywordMode( ...@@ -105,13 +106,13 @@ void KeywordExtensionsDelegateImpl::EnterExtensionKeywordMode(
current_keyword_extension_id_ = extension_id; current_keyword_extension_id_ = extension_id;
extensions::ExtensionOmniboxEventRouter::OnInputStarted( extensions::ExtensionOmniboxEventRouter::OnInputStarted(
profile(), current_keyword_extension_id_); profile_, current_keyword_extension_id_);
} }
void KeywordExtensionsDelegateImpl::MaybeEndExtensionKeywordMode() { void KeywordExtensionsDelegateImpl::MaybeEndExtensionKeywordMode() {
if (!current_keyword_extension_id_.empty()) { if (!current_keyword_extension_id_.empty()) {
extensions::ExtensionOmniboxEventRouter::OnInputCancelled( extensions::ExtensionOmniboxEventRouter::OnInputCancelled(
profile(), current_keyword_extension_id_); profile_, current_keyword_extension_id_);
current_keyword_extension_id_.clear(); current_keyword_extension_id_.clear();
} }
} }
...@@ -145,7 +146,7 @@ void KeywordExtensionsDelegateImpl::Observe( ...@@ -145,7 +146,7 @@ void KeywordExtensionsDelegateImpl::Observe(
const TemplateURL* template_url( const TemplateURL* template_url(
model->GetTemplateURLForKeyword(keyword)); model->GetTemplateURLForKeyword(keyword));
extensions::ApplyDefaultSuggestionForExtensionKeyword( extensions::ApplyDefaultSuggestionForExtensionKeyword(
profile(), template_url, remaining_input, &matches()->front()); profile_, template_url, remaining_input, &matches()->front());
OnProviderUpdate(true); OnProviderUpdate(true);
return; return;
} }
......
...@@ -23,17 +23,18 @@ ...@@ -23,17 +23,18 @@
#error "Should not be included when extensions are disabled" #error "Should not be included when extensions are disabled"
#endif #endif
class Profile;
class KeywordExtensionsDelegateImpl : public KeywordExtensionsDelegate, class KeywordExtensionsDelegateImpl : public KeywordExtensionsDelegate,
public content::NotificationObserver { public content::NotificationObserver {
public: public:
explicit KeywordExtensionsDelegateImpl(KeywordProvider* provider); KeywordExtensionsDelegateImpl(Profile* profile, KeywordProvider* provider);
virtual ~KeywordExtensionsDelegateImpl(); virtual ~KeywordExtensionsDelegateImpl();
private: private:
// KeywordExtensionsDelegate: // KeywordExtensionsDelegate:
virtual void IncrementInputId() OVERRIDE; virtual void IncrementInputId() OVERRIDE;
virtual bool IsEnabledExtension(Profile* profile, virtual bool IsEnabledExtension(const std::string& extension_id) OVERRIDE;
const std::string& extension_id) OVERRIDE;
virtual bool Start(const AutocompleteInput& input, virtual bool Start(const AutocompleteInput& input,
bool minimal_changes, bool minimal_changes,
const TemplateURL* template_url, const TemplateURL* template_url,
...@@ -47,7 +48,6 @@ class KeywordExtensionsDelegateImpl : public KeywordExtensionsDelegate, ...@@ -47,7 +48,6 @@ class KeywordExtensionsDelegateImpl : public KeywordExtensionsDelegate,
const content::NotificationSource& source, const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE; const content::NotificationDetails& details) OVERRIDE;
Profile* profile() { return provider_->profile_; }
ACMatches* matches() { return &provider_->matches_; } ACMatches* matches() { return &provider_->matches_; }
void set_done(bool done) { void set_done(bool done) {
provider_->done_ = done; provider_->done_ = done;
...@@ -73,6 +73,8 @@ class KeywordExtensionsDelegateImpl : public KeywordExtensionsDelegate, ...@@ -73,6 +73,8 @@ class KeywordExtensionsDelegateImpl : public KeywordExtensionsDelegate,
// the URL bar while the autocomplete popup is open. // the URL bar while the autocomplete popup is open.
std::string current_keyword_extension_id_; std::string current_keyword_extension_id_;
Profile* profile_;
// The owner of this class. // The owner of this class.
KeywordProvider* provider_; KeywordProvider* provider_;
......
...@@ -11,26 +11,16 @@ ...@@ -11,26 +11,16 @@
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/autocomplete/keyword_extensions_delegate.h" #include "chrome/browser/autocomplete/keyword_extensions_delegate.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "components/metrics/proto/omnibox_input_type.pb.h" #include "components/metrics/proto/omnibox_input_type.pb.h"
#include "components/omnibox/autocomplete_match.h" #include "components/omnibox/autocomplete_match.h"
#include "components/omnibox/autocomplete_provider_listener.h" #include "components/omnibox/autocomplete_provider_listener.h"
#include "components/search_engines/template_url.h" #include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h" #include "components/search_engines/template_url_service.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "extensions/browser/extension_system.h"
#include "grit/generated_resources.h" #include "grit/generated_resources.h"
#include "net/base/escape.h" #include "net/base/escape.h"
#include "net/base/net_util.h" #include "net/base/net_util.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#if defined(ENABLE_EXTENSIONS)
#include "chrome/browser/autocomplete/keyword_extensions_delegate_impl.h"
#endif
namespace { namespace {
// Helper functor for Start(), for sorting keyword matches by quality. // Helper functor for Start(), for sorting keyword matches by quality.
...@@ -79,22 +69,11 @@ void ScopedEndExtensionKeywordMode::StayInKeywordMode() { ...@@ -79,22 +69,11 @@ void ScopedEndExtensionKeywordMode::StayInKeywordMode() {
} // namespace } // namespace
KeywordProvider::KeywordProvider(AutocompleteProviderListener* listener, KeywordProvider::KeywordProvider(
Profile* profile) AutocompleteProviderListener* listener,
: AutocompleteProvider(AutocompleteProvider::TYPE_KEYWORD),
listener_(listener),
profile_(profile),
model_(NULL) {
#if defined(ENABLE_EXTENSIONS)
extensions_delegate_.reset(new KeywordExtensionsDelegateImpl(this));
#endif
}
KeywordProvider::KeywordProvider(AutocompleteProviderListener* listener,
TemplateURLService* model) TemplateURLService* model)
: AutocompleteProvider(AutocompleteProvider::TYPE_KEYWORD), : AutocompleteProvider(AutocompleteProvider::TYPE_KEYWORD),
listener_(listener), listener_(listener),
profile_(NULL),
model_(model) { model_(model) {
} }
...@@ -203,8 +182,7 @@ base::string16 KeywordProvider::GetKeywordForText( ...@@ -203,8 +182,7 @@ base::string16 KeywordProvider::GetKeywordForText(
// Don't provide a keyword for inactive/disabled extension keywords. // Don't provide a keyword for inactive/disabled extension keywords.
if ((template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) && if ((template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) &&
extensions_delegate_ && extensions_delegate_ &&
!extensions_delegate_->IsEnabledExtension( !extensions_delegate_->IsEnabledExtension(template_url->GetExtensionId()))
profile_, template_url->GetExtensionId()))
return base::string16(); return base::string16();
return keyword; return keyword;
...@@ -270,11 +248,10 @@ void KeywordProvider::Start(const AutocompleteInput& input, ...@@ -270,11 +248,10 @@ void KeywordProvider::Start(const AutocompleteInput& input,
// Prune any extension keywords that are disallowed in incognito mode (if // Prune any extension keywords that are disallowed in incognito mode (if
// we're incognito), or disabled. // we're incognito), or disabled.
if (profile_ && if (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION &&
(template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) &&
extensions_delegate_ && extensions_delegate_ &&
!extensions_delegate_->IsEnabledExtension( !extensions_delegate_->IsEnabledExtension(
profile_, template_url->GetExtensionId())) { template_url->GetExtensionId())) {
i = matches.erase(i); i = matches.erase(i);
continue; continue;
} }
...@@ -320,7 +297,7 @@ void KeywordProvider::Start(const AutocompleteInput& input, ...@@ -320,7 +297,7 @@ void KeywordProvider::Start(const AutocompleteInput& input,
matches_.push_back(CreateAutocompleteMatch( matches_.push_back(CreateAutocompleteMatch(
template_url, input, keyword.length(), remaining_input, true, -1)); template_url, input, keyword.length(), remaining_input, true, -1));
if (profile_ && is_extension_keyword && extensions_delegate_) { if (is_extension_keyword && extensions_delegate_) {
if (extensions_delegate_->Start(input, minimal_changes, template_url, if (extensions_delegate_->Start(input, minimal_changes, template_url,
remaining_input)) remaining_input))
keyword_mode_toggle.StayInKeywordMode(); keyword_mode_toggle.StayInKeywordMode();
...@@ -487,11 +464,8 @@ void KeywordProvider::FillInURLAndContents( ...@@ -487,11 +464,8 @@ void KeywordProvider::FillInURLAndContents(
} }
TemplateURLService* KeywordProvider::GetTemplateURLService() const { TemplateURLService* KeywordProvider::GetTemplateURLService() const {
TemplateURLService* service = profile_ ?
TemplateURLServiceFactory::GetForProfile(profile_) : model_;
// Make sure the model is loaded. This is cheap and quickly bails out if // Make sure the model is loaded. This is cheap and quickly bails out if
// the model is already loaded. // the model is already loaded.
DCHECK(service); model_->Load();
service->Load(); return model_;
return service;
} }
...@@ -16,13 +16,13 @@ ...@@ -16,13 +16,13 @@
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "chrome/browser/autocomplete/keyword_extensions_delegate.h"
#include "components/metrics/proto/omnibox_input_type.pb.h" #include "components/metrics/proto/omnibox_input_type.pb.h"
#include "components/omnibox/autocomplete_input.h" #include "components/omnibox/autocomplete_input.h"
#include "components/omnibox/autocomplete_provider.h" #include "components/omnibox/autocomplete_provider.h"
class AutocompleteProviderListener; class AutocompleteProviderListener;
class KeywordExtensionsDelegate; class KeywordExtensionsDelegate;
class Profile;
class TemplateURL; class TemplateURL;
class TemplateURLService; class TemplateURLService;
...@@ -50,11 +50,14 @@ class TemplateURLService; ...@@ -50,11 +50,14 @@ class TemplateURLService;
// "<enter term(s)>" as the substituted input, and does nothing when selected. // "<enter term(s)>" as the substituted input, and does nothing when selected.
class KeywordProvider : public AutocompleteProvider { class KeywordProvider : public AutocompleteProvider {
public: public:
KeywordProvider(AutocompleteProviderListener* listener, Profile* profile);
// For testing.
KeywordProvider(AutocompleteProviderListener* listener, KeywordProvider(AutocompleteProviderListener* listener,
TemplateURLService* model); TemplateURLService* model);
void set_extensions_delegate(
scoped_ptr<KeywordExtensionsDelegate> extensions_delegate) {
extensions_delegate_ = extensions_delegate.Pass();
}
// Extracts the next whitespace-delimited token from input and returns it. // Extracts the next whitespace-delimited token from input and returns it.
// Sets |remaining_input| to everything after the first token (skipping over // Sets |remaining_input| to everything after the first token (skipping over
// the first intervening whitespace). // the first intervening whitespace).
...@@ -141,10 +144,8 @@ class KeywordProvider : public AutocompleteProvider { ...@@ -141,10 +144,8 @@ class KeywordProvider : public AutocompleteProvider {
TemplateURLService* GetTemplateURLService() const; TemplateURLService* GetTemplateURLService() const;
AutocompleteProviderListener* listener_; AutocompleteProviderListener* listener_;
Profile* profile_;
// Model for the keywords. This is only non-null when testing, otherwise the // Model for the keywords.
// TemplateURLService from the Profile is used.
TemplateURLService* model_; TemplateURLService* model_;
// Delegate to handle the extensions-only logic for KeywordProvider. // Delegate to handle the extensions-only logic for KeywordProvider.
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
#include "chrome/browser/autocomplete/keyword_provider.h" #include "chrome/browser/autocomplete/keyword_provider.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/metrics/proto/omnibox_event.pb.h" #include "components/metrics/proto/omnibox_event.pb.h"
#include "components/omnibox/autocomplete_match.h" #include "components/omnibox/autocomplete_match.h"
#include "components/search_engines/search_engines_switches.h" #include "components/search_engines/search_engines_switches.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