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.
5 #ifndef CHROME_BROWSER_INSTANT_INSTANT_SERVICE_H_
6 #define CHROME_BROWSER_INSTANT_INSTANT_SERVICE_H_
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "chrome/browser/profiles/profile_keyed_service.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
20 class InstantIOContext
;
27 // Tracks render process host IDs that are associated with Instant.
28 class InstantService
: public ProfileKeyedService
,
29 public content::NotificationObserver
{
31 explicit InstantService(Profile
* profile
);
32 virtual ~InstantService();
34 // A utility to translate an Instant path if it is of Most Visited item ID
35 // form. If path is a Most Visited item ID and we have a URL for it, then
36 // this URL is returned in string form. The |path| is a URL fragment
37 // corresponding to the path of url with the leading slash ("/") stripped.
38 // For example, chrome-search://favicon/72 would yield a |path| value of "72",
39 // and since 72 is a valid uint64 the path is translated to a valid url,
40 // "http://bingo.com/", say.
41 static const std::string
MaybeTranslateInstantPathOnUI(
42 Profile
* profile
, const std::string
& path
);
43 static const std::string
MaybeTranslateInstantPathOnIO(
44 const net::URLRequest
* request
, const std::string
& path
);
45 static bool IsInstantPath(const GURL
& url
);
47 // Add, remove, and query RenderProcessHost IDs that are associated with
49 void AddInstantProcess(int process_id
);
50 bool IsInstantProcess(int process_id
) const;
52 #if defined(UNIT_TEST)
53 int GetInstantProcessCount() const {
54 return process_ids_
.size();
58 // If |url| is known the existing Most Visited item ID is returned. Otherwise
59 // a new Most Visited item ID is associated with the |url| and returned.
60 uint64
AddURL(const GURL
& url
);
62 // If there is a mapping for the |url|, sets |most_visited_item_id| and
64 bool GetMostVisitedItemIDForURL(const GURL
& url
,
65 uint64
* most_visited_item_id
);
67 // If there is a mapping for the |most_visited_item_id|, sets |url| and
69 bool GetURLForMostVisitedItemId(uint64 most_visited_item_id
, GURL
* url
);
72 // Overridden from ProfileKeyedService:
73 virtual void Shutdown() OVERRIDE
;
75 // Overridden from content::NotificationObserver:
76 virtual void Observe(int type
,
77 const content::NotificationSource
& source
,
78 const content::NotificationDetails
& details
) OVERRIDE
;
80 // Removes entries of each url in |deleted_urls| from the ID maps. Or all
81 // IDs if |all_history| is true. |deleted_ids| is filled with the newly
82 // deleted Most Visited item IDs.
83 void DeleteHistoryURLs(const std::vector
<GURL
>& deleted_urls
,
84 std::vector
<uint64
>* deleted_ids
);
86 Profile
* const profile_
;
88 // The process ids associated with Instant processes.
89 std::set
<int> process_ids_
;
91 // A mapping of Most Visited IDs to URLs. Used to hide Most Visited and
92 // Favicon URLs from the Instant search provider.
93 uint64 last_most_visited_item_id_
;
94 std::map
<uint64
, GURL
> most_visited_item_id_to_url_map_
;
95 std::map
<GURL
, uint64
> url_to_most_visited_item_id_map_
;
97 content::NotificationRegistrar registrar_
;
99 scoped_refptr
<InstantIOContext
> instant_io_context_
;
101 DISALLOW_COPY_AND_ASSIGN(InstantService
);
104 #endif // CHROME_BROWSER_INSTANT_INSTANT_SERVICE_H_