Home | History | Annotate | Download | only in profiles
      1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_PROFILES_PROFILE_DESTROYER_H_
      6 #define CHROME_BROWSER_PROFILES_PROFILE_DESTROYER_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "base/timer/timer.h"
     12 #include "content/public/browser/notification_observer.h"
     13 #include "content/public/browser/notification_registrar.h"
     14 
     15 class Profile;
     16 namespace content {
     17 class RenderProcessHost;
     18 }
     19 
     20 // We use this class to destroy the off the record profile so that we can make
     21 // sure it gets done asynchronously after all render process hosts are gone.
     22 class ProfileDestroyer
     23     : public content::NotificationObserver {
     24  public:
     25   static void DestroyProfileWhenAppropriate(Profile* const profile);
     26   static void DestroyOffTheRecordProfileNow(Profile* const profile);
     27 
     28  private:
     29   friend class base::RefCounted<ProfileDestroyer>;
     30 
     31   ProfileDestroyer(
     32       Profile* const profile,
     33       const std::vector<content::RenderProcessHost*>& hosts);
     34   virtual ~ProfileDestroyer();
     35 
     36   // content::NotificationObserver override.
     37   virtual void Observe(int type,
     38                        const content::NotificationSource& source,
     39                        const content::NotificationDetails& details) OVERRIDE;
     40   // Called by the timer to cancel the pending detruction and do it now.
     41   void DestroyProfile();
     42 
     43   // Fetch the list of render process hosts that still refer to the profile.
     44   // Return true if we found at least one, false otherwise.
     45   static bool GetHostsForProfile(
     46       Profile* const profile,
     47       std::vector<content::RenderProcessHost*> *hosts);
     48 
     49   // We need access to all pending destroyers so we can cancel them.
     50   static std::vector<ProfileDestroyer*>* pending_destroyers_;
     51 
     52   // We register for render process host termination.
     53   content::NotificationRegistrar registrar_;
     54 
     55   // We don't want to wait forever, so we have a cancellation timer.
     56   base::Timer timer_;
     57 
     58   // Used to count down the number of render process host left.
     59   uint32 num_hosts_;
     60 
     61   Profile* profile_;
     62 
     63   base::WeakPtrFactory<ProfileDestroyer> weak_ptr_factory_;
     64 
     65   DISALLOW_COPY_AND_ASSIGN(ProfileDestroyer);
     66 };
     67 
     68 #endif  // CHROME_BROWSER_PROFILES_PROFILE_DESTROYER_H_
     69