Commit 00605fc0 authored by yawano's avatar yawano Committed by Commit bot

Implement onQueryEnded.

BUG=440649

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

Cr-Commit-Position: refs/heads/master@{#324010}
parent 0531a97f
......@@ -20,7 +20,10 @@ namespace launcher_search_provider {
Service::Service(Profile* profile,
extensions::ExtensionRegistry* extension_registry)
: profile_(profile), extension_registry_(extension_registry), query_id_(0) {
: profile_(profile),
extension_registry_(extension_registry),
query_id_(0),
is_query_running_(false) {
}
Service::~Service() {
......@@ -32,6 +35,9 @@ Service* Service::Get(content::BrowserContext* context) {
}
void Service::OnQueryStarted(const std::string& query, const int max_result) {
DCHECK(!is_query_running_);
is_query_running_ = true;
++query_id_;
extensions::EventRouter* event_router =
......@@ -48,6 +54,29 @@ void Service::OnQueryStarted(const std::string& query, const int max_result) {
}
}
void Service::OnQueryEnded() {
DCHECK(is_query_running_);
extensions::EventRouter* event_router =
extensions::EventRouter::Get(profile_);
std::set<ExtensionId> extension_ids = GetListenerExtensionIds();
for (const ExtensionId extension_id : extension_ids) {
event_router->DispatchEventToExtension(
extension_id,
make_scoped_ptr(new extensions::Event(
api_launcher_search_provider::OnQueryEnded::kEventName,
api_launcher_search_provider::OnQueryEnded::Create(
std::to_string(query_id_)))));
}
is_query_running_ = false;
}
bool Service::IsQueryRunning() const {
return is_query_running_;
}
std::set<ExtensionId> Service::GetListenerExtensionIds() {
std::set<ExtensionId> extension_ids;
......
......@@ -29,6 +29,12 @@ class Service : public KeyedService {
// Dispatches onQueryStarted events to listener extensions.
void OnQueryStarted(const std::string& query, const int max_result);
// Dispatches onQueryEnded events to listener extensions.
void OnQueryEnded();
// Returns true if there is a running query.
bool IsQueryRunning() const;
private:
// Returns extension ids of listener extensions.
std::set<extensions::ExtensionId> GetListenerExtensionIds();
......@@ -36,6 +42,7 @@ class Service : public KeyedService {
Profile* const profile_;
extensions::ExtensionRegistry* extension_registry_;
uint32 query_id_;
bool is_query_running_;
DISALLOW_COPY_AND_ASSIGN(Service);
};
......
......@@ -7,6 +7,8 @@
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chromeos/launcher_search_provider/service.h"
using chromeos::launcher_search_provider::Service;
namespace app_list {
namespace {
......@@ -30,11 +32,21 @@ void LauncherSearchProvider::Start(bool /*is_voice_query*/,
}
void LauncherSearchProvider::Stop() {
// Since app_list code can call Stop() at any time, we stop timer here in
// order not to start query after Stop() is called.
query_timer_.Stop();
Service* service = Service::Get(profile_);
// Since we delay queries and filter out empty string queries, it can happen
// that no query is running at service side.
if (service->IsQueryRunning())
service->OnQueryEnded();
}
void LauncherSearchProvider::DelayQuery(const base::Closure& closure) {
base::TimeDelta delay = base::TimeDelta::FromMilliseconds(
kLauncherSearchProviderQueryDelayInMs);
base::TimeDelta delay =
base::TimeDelta::FromMilliseconds(kLauncherSearchProviderQueryDelayInMs);
if (base::Time::Now() - last_query_time_ > delay) {
query_timer_.Stop();
closure.Run();
......@@ -46,8 +58,8 @@ void LauncherSearchProvider::DelayQuery(const base::Closure& closure) {
void LauncherSearchProvider::StartInternal(const base::string16& query) {
if (!query.empty()) {
chromeos::launcher_search_provider::Service::Get(profile_)->OnQueryStarted(
base::UTF16ToUTF8(query), kLauncherSearchProviderMaxResults);
Service::Get(profile_)->OnQueryStarted(base::UTF16ToUTF8(query),
kLauncherSearchProviderMaxResults);
}
}
......
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