File: web_data_request_manager.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (136 lines) | stat: -rw-r--r-- 5,013 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Chromium settings and storage represent user-selected preferences and
// information and MUST not be extracted, overwritten or modified except
// through Chromium defined APIs.

#ifndef COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__
#define COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__

#include <atomic>
#include <map>
#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.h"
#include "base/task/sequenced_task_runner.h"
#include "components/webdata/common/web_data_results.h"
#include "components/webdata/common/web_data_service_base.h"
#include "components/webdata/common/web_data_service_consumer.h"
#include "components/webdata/common/web_database_service.h"

class WebDataRequestManager;

//////////////////////////////////////////////////////////////////////////////
//
// WebData requests
//
// Every request is processed using a request object. The object contains
// both the request parameters and the results.
//////////////////////////////////////////////////////////////////////////////
class WebDataRequest {
 public:
  WebDataRequest(const WebDataRequest&) = delete;
  WebDataRequest& operator=(const WebDataRequest&) = delete;

  virtual ~WebDataRequest();

  // Returns the identifier for this request.
  WebDataServiceBase::Handle GetHandle() const;

  // Returns |true| if the request is active and |false| if the request has been
  // cancelled or has already completed.
  bool IsActive();

 private:
  // For access to the web request mutable state under the manager's lock.
  friend class WebDataRequestManager;

  // Private constructor called for WebDataRequestManager::NewRequest.
  WebDataRequest(WebDataRequestManager* manager,
                 WebDataServiceRequestCallback consumer,
                 WebDataServiceBase::Handle handle);

  // Retrieves the manager set in the constructor, if the request is still
  // active, or nullptr if the request is inactive. The returned value may
  // change between calls.
  WebDataRequestManager* GetManager();

  // Retrieves and resets the |consumer_| set in the constructor.
  WebDataServiceRequestCallback ExtractConsumer() &&;

  // Retrieves the original task runner of the request.  This may be null if the
  // original task was not posted as a sequenced task.
  scoped_refptr<base::SequencedTaskRunner> GetTaskRunner();

  // Marks the current request as inactive, either due to cancellation or
  // completion.
  void MarkAsInactive();

  // Tracks task runner that the request originated on.
  const scoped_refptr<base::SequencedTaskRunner> task_runner_;

  // The manager associated with this request. This is stored as a raw (untyped)
  // pointer value because it does double duty as the flag indicating whether or
  // not this request is active (non-nullptr => active).
  std::atomic<WebDataRequestManager*> atomic_manager_;

  // The originator of the service request.
  WebDataServiceRequestCallback consumer_;

  // Identifier for this request.
  const WebDataServiceBase::Handle handle_;
};

//////////////////////////////////////////////////////////////////////////////
//
// WebData Request Manager
//
// Tracks all WebDataRequests for a WebDataService.
//
// Note: This is an internal interface, not to be used outside of webdata/
//////////////////////////////////////////////////////////////////////////////
class WebDataRequestManager
    : public base::RefCountedThreadSafe<WebDataRequestManager> {
 public:
  WebDataRequestManager();

  WebDataRequestManager(const WebDataRequestManager&) = delete;
  WebDataRequestManager& operator=(const WebDataRequestManager&) = delete;

  // Factory function to create a new WebDataRequest.
  std::unique_ptr<WebDataRequest> NewRequest(
      WebDataServiceRequestCallback consumer);

  // Cancel any pending request.
  void CancelRequest(WebDataServiceBase::Handle h);

  // Invoked by the WebDataService when |request| has been completed.
  void RequestCompleted(std::unique_ptr<WebDataRequest> request,
                        std::unique_ptr<WDTypedResult> result);

 private:
  friend class base::RefCountedThreadSafe<WebDataRequestManager>;

  ~WebDataRequestManager();

  // This will notify the consumer in whatever thread was used to create this
  // request.
  void RequestCompletedOnThread(std::unique_ptr<WebDataRequest> request,
                                std::unique_ptr<WDTypedResult> result);

  // A lock to protect pending requests and next request handle.
  base::Lock pending_lock_;

  // Next handle to be used for requests. Incremented for each use.
  WebDataServiceBase::Handle next_request_handle_;

  std::map<WebDataServiceBase::Handle, raw_ptr<WebDataRequest, CtnExperimental>>
      pending_requests_;
};

#endif  // COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__