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
17class GURL;
18class Profile;
19class SearchHostToURLsMap;
20class TemplateURL;
21class TemplateURLService;
22
23namespace content {
24class 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.
31class 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(Profile* profile, content::RenderProcessHost* host);
49  virtual ~SearchProviderInstallData();
50
51  // Use to determine when the search provider information is loaded. The
52  // callback may happen synchronously or asynchronously. There is no need to do
53  // anything special to make it function (as it just relies on the normal I/O
54  // thread message loop).
55  void CallWhenLoaded(const base::Closure& closure);
56
57  // Returns the search provider install state for the given origin.
58  // This should only be called while a task is called back from CallWhenLoaded.
59  State GetInstallState(const GURL& requested_origin);
60
61  // Called when the google base url has changed.
62  void OnGoogleURLChange(const std::string& google_base_url);
63
64 private:
65  // Receives a copy of the TemplateURLService's keywords on the IO thread.
66  void OnTemplateURLsLoaded(ScopedVector<TemplateURL> template_urls,
67                            TemplateURL* default_provider);
68
69  // Stores information about the default search provider.
70  void SetDefault(const TemplateURL* template_url);
71
72  // Sets up the loaded state and then lets clients know that the search
73  // provider install state has been loaded.
74  void OnLoadFailed();
75
76  // Does notifications to let clients know that the search provider
77  // install state has been loaded.
78  void NotifyLoaded();
79
80  // The original data source. Only accessed on the UI thread.
81  TemplateURLService* template_url_service_;
82
83  // The list of closures to call after the load has finished. If empty, there
84  // is no pending load.
85  std::vector<base::Closure> closure_queue_;
86
87  // Holds results of a load that was done using this class.
88  scoped_ptr<SearchHostToURLsMap> provider_map_;
89
90  // The list of template urls that are owned by the class.
91  ScopedVector<TemplateURL> template_urls_;
92
93  // The security origin for the default search provider.
94  std::string default_search_origin_;
95
96  // The google base url.
97  std::string google_base_url_;
98
99  base::WeakPtrFactory<SearchProviderInstallData> weak_factory_;
100
101  DISALLOW_COPY_AND_ASSIGN(SearchProviderInstallData);
102};
103
104#endif  // CHROME_BROWSER_SEARCH_ENGINES_SEARCH_PROVIDER_INSTALL_DATA_H_
105