Moved from http://codereview.chromium.org/7811020/ (same CL but was on a read-only check-out)

BUG=94920 

-Related:
Check in a new CRX:
http://codereview.chromium.org/7828014/
Check in a few GYP changes:
http://codereview.chromium.org/7827021/

-Modified profile_impl to search for and auto-install apps found in DIR_DEFAULT_APPS when profile created 

-Added tracking code for number of extension installs and uninstalls and number of webstore visits.
Review URL: http://codereview.chromium.org/7839009

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@100727 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/extensions/crx_installer.cc b/chrome/browser/extensions/crx_installer.cc
index aebe4cb..db0a1c0 100644
--- a/chrome/browser/extensions/crx_installer.cc
+++ b/chrome/browser/extensions/crx_installer.cc
@@ -31,6 +31,7 @@
 #include "chrome/common/extensions/extension_constants.h"
 #include "chrome/common/extensions/extension_file_util.h"
 #include "content/browser/browser_thread.h"
+#include "content/browser/user_metrics.h"
 #include "content/common/notification_service.h"
 #include "grit/chromium_strings.h"
 #include "grit/generated_resources.h"
@@ -538,6 +539,13 @@
 
 void CrxInstaller::ReportSuccessFromFileThread() {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+
+  // Tracking number of extensions installed by users
+  if (install_cause() == extension_misc::INSTALL_CAUSE_USER_DOWNLOAD) {
+    UserMetrics::RecordAction(
+        UserMetricsAction("Extensions.ExtensionInstalled"));
+  }
+
   if (!BrowserThread::PostTask(
           BrowserThread::UI, FROM_HERE,
           NewRunnableMethod(this,
diff --git a/chrome/browser/extensions/extension_service.cc b/chrome/browser/extensions/extension_service.cc
index dc4d403..59770e7 100644
--- a/chrome/browser/extensions/extension_service.cc
+++ b/chrome/browser/extensions/extension_service.cc
@@ -78,6 +78,7 @@
 #include "content/browser/plugin_process_host.h"
 #include "content/browser/plugin_service.h"
 #include "content/browser/renderer_host/render_process_host.h"
+#include "content/browser/user_metrics.h"
 #include "content/common/content_notification_types.h"
 #include "content/common/json_value_serializer.h"
 #include "content/common/notification_service.h"
@@ -956,6 +957,10 @@
     sync_bundle->synced_extensions.erase(extension_id);
   }
 
+  // Track the uninstallation.
+  UserMetrics::RecordAction(
+      UserMetricsAction("Extensions.ExtensionUninstalled"));
+
   return true;
 }
 
diff --git a/chrome/browser/profiles/profile_impl.cc b/chrome/browser/profiles/profile_impl.cc
index af5435f8a..c790d55 100644
--- a/chrome/browser/profiles/profile_impl.cc
+++ b/chrome/browser/profiles/profile_impl.cc
@@ -26,6 +26,7 @@
 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
 #include "chrome/browser/defaults.h"
 #include "chrome/browser/download/chrome_download_manager_delegate.h"
+#include "chrome/browser/extensions/crx_installer.h"
 #include "chrome/browser/extensions/extension_devtools_manager.h"
 #include "chrome/browser/extensions/extension_error_reporter.h"
 #include "chrome/browser/extensions/extension_event_router.h"
@@ -38,11 +39,13 @@
 #include "chrome/browser/extensions/extension_special_storage_policy.h"
 #include "chrome/browser/extensions/user_script_master.h"
 #include "chrome/browser/favicon/favicon_service.h"
+#include "chrome/browser/first_run/first_run.h"
 #include "chrome/browser/geolocation/chrome_geolocation_permission_context.h"
 #include "chrome/browser/history/history.h"
 #include "chrome/browser/history/shortcuts_backend.h"
 #include "chrome/browser/history/top_sites.h"
 #include "chrome/browser/instant/instant_controller.h"
+#include "chrome/browser/mac/keystone_glue.h"
 #include "chrome/browser/metrics/metrics_service.h"
 #include "chrome/browser/net/chrome_url_request_context.h"
 #include "chrome/browser/net/gaia/token_service.h"
@@ -87,6 +90,7 @@
 #include "chrome/common/pref_names.h"
 #include "chrome/common/render_messages.h"
 #include "chrome/common/spellcheck_messages.h"
+#include "chrome/installer/util/google_update_settings.h"
 #include "content/browser/appcache/chrome_appcache_service.h"
 #include "content/browser/browser_thread.h"
 #include "content/browser/chrome_blob_storage_context.h"
@@ -500,6 +504,26 @@
       extensions_enabled));
 
   RegisterComponentExtensions();
