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"
18 // This is a pretty arbitrary limit on the byte size of the NaCl manifest file.
19 // Note that the resulting string object has to have at least one byte extra
20 // for the null termination character.
21 const size_t kNaClManifestMaxFileBytes = 1024 * 1024;
24 ManifestDownloader::ManifestDownloader(
26 ManifestDownloaderCallback cb)
27 : is_installed_(is_installed),
30 pp_nacl_error_(PP_NACL_ERROR_LOAD_SUCCESS) {
34 ManifestDownloader::~ManifestDownloader() {
37 void ManifestDownloader::didReceiveResponse(
38 blink::WebURLLoader* loader,
39 const blink::WebURLResponse& response) {
40 if (response.httpStatusCode() != 200)
41 pp_nacl_error_ = PP_NACL_ERROR_MANIFEST_LOAD_URL;
42 status_code_ = response.httpStatusCode();
45 void ManifestDownloader::didReceiveData(
46 blink::WebURLLoader* loader,
49 int encoded_data_length) {
50 if (buffer_.size() + data_length > kNaClManifestMaxFileBytes) {
51 pp_nacl_error_ = PP_NACL_ERROR_MANIFEST_TOO_LARGE;
55 if (pp_nacl_error_ == PP_NACL_ERROR_LOAD_SUCCESS)
56 buffer_.append(data, data_length);
59 void ManifestDownloader::didFinishLoading(
60 blink::WebURLLoader* loader,
62 int64_t total_encoded_data_length) {
63 // We log the status code here instead of in didReceiveResponse so that we
64 // always log a histogram value, even when we never receive a status code.
65 HistogramHTTPStatusCode(
66 is_installed_ ? "NaCl.HttpStatusCodeClass.Manifest.InstalledApp" :
67 "NaCl.HttpStatusCodeClass.Manifest.NotInstalledApp",
70 cb_.Run(pp_nacl_error_, buffer_);
74 void ManifestDownloader::didFail(
75 blink::WebURLLoader* loader,
76 const blink::WebURLError& error) {
77 // TODO(teravest): Find a place to share this code with PepperURLLoaderHost.
78 pp_nacl_error_ = PP_NACL_ERROR_MANIFEST_LOAD_URL;
79 if (error.domain.equals(blink::WebString::fromUTF8(net::kErrorDomain))) {
80 switch (error.reason) {
81 case net::ERR_ACCESS_DENIED:
82 case net::ERR_NETWORK_ACCESS_DENIED:
83 pp_nacl_error_ = PP_NACL_ERROR_MANIFEST_NOACCESS_URL;
87 // It's a WebKit error.
88 pp_nacl_error_ = PP_NACL_ERROR_MANIFEST_NOACCESS_URL;