summaryrefslogtreecommitdiffstats
path: root/chrome/browser/background_sync
diff options
context:
space:
mode:
Diffstat (limited to 'chrome/browser/background_sync')
-rw-r--r--chrome/browser/background_sync/OWNERS1
-rw-r--r--chrome/browser/background_sync/PRESUBMIT.py12
-rw-r--r--chrome/browser/background_sync/background_sync_controller_factory.cc41
-rw-r--r--chrome/browser/background_sync/background_sync_controller_factory.h35
-rw-r--r--chrome/browser/background_sync/background_sync_controller_impl.cc30
-rw-r--r--chrome/browser/background_sync/background_sync_controller_impl.h38
-rw-r--r--chrome/browser/background_sync/background_sync_controller_impl_unittest.cc70
7 files changed, 227 insertions, 0 deletions
diff --git a/chrome/browser/background_sync/OWNERS b/chrome/browser/background_sync/OWNERS
new file mode 100644
index 0000000..1e6deee
--- /dev/null
+++ b/chrome/browser/background_sync/OWNERS
@@ -0,0 +1 @@
+jkarlin@chromium.org
diff --git a/chrome/browser/background_sync/PRESUBMIT.py b/chrome/browser/background_sync/PRESUBMIT.py
new file mode 100644
index 0000000..7df0d54
--- /dev/null
+++ b/chrome/browser/background_sync/PRESUBMIT.py
@@ -0,0 +1,12 @@
+# 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.
+
+"""Top-level presubmit script for src/chrome/browser/background_sync/
+
+See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
+for more details about the presubmit API built into depot_tools.
+"""
+
+def CheckChangeOnUpload(input_api, output_api):
+ return input_api.canned_checks.CheckPatchFormatted(input_api, output_api)
diff --git a/chrome/browser/background_sync/background_sync_controller_factory.cc b/chrome/browser/background_sync/background_sync_controller_factory.cc
new file mode 100644
index 0000000..aefa72b
--- /dev/null
+++ b/chrome/browser/background_sync/background_sync_controller_factory.cc
@@ -0,0 +1,41 @@
+// 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 "background_sync_controller_factory.h"
+
+#include "chrome/browser/background_sync/background_sync_controller_impl.h"
+#include "chrome/browser/profiles/incognito_helpers.h"
+#include "chrome/browser/profiles/profile.h"
+#include "components/keyed_service/content/browser_context_dependency_manager.h"
+
+// static
+BackgroundSyncControllerImpl* BackgroundSyncControllerFactory::GetForProfile(
+ Profile* profile) {
+ return static_cast<BackgroundSyncControllerImpl*>(
+ GetInstance()->GetServiceForBrowserContext(profile, true));
+}
+
+// static
+BackgroundSyncControllerFactory*
+BackgroundSyncControllerFactory::GetInstance() {
+ return base::Singleton<BackgroundSyncControllerFactory>::get();
+}
+
+BackgroundSyncControllerFactory::BackgroundSyncControllerFactory()
+ : BrowserContextKeyedServiceFactory(
+ "BackgroundSyncService",
+ BrowserContextDependencyManager::GetInstance()) {}
+
+BackgroundSyncControllerFactory::~BackgroundSyncControllerFactory() {}
+
+KeyedService* BackgroundSyncControllerFactory::BuildServiceInstanceFor(
+ content::BrowserContext* context) const {
+ return new BackgroundSyncControllerImpl(Profile::FromBrowserContext(context));
+}
+
+content::BrowserContext*
+BackgroundSyncControllerFactory::GetBrowserContextToUse(
+ content::BrowserContext* context) const {
+ return chrome::GetBrowserContextOwnInstanceInIncognito(context);
+}
diff --git a/chrome/browser/background_sync/background_sync_controller_factory.h b/chrome/browser/background_sync/background_sync_controller_factory.h
new file mode 100644
index 0000000..b93dbe9
--- /dev/null
+++ b/chrome/browser/background_sync/background_sync_controller_factory.h
@@ -0,0 +1,35 @@
+// 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.
+
+#ifndef CHROME_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_CONTROLLER_FACTORY_H_
+#define CHROME_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_CONTROLLER_FACTORY_H_
+
+#include "base/memory/singleton.h"
+#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
+
+class BackgroundSyncControllerImpl;
+class Profile;
+
+class BackgroundSyncControllerFactory
+ : public BrowserContextKeyedServiceFactory {
+ public:
+ static BackgroundSyncControllerImpl* GetForProfile(Profile* profile);
+ static BackgroundSyncControllerFactory* GetInstance();
+
+ private:
+ friend struct base::DefaultSingletonTraits<BackgroundSyncControllerFactory>;
+
+ BackgroundSyncControllerFactory();
+ ~BackgroundSyncControllerFactory() override;
+
+ // BrowserContextKeyedBaseFactory methods:
+ KeyedService* BuildServiceInstanceFor(
+ content::BrowserContext* context) const override;
+ content::BrowserContext* GetBrowserContextToUse(
+ content::BrowserContext* context) const override;
+
+ DISALLOW_COPY_AND_ASSIGN(BackgroundSyncControllerFactory);
+};
+
+#endif // CHROME_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_CONTROLLER_FACTORY_H_
diff --git a/chrome/browser/background_sync/background_sync_controller_impl.cc b/chrome/browser/background_sync/background_sync_controller_impl.cc
new file mode 100644
index 0000000..7668229
--- /dev/null
+++ b/chrome/browser/background_sync/background_sync_controller_impl.cc
@@ -0,0 +1,30 @@
+// 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/background_sync/background_sync_controller_impl.h"
+
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/profiles/profile.h"
+#include "components/rappor/rappor_utils.h"
+
+BackgroundSyncControllerImpl::BackgroundSyncControllerImpl(Profile* profile)
+ : profile_(profile) {}
+
+BackgroundSyncControllerImpl::~BackgroundSyncControllerImpl() = default;
+
+rappor::RapporService* BackgroundSyncControllerImpl::GetRapporService() {
+ return g_browser_process->rappor_service();
+}
+
+void BackgroundSyncControllerImpl::NotifyBackgroundSyncRegistered(
+ const GURL& origin) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ DCHECK_EQ(origin, origin.GetOrigin());
+
+ if (profile_->IsOffTheRecord())
+ return;
+
+ rappor::SampleDomainAndRegistryFromGURL(
+ GetRapporService(), "BackgroundSync.Register.Origin", origin);
+}
diff --git a/chrome/browser/background_sync/background_sync_controller_impl.h b/chrome/browser/background_sync/background_sync_controller_impl.h
new file mode 100644
index 0000000..156a6244
--- /dev/null
+++ b/chrome/browser/background_sync/background_sync_controller_impl.h
@@ -0,0 +1,38 @@
+// 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.
+
+#ifndef CHROME_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_CONTROLLER_IMPL_H_
+#define CHROME_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_CONTROLLER_IMPL_H_
+
+#include "content/public/browser/background_sync_controller.h"
+
+#include "components/keyed_service/core/keyed_service.h"
+#include "content/public/browser/browser_thread.h"
+
+namespace rappor {
+class RapporService;
+}
+
+class Profile;
+
+class BackgroundSyncControllerImpl : public content::BackgroundSyncController,
+ public KeyedService {
+ public:
+ explicit BackgroundSyncControllerImpl(Profile* profile);
+ ~BackgroundSyncControllerImpl() override;
+
+ // content::BackgroundSyncController overrides.
+ void NotifyBackgroundSyncRegistered(const GURL& origin) override;
+
+ protected:
+ // Virtual for testing.
+ virtual rappor::RapporService* GetRapporService();
+
+ private:
+ Profile* profile_; // This object is owned by profile_.
+
+ DISALLOW_COPY_AND_ASSIGN(BackgroundSyncControllerImpl);
+};
+
+#endif // CHROME_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_CONTROLLER_IMPL_H_
diff --git a/chrome/browser/background_sync/background_sync_controller_impl_unittest.cc b/chrome/browser/background_sync/background_sync_controller_impl_unittest.cc
new file mode 100644
index 0000000..1e20813
--- /dev/null
+++ b/chrome/browser/background_sync/background_sync_controller_impl_unittest.cc
@@ -0,0 +1,70 @@
+// 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/background_sync/background_sync_controller_impl.h"
+
+#include "chrome/test/base/testing_profile.h"
+#include "components/rappor/test_rappor_service.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "url/gurl.h"
+
+namespace {
+
+class TestBackgroundSyncControllerImpl : public BackgroundSyncControllerImpl {
+ public:
+ TestBackgroundSyncControllerImpl(Profile* profile,
+ rappor::TestRapporService* rappor_service)
+ : BackgroundSyncControllerImpl(profile),
+ rappor_service_(rappor_service) {}
+
+ protected:
+ rappor::RapporService* GetRapporService() override { return rappor_service_; }
+
+ private:
+ rappor::TestRapporService* rappor_service_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestBackgroundSyncControllerImpl);
+};
+
+class BackgroundSyncControllerImplTest : public testing::Test {
+ protected:
+ BackgroundSyncControllerImplTest()
+ : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
+ controller_(new TestBackgroundSyncControllerImpl(&profile_,
+ &rappor_service_)) {}
+
+ content::TestBrowserThreadBundle thread_bundle_;
+ TestingProfile profile_;
+ rappor::TestRapporService rappor_service_;
+ scoped_ptr<TestBackgroundSyncControllerImpl> controller_;
+
+ DISALLOW_COPY_AND_ASSIGN(BackgroundSyncControllerImplTest);
+};
+
+TEST_F(BackgroundSyncControllerImplTest, RapporTest) {
+ GURL url("http://www.example.com/foo/");
+ EXPECT_EQ(0, rappor_service_.GetReportsCount());
+ controller_->NotifyBackgroundSyncRegistered(url.GetOrigin());
+ EXPECT_EQ(1, rappor_service_.GetReportsCount());
+
+ std::string sample;
+ rappor::RapporType type;
+ LOG(ERROR) << url.GetOrigin().GetOrigin();
+ EXPECT_TRUE(rappor_service_.GetRecordedSampleForMetric(
+ "BackgroundSync.Register.Origin", &sample, &type));
+ EXPECT_EQ("example.com", sample);
+ EXPECT_EQ(rappor::ETLD_PLUS_ONE_RAPPOR_TYPE, type);
+}
+
+TEST_F(BackgroundSyncControllerImplTest, NoRapporWhenOffTheRecord) {
+ GURL url("http://www.example.com/foo/");
+ controller_.reset(new TestBackgroundSyncControllerImpl(
+ profile_.GetOffTheRecordProfile(), &rappor_service_));
+
+ controller_->NotifyBackgroundSyncRegistered(url.GetOrigin());
+ EXPECT_EQ(0, rappor_service_.GetReportsCount());
+}
+
+} // namespace