ClearSigninProfile refactored and used in <webview>-based enrollment.

|web_contents| param was removed from ClearSigninProfile. Now ClearSigninProfile
unconditionally clears storage partition that is used for authentication, if it
is available. The partition is retreived by login::GetSigninPartition().

ClearSigninProfile used for clearing signin partition during enrollement, when
authentication made with <webview>.

BUG=464105
TEST=manually

Review URL: https://codereview.chromium.org/1048233002

Cr-Commit-Position: refs/heads/master@{#323472}
diff --git a/chrome/browser/chromeos/profiles/profile_helper.cc b/chrome/browser/chromeos/profiles/profile_helper.cc
index 083ec00..1fb8c919 100644
--- a/chrome/browser/chromeos/profiles/profile_helper.cc
+++ b/chrome/browser/chromeos/profiles/profile_helper.cc
@@ -9,6 +9,7 @@
 #include "base/command_line.h"
 #include "chrome/browser/browser_process.h"
 #include "chrome/browser/browsing_data/browsing_data_helper.h"
+#include "chrome/browser/chromeos/login/helper.h"
 #include "chrome/browser/chromeos/login/signin/oauth2_login_manager_factory.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/profiles/profile_manager.h"
@@ -53,38 +54,6 @@
   const std::string& username_hash;
 };
 
-bool GetWebViewPartition(std::set<content::StoragePartition*>* partition_set,
-                         content::WebContents* web_contents) {
-  extensions::WebViewGuest* view_guest =
-      extensions::WebViewGuest::FromWebContents(web_contents);
-  if (view_guest) {
-    content::StoragePartition* partition =
-        content::BrowserContext::GetStoragePartition(
-            web_contents->GetBrowserContext(), web_contents->GetSiteInstance());
-    partition_set->insert(partition);
-  }
-  return false;
-}
-
-void ClearContentsData(content::WebContents* web_contents,
-                       const base::Closure& on_clear_callback) {
-  content::BrowserContext* context = web_contents->GetBrowserContext();
-  extensions::GuestViewManager* manager =
-      extensions::GuestViewManager::FromBrowserContext(context);
-  std::set<content::StoragePartition*> partition_set;
-  manager->ForEachGuest(web_contents,
-                        base::Bind(&GetWebViewPartition, &partition_set));
-  base::Closure barrier_closure =
-      base::BarrierClosure(partition_set.size(), on_clear_callback);
-  for (const auto& partition : partition_set) {
-    partition->ClearData(
-        content::StoragePartition::REMOVE_DATA_MASK_ALL,
-        content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, GURL(),
-        content::StoragePartition::OriginMatcherFunction(), base::Time(),
-        base::Time::Now(), barrier_closure);
-  }
-}
-
 }  // anonymous namespace
 
 // static
@@ -95,7 +64,7 @@
 // ProfileHelper, public
 
 ProfileHelper::ProfileHelper()
-    : signin_profile_clear_requested_(false) {
+    : browsing_data_remover_(nullptr), weak_factory_(this) {
 }
 
 ProfileHelper::~ProfileHelper() {
@@ -103,6 +72,12 @@
   // when ScopedTestUserManager is used.
   if (user_manager::UserManager::IsInitialized())
     user_manager::UserManager::Get()->RemoveSessionStateObserver(this);
+
+  if (browsing_data_remover_) {
+    browsing_data_remover_->RemoveObserver(this);
+    // BrowsingDataRemover deletes itself.
+    browsing_data_remover_ = nullptr;
+  }
 }
 
 // static
@@ -234,26 +209,40 @@
   user_manager::UserManager::Get()->AddSessionStateObserver(this);
 }
 
-void ProfileHelper::ClearSigninProfile(const base::Closure& on_clear_callback,
-                                       content::WebContents* webview_contents) {
-  on_clear_callbacks_.push_back(
-      webview_contents
-          ? base::Bind(&ClearContentsData, webview_contents, on_clear_callback)
-          : on_clear_callback);
-  if (signin_profile_clear_requested_)
+void ProfileHelper::ClearSigninProfile(const base::Closure& on_clear_callback) {
+  on_clear_callbacks_.push_back(on_clear_callback);
+
+  // Profile is already clearing.
+  if (on_clear_callbacks_.size() > 1)
     return;
+
+  on_clear_profile_stage_finished_ =
+      base::BarrierClosure(2, base::Bind(&ProfileHelper::OnSigninProfileCleared,
+                                         weak_factory_.GetWeakPtr()));
+
   ProfileManager* profile_manager = g_browser_process->profile_manager();
   // Check if signin profile was loaded.
-  if (!profile_manager->GetProfileByPath(GetSigninProfileDir())) {
-    OnBrowsingDataRemoverDone();
-    return;
+  if (Profile* signin_profile =
+          profile_manager->GetProfileByPath(GetSigninProfileDir())) {
+    LOG_ASSERT(!browsing_data_remover_);
+    browsing_data_remover_ =
+        BrowsingDataRemover::CreateForUnboundedRange(signin_profile);
+    browsing_data_remover_->AddObserver(this);
+    browsing_data_remover_->Remove(BrowsingDataRemover::REMOVE_SITE_DATA,
+                                   BrowsingDataHelper::ALL);
+  } else {
+    on_clear_profile_stage_finished_.Run();
   }
-  signin_profile_clear_requested_ = true;
-  BrowsingDataRemover* remover =
-      BrowsingDataRemover::CreateForUnboundedRange(GetSigninProfile());
-  remover->AddObserver(this);
-  remover->Remove(BrowsingDataRemover::REMOVE_SITE_DATA,
-                  BrowsingDataHelper::ALL);
+
+  if (content::StoragePartition* partition = login::GetSigninPartition()) {
+    partition->ClearData(
+        content::StoragePartition::REMOVE_DATA_MASK_ALL,
+        content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, GURL(),
+        content::StoragePartition::OriginMatcherFunction(), base::Time(),
+        base::Time::Now(), on_clear_profile_stage_finished_);
+  } else {
+    on_clear_profile_stage_finished_.Run();
+  }
 }
 
 Profile* ProfileHelper::GetProfileByUser(const user_manager::User* user) {
@@ -366,16 +355,25 @@
       GetUserByProfile(static_cast<const Profile*>(profile)));
 }
 
+void ProfileHelper::OnSigninProfileCleared() {
+  std::vector<base::Closure> callbacks;
+  callbacks.swap(on_clear_callbacks_);
+  for (const base::Closure& callback : callbacks) {
+    if (!callback.is_null())
+      callback.Run();
+  }
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // ProfileHelper, BrowsingDataRemover::Observer implementation:
 
 void ProfileHelper::OnBrowsingDataRemoverDone() {
-  signin_profile_clear_requested_ = false;
-  for (size_t i = 0; i < on_clear_callbacks_.size(); ++i) {
-    if (!on_clear_callbacks_[i].is_null())
-      on_clear_callbacks_[i].Run();
-  }
-  on_clear_callbacks_.clear();
+  LOG_ASSERT(browsing_data_remover_);
+  browsing_data_remover_->RemoveObserver(this);
+  // BrowsingDataRemover deletes itself.
+  browsing_data_remover_ = nullptr;
+
+  on_clear_profile_stage_finished_.Run();
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -391,7 +389,7 @@
         chromeos::OAuth2LoginManagerFactory::GetInstance()->
             GetForProfile(user_profile);
     login_manager->RemoveObserver(this);
-    ClearSigninProfile(base::Closure(), nullptr);
+    ClearSigninProfile(base::Closure());
   }
 }