summaryrefslogtreecommitdiffstats
path: root/chrome/browser/importer/importer_list.h
blob: b8330241f2dc591064010830cc578539c987d479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_IMPORTER_IMPORTER_LIST_H_
#define CHROME_BROWSER_IMPORTER_IMPORTER_LIST_H_
#pragma once

#include <vector>

#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_vector.h"
#include "base/string16.h"
#include "content/public/browser/browser_thread.h"
#include "net/url_request/url_request_context_getter.h"

namespace importer {
class ImporterListObserver;
struct SourceProfile;
}

class ImporterList : public base::RefCountedThreadSafe<ImporterList> {
 public:
  explicit ImporterList(net::URLRequestContextGetter* request_context_getter);

  // Detects the installed browsers and their associated profiles, then stores
  // their information in a list. It returns the list of description of all
  // profiles. Calls into DetectSourceProfilesWorker() on the FILE thread to do
  // the real work of detecting source profiles. |observer| must be non-NULL.
  void DetectSourceProfiles(importer::ImporterListObserver* observer);

  // Sets the observer of this object. When the current observer is destroyed,
  // this method should be called with a NULL |observer| so it is not notified
  // after destruction.
  void SetObserver(importer::ImporterListObserver* observer);

  // DEPRECATED: This method is synchronous and performs file operations which
  // may end up blocking the current thread, which is usually the UI thread.
  void DetectSourceProfilesHack();

  // Returns the number of different source profiles you can import from.
  size_t count() const { return source_profiles_.size(); }

  // Returns the SourceProfile at |index|. The profiles are ordered such that
  // the profile at index 0 is the likely default browser. The SourceProfile
  // should be passed to ImporterHost::StartImportSettings().
  const importer::SourceProfile& GetSourceProfileAt(size_t index) const;

  // Returns the SourceProfile for the given |importer_type|.
  const importer::SourceProfile& GetSourceProfileForImporterType(
      int importer_type) const;

 private:
  friend class base::RefCountedThreadSafe<ImporterList>;

  ~ImporterList();

  // The worker method for DetectSourceProfiles(). Must be called on the FILE
  // thread.
  void DetectSourceProfilesWorker();

  // Called by DetectSourceProfilesWorker() on the source thread. This method
  // notifies |observer_| that the source profiles are loaded. |profiles| is
  // the vector of loaded profiles.
  void SourceProfilesLoaded(
      const std::vector<importer::SourceProfile*>& profiles);

  // The list of profiles with the default one first.
  ScopedVector<importer::SourceProfile> source_profiles_;

  // Needed for Google Toolbar Import to connect to Toolbar server.
  scoped_refptr<net::URLRequestContextGetter> request_context_getter_;

  // The ID of the thread DetectSourceProfiles() is called on. Only valid after
  // DetectSourceProfiles() is called and until SourceProfilesLoaded() has
  // returned.
  content::BrowserThread::ID source_thread_id_;

  // Weak reference. Only valid after DetectSourceProfiles() is called and until
  // SourceProfilesLoaded() has returned.
  importer::ImporterListObserver* observer_;

  // True if |observer_| is set during the lifetime of source profile detection.
  // This hack is necessary in order to not use |observer_| != NULL as a method
  // of determining whether this object is being observed or not.
  // TODO(jhawkins): Remove once DetectSourceProfilesHack() is removed.
  bool is_observed_;

  // True if source profiles are loaded.
  bool source_profiles_loaded_;

  DISALLOW_COPY_AND_ASSIGN(ImporterList);
};

#endif  // CHROME_BROWSER_IMPORTER_IMPORTER_LIST_H_