blob: 8bd6cbd403497532b37c1014f008cb5697d585b7 [file] [log] [blame]
[email protected]c5dec6292013-01-25 04:54:521// 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]a7b8e43d2013-03-18 18:52:435#ifndef CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_
6#define CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_
[email protected]c5dec6292013-01-25 04:54:527
[email protected]e7868c82013-03-12 03:55:568#include <map>
[email protected]c5dec6292013-01-25 04:54:529#include <set>
[email protected]e7868c82013-03-12 03:55:5610#include <string>
[email protected]c5dec6292013-01-25 04:54:5211
12#include "base/basictypes.h"
13#include "base/compiler_specific.h"
[email protected]672c8c12013-03-07 12:30:0614#include "base/memory/ref_counted.h"
[email protected]280be992013-06-04 03:19:5115#include "base/memory/weak_ptr.h"
16#include "chrome/browser/history/history_types.h"
[email protected]8b169b4b2013-03-23 18:24:4617#include "chrome/common/instant_restricted_id_cache.h"
[email protected]0dd6f2032013-05-20 23:33:4018#include "components/browser_context_keyed_service/browser_context_keyed_service.h"
[email protected]54e0ee62013-02-01 17:25:0319#include "content/public/browser/notification_observer.h"
20#include "content/public/browser/notification_registrar.h"
[email protected]c5dec6292013-01-25 04:54:5221
[email protected]e7868c82013-03-12 03:55:5622class GURL;
[email protected]672c8c12013-03-07 12:30:0623class InstantIOContext;
24class Profile;
25
[email protected]e7868c82013-03-12 03:55:5626namespace net {
27class URLRequest;
28}
29
[email protected]d572bfd2013-02-14 06:14:2030// Tracks render process host IDs that are associated with Instant.
[email protected]f1484c52013-05-22 23:25:4431class InstantService : public BrowserContextKeyedService,
[email protected]54e0ee62013-02-01 17:25:0332 public content::NotificationObserver {
[email protected]c5dec6292013-01-25 04:54:5233 public:
[email protected]672c8c12013-03-07 12:30:0634 explicit InstantService(Profile* profile);
[email protected]c5dec6292013-01-25 04:54:5235 virtual ~InstantService();
36
[email protected]e7868c82013-03-12 03:55:5637 // 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]c5dec6292013-01-25 04:54:5250 // Add, remove, and query RenderProcessHost IDs that are associated with
51 // Instant processes.
52 void AddInstantProcess(int process_id);
[email protected]c5dec6292013-01-25 04:54:5253 bool IsInstantProcess(int process_id) const;
54
[email protected]d572bfd2013-02-14 06:14:2055#if defined(UNIT_TEST)
56 int GetInstantProcessCount() const {
57 return process_ids_.size();
58 }
59#endif
[email protected]c5dec6292013-01-25 04:54:5260
[email protected]8b169b4b2013-03-23 18:24:4661 // Most visited item API.
[email protected]e7868c82013-03-12 03:55:5662
[email protected]8b169b4b2013-03-23 18:24:4663 // 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]e7868c82013-03-12 03:55:5666
[email protected]280be992013-06-04 03:19:5167 // 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]8b169b4b2013-03-23 18:24:4679 // 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]c5dec6292013-01-25 04:54:5285 private:
[email protected]f1484c52013-05-22 23:25:4486 // Overridden from BrowserContextKeyedService:
[email protected]c5dec6292013-01-25 04:54:5287 virtual void Shutdown() OVERRIDE;
88
[email protected]d572bfd2013-02-14 06:14:2089 // Overridden from content::NotificationObserver:
[email protected]54e0ee62013-02-01 17:25:0390 virtual void Observe(int type,
91 const content::NotificationSource& source,
92 const content::NotificationDetails& details) OVERRIDE;
93
[email protected]280be992013-06-04 03:19:5194 // 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]672c8c12013-03-07 12:30:06104 Profile* const profile_;
105
[email protected]c5dec6292013-01-25 04:54:52106 // The process ids associated with Instant processes.
107 std::set<int> process_ids_;
108
[email protected]8b169b4b2013-03-23 18:24:46109 // A cache of the InstantMostVisitedItems sent to the Instant Pages.
110 InstantRestrictedIDCache<InstantMostVisitedItem> most_visited_item_cache_;
[email protected]e7868c82013-03-12 03:55:56111
[email protected]54e0ee62013-02-01 17:25:03112 content::NotificationRegistrar registrar_;
113
[email protected]672c8c12013-03-07 12:30:06114 scoped_refptr<InstantIOContext> instant_io_context_;
115
[email protected]280be992013-06-04 03:19:51116 // Used for Top Sites async retrieval.
117 base::WeakPtrFactory<InstantService> weak_ptr_factory_;
118
[email protected]c5dec6292013-01-25 04:54:52119 DISALLOW_COPY_AND_ASSIGN(InstantService);
120};
121
[email protected]a7b8e43d2013-03-18 18:52:43122#endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_