OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_INTENTS_WEB_INTENTS_REGISTRY_H_ | 5 #ifndef CHROME_BROWSER_INTENTS_WEB_INTENTS_REGISTRY_H_ |
6 #define CHROME_BROWSER_INTENTS_WEB_INTENTS_REGISTRY_H_ | 6 #define CHROME_BROWSER_INTENTS_WEB_INTENTS_REGISTRY_H_ |
7 | 7 |
8 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
9 #include "base/hash_tables.h" | 9 #include "base/hash_tables.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
12 #include "chrome/browser/profiles/profile_keyed_service.h" | 12 #include "chrome/browser/profiles/profile_keyed_service.h" |
13 #include "chrome/browser/webdata/web_data_service.h" | 13 #include "chrome/browser/webdata/web_data_service.h" |
14 #include "webkit/glue/web_intent_service_data.h" | 14 #include "webkit/glue/web_intent_service_data.h" |
15 | 15 |
16 struct DefaultWebIntentService; | 16 struct DefaultWebIntentService; |
17 | 17 |
18 namespace extensions { | 18 namespace extensions { |
19 class Extension; | 19 class Extension; |
20 } | 20 } |
21 | 21 |
22 // Handles storing and retrieving of web intents services in the web database. | 22 // Handles storing and retrieving of web intents services in the web database. |
23 // The registry provides filtering logic to retrieve specific types of services. | 23 // The registry provides filtering logic to retrieve specific types of services. |
24 class WebIntentsRegistry | 24 class WebIntentsRegistry : public ProfileKeyedService { |
25 : public WebDataServiceConsumer, | |
26 public ProfileKeyedService { | |
27 public: | 25 public: |
28 typedef std::vector<webkit_glue::WebIntentServiceData> IntentServiceList; | 26 typedef std::vector<webkit_glue::WebIntentServiceData> IntentServiceList; |
29 | 27 |
30 // Callback used by WebIntentsRegistry to return results of data fetch. | 28 // Callback used by WebIntentsRegistry to return results of data fetch. |
31 typedef base::Callback<void(const IntentServiceList&)> | 29 typedef base::Callback<void(const IntentServiceList&)> |
32 QueryCallback; | 30 QueryCallback; |
33 | 31 |
34 // Callback to return results of a defaults query. | 32 // Callback to return results of a defaults query. |
35 typedef base::Callback<void(const DefaultWebIntentService&)> | 33 typedef base::Callback<void(const DefaultWebIntentService&)> |
36 DefaultQueryCallback; | 34 DefaultQueryCallback; |
37 | 35 |
38 // Initializes, binds to a valid WebDataService. | 36 // Initializes, binds to a valid WebDataService. |
39 void Initialize(scoped_refptr<WebDataService> wds, | 37 void Initialize(scoped_refptr<WebDataService> wds, |
40 ExtensionServiceInterface* extension_service); | 38 ExtensionServiceInterface* extension_service); |
41 | 39 |
42 // Registers a service. | 40 // Registers a service. |
43 virtual void RegisterIntentService( | 41 virtual void RegisterIntentService( |
44 const webkit_glue::WebIntentServiceData& service); | 42 const webkit_glue::WebIntentServiceData& service); |
45 | 43 |
46 // Removes a service from the registry. | 44 // Removes a service from the registry. |
47 void UnregisterIntentService( | 45 void UnregisterIntentService( |
48 const webkit_glue::WebIntentServiceData& service); | 46 const webkit_glue::WebIntentServiceData& service); |
49 | 47 |
50 // Requests all services matching |action| and |mimetype|. | 48 // Requests all services matching |action| and |type|. |
51 // |mimetype| can contain wildcards, i.e. "image/*" or "*". | 49 // |type| can contain wildcards, i.e. "image/*" or "*". |
52 // |callback| must not be null. | 50 // |callback| must not be null. |
53 void GetIntentServices(const string16& action, | 51 void GetIntentServices(const string16& action, |
54 const string16& mimetype, | 52 const string16& type, |
55 const QueryCallback& callback); | 53 const QueryCallback& callback); |
56 | 54 |
57 // Requests all services. | 55 // Requests all services. |
58 // |callback| must not be null. | 56 // |callback| must not be null. |
59 void GetAllIntentServices(const QueryCallback& callback); | 57 void GetAllIntentServices(const QueryCallback& callback); |
60 | 58 |
61 // Tests for the existence of the given |service|. Calls the | 59 // Tests for the existence of the given |service|. Calls the |
62 // provided |callback| with true if it exists, false if it does not. | 60 // provided |callback| with true if it exists, false if it does not. |
63 // Checks for |service| equality with ==. | 61 // Checks for |service| equality with ==. |
64 // |callback| must not be null. | 62 // |callback| must not be null. |
65 void IntentServiceExists( | 63 void IntentServiceExists( |
66 const webkit_glue::WebIntentServiceData& service, | 64 const webkit_glue::WebIntentServiceData& service, |
67 const base::Callback<void(bool)>& callback); | 65 const base::Callback<void(bool)>& callback); |
68 | 66 |
69 // Requests all extension services matching |action|, |mimetype| and | 67 // Requests all extension services matching |action|, |type| and |
70 // |extension_id|. | 68 // |extension_id|. |
71 // |mimetype| must conform to definition as outlined for GetIntentServices. | 69 // |type| must conform to definition as outlined for GetIntentServices. |
72 // |callback| must not be null. | 70 // |callback| must not be null. |
73 void GetIntentServicesForExtensionFilter(const string16& action, | 71 void GetIntentServicesForExtensionFilter(const string16& action, |
74 const string16& mimetype, | 72 const string16& type, |
75 const std::string& extension_id, | 73 const std::string& extension_id, |
76 const QueryCallback& callback); | 74 const QueryCallback& callback); |
77 | 75 |
78 // Record the given default service entry. | 76 // Record the given default service entry. |
79 virtual void RegisterDefaultIntentService( | 77 virtual void RegisterDefaultIntentService( |
80 const DefaultWebIntentService& default_service); | 78 const DefaultWebIntentService& default_service); |
81 | 79 |
82 // Delete the given default service entry. Deletes entries matching | 80 // Delete the given default service entry. Deletes entries matching |
83 // the |action|, |type|, and |url_pattern| of |default_service|. | 81 // the |action|, |type|, and |url_pattern| of |default_service|. |
84 virtual void UnregisterDefaultIntentService( | 82 virtual void UnregisterDefaultIntentService( |
85 const DefaultWebIntentService& default_service); | 83 const DefaultWebIntentService& default_service); |
86 | 84 |
87 // Requests the best default intent service for the given invocation | 85 // Requests the best default intent service for the given invocation |
88 // parameters. | 86 // parameters. |
89 // |callback| must not be null. | 87 // |callback| must not be null. |
90 void GetDefaultIntentService(const string16& action, | 88 void GetDefaultIntentService(const string16& action, |
91 const string16& type, | 89 const string16& type, |
92 const GURL& invoking_url, | 90 const GURL& invoking_url, |
93 const DefaultQueryCallback& callback); | 91 const DefaultQueryCallback& callback); |
94 | 92 |
95 protected: | 93 protected: |
96 // Make sure that only WebIntentsRegistryFactory can create an instance of | 94 // Make sure that only WebIntentsRegistryFactory can create an instance of |
97 // WebIntentsRegistry. | 95 // WebIntentsRegistry. |
98 friend class WebIntentsRegistryFactory; | 96 friend class WebIntentsRegistryFactory; |
99 friend class WebIntentsRegistryTest; | 97 friend class WebIntentsRegistryTest; |
100 friend class WebIntentsModelTest; | 98 friend class WebIntentsModelTest; |
101 FRIEND_TEST_ALL_PREFIXES(WebIntentsRegistryTest, CollapseIntents); | 99 FRIEND_TEST_ALL_PREFIXES(WebIntentsRegistryTest, CollapseIntents); |
102 | 100 |
103 WebIntentsRegistry(); | 101 WebIntentsRegistry(); |
104 virtual ~WebIntentsRegistry(); | 102 virtual ~WebIntentsRegistry(); |
105 | 103 |
106 // Collapses a list of IntentServices by joining intents that have identical | 104 // Collapses a list of IntentServices by joining intents that have identical |
107 // service URLs, actions, and mime types. | 105 // service URLs, actions, and mime types. |
108 // |services| is the list of intent services to be collapsed when passed in | 106 // |services| is the list of intent services to be collapsed when passed in |
109 // and will be modified with the new list in-place. | 107 // and will be modified with the new list in-place. |
110 void CollapseIntents(IntentServiceList* services); | 108 void CollapseIntents(IntentServiceList* services); |
111 | 109 |
112 private: | 110 private: |
113 const extensions::Extension* ExtensionForURL(const std::string& url); | 111 struct IntentsQuery; |
| 112 typedef std::vector<IntentsQuery*> QueryVector; |
114 | 113 |
115 struct IntentsQuery; | 114 // Handles services loaded |
| 115 void OnWebIntentsResultReceived( |
| 116 IntentsQuery* query, |
| 117 const WDTypedResult* result); |
116 | 118 |
117 // Maps web data requests to intents queries. | 119 // Handles default services loaded |
118 // Allows OnWebDataServiceRequestDone to forward to appropriate consumer. | 120 void OnWebIntentsDefaultsResultReceived( |
119 typedef base::hash_map<WebDataService::Handle, IntentsQuery*> QueryMap; | 121 IntentsQuery* query, |
120 | |
121 // WebDataServiceConsumer implementation. | |
122 virtual void OnWebDataServiceRequestDone( | |
123 WebDataService::Handle h, | |
124 const WDTypedResult* result) OVERRIDE; | |
125 | |
126 // Delegate for defaults requests from OnWebDataServiceRequestDone. | |
127 virtual void OnWebDataServiceDefaultsRequestDone( | |
128 WebDataService::Handle h, | |
129 const WDTypedResult* result); | 122 const WDTypedResult* result); |
130 | 123 |
131 // Implementation of GetIntentServicesForExtensionFilter. | 124 // Implementation of GetIntentServicesForExtensionFilter. |
132 void DoGetIntentServicesForExtensionFilter(scoped_ptr<IntentsQuery> query, | 125 void DoGetIntentServicesForExtensionFilter(scoped_ptr<IntentsQuery> query, |
133 const std::string& extension_id); | 126 const std::string& extension_id); |
134 | 127 |
| 128 const extensions::Extension* ExtensionForURL(const std::string& url); |
| 129 |
| 130 // Adds a query to the list of pending queries. |
| 131 void TrackQuery(IntentsQuery* query); |
| 132 |
| 133 // Takes ownership of a query. This removes a query from the list |
| 134 // of pending queries. |
| 135 bool ReleaseQuery(IntentsQuery* query); |
| 136 |
135 // Map for all in-flight web data requests/intent queries. | 137 // Map for all in-flight web data requests/intent queries. |
136 QueryMap queries_; | 138 QueryVector pending_queries_; |
137 | 139 |
138 // Local reference to Web Data Service. | 140 // Local reference to Web Data Service. |
139 scoped_refptr<WebDataService> wds_; | 141 scoped_refptr<WebDataService> wds_; |
140 | 142 |
141 // Local reference to the ExtensionService. | 143 // Local reference to the ExtensionService. |
142 // Shutdown/cleanup is handled by ProfileImpl. We are guaranteed that any | 144 // Shutdown/cleanup is handled by ProfileImpl. We are guaranteed that any |
143 // ProfileKeyedService will be shut down before data on ProfileImpl is | 145 // ProfileKeyedService will be shut down before data on ProfileImpl is |
144 // destroyed (i.e. |extension_service_|), so |extension_service_| is valid | 146 // destroyed (i.e. |extension_service_|), so |extension_service_| is valid |
145 // for the lifetime of the WebIntentsRegistry object. | 147 // for the lifetime of the WebIntentsRegistry object. |
146 ExtensionServiceInterface* extension_service_; | 148 ExtensionServiceInterface* extension_service_; |
147 | 149 |
148 DISALLOW_COPY_AND_ASSIGN(WebIntentsRegistry); | 150 DISALLOW_COPY_AND_ASSIGN(WebIntentsRegistry); |
149 }; | 151 }; |
150 | 152 |
151 #endif // CHROME_BROWSER_INTENTS_WEB_INTENTS_REGISTRY_H_ | 153 #endif // CHROME_BROWSER_INTENTS_WEB_INTENTS_REGISTRY_H_ |
OLD | NEW |