Home | History | Annotate | Download | only in importer
      1 // Copyright (c) 2011 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_IMPORTER_IMPORTER_LIST_H_
      6 #define CHROME_BROWSER_IMPORTER_IMPORTER_LIST_H_
      7 #pragma once
      8 
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_vector.h"
     14 #include "base/string16.h"
     15 #include "content/browser/browser_thread.h"
     16 
     17 namespace importer {
     18 struct SourceProfile;
     19 }
     20 
     21 class ImporterList : public base::RefCountedThreadSafe<ImporterList> {
     22  public:
     23   // Any class calling DetectSourceProfiles() must implement this interface in
     24   // order to be called back when the source profiles are loaded.
     25   class Observer {
     26    public:
     27     virtual void OnSourceProfilesLoaded() = 0;
     28 
     29    protected:
     30     virtual ~Observer() {}
     31   };
     32 
     33   ImporterList();
     34 
     35   // Detects the installed browsers and their associated profiles, then stores
     36   // their information in a list. It returns the list of description of all
     37   // profiles. Calls into DetectSourceProfilesWorker() on the FILE thread to do
     38   // the real work of detecting source profiles. |observer| must be non-NULL.
     39   void DetectSourceProfiles(Observer* observer);
     40 
     41   // Sets the observer of this object. When the current observer is destroyed,
     42   // this method should be called with a NULL |observer| so it is not notified
     43   // after destruction.
     44   void SetObserver(Observer* observer);
     45 
     46   // DEPRECATED: This method is synchronous and performs file operations which
     47   // may end up blocking the current thread, which is usually the UI thread.
     48   void DetectSourceProfilesHack();
     49 
     50   // Returns the number of different source profiles you can import from.
     51   size_t count() const { return source_profiles_.size(); }
     52 
     53   // Returns the SourceProfile at |index|. The profiles are ordered such that
     54   // the profile at index 0 is the likely default browser. The SourceProfile
     55   // should be passed to ImporterHost::StartImportSettings().
     56   const importer::SourceProfile& GetSourceProfileAt(size_t index) const;
     57 
     58   // Returns the SourceProfile for the given |importer_type|.
     59   const importer::SourceProfile& GetSourceProfileForImporterType(
     60       int importer_type) const;
     61 
     62  private:
     63   friend class base::RefCountedThreadSafe<ImporterList>;
     64 
     65   ~ImporterList();
     66 
     67   // The worker method for DetectSourceProfiles(). Must be called on the FILE
     68   // thread.
     69   void DetectSourceProfilesWorker();
     70 
     71   // Called by DetectSourceProfilesWorker() on the source thread. This method
     72   // notifies |observer_| that the source profiles are loaded. |profiles| is
     73   // the vector of loaded profiles.
     74   void SourceProfilesLoaded(
     75       const std::vector<importer::SourceProfile*>& profiles);
     76 
     77   // The list of profiles with the default one first.
     78   ScopedVector<importer::SourceProfile> source_profiles_;
     79 
     80   // The ID of the thread DetectSourceProfiles() is called on. Only valid after
     81   // DetectSourceProfiles() is called and until SourceProfilesLoaded() has
     82   // returned.
     83   BrowserThread::ID source_thread_id_;
     84 
     85   // Weak reference. Only valid after DetectSourceProfiles() is called and until
     86   // SourceProfilesLoaded() has returned.
     87   Observer* observer_;
     88 
     89   // True if |observer_| is set during the lifetime of source profile detection.
     90   // This hack is necessary in order to not use |observer_| != NULL as a method
     91   // of determining whether this object is being observed or not.
     92   // TODO(jhawkins): Remove once DetectSourceProfilesHack() is removed.
     93   bool is_observed_;
     94 
     95   // True if source profiles are loaded.
     96   bool source_profiles_loaded_;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(ImporterList);
     99 };
    100 
    101 #endif  // CHROME_BROWSER_IMPORTER_IMPORTER_LIST_H_
    102