[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 1 | // Copyright 2013 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 | |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |
| 6 | #define CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 7 | |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 8 | #include <map> |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 9 | #include <set> |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 10 | #include <string> |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 11 | |
| 12 | #include "base/basictypes.h" |
| 13 | #include "base/compiler_specific.h" |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame^] | 15 | #include "base/memory/weak_ptr.h" |
| 16 | #include "chrome/browser/history/history_types.h" |
[email protected] | 8b169b4b | 2013-03-23 18:24:46 | [diff] [blame] | 17 | #include "chrome/common/instant_restricted_id_cache.h" |
[email protected] | 0dd6f203 | 2013-05-20 23:33:40 | [diff] [blame] | 18 | #include "components/browser_context_keyed_service/browser_context_keyed_service.h" |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 19 | #include "content/public/browser/notification_observer.h" |
| 20 | #include "content/public/browser/notification_registrar.h" |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 21 | |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 22 | class GURL; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 23 | class InstantIOContext; |
| 24 | class Profile; |
| 25 | |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 26 | namespace net { |
| 27 | class URLRequest; |
| 28 | } |
| 29 | |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 30 | // Tracks render process host IDs that are associated with Instant. |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame] | 31 | class InstantService : public BrowserContextKeyedService, |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 32 | public content::NotificationObserver { |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 33 | public: |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 34 | explicit InstantService(Profile* profile); |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 35 | virtual ~InstantService(); |
| 36 | |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 37 | // A utility to translate an Instant path if it is of Most Visited item ID |
| 38 | // form. If path is a Most Visited item ID and we have a URL for it, then |
| 39 | // this URL is returned in string form. The |path| is a URL fragment |
| 40 | // corresponding to the path of url with the leading slash ("/") stripped. |
| 41 | // For example, chrome-search://favicon/72 would yield a |path| value of "72", |
| 42 | // and since 72 is a valid uint64 the path is translated to a valid url, |
| 43 | // "http://bingo.com/", say. |
| 44 | static const std::string MaybeTranslateInstantPathOnUI( |
| 45 | Profile* profile, const std::string& path); |
| 46 | static const std::string MaybeTranslateInstantPathOnIO( |
| 47 | const net::URLRequest* request, const std::string& path); |
| 48 | static bool IsInstantPath(const GURL& url); |
| 49 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 50 | // Add, remove, and query RenderProcessHost IDs that are associated with |
| 51 | // Instant processes. |
| 52 | void AddInstantProcess(int process_id); |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 53 | bool IsInstantProcess(int process_id) const; |
| 54 | |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 55 | #if defined(UNIT_TEST) |
| 56 | int GetInstantProcessCount() const { |
| 57 | return process_ids_.size(); |
| 58 | } |
| 59 | #endif |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 60 | |
[email protected] | 8b169b4b | 2013-03-23 18:24:46 | [diff] [blame] | 61 | // Most visited item API. |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 62 | |
[email protected] | 8b169b4b | 2013-03-23 18:24:46 | [diff] [blame] | 63 | // Adds |items| to the |most_visited_item_cache_| assigning restricted IDs in |
| 64 | // the process. |
| 65 | void AddMostVisitedItems(const std::vector<InstantMostVisitedItem>& items); |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 66 | |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame^] | 67 | // Invoked by the InstantController when the Instant page wants to delete a |
| 68 | // Most Visited item. |
| 69 | void DeleteMostVisitedItem(const GURL& url); |
| 70 | |
| 71 | // Invoked by the InstantController when the Instant page wants to undo the |
| 72 | // blacklist action. |
| 73 | void UndoMostVisitedDeletion(const GURL& url); |
| 74 | |
| 75 | // Invoked by the InstantController when the Instant page wants to undo all |
| 76 | // Most Visited deletions. |
| 77 | void UndoAllMostVisitedDeletions(); |
| 78 | |
[email protected] | 8b169b4b | 2013-03-23 18:24:46 | [diff] [blame] | 79 | // Returns the last added InstantMostVisitedItems. After the call to |
| 80 | // |AddMostVisitedItems|, the caller should call this to get the items with |
| 81 | // the assigned IDs. |
| 82 | void GetCurrentMostVisitedItems( |
| 83 | std::vector<InstantMostVisitedItemIDPair>* items) const; |
| 84 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 85 | private: |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame] | 86 | // Overridden from BrowserContextKeyedService: |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 87 | virtual void Shutdown() OVERRIDE; |
| 88 | |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 89 | // Overridden from content::NotificationObserver: |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 90 | virtual void Observe(int type, |
| 91 | const content::NotificationSource& source, |
| 92 | const content::NotificationDetails& details) OVERRIDE; |
| 93 | |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame^] | 94 | // If the |most_visited_item_id| is found in the cache, sets the |item| to it |
| 95 | // and returns true. |
| 96 | bool GetMostVisitedItemForID(InstantRestrictedID most_visited_item_id, |
| 97 | InstantMostVisitedItem* item) const; |
| 98 | |
| 99 | // Called when we get new most visited items from TopSites, registered as an |
| 100 | // async callback. Parses them and sends them to the renderer via |
| 101 | // SendMostVisitedItems. |
| 102 | void OnMostVisitedItemsReceived(const history::MostVisitedURLList& data); |
| 103 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 104 | Profile* const profile_; |
| 105 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 106 | // The process ids associated with Instant processes. |
| 107 | std::set<int> process_ids_; |
| 108 | |
[email protected] | 8b169b4b | 2013-03-23 18:24:46 | [diff] [blame] | 109 | // A cache of the InstantMostVisitedItems sent to the Instant Pages. |
| 110 | InstantRestrictedIDCache<InstantMostVisitedItem> most_visited_item_cache_; |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 111 | |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 112 | content::NotificationRegistrar registrar_; |
| 113 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 114 | scoped_refptr<InstantIOContext> instant_io_context_; |
| 115 | |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame^] | 116 | // Used for Top Sites async retrieval. |
| 117 | base::WeakPtrFactory<InstantService> weak_ptr_factory_; |
| 118 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 119 | DISALLOW_COPY_AND_ASSIGN(InstantService); |
| 120 | }; |
| 121 | |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 122 | #endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |