Chromium Code Reviews| Index: chrome/browser/extensions/api/search_engines_private/search_engines_private_event_router.cc |
| diff --git a/chrome/browser/extensions/api/search_engines_private/search_engines_private_event_router.cc b/chrome/browser/extensions/api/search_engines_private/search_engines_private_event_router.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c2493315216f62cb6a6a852cdccf3369e4105dc8 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/search_engines_private/search_engines_private_event_router.cc |
| @@ -0,0 +1,118 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/api/search_engines_private/search_engines_private_event_router.h" |
| + |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/search_engines/template_url_service_factory.h" |
| +#include "chrome/common/extensions/api/search_engines_private.h" |
| +#include "content/public/browser/browser_context.h" |
| + |
| +namespace extensions { |
| + |
| +SearchEnginesPrivateEventRouter::SearchEnginesPrivateEventRouter( |
| + content::BrowserContext* context) |
| + : context_(context), listening_(false) { |
|
stevenjb
2015/04/23 17:26:01
Initialize template_url_service_
Oren Blasberg
2015/04/23 21:31:51
Done.
|
| + // Register with the event router so we know when renderers are listening to |
| + // our events. We first check and see if there *is* an event router, because |
| + // some unit tests try to create all context services, but don't initialize |
| + // the event router first. |
| + EventRouter* event_router = EventRouter::Get(context_); |
| + if (event_router) { |
|
stevenjb
2015/04/23 17:26:01
Early exit
Oren Blasberg
2015/04/23 21:31:51
Done.
|
| + event_router->RegisterObserver( |
| + this, |
| + api::search_engines_private::OnDefaultSearchEnginesChanged::kEventName); |
| + template_url_service_ = TemplateURLServiceFactory::GetForProfile( |
| + Profile::FromBrowserContext(context_)); |
| + StartOrStopListeningForSearchEnginesChanges(); |
| + } |
| +} |
| + |
| +SearchEnginesPrivateEventRouter::~SearchEnginesPrivateEventRouter() { |
| + DCHECK(!listening_); |
| +} |
| + |
| +void SearchEnginesPrivateEventRouter::Shutdown() { |
| + // Unregister with the event router. We first check and see if there *is* an |
| + // event router, because some unit tests try to shutdown all context services, |
| + // but didn't initialize the event router first. |
| + EventRouter* event_router = EventRouter::Get(context_); |
| + if (event_router) |
| + event_router->UnregisterObserver(this); |
| + |
| + if (listening_) { |
| + template_url_service_->RemoveObserver(this); |
| + } |
|
stevenjb
2015/04/23 17:26:01
no {}
Oren Blasberg
2015/04/23 21:31:51
Done.
|
| + listening_ = false; |
| +} |
| + |
| +void SearchEnginesPrivateEventRouter::OnListenerAdded( |
| + const EventListenerInfo& details) { |
| + // Start listening to search engines change events. |
| + StartOrStopListeningForSearchEnginesChanges(); |
| +} |
| + |
| +void SearchEnginesPrivateEventRouter::OnListenerRemoved( |
| + const EventListenerInfo& details) { |
| + // Stop listening to events if there are no more listeners. |
| + StartOrStopListeningForSearchEnginesChanges(); |
| +} |
| + |
| +void SearchEnginesPrivateEventRouter::OnTemplateURLServiceChanged() { |
| + if (!template_url_service_ || !template_url_service_->loaded()) |
| + return; |
| + |
| + const TemplateURL* default_url = |
| + template_url_service_->GetDefaultSearchProvider(); |
| + std::vector<TemplateURL*> urls = template_url_service_->GetTemplateURLs(); |
| + |
| + std::vector<linked_ptr<api::search_engines_private::SearchEngine>> engines; |
| + for (size_t i = 0; i < urls.size(); i++) { |
|
stevenjb
2015/04/23 17:26:01
for (TemplateURL* url : urls)
Oren Blasberg
2015/04/23 21:31:51
Done.
|
| + api::search_engines_private::SearchEngine* engine = |
| + new api::search_engines_private::SearchEngine(); |
| + engine->guid = urls[i]->sync_guid(); |
| + engine->name = UTF16ToASCII(urls[i]->short_name()); |
| + engine->is_selected = urls[i] == default_url; |
| + |
| + engines.push_back( |
| + linked_ptr<api::search_engines_private::SearchEngine>(engine)); |
| + } |
| + |
| + scoped_ptr<base::ListValue> args( |
| + api::search_engines_private::OnDefaultSearchEnginesChanged::Create( |
| + engines)); |
| + |
| + scoped_ptr<Event> extension_event(new Event( |
| + api::search_engines_private::OnDefaultSearchEnginesChanged::kEventName, |
| + args.Pass())); |
| + EventRouter::Get(context_)->BroadcastEvent(extension_event.Pass()); |
| +} |
| + |
| +void SearchEnginesPrivateEventRouter:: |
| + StartOrStopListeningForSearchEnginesChanges() { |
| + EventRouter* event_router = EventRouter::Get(context_); |
| + bool should_listen = event_router->HasEventListener( |
| + api::search_engines_private::OnDefaultSearchEnginesChanged::kEventName); |
| + |
| + if (should_listen && !listening_) { |
| + template_url_service_->Load(); |
| + template_url_service_->AddObserver(this); |
| + } else if (!should_listen && listening_) { |
| + template_url_service_->RemoveObserver(this); |
| + } |
| + listening_ = should_listen; |
| +} |
| + |
| +SearchEnginesPrivateEventRouter* SearchEnginesPrivateEventRouter::Create( |
| + content::BrowserContext* context) { |
| + return new SearchEnginesPrivateEventRouter(context); |
| +} |
| + |
| +} // namespace extensions |