Home | History | Annotate | Download | only in search_engines
      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_SEARCH_ENGINES_SEARCH_PROVIDER_INSTALL_DATA_H_
      6 #define CHROME_BROWSER_SEARCH_ENGINES_SEARCH_PROVIDER_INSTALL_DATA_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/callback.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/scoped_vector.h"
     15 #include "base/memory/weak_ptr.h"
     16 
     17 class GURL;
     18 class GoogleURLTracker;
     19 class SearchHostToURLsMap;
     20 class TemplateURL;
     21 class TemplateURLService;
     22 
     23 namespace content {
     24 class RenderProcessHost;
     25 }
     26 
     27 // Provides the search provider install state for the I/O thread. It works by
     28 // loading the data on demand (when CallWhenLoaded is called) and then throwing
     29 // away the results after the callbacks are done, so the results are always up
     30 // to date with what is in the database.
     31 class SearchProviderInstallData {
     32  public:
     33   enum State {
     34     // The search provider is not installed.
     35     NOT_INSTALLED = 0,
     36 
     37     // The search provider is in the user's set but is not
     38     INSTALLED_BUT_NOT_DEFAULT = 1,
     39 
     40     // The search provider is set as the user's default.
     41     INSTALLED_AS_DEFAULT = 2
     42   };
     43 
     44   // |host| is a RenderProcessHost that is observed, and whose destruction is a
     45   // signal to this class that it no longer needs to be kept up to date. (Note
     46   // that this class may be deleted before or after that death occurs. It
     47   // doesn't matter.)
     48   SearchProviderInstallData(TemplateURLService* template_url_service,
     49                             const std::string& google_base_url,
     50                             GoogleURLTracker* google_url_tracker,
     51                             content::RenderProcessHost* host);
     52   virtual ~SearchProviderInstallData();
     53 
     54   // Use to determine when the search provider information is loaded. The
     55   // callback may happen synchronously or asynchronously. There is no need to do
     56   // anything special to make it function (as it just relies on the normal I/O
     57   // thread message loop).
     58   void CallWhenLoaded(const base::Closure& closure);
     59 
     60   // Returns the search provider install state for the given origin.
     61   // This should only be called while a task is called back from CallWhenLoaded.
     62   State GetInstallState(const GURL& requested_origin);
     63 
     64   // Called when the google base url has changed.
     65   void OnGoogleURLChange(const std::string& google_base_url);
     66 
     67  private:
     68   // Receives a copy of the TemplateURLService's keywords on the IO thread.
     69   void OnTemplateURLsLoaded(ScopedVector<TemplateURL> template_urls,
     70                             TemplateURL* default_provider);
     71 
     72   // Stores information about the default search provider.
     73   void SetDefault(const TemplateURL* template_url);
     74 
     75   // Sets up the loaded state and then lets clients know that the search
     76   // provider install state has been loaded.
     77   void OnLoadFailed();
     78 
     79   // Does notifications to let clients know that the search provider
     80   // install state has been loaded.
     81   void NotifyLoaded();
     82 
     83   // The original data source. Only accessed on the UI thread.
     84   TemplateURLService* template_url_service_;
     85 
     86   // The list of closures to call after the load has finished. If empty, there
     87   // is no pending load.
     88   std::vector<base::Closure> closure_queue_;
     89 
     90   // Holds results of a load that was done using this class.
     91   scoped_ptr<SearchHostToURLsMap> provider_map_;
     92 
     93   // The list of template urls that are owned by the class.
     94   ScopedVector<TemplateURL> template_urls_;
     95 
     96   // The security origin for the default search provider.
     97   std::string default_search_origin_;
     98 
     99   // The google base url.
    100   std::string google_base_url_;
    101 
    102   base::WeakPtrFactory<SearchProviderInstallData> weak_factory_;
    103 
    104   DISALLOW_COPY_AND_ASSIGN(SearchProviderInstallData);
    105 };
    106 
    107 #endif  // CHROME_BROWSER_SEARCH_ENGINES_SEARCH_PROVIDER_INSTALL_DATA_H_
    108