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.
4
5#include "components/nacl/renderer/manifest_downloader.h"
6
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"
14
15namespace nacl {
16
17ManifestDownloader::ManifestDownloader(
18    scoped_ptr<blink::WebURLLoader> url_loader,
19    bool is_installed,
20    Callback cb)
21    : url_loader_(url_loader.Pass()),
22      is_installed_(is_installed),
23      cb_(cb),
24      status_code_(-1),
25      pp_nacl_error_(PP_NACL_ERROR_LOAD_SUCCESS) {
26  CHECK(!cb.is_null());
27}
28
29ManifestDownloader::~ManifestDownloader() { }
30
31void ManifestDownloader::Load(const blink::WebURLRequest& request) {
32  url_loader_->loadAsynchronously(request, this);
33}
34
35void 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();
41}
42
43void ManifestDownloader::didReceiveData(
44    blink::WebURLLoader* loader,
45    const char* data,
46    int data_length,
47    int encoded_data_length) {
48  if (buffer_.size() + data_length > kNaClManifestMaxFileBytes) {
49    pp_nacl_error_ = PP_NACL_ERROR_MANIFEST_TOO_LARGE;
50    buffer_.clear();
51  }
52
53  if (pp_nacl_error_ == PP_NACL_ERROR_LOAD_SUCCESS)
54    buffer_.append(data, data_length);
55}
56
57void ManifestDownloader::didFinishLoading(
58    blink::WebURLLoader* loader,
59    double finish_time,
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",
66      status_code_);
67
68  cb_.Run(pp_nacl_error_, buffer_);
69  delete this;
70}
71
72void 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;
82        break;
83    }
84  } else {
85    // It's a WebKit error.
86    pp_nacl_error_ = PP_NACL_ERROR_MANIFEST_NOACCESS_URL;
87  }
88}
89
90}  // namespace nacl
91