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_SEARCH_ENGINES_SEARCH_PROVIDER_INSTALL_DATA_H_
6#define CHROME_BROWSER_SEARCH_ENGINES_SEARCH_PROVIDER_INSTALL_DATA_H_
7#pragma once
8
9#include <string>
10
11#include "base/basictypes.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#include "base/task_queue.h"
17#include "chrome/browser/webdata/web_data_service.h"
18
19class GURL;
20class NotificationSource;
21class NotificationType;
22class SearchHostToURLsMap;
23class Task;
24class TemplateURL;
25
26// Provides the search provider install state for the I/O thread. It works by
27// loading the data on demand (when CallWhenLoaded is called) and then throwing
28// away the results after the callbacks are done, so the results are always up
29// to date with what is in the database.
30class SearchProviderInstallData : public WebDataServiceConsumer,
31    public base::SupportsWeakPtr<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  // |ui_death_notification| and |ui_death_source| indentify a notification that
45  // may be observed on the UI thread to know when this class no longer needs to
46  // be kept up to date. (Note that this class may be deleted before or after
47  // that notification occurs. It doesn't matter.)
48  SearchProviderInstallData(WebDataService* web_service,
49                            NotificationType ui_death_notification,
50                            const NotificationSource& ui_death_source);
51  virtual ~SearchProviderInstallData();
52
53  // Use to determine when the search provider information is loaded. The
54  // callback may happen synchronously or asynchronously. This takes ownership
55  // of |task|. There is no need to do anything special to make it function
56  // (as it just relies on the normal I/O thread message loop).
57  void CallWhenLoaded(Task* task);
58
59  // Returns the search provider install state for the given origin.
60  // This should only be called while a task is called back from CallWhenLoaded.
61  State GetInstallState(const GURL& requested_origin);
62
63  // Called when the google base url has changed.
64  void OnGoogleURLChange(const std::string& google_base_url);
65
66 private:
67  // WebDataServiceConsumer
68  // Notification that the keywords have been loaded.
69  // This is invoked from WebDataService, and should not be directly
70  // invoked.
71  virtual void OnWebDataServiceRequestDone(WebDataService::Handle h,
72                                           const WDTypedResult* result);
73
74  // Stores information about the default search provider.
75  void SetDefault(const TemplateURL* template_url);
76
77  // Sets up the loaded state and then lets clients know that the search
78  // provider install state has been loaded.
79  void OnLoadFailed();
80
81  // Does notifications to let clients know that the search provider
82  // install state has been loaded.
83  void NotifyLoaded();
84
85  // The list of tasks to call after the load has finished.
86  TaskQueue task_queue_;
87
88  // Service used to store entries.
89  scoped_refptr<WebDataService> web_service_;
90
91  // If non-zero, we're waiting on a load.
92  WebDataService::Handle load_handle_;
93
94  // Holds results of a load that was done using this class.
95  scoped_ptr<SearchHostToURLsMap> provider_map_;
96
97  // The list of template urls that are owned by the class.
98  ScopedVector<const TemplateURL> template_urls_;
99
100  // The security origin for the default search provider.
101  std::string default_search_origin_;
102
103  // The google base url.
104  std::string google_base_url_;
105
106  DISALLOW_COPY_AND_ASSIGN(SearchProviderInstallData);
107};
108
109#endif  // CHROME_BROWSER_SEARCH_ENGINES_SEARCH_PROVIDER_INSTALL_DATA_H_
110