instant_loader_manager.h revision 731df977c0511bca2206b5f333555b1205ff1f43
1// Copyright (c) 2010 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/scoped_ptr.h" 12#include "chrome/browser/search_engines/template_url_id.h" 13 14class InstantLoader; 15class 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. 34class 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 // Destroyes the specified loader. 60 void DestroyLoader(InstantLoader* loader); 61 62 // If |loader| is in |instant_loaders_| is it removed. 63 void RemoveLoaderFromInstant(InstantLoader* loader); 64 65 // Returns the current loader, may be null. 66 InstantLoader* current_loader() const { return current_loader_; } 67 68 // Returns the pending loader, may be null. 69 InstantLoader* pending_loader() const { return pending_loader_; } 70 71 // The active loader is the loader that should be used for new loads. It is 72 // either the pending loader or the current loader. 73 InstantLoader* active_loader() const { 74 return pending_loader_ ? pending_loader_ : current_loader_; 75 } 76 77 // Returns the number of instant loaders. 78 // This is exposed for tests. 79 size_t num_instant_loaders() const { return instant_loaders_.size(); } 80 81 private: 82 typedef std::map<TemplateURLID, InstantLoader*> Loaders; 83 84 // Creates a loader and if |id| is non-zero registers it in instant_loaders_. 85 InstantLoader* CreateLoader(TemplateURLID id); 86 87 // Returns the loader for loading instant results with the specified id. If 88 // there is no loader for the specified id a new one is created. 89 InstantLoader* GetInstantLoader(TemplateURLID id); 90 91 InstantLoaderDelegate* loader_delegate_; 92 93 // The current loader. 94 InstantLoader* current_loader_; 95 96 // Loader we want to use as soon as ready. This is only non-null if 97 // current_loader_ is ready and Update is invoked with a different template 98 // url id. 99 InstantLoader* pending_loader_; 100 101 // Maps for template url id to loader used for that template url id. 102 Loaders instant_loaders_; 103 104 DISALLOW_COPY_AND_ASSIGN(InstantLoaderManager); 105}; 106 107#endif // CHROME_BROWSER_INSTANT_INSTANT_LOADER_MANAGER_H_ 108