Convert bare new to base::MakeRefCounted<> in extensions directories

Converts all usages of bare new that:
(1) construct base::RefCounted objects,
(2) use public constructors,
(3) are in an extensions directory, and
(4) wouldn't cause changes in non-extensions directory
to use base::MakeRefCounted instead.

Bug: 919580
Change-Id: Ia6def2e499e5195640695b577004df2cd6d95300
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1772133
Commit-Queue: Matthew Denton <[email protected]>
Reviewed-by: Istiaque Ahmed <[email protected]>
Reviewed-by: Devlin <[email protected]>
Cr-Commit-Position: refs/heads/master@{#691910}
diff --git a/extensions/browser/api/api_resource_manager.h b/extensions/browser/api/api_resource_manager.h
index 9c0bd27..3141f848 100644
--- a/extensions/browser/api/api_resource_manager.h
+++ b/extensions/browser/api/api_resource_manager.h
@@ -105,7 +105,7 @@
                            public ProcessManagerObserver {
  public:
   explicit ApiResourceManager(content::BrowserContext* context)
-      : data_(new ApiResourceData()),
+      : data_(base::MakeRefCounted<ApiResourceData>()),
         extension_registry_observer_(this),
         process_manager_observer_(this) {
     extension_registry_observer_.Add(ExtensionRegistry::Get(context));
diff --git a/extensions/browser/api/declarative_webrequest/webrequest_action.cc b/extensions/browser/api/declarative_webrequest/webrequest_action.cc
index 14db9db..adb529d8 100644
--- a/extensions/browser/api/declarative_webrequest/webrequest_action.cc
+++ b/extensions/browser/api/declarative_webrequest/webrequest_action.cc
@@ -116,7 +116,7 @@
     const base::Value* value,
     std::string* error,
     bool* bad_message) {
-  return scoped_refptr<const WebRequestAction>(new T);
+  return base::MakeRefCounted<T>();
 }
 
 scoped_refptr<const WebRequestAction> CreateRedirectRequestAction(
diff --git a/extensions/browser/api/device_permissions_manager.cc b/extensions/browser/api/device_permissions_manager.cc
index 086987d..1df437f 100644
--- a/extensions/browser/api/device_permissions_manager.cc
+++ b/extensions/browser/api/device_permissions_manager.cc
@@ -9,6 +9,7 @@
 #include <utility>
 
 #include "base/bind.h"
+#include "base/memory/scoped_refptr.h"
 #include "base/memory/singleton.h"
 #include "base/strings/string_number_conversions.h"
 #include "base/strings/stringprintf.h"
@@ -225,11 +226,11 @@
   }
 
   if (type == kDeviceTypeUsb) {
-    return new DevicePermissionEntry(
+    return base::MakeRefCounted<DevicePermissionEntry>(
         DevicePermissionEntry::Type::USB, vendor_id, product_id, serial_number,
         manufacturer_string, product_string, last_used);
   } else if (type == kDeviceTypeHid) {
-    return new DevicePermissionEntry(
+    return base::MakeRefCounted<DevicePermissionEntry>(
         DevicePermissionEntry::Type::HID, vendor_id, product_id, serial_number,
         base::string16(), product_string, last_used);
   }
@@ -562,8 +563,7 @@
   DCHECK(thread_checker_.CalledOnValidThread());
   DevicePermissions* device_permissions = GetForExtension(extension_id);
 
-  scoped_refptr<DevicePermissionEntry> device_entry(
-      new DevicePermissionEntry(device));
+  auto device_entry = base::MakeRefCounted<DevicePermissionEntry>(device);
 
   if (device_entry->IsPersistent()) {
     for (const auto& entry : device_permissions->entries()) {
diff --git a/extensions/browser/api/device_permissions_prompt.cc b/extensions/browser/api/device_permissions_prompt.cc
index f28fa32..83cd02e 100644
--- a/extensions/browser/api/device_permissions_prompt.cc
+++ b/extensions/browser/api/device_permissions_prompt.cc
@@ -10,6 +10,7 @@
 #include "base/bind.h"
 #include "base/bind_helpers.h"
 #include "base/i18n/message_formatter.h"
+#include "base/memory/scoped_refptr.h"
 #include "base/scoped_observer.h"
 #include "base/strings/stringprintf.h"
 #include "base/strings/utf_string_conversions.h"
@@ -367,8 +368,8 @@
     bool multiple,
     std::vector<UsbDeviceFilterPtr> filters,
     const UsbDevicesCallback& callback) {
-  prompt_ = new UsbDevicePermissionsPrompt(extension, context, multiple,
-                                           std::move(filters), callback);
+  prompt_ = base::MakeRefCounted<UsbDevicePermissionsPrompt>(
+      extension, context, multiple, std::move(filters), callback);
   ShowDialog();
 }
 
@@ -378,8 +379,8 @@
     bool multiple,
     const std::vector<HidDeviceFilter>& filters,
     const HidDevicesCallback& callback) {
-  prompt_ = new HidDevicePermissionsPrompt(extension, context, multiple,
-                                           filters, callback);
+  prompt_ = base::MakeRefCounted<HidDevicePermissionsPrompt>(
+      extension, context, multiple, filters, callback);
   ShowDialog();
 }
 
diff --git a/extensions/browser/api/file_handlers/app_file_handler_util.cc b/extensions/browser/api/file_handlers/app_file_handler_util.cc
index 75126f5..5e1e198f 100644
--- a/extensions/browser/api/file_handlers/app_file_handler_util.cc
+++ b/extensions/browser/api/file_handlers/app_file_handler_util.cc
@@ -10,6 +10,7 @@
 #include "base/files/file.h"
 #include "base/files/file_path.h"
 #include "base/files/file_util.h"
+#include "base/memory/scoped_refptr.h"
 #include "base/task/post_task.h"
 #include "base/task/task_traits.h"
 #include "build/build_config.h"
@@ -325,8 +326,8 @@
     const std::set<base::FilePath>& directory_paths,
     const base::Closure& on_success,
     const base::Callback<void(const base::FilePath&)>& on_failure) {
-  scoped_refptr<WritableFileChecker> checker(new WritableFileChecker(
-      paths, context, directory_paths, on_success, on_failure));
+  auto checker = base::MakeRefCounted<WritableFileChecker>(
+      paths, context, directory_paths, on_success, on_failure);
   checker->Check();
 }
 
diff --git a/extensions/browser/api/networking_private/networking_private_linux.cc b/extensions/browser/api/networking_private/networking_private_linux.cc
index 4d3f105..37ca2849b 100644
--- a/extensions/browser/api/networking_private/networking_private_linux.cc
+++ b/extensions/browser/api/networking_private/networking_private_linux.cc
@@ -9,7 +9,7 @@
 #include "base/bind.h"
 #include "base/bind_helpers.h"
 #include "base/callback.h"
-#include "base/message_loop/message_pump_type.h"
+#include "base/memory/scoped_refptr.h"
 #include "base/strings/string16.h"
 #include "base/strings/string_split.h"
 #include "base/strings/utf_string_conversions.h"
@@ -169,7 +169,7 @@
   dbus_options.connection_type = dbus::Bus::PRIVATE;
   dbus_options.dbus_task_runner = dbus_task_runner_;
 
-  dbus_ = new dbus::Bus(dbus_options);
+  dbus_ = base::MakeRefCounted<dbus::Bus>(dbus_options);
   network_manager_proxy_ = dbus_->GetObjectProxy(
       networking_private::kNetworkManagerNamespace,
       dbus::ObjectPath(networking_private::kNetworkManagerPath));
diff --git a/extensions/browser/api/web_request/web_request_api_helpers.cc b/extensions/browser/api/web_request/web_request_api_helpers.cc
index b1cc646d..701a4dc 100644
--- a/extensions/browser/api/web_request/web_request_api_helpers.cc
+++ b/extensions/browser/api/web_request/web_request_api_helpers.cc
@@ -16,6 +16,7 @@
 #include "base/containers/adapters.h"
 #include "base/containers/flat_map.h"
 #include "base/macros.h"
+#include "base/memory/scoped_refptr.h"
 #include "base/metrics/histogram_macros.h"
 #include "base/no_destructor.h"
 #include "base/stl_util.h"
@@ -1227,7 +1228,7 @@
 
   // Only create a copy if we really want to modify the response headers.
   if (override_response_headers->get() == NULL) {
-    *override_response_headers = new net::HttpResponseHeaders(
+    *override_response_headers = base::MakeRefCounted<net::HttpResponseHeaders>(
         original_response_headers->raw_headers());
   }
 
@@ -1353,8 +1354,9 @@
   if (new_url.is_valid()) {
     // Only create a copy if we really want to modify the response headers.
     if (override_response_headers->get() == NULL) {
-      *override_response_headers = new net::HttpResponseHeaders(
-          original_response_headers->raw_headers());
+      *override_response_headers =
+          base::MakeRefCounted<net::HttpResponseHeaders>(
+              original_response_headers->raw_headers());
     }
     (*override_response_headers)->ReplaceStatusLine("HTTP/1.1 302 Found");
     (*override_response_headers)->RemoveHeader("location");
diff --git a/extensions/browser/content_verifier.cc b/extensions/browser/content_verifier.cc
index a48e3e28..a8c35d1 100644
--- a/extensions/browser/content_verifier.cc
+++ b/extensions/browser/content_verifier.cc
@@ -11,6 +11,7 @@
 #include "base/bind.h"
 #include "base/bind_helpers.h"
 #include "base/files/file_path.h"
+#include "base/memory/scoped_refptr.h"
 #include "base/memory/weak_ptr.h"
 #include "base/metrics/histogram_macros.h"
 #include "base/stl_util.h"
diff --git a/extensions/browser/content_verifier.h b/extensions/browser/content_verifier.h
index 39c6bff..1c275b47 100644
--- a/extensions/browser/content_verifier.h
+++ b/extensions/browser/content_verifier.h
@@ -11,6 +11,7 @@
 
 #include "base/macros.h"
 #include "base/memory/ref_counted.h"
+#include "base/memory/scoped_refptr.h"
 #include "base/scoped_observer.h"
 #include "base/version.h"
 #include "content/public/browser/browser_thread.h"
diff --git a/extensions/browser/extension_function_dispatcher.cc b/extensions/browser/extension_function_dispatcher.cc
index 15b13c88..96509e3 100644
--- a/extensions/browser/extension_function_dispatcher.cc
+++ b/extensions/browser/extension_function_dispatcher.cc
@@ -13,6 +13,7 @@
 #include "base/macros.h"
 #include "base/memory/ptr_util.h"
 #include "base/memory/ref_counted.h"
+#include "base/memory/scoped_refptr.h"
 #include "base/metrics/histogram_functions.h"
 #include "base/metrics/histogram_macros.h"
 #include "base/process/process.h"
@@ -335,9 +336,9 @@
   if (render_frame_host)
     DCHECK_EQ(render_process_id, render_frame_host->GetProcess()->GetID());
 
-  scoped_refptr<ExtensionFunction> function(CreateExtensionFunction(
+  scoped_refptr<ExtensionFunction> function = CreateExtensionFunction(
       params, extension, render_process_id, *process_map,
-      ExtensionAPI::GetSharedInstance(), browser_context_, callback));
+      ExtensionAPI::GetSharedInstance(), browser_context_, callback);
   if (!function.get())
     return;
 
@@ -482,7 +483,8 @@
 }
 
 // static
-ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
+scoped_refptr<ExtensionFunction>
+ExtensionFunctionDispatcher::CreateExtensionFunction(
     const ExtensionHostMsg_Request_Params& params,
     const Extension* extension,
     int requesting_process_id,
@@ -490,7 +492,7 @@
     ExtensionAPI* api,
     void* profile_id,
     const ExtensionFunction::ResponseCallback& callback) {
-  ExtensionFunction* function =
+  scoped_refptr<ExtensionFunction> function =
       ExtensionFunctionRegistry::GetInstance().NewFunction(params.name);
   if (!function) {
     LOG(ERROR) << "Unknown Extension API - " << params.name;
diff --git a/extensions/browser/extension_function_dispatcher.h b/extensions/browser/extension_function_dispatcher.h
index 92d3529..6721cee 100644
--- a/extensions/browser/extension_function_dispatcher.h
+++ b/extensions/browser/extension_function_dispatcher.h
@@ -28,9 +28,6 @@
 class ProcessMap;
 class WindowController;
 
-// A factory function for creating new ExtensionFunction instances.
-typedef ExtensionFunction* (*ExtensionFunctionFactory)();
-
 // ExtensionFunctionDispatcher receives requests to execute functions from
 // Chrome extensions running in a RenderFrameHost and dispatches them to the
 // appropriate handler. It lives entirely on the UI thread.
@@ -128,7 +125,7 @@
   // Helper to create an ExtensionFunction to handle the function given by
   // |params|. Can be called on any thread.
   // Does not set subclass properties, or include_incognito.
-  static ExtensionFunction* CreateExtensionFunction(
+  static scoped_refptr<ExtensionFunction> CreateExtensionFunction(
       const ExtensionHostMsg_Request_Params& params,
       const Extension* extension,
       int requesting_process_id,
diff --git a/extensions/browser/extension_function_registry.cc b/extensions/browser/extension_function_registry.cc
index f9a8d72..6b8fb3e4 100644
--- a/extensions/browser/extension_function_registry.cc
+++ b/extensions/browser/extension_function_registry.cc
@@ -34,13 +34,13 @@
   return true;
 }
 
-ExtensionFunction* ExtensionFunctionRegistry::NewFunction(
+scoped_refptr<ExtensionFunction> ExtensionFunctionRegistry::NewFunction(
     const std::string& name) {
   auto iter = factories_.find(name);
   if (iter == factories_.end()) {
     return NULL;
   }
-  ExtensionFunction* function = iter->second.factory_();
+  scoped_refptr<ExtensionFunction> function = iter->second.factory_();
   function->set_name(iter->second.function_name_);
   function->set_histogram_value(iter->second.histogram_value_);
   return function;
diff --git a/extensions/browser/extension_function_registry.h b/extensions/browser/extension_function_registry.h
index 93b5411..0e26517 100644
--- a/extensions/browser/extension_function_registry.h
+++ b/extensions/browser/extension_function_registry.h
@@ -9,17 +9,18 @@
 #include <string>
 
 #include "base/macros.h"
+#include "base/memory/scoped_refptr.h"
 #include "extensions/browser/extension_function_histogram_value.h"
 
 class ExtensionFunction;
 
 // A factory function for creating new ExtensionFunction instances.
-using ExtensionFunctionFactory = ExtensionFunction* (*)();
+using ExtensionFunctionFactory = scoped_refptr<ExtensionFunction> (*)();
 
 // Template for defining ExtensionFunctionFactory.
 template <class T>
-ExtensionFunction* NewExtensionFunction() {
-  return new T();
+scoped_refptr<ExtensionFunction> NewExtensionFunction() {
+  return base::MakeRefCounted<T>();
 }
 
 // Contains a list of all known extension functions and allows clients to
@@ -54,7 +55,7 @@
                                   ExtensionFunctionFactory factory);
 
   // Factory method for the ExtensionFunction registered as 'name'.
-  ExtensionFunction* NewFunction(const std::string& name);
+  scoped_refptr<ExtensionFunction> NewFunction(const std::string& name);
 
   // Registers a new extension function. This will override any existing entry.
   void Register(const FactoryEntry& entry);
diff --git a/extensions/browser/guest_view/web_view/web_view_find_helper.cc b/extensions/browser/guest_view/web_view/web_view_find_helper.cc
index 5c74ebe8..defb854 100644
--- a/extensions/browser/guest_view/web_view/web_view_find_helper.cc
+++ b/extensions/browser/guest_view/web_view/web_view_find_helper.cc
@@ -6,6 +6,7 @@
 
 #include <utility>
 
+#include "base/memory/scoped_refptr.h"
 #include "components/guest_view/browser/guest_view_event.h"
 #include "extensions/browser/api/guest_view/web_view/web_view_internal_api.h"
 #include "extensions/browser/guest_view/web_view/web_view_constants.h"
@@ -96,9 +97,9 @@
   // function can be called when the find results are available.
   std::pair<FindInfoMap::iterator, bool> insert_result =
       find_info_map_.insert(std::make_pair(
-          current_find_request_id_, base::WrapRefCounted(new FindInfo(
-                                        current_find_request_id_, search_text,
-                                        options.Clone(), find_function))));
+          current_find_request_id_,
+          base::MakeRefCounted<FindInfo>(current_find_request_id_, search_text,
+                                         options.Clone(), find_function)));
   // No duplicate insertions.
   DCHECK(insert_result.second);
 
diff --git a/extensions/browser/value_store/value_store_factory_impl.cc b/extensions/browser/value_store/value_store_factory_impl.cc
index 3689421..09f3a94 100644
--- a/extensions/browser/value_store/value_store_factory_impl.cc
+++ b/extensions/browser/value_store/value_store_factory_impl.cc
@@ -4,6 +4,7 @@
 
 #include "extensions/browser/value_store/value_store_factory_impl.h"
 
+#include "base/memory/scoped_refptr.h"
 #include "extensions/browser/value_store/legacy_value_store_factory.h"
 
 namespace extensions {
@@ -11,7 +12,8 @@
 using SettingsNamespace = settings_namespace::Namespace;
 
 ValueStoreFactoryImpl::ValueStoreFactoryImpl(const base::FilePath& profile_path)
-    : legacy_factory_(new LegacyValueStoreFactory(profile_path)) {}
+    : legacy_factory_(
+          base::MakeRefCounted<LegacyValueStoreFactory>(profile_path)) {}
 
 ValueStoreFactoryImpl::~ValueStoreFactoryImpl() = default;
 
diff --git a/extensions/browser/value_store/value_store_frontend.cc b/extensions/browser/value_store/value_store_frontend.cc
index 6e7348c..931c1f8 100644
--- a/extensions/browser/value_store/value_store_frontend.cc
+++ b/extensions/browser/value_store/value_store_frontend.cc
@@ -10,6 +10,7 @@
 #include "base/files/file_path.h"
 #include "base/logging.h"
 #include "base/macros.h"
+#include "base/memory/scoped_refptr.h"
 #include "base/task/post_task.h"
 #include "base/trace_event/trace_event.h"
 #include "content/public/browser/browser_task_traits.h"
@@ -113,7 +114,7 @@
 ValueStoreFrontend::ValueStoreFrontend(
     const scoped_refptr<ValueStoreFactory>& store_factory,
     BackendType backend_type)
-    : backend_(new Backend(store_factory, backend_type)) {
+    : backend_(base::MakeRefCounted<Backend>(store_factory, backend_type)) {
   DCHECK_CURRENTLY_ON(BrowserThread::UI);
 }
 
diff --git a/extensions/renderer/api/automation/automation_internal_custom_bindings.cc b/extensions/renderer/api/automation/automation_internal_custom_bindings.cc
index 91d70df..404c61a 100644
--- a/extensions/renderer/api/automation/automation_internal_custom_bindings.cc
+++ b/extensions/renderer/api/automation/automation_internal_custom_bindings.cc
@@ -1445,7 +1445,8 @@
     scoped_refptr<base::SingleThreadTaskRunner> task_runner =
         context()->web_frame()->GetTaskRunner(
             blink::TaskType::kInternalDefault);
-    message_filter_ = new AutomationMessageFilter(this, std::move(task_runner));
+    message_filter_ = base::MakeRefCounted<AutomationMessageFilter>(
+        this, std::move(task_runner));
   }
 }
 
@@ -1845,15 +1846,15 @@
 void AutomationInternalCustomBindings::RouteNodeIDFunction(
     const std::string& name,
     NodeIDFunction callback) {
-  scoped_refptr<NodeIDWrapper> wrapper = new NodeIDWrapper(this, callback);
+  auto wrapper = base::MakeRefCounted<NodeIDWrapper>(this, callback);
   RouteHandlerFunction(name, base::BindRepeating(&NodeIDWrapper::Run, wrapper));
 }
 
 void AutomationInternalCustomBindings::RouteNodeIDPlusAttributeFunction(
     const std::string& name,
     NodeIDPlusAttributeFunction callback) {
-  scoped_refptr<NodeIDPlusAttributeWrapper> wrapper =
-      new NodeIDPlusAttributeWrapper(this, callback);
+  auto wrapper =
+      base::MakeRefCounted<NodeIDPlusAttributeWrapper>(this, callback);
   RouteHandlerFunction(
       name, base::BindRepeating(&NodeIDPlusAttributeWrapper::Run, wrapper));
 }
@@ -1861,8 +1862,7 @@
 void AutomationInternalCustomBindings::RouteNodeIDPlusRangeFunction(
     const std::string& name,
     NodeIDPlusRangeFunction callback) {
-  scoped_refptr<NodeIDPlusRangeWrapper> wrapper =
-      new NodeIDPlusRangeWrapper(this, callback);
+  auto wrapper = base::MakeRefCounted<NodeIDPlusRangeWrapper>(this, callback);
   RouteHandlerFunction(
       name, base::BindRepeating(&NodeIDPlusRangeWrapper::Run, wrapper));
 }
@@ -1870,8 +1870,8 @@
 void AutomationInternalCustomBindings::RouteNodeIDPlusStringBoolFunction(
     const std::string& name,
     NodeIDPlusStringBoolFunction callback) {
-  scoped_refptr<NodeIDPlusStringBoolWrapper> wrapper =
-      new NodeIDPlusStringBoolWrapper(this, callback);
+  auto wrapper =
+      base::MakeRefCounted<NodeIDPlusStringBoolWrapper>(this, callback);
   RouteHandlerFunction(
       name, base::BindRepeating(&NodeIDPlusStringBoolWrapper::Run, wrapper));
 }
@@ -1879,8 +1879,8 @@
 void AutomationInternalCustomBindings::RouteNodeIDPlusDimensionsFunction(
     const std::string& name,
     NodeIDPlusDimensionsFunction callback) {
-  scoped_refptr<NodeIDPlusDimensionsWrapper> wrapper =
-      new NodeIDPlusDimensionsWrapper(this, callback);
+  auto wrapper =
+      base::MakeRefCounted<NodeIDPlusDimensionsWrapper>(this, callback);
   RouteHandlerFunction(
       name, base::BindRepeating(&NodeIDPlusDimensionsWrapper::Run, wrapper));
 }