summaryrefslogtreecommitdiff
path: root/chromium/content/browser/background_fetch/background_fetch_delegate_proxy.h
blob: 3b905d3631de41e582b7f4ef6b36aea314479c8a (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DELEGATE_PROXY_H_
#define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DELEGATE_PROXY_H_

#include <stdint.h>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "content/browser/background_fetch/background_fetch_request_info.h"
#include "content/public/browser/background_fetch_delegate.h"
#include "content/public/browser/background_fetch_response.h"
#include "content/public/browser/browser_thread.h"

class SkBitmap;

namespace content {

// Proxy class for passing messages between BackgroundFetchJobControllers on the
// IO thread and BackgroundFetchDelegate on the UI thread.
class CONTENT_EXPORT BackgroundFetchDelegateProxy {
 public:
  // Subclasses must only be destroyed on the IO thread, since these methods
  // will be called on the IO thread.
  class Controller {
   public:
    // Called when the given |request| has started fetching.
    virtual void DidStartRequest(
        const scoped_refptr<BackgroundFetchRequestInfo>& request) = 0;

    // Called when the given |request| has an update, meaning that a total of
    // |bytes_downloaded| are now available for the response.
    virtual void DidUpdateRequest(
        const scoped_refptr<BackgroundFetchRequestInfo>& request,
        uint64_t bytes_downloaded) = 0;

    // Called when the given |request| has been completed.
    virtual void DidCompleteRequest(
        const scoped_refptr<BackgroundFetchRequestInfo>& request) = 0;

    // Called when the user aborts a Background Fetch registration.
    virtual void AbortFromUser() = 0;

    virtual ~Controller() {}
  };

  explicit BackgroundFetchDelegateProxy(BackgroundFetchDelegate* delegate);

  ~BackgroundFetchDelegateProxy();

  // Gets size of the icon to display with the Background Fetch UI.
  void GetIconDisplaySize(
      BackgroundFetchDelegate::GetIconDisplaySizeCallback callback);

  // Creates a new download grouping identified by |job_unique_id|. Further
  // downloads started by StartRequest will also use this |job_unique_id| so
  // that a notification can be updated with the current status. If the download
  // was already started in a previous browser session, then |current_guids|
  // should contain the GUIDs of in progress downloads, while completed
  // downloads are recorded in |completed_parts|.
  // Should only be called from the Controller (on the IO
  // thread).
  void CreateDownloadJob(const std::string& job_unique_id,
                         const std::string& title,
                         const url::Origin& origin,
                         const SkBitmap& icon,
                         base::WeakPtr<Controller> controller,
                         int completed_parts,
                         int total_parts,
                         const std::vector<std::string>& current_guids);

  // Requests that the download manager start fetching |request|.
  // Should only be called from the Controller (on the IO
  // thread).
  void StartRequest(const std::string& job_unique_id,
                    const url::Origin& origin,
                    scoped_refptr<BackgroundFetchRequestInfo> request);

  // Updates the representation of this registration in the user interface to
  // match the given |title|. Called from the Controller (on the IO thread).
  void UpdateUI(const std::string& job_unique_id, const std::string& title);

  // Aborts in progress downloads for the given registration. Called from the
  // Controller (on the IO thread) after it is aborted, either by the user or
  // website. May occur even if all requests already called OnDownloadComplete.
  void Abort(const std::string& job_unique_id);

 private:
  class Core;

  // Called when the job identified by |job_unique|id| was cancelled by the
  // delegate. Should only be called on the IO thread.
  void OnJobCancelled(const std::string& job_unique_id);

  // Called when the download identified by |guid| has succeeded/failed/aborted.
  // Should only be called on the IO thread.
  void OnDownloadComplete(const std::string& job_unique_id,
                          const std::string& guid,
                          std::unique_ptr<BackgroundFetchResult> result);

  // Called when progress has been made for the download identified by |guid|.
  // Should only be called on the IO thread.
  void OnDownloadUpdated(const std::string& job_unique_id,
                         const std::string& guid,
                         uint64_t bytes_downloaded);

  // Should only be called from the BackgroundFetchDelegate (on the IO thread).
  void DidStartRequest(const std::string& job_unique_id,
                       const std::string& guid,
                       std::unique_ptr<BackgroundFetchResponse> response);

  std::unique_ptr<Core, BrowserThread::DeleteOnUIThread> ui_core_;
  base::WeakPtr<Core> ui_core_ptr_;

  struct JobDetails {
    explicit JobDetails(base::WeakPtr<Controller> controller);
    JobDetails(JobDetails&& details);
    ~JobDetails();

    base::WeakPtr<Controller> controller;

    // Map from DownloadService GUIDs to their corresponding request.
    base::flat_map<std::string, scoped_refptr<BackgroundFetchRequestInfo>>
        current_request_map;

   private:
    DISALLOW_COPY_AND_ASSIGN(JobDetails);
  };

  // Map from unique job ids to a JobDetails containing the outstanding download
  // GUIDs and the controller that started the download.
  std::map<std::string, JobDetails> job_details_map_;

  base::WeakPtrFactory<BackgroundFetchDelegateProxy> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(BackgroundFetchDelegateProxy);
};

}  // namespace content

#endif  // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DELEGATE_PROXY_H_