Remove NOTIFICATION_EXTENSION_ENABLED.

Enablement can happen for the following 3 cases:
1. New extension is added
2. User enables a disabled extension
3. An extension is reloaded or a terminated extension is reloaded

It seems we used this in two classes in
a) theme_service.cc and
b) event_router.cc

a) For themes, this can be simplified to load the theme on
OnExtensionLoaded. Themes don't have process and it can't be
terminated. We still have to remember whether the load is due
to a theme update or not (OnExtensionWillBeInstalled). It is
possible to avoid this by remembering both current theme's
id (already done) and version in the prefs. This CL does not do
that.

b) The primary reason of EventRouter code is to make sure we
are aware of new lazy events on extension upgrade, and to dispatch
onInstalled event. The CL wraps the logic to use
OnExtensionWillBeInstalled + OnExtensionLoaded into a separate
class: LazyEventDispatchUtil.

However, there are also 2 other reasons for EventRouter code in b):
b1) We also need to reconnect devtools to a reloaded event
page. That was happening through event_router. Pull that logic into
extension_service as extension_service is already responsible
for remembering orphaned devtools.
b2) A component extension reload does not go through
ExtensionRegistry::TriggerOnInstalled, so it doesn't see any
OnExtensionWillBeInstalled. Couple the logic with b1).

[email protected] for added test in app_browsertest.cc
BUG=723754, 411569
Test=No visible changes expected.

Review-Url: https://codereview.chromium.org/2893693002
Cr-Commit-Position: refs/heads/master@{#477357}
diff --git a/chrome/browser/extensions/extension_service.cc b/chrome/browser/extensions/extension_service.cc
index 8654385..a77c73d 100644
--- a/chrome/browser/extensions/extension_service.cc
+++ b/chrome/browser/extensions/extension_service.cc
@@ -79,6 +79,7 @@
 #include "extensions/browser/extensions_browser_client.h"
 #include "extensions/browser/external_install_info.h"
 #include "extensions/browser/install_flag.h"
+#include "extensions/browser/lazy_background_task_queue.h"
 #include "extensions/browser/renderer_startup_helper.h"
 #include "extensions/browser/runtime_data.h"
 #include "extensions/browser/uninstall_reason.h"
@@ -144,6 +145,8 @@
 // TODO(samuong): Remove this in M58 (see comment in ExtensionService::Init).
 const char kDeprecatedLoadComponentExtension[] = "load-component-extension";
 
+void DoNothingWithExtensionHost(extensions::ExtensionHost* host) {}
+
 }  // namespace
 
 // ExtensionService.
@@ -924,11 +927,7 @@
 
   NotifyExtensionLoaded(extension);
 
-  // Notify listeners that the extension was enabled.
-  content::NotificationService::current()->Notify(
-      extensions::NOTIFICATION_EXTENSION_ENABLED,
-      content::Source<Profile>(profile_),
-      content::Details<const Extension>(extension));
+  MaybeSpinUpLazyBackgroundPage(extension);
 }
 
 void ExtensionService::DisableExtension(const std::string& extension_id,
@@ -2557,3 +2556,29 @@
 
   OnBlacklistUpdated();
 }
+
+void ExtensionService::MaybeSpinUpLazyBackgroundPage(
+    const Extension* extension) {
+  if (!extensions::BackgroundInfo::HasLazyBackgroundPage(extension))
+    return;
+
+  // For orphaned devtools, we will reconnect devtools to it later in
+  // DidCreateRenderViewForBackgroundPage().
+  OrphanedDevTools::iterator iter = orphaned_dev_tools_.find(extension->id());
+  bool has_orphaned_dev_tools = iter != orphaned_dev_tools_.end();
+
+  // Reloading component extension does not trigger install, so RuntimeAPI won't
+  // be able to detect its loading. Therefore, we need to spin up its lazy
+  // background page.
+  bool is_component_extension =
+      Manifest::IsComponentLocation(extension->location());
+
+  if (!has_orphaned_dev_tools && !is_component_extension)
+    return;
+
+  // Wake up the event page by posting a dummy task.
+  extensions::LazyBackgroundTaskQueue* queue =
+      extensions::LazyBackgroundTaskQueue::Get(profile_);
+  queue->AddPendingTask(profile_, extension->id(),
+                        base::Bind(&DoNothingWithExtensionHost));
+}