summaryrefslogtreecommitdiffstats
path: root/chrome/browser/instant/instant_loader_manager.h
blob: ab528fa6f53f7f45f73d415a276ecd6332b6ebb6 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) 2010 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_INSTANT_INSTANT_LOADER_MANAGER_H_
#define CHROME_BROWSER_INSTANT_INSTANT_LOADER_MANAGER_H_
#pragma once

#include <map>

#include "base/scoped_ptr.h"
#include "chrome/browser/search_engines/template_url_id.h"

class InstantLoader;
class InstantLoaderDelegate;

// InstantLoaderManager is responsible for maintaining the InstantLoaders for
// InstantController. InstantLoaderManager keeps track of one loader for loading
// non-instant urls, and a loader per TemplateURLID for loading instant urls. A
// loader per TemplateURLID is necessitated due to not knowing in advance if a
// site really supports instant (for example, the user might have opted out even
// though it's supported).
//
// Users of InstantLoaderManager need only concern themselves with the current
// and pending loaders. The current loader is the loader that if ready is shown
// by InstantController. The pending loader is used if the current loader is
// ready and update is invoked with a different id. In this case the current
// loader is left as current (and it's preview contents stopped) and the newly
// created loader is set to pending.  Once the pending loader is ready
// MakePendingCurrent should be invoked to make the pending the current loader.
//
// InstantLoader owns all the InstantLoaders returned. You can take
// ownership of the current loader by invoking ReleaseCurrentLoader.
class InstantLoaderManager {
 public:
  explicit InstantLoaderManager(InstantLoaderDelegate* loader_delegate);
  ~InstantLoaderManager();

  // Updates the current loader. If the current loader is replaced and should be
  // deleted it is set in |old_loader|. This is done to allow the caller to
  // notify delegates before the old loader is destroyed. This returns the
  // active InstantLoader that should be used.
  InstantLoader* UpdateLoader(TemplateURLID instant_id,
                              scoped_ptr<InstantLoader>* old_loader);

  // Returns true if invoking |UpdateLoader| with |instant_id| would change the
  // active loader.
  bool WillUpateChangeActiveLoader(TemplateURLID instant_id);

  // Makes the pending loader the current loader. If ownership of the old
  // loader is to pass to the caller |old_loader| is set appropriately.
  void MakePendingCurrent(scoped_ptr<InstantLoader>* old_loader);

  // Returns the current loader and clears internal references to it.  This
  // should be used prior to destroying the InstantLoaderManager when the owner
  // of InstantLoaderManager wants to take ownership of the loader.
  InstantLoader* ReleaseCurrentLoader();

  // Destroys the specified loader.
  void DestroyLoader(InstantLoader* loader);

  // Removes references to loader.
  InstantLoader* ReleaseLoader(InstantLoader* loader);

  // If |loader| is in |instant_loaders_| it is removed.
  void RemoveLoaderFromInstant(InstantLoader* loader);

  // Returns the current loader, may be null.
  InstantLoader* current_loader() const { return current_loader_; }

  // Returns the pending loader, may be null.
  InstantLoader* pending_loader() const { return pending_loader_; }

  // The active loader is the loader that should be used for new loads. It is
  // either the pending loader or the current loader.
  InstantLoader* active_loader() const {
    return pending_loader_ ? pending_loader_ : current_loader_;
  }

  // Returns the number of instant loaders.
  // This is exposed for tests.
  size_t num_instant_loaders() const { return instant_loaders_.size(); }

 private:
  typedef std::map<TemplateURLID, InstantLoader*> Loaders;

  // Creates a loader and if |id| is non-zero registers it in instant_loaders_.
  InstantLoader* CreateLoader(TemplateURLID id);

  // Returns the loader for loading instant results with the specified id. If
  // there is no loader for the specified id a new one is created.
  InstantLoader* GetInstantLoader(TemplateURLID id);

  InstantLoaderDelegate* loader_delegate_;

  // The current loader.
  InstantLoader* current_loader_;

  // Loader we want to use as soon as ready. This is only non-null if
  // current_loader_ is ready and Update is invoked with a different template
  // url id.
  InstantLoader* pending_loader_;

  // Maps for template url id to loader used for that template url id.
  Loaders instant_loaders_;

  DISALLOW_COPY_AND_ASSIGN(InstantLoaderManager);
};

#endif  // CHROME_BROWSER_INSTANT_INSTANT_LOADER_MANAGER_H_