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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
// Copyright 2014 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.
#include "content/renderer/manifest/manifest_manager.h"
#include "base/bind.h"
#include "base/strings/nullable_string16.h"
#include "content/common/manifest_manager_messages.h"
#include "content/public/common/associated_interface_provider.h"
#include "content/public/renderer/render_frame.h"
#include "content/renderer/fetchers/manifest_fetcher.h"
#include "content/renderer/manifest/manifest_parser.h"
#include "content/renderer/manifest/manifest_uma_util.h"
#include "third_party/WebKit/public/platform/WebURLResponse.h"
#include "third_party/WebKit/public/web/WebConsoleMessage.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
namespace content {
ManifestManager::ManifestManager(RenderFrame* render_frame)
: RenderFrameObserver(render_frame),
may_have_manifest_(false),
manifest_dirty_(true),
weak_factory_(this) {}
ManifestManager::~ManifestManager() {
if (fetcher_)
fetcher_->Cancel();
// Consumers in the browser process will not receive this message but they
// will be aware of the RenderFrame dying and should act on that. Consumers
// in the renderer process should be correctly notified.
ResolveCallbacks(ResolveStateFailure);
}
bool ManifestManager::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(ManifestManager, message)
IPC_MESSAGE_HANDLER(ManifestManagerMsg_RequestManifest, OnRequestManifest)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void ManifestManager::OnRequestManifest(int request_id) {
GetManifest(base::Bind(&ManifestManager::OnRequestManifestComplete,
base::Unretained(this), request_id));
}
void ManifestManager::OnRequestManifestComplete(int request_id,
const GURL& manifest_url,
const Manifest& manifest,
const ManifestDebugInfo&) {
// When sent via IPC, the Manifest must follow certain security rules.
Manifest ipc_manifest = manifest;
ipc_manifest.name = base::NullableString16(
ipc_manifest.name.string().substr(0, Manifest::kMaxIPCStringLength),
ipc_manifest.name.is_null());
ipc_manifest.short_name = base::NullableString16(
ipc_manifest.short_name.string().substr(0, Manifest::kMaxIPCStringLength),
ipc_manifest.short_name.is_null());
for (auto& icon : ipc_manifest.icons)
icon.type = icon.type.substr(0, Manifest::kMaxIPCStringLength);
ipc_manifest.gcm_sender_id = base::NullableString16(
ipc_manifest.gcm_sender_id.string().substr(
0, Manifest::kMaxIPCStringLength),
ipc_manifest.gcm_sender_id.is_null());
for (auto& related_application : ipc_manifest.related_applications) {
related_application.id =
base::NullableString16(related_application.id.string().substr(
0, Manifest::kMaxIPCStringLength),
related_application.id.is_null());
}
Send(new ManifestManagerHostMsg_RequestManifestResponse(
routing_id(), request_id, manifest_url, ipc_manifest));
}
void ManifestManager::GetManifest(const GetManifestCallback& callback) {
if (!may_have_manifest_) {
callback.Run(GURL(), Manifest(), ManifestDebugInfo());
return;
}
if (!manifest_dirty_) {
callback.Run(manifest_url_, manifest_, manifest_debug_info_);
return;
}
pending_callbacks_.push_back(callback);
// Just wait for the running call to be done if there are other callbacks.
if (pending_callbacks_.size() > 1)
return;
FetchManifest();
}
void ManifestManager::DidChangeManifest() {
may_have_manifest_ = true;
manifest_dirty_ = true;
manifest_url_ = GURL();
if (!render_frame()->IsMainFrame())
return;
if (weak_factory_.HasWeakPtrs())
return;
// Changing the manifest URL can trigger multiple notifications; the manifest
// URL update may involve removing the old manifest link before adding the new
// one, triggering multiple calls to DidChangeManifest(). Coalesce changes
// during a single event loop task to avoid sending spurious notifications to
// the browser.
//
// During document load, coalescing is disabled to maintain relative ordering
// of this notification and the favicon URL reporting.
if (!render_frame()->GetWebFrame()->IsLoading()) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(&ManifestManager::ReportManifestChange,
weak_factory_.GetWeakPtr()));
return;
}
ReportManifestChange();
}
void ManifestManager::DidCommitProvisionalLoad(
bool is_new_navigation,
bool is_same_document_navigation) {
if (is_same_document_navigation)
return;
may_have_manifest_ = false;
manifest_dirty_ = true;
manifest_url_ = GURL();
}
void ManifestManager::FetchManifest() {
manifest_url_ = render_frame()->GetWebFrame()->GetDocument().ManifestURL();
if (manifest_url_.is_empty()) {
ManifestUmaUtil::FetchFailed(ManifestUmaUtil::FETCH_EMPTY_URL);
ResolveCallbacks(ResolveStateFailure);
return;
}
fetcher_.reset(new ManifestFetcher(manifest_url_));
fetcher_->Start(
render_frame()->GetWebFrame(),
render_frame()->GetWebFrame()->GetDocument().ManifestUseCredentials(),
base::Bind(&ManifestManager::OnManifestFetchComplete,
base::Unretained(this),
render_frame()->GetWebFrame()->GetDocument().Url()));
}
static const std::string& GetMessagePrefix() {
CR_DEFINE_STATIC_LOCAL(std::string, message_prefix, ("Manifest: "));
return message_prefix;
}
void ManifestManager::OnManifestFetchComplete(
const GURL& document_url,
const blink::WebURLResponse& response,
const std::string& data) {
if (response.IsNull() && data.empty()) {
ManifestUmaUtil::FetchFailed(ManifestUmaUtil::FETCH_UNSPECIFIED_REASON);
ResolveCallbacks(ResolveStateFailure);
return;
}
ManifestUmaUtil::FetchSucceeded();
GURL response_url = response.Url();
base::StringPiece data_piece(data);
ManifestParser parser(data_piece, response_url, document_url);
parser.Parse();
fetcher_.reset();
manifest_debug_info_.raw_data = data;
parser.TakeErrors(&manifest_debug_info_.errors);
for (const auto& error : manifest_debug_info_.errors) {
blink::WebConsoleMessage message;
message.level = error.critical ? blink::WebConsoleMessage::kLevelError
: blink::WebConsoleMessage::kLevelWarning;
message.text =
blink::WebString::FromUTF8(GetMessagePrefix() + error.message);
message.url =
render_frame()->GetWebFrame()->GetDocument().ManifestURL().GetString();
message.line_number = error.line;
message.column_number = error.column;
render_frame()->GetWebFrame()->AddMessageToConsole(message);
}
// Having errors while parsing the manifest doesn't mean the manifest parsing
// failed. Some properties might have been ignored but some others kept.
if (parser.failed()) {
ResolveCallbacks(ResolveStateFailure);
return;
}
manifest_url_ = response.Url();
manifest_ = parser.manifest();
ResolveCallbacks(ResolveStateSuccess);
}
void ManifestManager::ResolveCallbacks(ResolveState state) {
// Do not reset |manifest_url_| on failure here. If manifest_url_ is
// non-empty, that means the link 404s, we failed to fetch it, or it was
// unparseable. However, the site still tried to specify a manifest, so
// preserve that information in the URL for the callbacks.
// |manifest_url| will be reset on navigation or if we receive a didchange
// event.
if (state == ResolveStateFailure)
manifest_ = Manifest();
manifest_dirty_ = state != ResolveStateSuccess;
std::list<GetManifestCallback> callbacks;
callbacks.swap(pending_callbacks_);
for (std::list<GetManifestCallback>::const_iterator it = callbacks.begin();
it != callbacks.end(); ++it) {
it->Run(manifest_url_, manifest_, manifest_debug_info_);
}
}
void ManifestManager::OnDestruct() {
delete this;
}
void ManifestManager::ReportManifestChange() {
auto manifest_url =
render_frame()->GetWebFrame()->GetDocument().ManifestURL();
if (manifest_url.IsNull()) {
GetManifestChangeObserver().ManifestUrlChanged(base::nullopt);
} else {
GetManifestChangeObserver().ManifestUrlChanged(GURL(manifest_url));
}
}
mojom::ManifestUrlChangeObserver& ManifestManager::GetManifestChangeObserver() {
if (!manifest_change_observer_) {
render_frame()->GetRemoteAssociatedInterfaces()->GetInterface(
&manifest_change_observer_);
}
return *manifest_change_observer_;
}
} // namespace content
|