forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalization_peer.h
144 lines (117 loc) · 5.32 KB
/
localization_peer.h
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
// Copyright 2013 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 EXTENSIONS_RENDERER_LOCALIZATION_PEER_H_
#define EXTENSIONS_RENDERER_LOCALIZATION_PEER_H_
#include <stdint.h>
#include <memory>
#include <string>
#include "mojo/public/cpp/system/data_pipe.h"
#include "mojo/public/cpp/system/simple_watcher.h"
#include "services/network/public/cpp/url_loader_completion_status.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "third_party/blink/public/platform/web_request_peer.h"
namespace IPC {
class Sender;
}
// The ExtensionLocalizationPeer is a proxy to a
// blink::WebRequestPeer instance. It is used to pre-process
// CSS files requested by extensions to replace localization templates with the
// appropriate localized strings.
//
// Call the factory method CreateExtensionLocalizationPeer() to obtain an
// instance of ExtensionLocalizationPeer based on the original Peer.
//
// The main flow of method calls is like this:
// 1. OnReceivedResponse() when the response header is ready.
// 2-a. OnStartLoadingResponseBody() when the body streaming starts. It starts
// to read the body from the data pipe. After finishing to read the whole
// body, this class replaces the body using the message catalogs, sends the
// response header, sends a data pipe to the original peer, and starts to
// send the body over the data pipe.
// 2-b. OnCompletedRequest() when the final status is available. The status code
// is stored as a member.
// 3. CompleteRequest() when both of 2-a and 2-b finish. Sends the stored
// status code to the original peer.
//
// Note that OnCompletedRequest() can be called at any time, even before
// OnReceivedResponse().
class ExtensionLocalizationPeer : public blink::WebRequestPeer {
public:
static scoped_refptr<blink::WebRequestPeer> CreateExtensionLocalizationPeer(
scoped_refptr<blink::WebRequestPeer> peer,
IPC::Sender* message_sender,
const std::string& mime_type,
const GURL& request_url);
ExtensionLocalizationPeer(const ExtensionLocalizationPeer&) = delete;
ExtensionLocalizationPeer& operator=(const ExtensionLocalizationPeer&) =
delete;
// blink::WebRequestPeer methods.
void OnUploadProgress(uint64_t position, uint64_t size) override;
bool OnReceivedRedirect(const net::RedirectInfo& redirect_info,
network::mojom::URLResponseHeadPtr head,
std::vector<std::string>*) override;
void OnReceivedResponse(network::mojom::URLResponseHeadPtr head) override;
void OnStartLoadingResponseBody(
mojo::ScopedDataPipeConsumerHandle body) override;
void OnTransferSizeUpdated(int transfer_size_diff) override;
void OnCompletedRequest(
const network::URLLoaderCompletionStatus& status) override;
private:
friend class ExtensionLocalizationPeerTest;
// Use CreateExtensionLocalizationPeer to create an instance.
ExtensionLocalizationPeer(scoped_refptr<blink::WebRequestPeer> peer,
IPC::Sender* message_sender,
const GURL& request_url);
void OnReadableBody(MojoResult, const mojo::HandleSignalsState&);
void StartSendingBody();
void OnWritableBody(MojoResult, const mojo::HandleSignalsState&);
// Loads message catalogs, and replaces all __MSG_some_name__ templates within
// loaded file.
void ReplaceMessages();
void CompleteRequest();
~ExtensionLocalizationPeer() override;
// Original peer that handles the request once we are done processing data_.
scoped_refptr<blink::WebRequestPeer> original_peer_;
// We just pass though the response info. This holds the copy of the original.
network::mojom::URLResponseHeadPtr response_head_;
struct DataPipeState {
DataPipeState();
~DataPipeState();
// Data pipe for reading the body which is passed on
// OnStartLoadingResponseBody() and its watcher. When reading the body
// reaches to the end, the handle will be reset.
mojo::ScopedDataPipeConsumerHandle source_handle_;
mojo::SimpleWatcher source_watcher_;
// Data pipe for pushing the body to the |original_peer_| and its
// watcher.
mojo::ScopedDataPipeProducerHandle destination_handle_;
mojo::SimpleWatcher destination_watcher_;
// Size sent to the destination.
size_t sent_in_bytes_ = 0;
// Shows the state of streaming the body to the |original_peer_|.
enum class BodyState {
// Before getting |source_handle_|.
kInitial,
// Reading the body from |source_handle_|.
kReadingBody,
// Sending the body via |destination_handle_|.
kSendingBody,
// Sent all the body to |destination_handle_|.
kDone
};
BodyState body_state_ = BodyState::kInitial;
};
DataPipeState data_pipe_state_;
// Set when OnCompletedRequest() is called, and sent to the original peer on
// CompleteRequest().
absl::optional<network::URLLoaderCompletionStatus> completion_status_;
// Sends ExtensionHostMsg_GetMessageBundle message to the browser to fetch
// message catalog.
IPC::Sender* message_sender_;
// Buffer for incoming data. We wait until OnCompletedRequest before using it.
std::string data_;
// Original request URL.
GURL request_url_;
};
#endif // EXTENSIONS_RENDERER_LOCALIZATION_PEER_H_