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