Home | History | Annotate | Download | only in predictors
      1 // Copyright (c) 2012 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_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_
      6 #define CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_
      7 
      8 #include <map>
      9 
     10 #include "base/gtest_prod_util.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/strings/string16.h"
     15 #include "chrome/browser/history/history_types.h"
     16 #include "chrome/browser/predictors/autocomplete_action_predictor_table.h"
     17 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
     18 #include "content/public/browser/navigation_controller.h"
     19 #include "content/public/browser/notification_observer.h"
     20 #include "content/public/browser/notification_registrar.h"
     21 #include "url/gurl.h"
     22 
     23 struct AutocompleteMatch;
     24 class AutocompleteResult;
     25 class HistoryService;
     26 struct OmniboxLog;
     27 class PredictorsHandler;
     28 class Profile;
     29 
     30 namespace content {
     31 class SessionStorageNamespace;
     32 }
     33 
     34 namespace gfx {
     35 class Size;
     36 }
     37 
     38 namespace history {
     39 class URLDatabase;
     40 }
     41 
     42 namespace prerender {
     43 class PrerenderHandle;
     44 }
     45 
     46 namespace predictors {
     47 
     48 // This class is responsible for determining the correct predictive network
     49 // action to take given for a given AutocompleteMatch and entered text. It can
     50 // be instantiated for both normal and incognito profiles.  For normal profiles,
     51 // it uses an AutocompleteActionPredictorTable accessed asynchronously on the DB
     52 // thread to permanently store the data used to make predictions, and keeps
     53 // local caches of that data to be able to make predictions synchronously on the
     54 // UI thread where it lives.  For incognito profiles, there is no table; the
     55 // local caches are copied from the main profile at creation and from there on
     56 // are the only thing used.
     57 //
     58 // This class can be accessed as a weak pointer so that it can safely use
     59 // PostTaskAndReply without fear of crashes if it is destroyed before the reply
     60 // triggers. This is necessary during initialization.
     61 class AutocompleteActionPredictor
     62     : public BrowserContextKeyedService,
     63       public content::NotificationObserver,
     64       public base::SupportsWeakPtr<AutocompleteActionPredictor> {
     65  public:
     66   enum Action {
     67     ACTION_PRERENDER = 0,
     68     ACTION_PRECONNECT,
     69     ACTION_NONE,
     70     LAST_PREDICT_ACTION = ACTION_NONE
     71   };
     72 
     73   explicit AutocompleteActionPredictor(Profile* profile);
     74   virtual ~AutocompleteActionPredictor();
     75 
     76   // Registers an AutocompleteResult for a given |user_text|. This will be used
     77   // when the user navigates from the Omnibox to determine early opportunities
     78   // to predict their actions.
     79   void RegisterTransitionalMatches(const base::string16& user_text,
     80                                    const AutocompleteResult& result);
     81 
     82   // Clears any transitional matches that have been registered. Called when, for
     83   // example, the OmniboxEditModel is reverted.
     84   void ClearTransitionalMatches();
     85 
     86   // Return the recommended action given |user_text|, the text the user has
     87   // entered in the Omnibox, and |match|, the suggestion from Autocomplete.
     88   // This method uses information from the ShortcutsBackend including how much
     89   // of the matching entry the user typed, and how long it's been since the user
     90   // visited the matching URL, to calculate a score between 0 and 1. This score
     91   // is then mapped to an Action.
     92   Action RecommendAction(const base::string16& user_text,
     93                          const AutocompleteMatch& match) const;
     94 
     95   // Begin prerendering |url| with |session_storage_namespace|. The |size| gives
     96   // the initial size for the target prerender. The predictor will run at most
     97   // one prerender at a time, so launching a prerender will cancel our previous
     98   // prerenders (if any).
     99   void StartPrerendering(
    100       const GURL& url,
    101       const content::SessionStorageNamespaceMap& session_storage_namespace_map,
    102       const gfx::Size& size);
    103 
    104   // Return true if the suggestion type warrants a TCP/IP preconnection.
    105   // i.e., it is now quite likely that the user will select the related domain.
    106   static bool IsPreconnectable(const AutocompleteMatch& match);
    107 
    108  private:
    109   friend class AutocompleteActionPredictorTest;
    110   friend class ::PredictorsHandler;
    111 
    112   struct TransitionalMatch {
    113     TransitionalMatch();
    114     ~TransitionalMatch();
    115 
    116     base::string16 user_text;
    117     std::vector<GURL> urls;
    118 
    119     bool operator==(const base::string16& other_user_text) const {
    120       return user_text == other_user_text;
    121     }
    122   };
    123 
    124   struct DBCacheKey {
    125     base::string16 user_text;
    126     GURL url;
    127 
    128     bool operator<(const DBCacheKey& rhs) const {
    129       return (user_text != rhs.user_text) ?
    130           (user_text < rhs.user_text) :  (url < rhs.url);
    131     }
    132 
    133     bool operator==(const DBCacheKey& rhs) const {
    134       return (user_text == rhs.user_text) && (url == rhs.url);
    135     }
    136   };
    137 
    138   struct DBCacheValue {
    139     int number_of_hits;
    140     int number_of_misses;
    141   };
    142 
    143   typedef std::map<DBCacheKey, DBCacheValue> DBCacheMap;
    144   typedef std::map<DBCacheKey, AutocompleteActionPredictorTable::Row::Id>
    145       DBIdCacheMap;
    146 
    147   static const int kMaximumDaysToKeepEntry;
    148 
    149   // NotificationObserver
    150   virtual void Observe(int type,
    151                        const content::NotificationSource& source,
    152                        const content::NotificationDetails& details) OVERRIDE;
    153 
    154   // The first step in initializing the predictor is accessing the database and
    155   // building the local cache. This should be delayed until after critical DB
    156   // and IO processes have completed.
    157   void CreateLocalCachesFromDatabase();
    158 
    159   // Removes all rows from the database and caches.
    160   void DeleteAllRows();
    161 
    162   // Removes rows from the database and caches that contain a URL in |rows|.
    163   void DeleteRowsWithURLs(const history::URLRows& rows);
    164 
    165   // Called when NOTIFICATION_OMNIBOX_OPENED_URL is observed.
    166   void OnOmniboxOpenedUrl(const OmniboxLog& log);
    167 
    168   // Adds and updates rows in the database and caches.
    169   void AddAndUpdateRows(
    170     const AutocompleteActionPredictorTable::Rows& rows_to_add,
    171     const AutocompleteActionPredictorTable::Rows& rows_to_update);
    172 
    173   // Called to populate the local caches. This also calls DeleteOldEntries
    174   // if the history service is available, or registers for the notification of
    175   // it becoming available.
    176   void CreateCaches(
    177       std::vector<AutocompleteActionPredictorTable::Row>* row_buffer);
    178 
    179   // Attempts to call DeleteOldEntries if the in-memory database has been loaded
    180   // by |service|. Returns success as a boolean.
    181   bool TryDeleteOldEntries(HistoryService* service);
    182 
    183   // Called to delete any old or invalid entries from the database. Called after
    184   // the local caches are created once the history service is available.
    185   void DeleteOldEntries(history::URLDatabase* url_db);
    186 
    187   // Deletes any old or invalid entries from the local caches. |url_db| and
    188   // |id_list| must not be NULL. Every row id deleted will be added to id_list.
    189   void DeleteOldIdsFromCaches(
    190       history::URLDatabase* url_db,
    191       std::vector<AutocompleteActionPredictorTable::Row::Id>* id_list);
    192 
    193   // Called on an incognito-owned predictor to copy the current caches from the
    194   // main profile.
    195   void CopyFromMainProfile();
    196 
    197   // Registers for notifications and sets the |initialized_| flag.
    198   void FinishInitialization();
    199 
    200   // Uses local caches to calculate an exact percentage prediction that the user
    201   // will take a particular match given what they have typed. |is_in_db| is set
    202   // to differentiate trivial zero results resulting from a match not being
    203   // found from actual zero results where the calculation returns 0.0.
    204   double CalculateConfidence(const base::string16& user_text,
    205                              const AutocompleteMatch& match,
    206                              bool* is_in_db) const;
    207 
    208   // Calculates the confidence for an entry in the DBCacheMap.
    209   double CalculateConfidenceForDbEntry(DBCacheMap::const_iterator iter) const;
    210 
    211   Profile* profile_;
    212 
    213   // Set when this is a predictor for an incognito profile.
    214   AutocompleteActionPredictor* main_profile_predictor_;
    215 
    216   // Set when this is a predictor for a non-incognito profile, and the incognito
    217   // profile creates a predictor.  If this is non-NULL when we finish
    218   // initialization, we should call CopyFromMainProfile() on it.
    219   AutocompleteActionPredictor* incognito_predictor_;
    220 
    221   // The backing data store.  This is NULL for incognito-owned predictors.
    222   scoped_refptr<AutocompleteActionPredictorTable> table_;
    223 
    224   content::NotificationRegistrar notification_registrar_;
    225 
    226   // This is cleared after every Omnibox navigation.
    227   std::vector<TransitionalMatch> transitional_matches_;
    228 
    229   scoped_ptr<prerender::PrerenderHandle> prerender_handle_;
    230 
    231   // This allows us to predict the effect of confidence threshold changes on
    232   // accuracy.  This is cleared after every omnibox navigation.
    233   mutable std::vector<std::pair<GURL, double> > tracked_urls_;
    234 
    235   // Local caches of the data store.  For incognito-owned predictors this is the
    236   // only copy of the data.
    237   DBCacheMap db_cache_;
    238   DBIdCacheMap db_id_cache_;
    239 
    240   bool initialized_;
    241 
    242   DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictor);
    243 };
    244 
    245 }  // namespace predictors
    246 
    247 #endif  // CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_
    248