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