| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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_SEARCH_INSTANT_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |
| 6 #define CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ | 6 #define CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 13 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 16 #include "chrome/browser/history/history_types.h" | 17 #include "chrome/browser/history/history_types.h" |
| 17 #include "chrome/common/instant_restricted_id_cache.h" | 18 #include "chrome/common/instant_types.h" |
| 18 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 19 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
| 19 #include "content/public/browser/notification_observer.h" | 20 #include "content/public/browser/notification_observer.h" |
| 20 #include "content/public/browser/notification_registrar.h" | 21 #include "content/public/browser/notification_registrar.h" |
| 21 | 22 |
| 22 class GURL; | 23 class GURL; |
| 23 class InstantIOContext; | 24 class InstantIOContext; |
| 24 class Profile; | 25 class Profile; |
| 25 | 26 |
| 26 namespace net { | 27 namespace net { |
| 27 class URLRequest; | 28 class URLRequest; |
| 28 } | 29 } |
| 29 | 30 |
| 30 // Tracks render process host IDs that are associated with Instant. | 31 // Tracks render process host IDs that are associated with Instant. |
| 31 class InstantService : public BrowserContextKeyedService, | 32 class InstantService : public BrowserContextKeyedService, |
| 32 public content::NotificationObserver { | 33 public content::NotificationObserver { |
| 33 public: | 34 public: |
| 34 explicit InstantService(Profile* profile); | 35 explicit InstantService(Profile* profile); |
| 35 virtual ~InstantService(); | 36 virtual ~InstantService(); |
| 36 | 37 |
| 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 | |
| 50 // Add, remove, and query RenderProcessHost IDs that are associated with | 38 // Add, remove, and query RenderProcessHost IDs that are associated with |
| 51 // Instant processes. | 39 // Instant processes. |
| 52 void AddInstantProcess(int process_id); | 40 void AddInstantProcess(int process_id); |
| 53 bool IsInstantProcess(int process_id) const; | 41 bool IsInstantProcess(int process_id) const; |
| 54 | 42 |
| 55 #if defined(UNIT_TEST) | 43 #if defined(UNIT_TEST) |
| 56 int GetInstantProcessCount() const { | 44 int GetInstantProcessCount() const { |
| 57 return process_ids_.size(); | 45 return process_ids_.size(); |
| 58 } | 46 } |
| 59 #endif | 47 #endif |
| 60 | 48 |
| 61 // Most visited item API. | 49 // Most visited item API. |
| 62 | 50 |
| 63 // Adds |items| to the |most_visited_item_cache_| assigning restricted IDs in | |
| 64 // the process. | |
| 65 void AddMostVisitedItems(const std::vector<InstantMostVisitedItem>& items); | |
| 66 | |
| 67 // Invoked by the InstantController when the Instant page wants to delete a | 51 // Invoked by the InstantController when the Instant page wants to delete a |
| 68 // Most Visited item. | 52 // Most Visited item. |
| 69 void DeleteMostVisitedItem(const GURL& url); | 53 void DeleteMostVisitedItem(const GURL& url); |
| 70 | 54 |
| 71 // Invoked by the InstantController when the Instant page wants to undo the | 55 // Invoked by the InstantController when the Instant page wants to undo the |
| 72 // blacklist action. | 56 // blacklist action. |
| 73 void UndoMostVisitedDeletion(const GURL& url); | 57 void UndoMostVisitedDeletion(const GURL& url); |
| 74 | 58 |
| 75 // Invoked by the InstantController when the Instant page wants to undo all | 59 // Invoked by the InstantController when the Instant page wants to undo all |
| 76 // Most Visited deletions. | 60 // Most Visited deletions. |
| 77 void UndoAllMostVisitedDeletions(); | 61 void UndoAllMostVisitedDeletions(); |
| 78 | 62 |
| 79 // Returns the last added InstantMostVisitedItems. After the call to | 63 // Returns the last added InstantMostVisitedItems. |
| 80 // |AddMostVisitedItems|, the caller should call this to get the items with | |
| 81 // the assigned IDs. | |
| 82 void GetCurrentMostVisitedItems( | 64 void GetCurrentMostVisitedItems( |
| 83 std::vector<InstantMostVisitedItemIDPair>* items) const; | 65 std::vector<InstantMostVisitedItem>* items) const; |
| 84 | 66 |
| 85 private: | 67 private: |
| 86 // Overridden from BrowserContextKeyedService: | 68 // Overridden from BrowserContextKeyedService: |
| 87 virtual void Shutdown() OVERRIDE; | 69 virtual void Shutdown() OVERRIDE; |
| 88 | 70 |
| 89 // Overridden from content::NotificationObserver: | 71 // Overridden from content::NotificationObserver: |
| 90 virtual void Observe(int type, | 72 virtual void Observe(int type, |
| 91 const content::NotificationSource& source, | 73 const content::NotificationSource& source, |
| 92 const content::NotificationDetails& details) OVERRIDE; | 74 const content::NotificationDetails& details) OVERRIDE; |
| 93 | 75 |
| 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 the TopNav code, | 76 // Called when we get new most visited items from the TopNav code, |
| 100 // registered as an async callback. Parses them and sends them to the renderer | 77 // registered as an async callback. Parses them and sends them to the renderer |
| 101 // via SendMostVisitedItems. | 78 // via SendMostVisitedItems. |
| 102 void OnMostVisitedItemsReceived(const history::MostVisitedURLList& data); | 79 void OnMostVisitedItemsReceived(const history::MostVisitedURLList& data); |
| 103 | 80 |
| 104 Profile* const profile_; | 81 Profile* const profile_; |
| 105 | 82 |
| 106 // The process ids associated with Instant processes. | 83 // The process ids associated with Instant processes. |
| 107 std::set<int> process_ids_; | 84 std::set<int> process_ids_; |
| 108 | 85 |
| 109 // A cache of the InstantMostVisitedItems sent to the Instant Pages. | 86 // InstantMostVisitedItems sent to the Instant Pages. |
| 110 InstantRestrictedIDCache<InstantMostVisitedItem> most_visited_item_cache_; | 87 std::vector<InstantMostVisitedItem> most_visited_items_; |
| 111 | 88 |
| 112 content::NotificationRegistrar registrar_; | 89 content::NotificationRegistrar registrar_; |
| 113 | 90 |
| 114 scoped_refptr<InstantIOContext> instant_io_context_; | 91 scoped_refptr<InstantIOContext> instant_io_context_; |
| 115 | 92 |
| 116 // Used for Top Sites async retrieval. | 93 // Used for Top Sites async retrieval. |
| 117 base::WeakPtrFactory<InstantService> weak_ptr_factory_; | 94 base::WeakPtrFactory<InstantService> weak_ptr_factory_; |
| 118 | 95 |
| 119 DISALLOW_COPY_AND_ASSIGN(InstantService); | 96 DISALLOW_COPY_AND_ASSIGN(InstantService); |
| 120 }; | 97 }; |
| 121 | 98 |
| 122 #endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ | 99 #endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |
| OLD | NEW |