instant_io_context.h revision 868fa2fe829687343ffae624259930155e16dbd8
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
5#ifndef CHROME_BROWSER_SEARCH_INSTANT_IO_CONTEXT_H_
6#define CHROME_BROWSER_SEARCH_INSTANT_IO_CONTEXT_H_
7
8#include <set>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12
13class GURL;
14
15namespace content {
16class ResourceContext;
17}
18
19namespace net {
20class URLRequest;
21}
22
23// IO thread data held for Instant.  This reflects the data held in
24// InstantService for use on the IO thread.  Owned by ResourceContext
25// as user data.
26class InstantIOContext : public base::RefCountedThreadSafe<InstantIOContext> {
27 public:
28  InstantIOContext();
29
30  // Key name for context UserData.  UserData is created by InstantService
31  // but accessed by InstantIOContext.
32  static const char kInstantIOContextKeyName[];
33
34  // Installs the |instant_io_context| into the UserData of the
35  // |resource_context|.
36  static void SetUserDataOnIO(
37      content::ResourceContext* resource_context,
38      scoped_refptr<InstantIOContext> instant_io_context);
39
40  // Add and remove RenderProcessHost IDs that are associated with Instant
41  // processes.  Used to keep process IDs in sync with InstantService.
42  static void AddInstantProcessOnIO(
43      scoped_refptr<InstantIOContext> instant_io_context,
44      int process_id);
45  static void RemoveInstantProcessOnIO(
46      scoped_refptr<InstantIOContext> instant_io_context,
47      int process_id);
48  static void ClearInstantProcessesOnIO(
49      scoped_refptr<InstantIOContext> instant_io_context);
50
51  // Determine if this chrome-search: request is coming from an Instant render
52  // process.
53  static bool ShouldServiceRequest(const net::URLRequest* request);
54
55 protected:
56   virtual ~InstantIOContext();
57
58 private:
59  friend class base::RefCountedThreadSafe<InstantIOContext>;
60
61  // Check that |process_id| is in the known set of Instant processes, ie.
62  // |process_ids_|.
63  bool IsInstantProcess(int process_id) const;
64
65  // The process IDs associated with Instant processes.  Mirror of the process
66  // IDs in InstantService.  Duplicated here for synchronous access on the IO
67  // thread.
68  std::set<int> process_ids_;
69
70  DISALLOW_COPY_AND_ASSIGN(InstantIOContext);
71};
72
73#endif  // CHROME_BROWSER_SEARCH_INSTANT_IO_CONTEXT_H_
74