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::Close() {
58 // We log the status code here instead of in didReceiveResponse so that we
59 // always log a histogram value, even when we never receive a status code.
60 HistogramHTTPStatusCode(
61 is_installed_
? "NaCl.HttpStatusCodeClass.Manifest.InstalledApp" :
62 "NaCl.HttpStatusCodeClass.Manifest.NotInstalledApp",
65 cb_
.Run(pp_nacl_error_
, buffer_
);
69 void ManifestDownloader::didFinishLoading(
70 blink::WebURLLoader
* loader
,
72 int64_t total_encoded_data_length
) {
76 void ManifestDownloader::didFail(
77 blink::WebURLLoader
* loader
,
78 const blink::WebURLError
& error
) {
79 // TODO(teravest): Find a place to share this code with PepperURLLoaderHost.
80 pp_nacl_error_
= PP_NACL_ERROR_MANIFEST_LOAD_URL
;
81 if (error
.domain
.equals(blink::WebString::fromUTF8(net::kErrorDomain
))) {
82 switch (error
.reason
) {
83 case net::ERR_ACCESS_DENIED
:
84 case net::ERR_NETWORK_ACCESS_DENIED
:
85 pp_nacl_error_
= PP_NACL_ERROR_MANIFEST_NOACCESS_URL
;
89 // It's a WebKit error.
90 pp_nacl_error_
= PP_NACL_ERROR_MANIFEST_NOACCESS_URL
;