+
+#if defined(GOOGLE_CHROME_BUILD)
+  // If first run and brand code not equal to ECDB, install default apps.
+#if defined(OS_WIN)
+  string16 brand;
+  GoogleUpdateSettings::GetBrand(&brand);
+#elif defined(OS_MACOSX)
+  std::string brand = keystone_glue::BrandCode();
+#else
+  std::string brand;
+#endif
+  // TODO(caitkp): when we move to multi-profiles (M16) we will want to change
+  // this check, as |FirstRun::IsChromeFirstRun()| checks for the first run
+  // ever, not first run per profile.
+  if (FirstRun::IsChromeFirstRun() &&
+      !LowerCaseEqualsASCII(brand, "ecdb")) {
+    InstallDefaultApps();
+  }
+#endif
+
   extension_service_->Init();
 
   if (extensions_enabled) {
@@ -536,6 +560,31 @@
   }
 }
 
+void ProfileImpl::InstallDefaultApps() {
+  FilePath apps_dir;
+  FilePath file;
+  std::list<FilePath> crx_path_list;
+
+  if (PathService::Get(chrome::DIR_DEFAULT_APPS, &apps_dir)) {
+    file_util::FileEnumerator file_enumerator(apps_dir, false,
+        file_util::FileEnumerator::FILES);
+    while (!(file = file_enumerator.Next()).value().empty()) {
+      if (LowerCaseEqualsASCII(file.Extension(), ".crx"))
+        crx_path_list.push_back(file);
+    }
+  }
+
+  for (std::list<FilePath>::iterator iter = crx_path_list.begin();
+       iter != crx_path_list.end(); ++iter) {
+    scoped_refptr<CrxInstaller> crx_installer =
+        extension_service_->MakeCrxInstaller(NULL);
+    crx_installer->set_allow_silent_install(true);
+    crx_installer->set_delete_source(false);
+    crx_installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
+    crx_installer->InstallCrx(*iter);
+  }
+}
+
 void ProfileImpl::RegisterComponentExtensions() {
   // Register the component extensions.
   //
diff --git a/chrome/browser/profiles/profile_impl.h b/chrome/browser/profiles/profile_impl.h
index 850b421..71aa59b 100644
--- a/chrome/browser/profiles/profile_impl.h
+++ b/chrome/browser/profiles/profile_impl.h
@@ -170,6 +170,8 @@
 
   void RegisterComponentExtensions();
 
+  void InstallDefaultApps();
+
   ExtensionPrefValueMap* GetExtensionPrefValueMap();
 
   void CreateQuotaManagerAndClients();
diff --git a/chrome/browser/ui/browser.cc b/chrome/browser/ui/browser.cc
index e73ef23..2656ae1 100644
--- a/chrome/browser/ui/browser.cc
+++ b/chrome/browser/ui/browser.cc
@@ -730,6 +730,11 @@
       extension_service->extension_prefs()->GetLaunchType(
           extension->id(), ExtensionPrefs::LAUNCH_DEFAULT);
   UMA_HISTOGRAM_ENUMERATION("Extensions.AppTabLaunchType", launch_type, 100);
+
+  // Track launches of the webstore specifically.
+  if (extension->id() == extension_misc::kWebStoreAppId)
+    UserMetrics::RecordAction(UserMetricsAction("Extensions.WebStoreLaunch"));
+
   int add_type = TabStripModel::ADD_ACTIVE;
   if (launch_type == ExtensionPrefs::LAUNCH_PINNED)
     add_type |= TabStripModel::ADD_PINNED;
diff --git a/chrome/common/chrome_paths.cc b/chrome/common/chrome_paths.cc
index 7f223cf7..db1f2b5 100644
--- a/chrome/common/chrome_paths.cc
+++ b/chrome/common/chrome_paths.cc
@@ -353,6 +353,16 @@
       create_dir = true;
 #endif
       break;
+    case chrome::DIR_DEFAULT_APPS:
+#if defined(OS_MACOSX)
+      cur = base::mac::MainAppBundlePath();
+      cur = cur.Append(FILE_PATH_LITERAL("Default Apps"));
+#else
+      if (!PathService::Get(chrome::DIR_APP, &cur))
+        return false;
+      cur = cur.Append(FILE_PATH_LITERAL("default_apps"));
+#endif
+      break;
     default:
       return false;
   }
diff --git a/chrome/common/chrome_paths.h b/chrome/common/chrome_paths.h
index 4b89a4e..78ca6b1 100644
--- a/chrome/common/chrome_paths.h
+++ b/chrome/common/chrome_paths.h
@@ -57,6 +57,8 @@
 #endif
 
   DIR_EXTERNAL_EXTENSIONS,      // Directory where installer places .crx files.
+  DIR_DEFAULT_APPS,             // Directory where installer places .crx files
+                                // to be installed when chrome is first run.
   FILE_RESOURCE_MODULE,         // Full path and filename of the module that
                                 // contains embedded resources (version,
                                 // strings, images, etc.).