summaryrefslogtreecommitdiffstats
path: root/chrome/browser/android/metrics/launch_metrics.cc
blob: 77003c611977a592b7e507fa3f7aa8deaa6f7adf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 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/android/metrics/launch_metrics.h"

#include "base/android/jni_string.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "base/time/time.h"
#include "chrome/browser/android/shortcut_info.h"
#include "chrome/browser/banners/app_banner_settings_helper.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/engagement/site_engagement_service.h"
#include "chrome/browser/profiles/profile.h"
#include "components/rappor/rappor_utils.h"
#include "content/public/browser/web_contents.h"
#include "jni/LaunchMetrics_jni.h"
#include "url/gurl.h"

namespace metrics {

enum HomeScreenLaunch {
  HOME_SCREEN_LAUNCH_STANDALONE = 0,
  HOME_SCREEN_LAUNCH_SHORTCUT = 1,
  HOME_SCREEN_LAUNCH_COUNT = 2
};

bool RegisterLaunchMetrics(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

static void RecordLaunch(JNIEnv* env,
                         const JavaParamRef<jclass>& caller,
                         jboolean standalone,
                         const JavaParamRef<jstring>& jurl,
                         int source,
                         const JavaParamRef<jobject>& jweb_contents) {
  GURL url(base::android::ConvertJavaStringToUTF8(env, jurl));

  content::WebContents* web_contents =
      content::WebContents::FromJavaWebContents(jweb_contents);
  if (web_contents && source == ShortcutInfo::SOURCE_APP_BANNER) {
    // What a user has installed on the Home screen can become disconnected from
    // what Chrome believes is on the Home screen if the user clears their data.
    // Use the launch as a signal that the shortcut still exists.
    AppBannerSettingsHelper::RecordBannerEvent(
        web_contents, url, url.spec(),
        AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN,
        base::Time::Now());

    // Tell the Site Engagement Service about this launch as sites recently
    // launched from a shortcut receive a boost to their engagement.
    SiteEngagementService* service = SiteEngagementService::Get(
        Profile::FromBrowserContext(web_contents->GetBrowserContext()));
    if (service)
      service->SetLastShortcutLaunchTime(url);
  }

  std::string rappor_metric_source;
  if (source == ShortcutInfo::SOURCE_ADD_TO_HOMESCREEN)
    rappor_metric_source = "Launch.HomeScreenSource.AddToHomeScreen";
  else if (source == ShortcutInfo::SOURCE_APP_BANNER)
    rappor_metric_source = "Launch.HomeScreenSource.AppBanner";
  else if (source == ShortcutInfo::SOURCE_BOOKMARK_NAVIGATOR_WIDGET)
    rappor_metric_source = "Launch.HomeScreenSource.BookmarkNavigatorWidget";
  else if (source == ShortcutInfo::SOURCE_BOOKMARK_SHORTCUT_WIDGET)
    rappor_metric_source = "Launch.HomeScreenSource.BookmarkShortcutWidget";
  else
    rappor_metric_source = "Launch.HomeScreenSource.Unknown";

  UMA_HISTOGRAM_ENUMERATION("Launch.HomeScreenSource", source,
                            ShortcutInfo::SOURCE_COUNT);

  rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(),
                                          rappor_metric_source, url);

  int action = standalone ? HOME_SCREEN_LAUNCH_STANDALONE
                          : HOME_SCREEN_LAUNCH_SHORTCUT;
  std::string rappor_metric_action = standalone ? "Launch.HomeScreen.Standalone"
                                                : "Launch.HomeScreen.Shortcut";

  UMA_HISTOGRAM_ENUMERATION("Launch.HomeScreen", action,
                            HOME_SCREEN_LAUNCH_COUNT);

  rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(),
                                          rappor_metric_action, url);
}

};  // namespace metrics