• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_INSTANT_INSTANT_LOADER_MANAGER_H_
6 #define CHROME_BROWSER_INSTANT_INSTANT_LOADER_MANAGER_H_
7 #pragma once
8 
9 #include <map>
10 
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/search_engines/template_url_id.h"
13 
14 class InstantLoader;
15 class InstantLoaderDelegate;
16 
17 // InstantLoaderManager is responsible for maintaining the InstantLoaders for
18 // InstantController. InstantLoaderManager keeps track of one loader for loading
19 // non-instant urls, and a loader per TemplateURLID for loading instant urls. A
20 // loader per TemplateURLID is necessitated due to not knowing in advance if a
21 // site really supports instant (for example, the user might have opted out even
22 // though it's supported).
23 //
24 // Users of InstantLoaderManager need only concern themselves with the current
25 // and pending loaders. The current loader is the loader that if ready is shown
26 // by InstantController. The pending loader is used if the current loader is
27 // ready and update is invoked with a different id. In this case the current
28 // loader is left as current (and it's preview contents stopped) and the newly
29 // created loader is set to pending.  Once the pending loader is ready
30 // MakePendingCurrent should be invoked to make the pending the current loader.
31 //
32 // InstantLoader owns all the InstantLoaders returned. You can take
33 // ownership of the current loader by invoking ReleaseCurrentLoader.
34 class InstantLoaderManager {
35  public:
36   explicit InstantLoaderManager(InstantLoaderDelegate* loader_delegate);
37   ~InstantLoaderManager();
38 
39   // Updates the current loader. If the current loader is replaced and should be
40   // deleted it is set in |old_loader|. This is done to allow the caller to
41   // notify delegates before the old loader is destroyed. This returns the
42   // active InstantLoader that should be used.
43   InstantLoader* UpdateLoader(TemplateURLID instant_id,
44                               scoped_ptr<InstantLoader>* old_loader);
45 
46   // Returns true if invoking |UpdateLoader| with |instant_id| would change the
47   // active loader.
48   bool WillUpateChangeActiveLoader(TemplateURLID instant_id);
49 
50   // Makes the pending loader the current loader. If ownership of the old
51   // loader is to pass to the caller |old_loader| is set appropriately.
52   void MakePendingCurrent(scoped_ptr<InstantLoader>* old_loader);
53 
54   // Returns the current loader and clears internal references to it.  This
55   // should be used prior to destroying the InstantLoaderManager when the owner
56   // of InstantLoaderManager wants to take ownership of the loader.
57   InstantLoader* ReleaseCurrentLoader();
58 
59   // Destroys the specified loader.
60   void DestroyLoader(InstantLoader* loader);
61 
62   // Removes references to loader.
63   InstantLoader* ReleaseLoader(InstantLoader* loader);
64 
65   // If |loader| is in |instant_loaders_| it is removed.
66   void RemoveLoaderFromInstant(InstantLoader* loader);
67 
68   // Returns the current loader, may be null.
current_loader()69   InstantLoader* current_loader() const { return current_loader_; }
70 
71   // Returns the pending loader, may be null.
pending_loader()72   InstantLoader* pending_loader() const { return pending_loader_; }
73 
74   // The active loader is the loader that should be used for new loads. It is
75   // either the pending loader or the current loader.
active_loader()76   InstantLoader* active_loader() const {
77     return pending_loader_ ? pending_loader_ : current_loader_;
78   }
79 
80   // Returns the number of instant loaders.
81   // This is exposed for tests.
num_instant_loaders()82   size_t num_instant_loaders() const { return instant_loaders_.size(); }
83 
84  private:
85   typedef std::map<TemplateURLID, InstantLoader*> Loaders;
86 
87   // Creates a loader and if |id| is non-zero registers it in instant_loaders_.
88   InstantLoader* CreateLoader(TemplateURLID id);
89 
90   // Returns the loader for loading instant results with the specified id. If
91   // there is no loader for the specified id a new one is created.
92   InstantLoader* GetInstantLoader(TemplateURLID id);
93 
94   InstantLoaderDelegate* loader_delegate_;
95 
96   // The current loader.
97   InstantLoader* current_loader_;
98 
99   // Loader we want to use as soon as ready. This is only non-null if
100   // current_loader_ is ready and Update is invoked with a different template
101   // url id.
102   InstantLoader* pending_loader_;
103 
104   // Maps for template url id to loader used for that template url id.
105   Loaders instant_loaders_;
106 
107   DISALLOW_COPY_AND_ASSIGN(InstantLoaderManager);
108 };
109 
110 #endif  // CHROME_BROWSER_INSTANT_INSTANT_LOADER_MANAGER_H_
111