1 // Copyright 2014 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.
5 #include "components/nacl/renderer/manifest_downloader.h"
7 #include "base/callback.h"
8 #include "components/nacl/renderer/histogram.h"
9 #include "components/nacl/renderer/nexe_load_manager.h"
10 #include "net/base/net_errors.h"
11 #include "third_party/WebKit/public/platform/WebURLError.h"
12 #include "third_party/WebKit/public/platform/WebURLLoader.h"
13 #include "third_party/WebKit/public/platform/WebURLResponse.h"
17 ManifestDownloader::ManifestDownloader(
18 scoped_ptr
<blink::WebURLLoader
> url_loader
,
21 : url_loader_(url_loader
.Pass()),
22 is_installed_(is_installed
),
25 pp_nacl_error_(PP_NACL_ERROR_LOAD_SUCCESS
) {
29 ManifestDownloader::~ManifestDownloader() { }
31 void ManifestDownloader::Load(const blink::WebURLRequest
& request
) {
32 url_loader_
->loadAsynchronously(request
, this);
35 void ManifestDownloader::didReceiveResponse(
36 blink::WebURLLoader
* loader
,
37 const blink::WebURLResponse
& response
) {
38 if (response
.httpStatusCode() != 200)
39 pp_nacl_error_
= PP_NACL_ERROR_MANIFEST_LOAD_URL
;
40 status_code_
= response
.httpStatusCode();
43 void ManifestDownloader::didReceiveData(
44 blink::WebURLLoader
* loader
,
47 int encoded_data_length
) {
48 if (buffer_
.size() + data_length
> kNaClManifestMaxFileBytes
) {
49 pp_nacl_error_
= PP_NACL_ERROR_MANIFEST_TOO_LARGE
;
53 if (pp_nacl_error_
== PP_NACL_ERROR_LOAD_SUCCESS
)
54 buffer_
.append(data
, data_length
);
57 void ManifestDownloader::didFinishLoading(
58 blink::WebURLLoader
* loader
,
60 int64_t total_encoded_data_length
) {
61 // We log the status code here instead of in didReceiveResponse so that we
62 // always log a histogram value, even when we never receive a status code.
63 HistogramHTTPStatusCode(
64 is_installed_
? "NaCl.HttpStatusCodeClass.Manifest.InstalledApp" :
65 "NaCl.HttpStatusCodeClass.Manifest.NotInstalledApp",
68 cb_
.Run(pp_nacl_error_
, buffer_
);
72 void ManifestDownloader::didFail(
73 blink::WebURLLoader
* loader
,
74 const blink::WebURLError
& error
) {
75 // TODO(teravest): Find a place to share this code with PepperURLLoaderHost.
76 pp_nacl_error_
= PP_NACL_ERROR_MANIFEST_LOAD_URL
;
77 if (error
.domain
.equals(blink::WebString::fromUTF8(net::kErrorDomain
))) {
78 switch (error
.reason
) {
79 case net::ERR_ACCESS_DENIED
:
80 case net::ERR_NETWORK_ACCESS_DENIED
:
81 pp_nacl_error_
= PP_NACL_ERROR_MANIFEST_NOACCESS_URL
;
85 // It's a WebKit error.
86 pp_nacl_error_
= PP_NACL_ERROR_MANIFEST_NOACCESS_URL
;