Home | History | Annotate | Download | only in ntp
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/metrics/histogram.h"
      9 #include "chrome/browser/profiles/profile.h"
     10 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
     11 #include "chrome/common/pref_names.h"
     12 #include "components/pref_registry/pref_registry_syncable.h"
     13 #include "content/public/browser/web_ui.h"
     14 #include "extensions/browser/extension_registry.h"
     15 #include "extensions/common/constants.h"
     16 #include "extensions/common/extension.h"
     17 #include "extensions/common/extension_set.h"
     18 #include "net/base/escape.h"
     19 
     20 namespace {
     21 const net::UnescapeRule::Type kUnescapeRules =
     22     net::UnescapeRule::NORMAL | net::UnescapeRule::URL_SPECIAL_CHARS;
     23 }
     24 
     25 CoreAppLauncherHandler::CoreAppLauncherHandler() {}
     26 
     27 CoreAppLauncherHandler::~CoreAppLauncherHandler() {}
     28 
     29 // static
     30 void CoreAppLauncherHandler::RecordAppLaunchType(
     31     extension_misc::AppLaunchBucket bucket,
     32     extensions::Manifest::Type app_type) {
     33   DCHECK_LT(bucket, extension_misc::APP_LAUNCH_BUCKET_BOUNDARY);
     34   if (app_type == extensions::Manifest::TYPE_PLATFORM_APP) {
     35     UMA_HISTOGRAM_ENUMERATION(extension_misc::kPlatformAppLaunchHistogram,
     36                               bucket,
     37                               extension_misc::APP_LAUNCH_BUCKET_BOUNDARY);
     38   } else {
     39     UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppLaunchHistogram,
     40                               bucket,
     41                               extension_misc::APP_LAUNCH_BUCKET_BOUNDARY);
     42   }
     43 }
     44 
     45 // static
     46 void CoreAppLauncherHandler::RecordAppListSearchLaunch(
     47     const extensions::Extension* extension) {
     48   extension_misc::AppLaunchBucket bucket =
     49       extension_misc::APP_LAUNCH_APP_LIST_SEARCH;
     50   if (extension->id() == extensions::kWebStoreAppId)
     51     bucket = extension_misc::APP_LAUNCH_APP_LIST_SEARCH_WEBSTORE;
     52   else if (extension->id() == extension_misc::kChromeAppId)
     53     bucket = extension_misc::APP_LAUNCH_APP_LIST_SEARCH_CHROME;
     54   RecordAppLaunchType(bucket, extension->GetType());
     55 }
     56 
     57 // static
     58 void CoreAppLauncherHandler::RecordAppListMainLaunch(
     59     const extensions::Extension* extension) {
     60   extension_misc::AppLaunchBucket bucket =
     61       extension_misc::APP_LAUNCH_APP_LIST_MAIN;
     62   if (extension->id() == extensions::kWebStoreAppId)
     63     bucket = extension_misc::APP_LAUNCH_APP_LIST_MAIN_WEBSTORE;
     64   else if (extension->id() == extension_misc::kChromeAppId)
     65     bucket = extension_misc::APP_LAUNCH_APP_LIST_MAIN_CHROME;
     66   RecordAppLaunchType(bucket, extension->GetType());
     67 }
     68 
     69 // static
     70 void CoreAppLauncherHandler::RecordWebStoreLaunch() {
     71   RecordAppLaunchType(extension_misc::APP_LAUNCH_NTP_WEBSTORE,
     72                       extensions::Manifest::TYPE_HOSTED_APP);
     73 }
     74 
     75 // static
     76 void CoreAppLauncherHandler::RegisterProfilePrefs(
     77     user_prefs::PrefRegistrySyncable* registry) {
     78   registry->RegisterListPref(prefs::kNtpAppPageNames,
     79                              user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
     80 }
     81 
     82 void CoreAppLauncherHandler::HandleRecordAppLaunchByUrl(
     83     const base::ListValue* args) {
     84   std::string url;
     85   CHECK(args->GetString(0, &url));
     86   double source;
     87   CHECK(args->GetDouble(1, &source));
     88 
     89   extension_misc::AppLaunchBucket bucket =
     90       static_cast<extension_misc::AppLaunchBucket>(static_cast<int>(source));
     91   CHECK(source < extension_misc::APP_LAUNCH_BUCKET_BOUNDARY);
     92 
     93   RecordAppLaunchByUrl(Profile::FromWebUI(web_ui()), url, bucket);
     94 }
     95 
     96 void CoreAppLauncherHandler::RecordAppLaunchByUrl(
     97     Profile* profile,
     98     std::string escaped_url,
     99     extension_misc::AppLaunchBucket bucket) {
    100   CHECK(bucket != extension_misc::APP_LAUNCH_BUCKET_INVALID);
    101 
    102   GURL url(net::UnescapeURLComponent(escaped_url, kUnescapeRules));
    103   if (!extensions::ExtensionRegistry::Get(profile)
    104           ->enabled_extensions().GetAppByURL(url)) {
    105     return;
    106   }
    107 
    108   RecordAppLaunchType(bucket, extensions::Manifest::TYPE_HOSTED_APP);
    109 }
    110 
    111 void CoreAppLauncherHandler::RegisterMessages() {
    112   web_ui()->RegisterMessageCallback("recordAppLaunchByURL",
    113       base::Bind(&CoreAppLauncherHandler::HandleRecordAppLaunchByUrl,
    114                  base::Unretained(this)));
    115 }
    116