Commit 9f2cc0c6 authored by jianli@chromium.org's avatar jianli@chromium.org

[GCM] Observe the event listener adding such that GCM can be started

BUG=349578
TEST=Manual test by following repro steps
R=fgorski@chromium.org

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@255729 0039d316-1c4b-4281-b951-d872f2087c98
parent 1a8b3cad
......@@ -15,7 +15,6 @@
#include "chrome/browser/services/gcm/gcm_profile_service.h"
#include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
#include "chrome/common/extensions/api/gcm.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension.h"
......@@ -179,9 +178,21 @@ bool GcmSendFunction::ValidateMessageData(
return total_size != 0;
}
GcmJsEventRouter::GcmJsEventRouter(Profile* profile) : profile_(profile) {}
GcmJsEventRouter::GcmJsEventRouter(Profile* profile) : profile_(profile) {
if (ExtensionSystem::Get(profile_)->event_router()) {
ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
this, api::gcm::OnMessage::kEventName);
ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
this, api::gcm::OnMessagesDeleted::kEventName);
ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
this, api::gcm::OnSendError::kEventName);
}
}
GcmJsEventRouter::~GcmJsEventRouter() {}
GcmJsEventRouter::~GcmJsEventRouter() {
if (ExtensionSystem::Get(profile_)->event_router())
ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
}
void GcmJsEventRouter::OnMessage(
const std::string& app_id,
......@@ -223,4 +234,12 @@ void GcmJsEventRouter::OnSendError(const std::string& app_id,
app_id, event.Pass());
}
void GcmJsEventRouter::OnListenerAdded(const EventListenerInfo& details) {
if (gcm::GCMProfileService::GetGCMEnabledState(profile_) ==
gcm::GCMProfileService::ALWAYS_DISABLED) {
return;
}
gcm::GCMProfileServiceFactory::GetForProfile(profile_)->Start();
}
} // namespace extensions
......@@ -7,6 +7,7 @@
#include "chrome/browser/services/gcm/gcm_event_router.h"
#include "chrome/common/extensions/api/gcm.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_function.h"
#include "google_apis/gcm/gcm_client.h"
......@@ -75,7 +76,8 @@ class GcmSendFunction : public GcmApiFunction {
bool ValidateMessageData(const gcm::GCMClient::MessageData& data) const;
};
class GcmJsEventRouter : public gcm::GCMEventRouter {
class GcmJsEventRouter : public gcm::GCMEventRouter,
public EventRouter::Observer {
public:
explicit GcmJsEventRouter(Profile* profile);
......@@ -90,6 +92,9 @@ class GcmJsEventRouter : public gcm::GCMEventRouter {
const std::string& message_id,
gcm::GCMClient::Result result) OVERRIDE;
// EventRouter::Observer:
virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
private:
// The application we route the event to is running in context of the
// |profile_| and the latter outlives the event router.
......
......@@ -569,6 +569,10 @@ void GCMProfileService::Initialize(
chrome:: NOTIFICATION_EXTENSION_UNINSTALLED,
content::Source<Profile>(profile_));
#if !defined(OS_ANDROID)
js_event_router_.reset(new extensions::GcmJsEventRouter(profile_));
#endif
// Get the list of available accounts.
std::vector<std::string> account_ids;
#if !defined(OS_ANDROID)
......@@ -618,6 +622,12 @@ void GCMProfileService::Stop() {
base::Bind(&GCMProfileService::IOWorker::Stop, io_worker_));
}
void GCMProfileService::Shutdown() {
#if !defined(OS_ANDROID)
js_event_router_.reset();
#endif
}
void GCMProfileService::Register(const std::string& app_id,
const std::vector<std::string>& sender_ids,
RegisterCallback callback) {
......@@ -825,11 +835,6 @@ void GCMProfileService::EnsureLoaded() {
return;
username_ = username;
#if !defined(OS_ANDROID)
if (!js_event_router_)
js_event_router_.reset(new extensions::GcmJsEventRouter(profile_));
#endif
DCHECK(!delayed_task_controller_);
delayed_task_controller_.reset(new DelayedTaskController);
......
......@@ -86,6 +86,9 @@ class GCMProfileService : public BrowserContextKeyedService,
void Stop();
// BrowserContextKeyedService implementation.
virtual void Shutdown() OVERRIDE;
// Registers |sender_id| for an app. A registration ID will be returned by
// the GCM server.
// |app_id|: application ID.
......
......@@ -30,6 +30,8 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -294,6 +296,12 @@ class GCMProfileServiceTestConsumer : public GCMProfileService::TestingDelegate{
CommandLine::ForCurrentProcess(), base::FilePath(), false);
extension_service_ = extension_system->Get(profile())->extension_service();
// EventRouter is needed for GcmJsEventRouter.
if (!extension_system->event_router()) {
extension_system->SetEventRouter(scoped_ptr<EventRouter>(
new EventRouter(profile(), ExtensionPrefs::Get(profile()))));
}
// Enable GCM such that tests could be run on all channels.
profile()->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, true);
......
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