Commit 74bc4bae authored by yawano's avatar yawano Committed by Commit bot

Cache listener extension ids.

Previously launcherSearchProvider API has always enumerated all enabled extensions to list up listener extensions.
This CL changes to cache the list.

BUG=488329

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

Cr-Commit-Position: refs/heads/master@{#330478}
parent fc2180d4
......@@ -28,9 +28,11 @@ Service::Service(Profile* profile,
provider_(nullptr),
query_id_(0),
is_query_running_(false) {
extension_registry_->AddObserver(this);
}
Service::~Service() {
extension_registry_->RemoveObserver(this);
}
// static
......@@ -50,8 +52,8 @@ void Service::OnQueryStarted(app_list::LauncherSearchProvider* provider,
extensions::EventRouter* event_router =
extensions::EventRouter::Get(profile_);
std::set<ExtensionId> extension_ids = GetListenerExtensionIds();
for (const ExtensionId extension_id : extension_ids) {
CacheListenerExtensionIds();
for (const ExtensionId extension_id : *cached_listener_extension_ids_.get()) {
// Convert query_id_ to string here since queryId is defined as string in
// javascript side API while we use uint32 internally to generate it.
event_router->DispatchEventToExtension(
......@@ -70,8 +72,8 @@ void Service::OnQueryEnded() {
extensions::EventRouter* event_router =
extensions::EventRouter::Get(profile_);
std::set<ExtensionId> extension_ids = GetListenerExtensionIds();
for (const ExtensionId extension_id : extension_ids) {
CacheListenerExtensionIds();
for (const ExtensionId extension_id : *cached_listener_extension_ids_.get()) {
event_router->DispatchEventToExtension(
extension_id,
make_scoped_ptr(new extensions::Event(
......@@ -84,7 +86,8 @@ void Service::OnQueryEnded() {
void Service::OnOpenResult(const ExtensionId& extension_id,
const std::string& item_id) {
CHECK(ContainsValue(GetListenerExtensionIds(), extension_id));
CacheListenerExtensionIds();
CHECK(ContainsValue(*cached_listener_extension_ids_.get(), extension_id));
extensions::EventRouter* event_router =
extensions::EventRouter::Get(profile_);
......@@ -107,7 +110,8 @@ void Service::SetSearchResults(
return;
// If |extension| is not in the listener extensions list, ignore it.
if (!ContainsValue(GetListenerExtensionIds(), extension->id()))
CacheListenerExtensionIds();
if (!ContainsValue(*cached_listener_extension_ids_.get(), extension->id()))
return;
// Set search results to provider.
......@@ -133,9 +137,26 @@ bool Service::IsQueryRunning() const {
return is_query_running_;
}
std::set<ExtensionId> Service::GetListenerExtensionIds() {
// TODO(yawano): Cache this result for optimization (crbug.com/440649).
std::set<ExtensionId> extension_ids;
void Service::OnExtensionLoaded(content::BrowserContext* browser_context,
const extensions::Extension* extension) {
// Invalidate cache.
cached_listener_extension_ids_.reset();
}
void Service::OnExtensionUnloaded(
content::BrowserContext* browser_context,
const extensions::Extension* extension,
extensions::UnloadedExtensionInfo::Reason reason) {
// Invalidate cache.
cached_listener_extension_ids_.reset();
}
void Service::CacheListenerExtensionIds() {
// If it's already cached, do nothing.
if (cached_listener_extension_ids_)
return;
cached_listener_extension_ids_.reset(new std::set<ExtensionId>());
const ExtensionSet& extension_set = extension_registry_->enabled_extensions();
for (scoped_refptr<const extensions::Extension> extension : extension_set) {
......@@ -144,10 +165,8 @@ std::set<ExtensionId> Service::GetListenerExtensionIds() {
const bool has_permission = permission_data->HasAPIPermission(
extensions::APIPermission::kLauncherSearchProvider);
if (has_permission)
extension_ids.insert(extension->id());
cached_listener_extension_ids_->insert(extension->id());
}
return extension_ids;
}
} // namespace launcher_search_provider
......
......@@ -12,6 +12,7 @@
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/browser_context.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/common/extension.h"
namespace app_list {
......@@ -31,7 +32,8 @@ const int kMaxSearchResultScore = 4;
// This service provides access control for it.
//
// TODO(yawano): Implement opt-in control (crbug.com/440649).
class Service : public KeyedService {
class Service : public KeyedService,
public extensions::ExtensionRegistryObserver {
public:
Service(Profile* profile, extensions::ExtensionRegistry* extension_registry);
~Service() override;
......@@ -60,15 +62,25 @@ class Service : public KeyedService {
// Returns true if there is a running query.
bool IsQueryRunning() const;
// extensions::ExtensionRegistryObserver override.
void OnExtensionLoaded(content::BrowserContext* browser_context,
const extensions::Extension* extension) override;
void OnExtensionUnloaded(
content::BrowserContext* browser_context,
const extensions::Extension* extension,
extensions::UnloadedExtensionInfo::Reason reason) override;
private:
// Returns extension ids of listener extensions.
std::set<extensions::ExtensionId> GetListenerExtensionIds();
// Cache listener extension ids and set them to
// |cached_listener_extension_ids_|.
void CacheListenerExtensionIds();
Profile* const profile_;
extensions::ExtensionRegistry* extension_registry_;
app_list::LauncherSearchProvider* provider_;
int query_id_;
bool is_query_running_;
scoped_ptr<std::set<extensions::ExtensionId>> cached_listener_extension_ids_;
DISALLOW_COPY_AND_ASSIGN(Service);
};
......
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