Chromium Code Reviews| Index: ios/chrome/browser/desktop_promotion/desktop_promotion_sync_observer.mm |
| diff --git a/ios/chrome/browser/desktop_promotion/desktop_promotion_sync_observer.mm b/ios/chrome/browser/desktop_promotion/desktop_promotion_sync_observer.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ecfe8365d1a9315d955850ba1a5c5262a6185504 |
| --- /dev/null |
| +++ b/ios/chrome/browser/desktop_promotion/desktop_promotion_sync_observer.mm |
| @@ -0,0 +1,85 @@ |
| +// Copyright 2017 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. |
| + |
| +#import "ios/chrome/browser/desktop_promotion/desktop_promotion_sync_observer.h" |
| + |
| +#include <memory> |
| + |
| +#include "base/metrics/histogram_macros.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/time/time.h" |
| +#include "components/browser_sync/profile_sync_service.h" |
| +#include "components/prefs/pref_service.h" |
| +#include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| +#include "ios/chrome/browser/desktop_promotion/desktop_promotion_prefs.h" |
| +#include "ios/chrome/browser/desktop_promotion/histogram_macros.h" |
| +#include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h" |
| + |
| +namespace { |
| + |
| +const char* kDesktopIOSPromotionEntrypointHistogramPrefix[] = { |
| + "SavePasswordsNewBubble", "BookmarksNewBubble", "BookmarksExistingBubble", |
| + "HistoryPage"}; |
| +} |
| + |
| +DesktopPromotionSyncObserver::DesktopPromotionSyncObserver( |
| + ios::ChromeBrowserState* browser_state) |
| + : browser_state_(browser_state) { |
| + browser_sync::ProfileSyncService* sync_service = |
|
sdefresne
2017/01/25 09:52:52
Unless there is no other way around passing the se
mrefaat1
2017/01/26 16:51:19
Acknowledged.
|
| + IOSChromeProfileSyncServiceFactory::GetForBrowserState(browser_state_); |
| + sync_service->AddObserver(this); |
| +} |
| + |
| +DesktopPromotionSyncObserver::~DesktopPromotionSyncObserver() { |
| + browser_sync::ProfileSyncService* sync_service = |
| + IOSChromeProfileSyncServiceFactory::GetForBrowserState(browser_state_); |
| + sync_service->RemoveObserver(this); |
| +} |
| + |
| +void DesktopPromotionSyncObserver::OnStateChanged() { |
| + browser_sync::ProfileSyncService* sync_service = |
| + IOSChromeProfileSyncServiceFactory::GetForBrowserState(browser_state_); |
| + if (!desktop_metrics_logger_initiated_ && |
|
sdefresne
2017/01/25 09:58:22
Unrelated to KeyedService, I think you should swap
mrefaat1
2017/01/26 16:51:19
Acknowledged.
|
| + sync_service->GetActiveDataTypes().Has(syncer::PRIORITY_PREFERENCES)) { |
| + desktop_metrics_logger_initiated_ = true; |
| + PrefService* prefs = browser_state_->GetPrefs(); |
| + bool done_logging = prefs->GetBoolean(prefs::kDesktopIOSPromotionDone); |
| + double last_impression = |
| + prefs->GetDouble(prefs::kDesktopIOSPromotionLastImpression); |
| + base::TimeDelta delta = |
| + base::Time::Now() - base::Time::FromDoubleT(last_impression); |
| + if (!done_logging && delta.InDays() < 7) { |
| + // This user have seen the promotion in the last 7 days so it may be a |
| + // reason of the installation. |
| + int sms_entrypoint = |
| + prefs->GetInteger(prefs::kDesktopIOSPromotionSMSEntryPoint); |
| + int targeted_entrypoints = |
| + prefs->GetInteger(prefs::kDesktopIOSPromotionTargetedEntryPoints); |
| + |
| + // Entry points are represented on the preference by integers [1..4]. |
| + int entrypoint_prefixes_count = |
| + arraysize(kDesktopIOSPromotionEntrypointHistogramPrefix); |
| + for (int i = 1; i < entrypoint_prefixes_count + 1; i++) { |
| + if (sms_entrypoint == i) { |
| + UMA_HISTOGRAM_ENUMERATION( |
| + "DesktopIOSPromotion.SMSSent.IOSSigninReason", i, |
| + entrypoint_prefixes_count + 1); |
| + UMA_HISTOGRAM_COUNT_1000_NO_CACHE( |
| + base::StringPrintf( |
| + "DesktopIOSPromotion.%s.CompletionTime", |
| + kDesktopIOSPromotionEntrypointHistogramPrefix[i - 1]), |
| + delta.InHours()); |
| + } else { |
| + // If the user saw this promotion type, log that it could be a reason |
| + // for the signin. |
| + if ((1 << i) & targeted_entrypoints) |
| + UMA_HISTOGRAM_ENUMERATION( |
| + "DesktopIOSPromotion.NoSMS.IOSSigninReason", i, |
| + entrypoint_prefixes_count + 1); |
| + } |
| + } |
| + prefs->SetBoolean(prefs::kDesktopIOSPromotionDone, true); |
| + } |
| + } |
| +} |