Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / nacl / renderer / manifest_downloader.cc
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
15 namespace nacl {
16
17 namespace {
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;
22 }  // namespace
23
24 ManifestDownloader::ManifestDownloader(
25     bool is_installed,
26     ManifestDownloaderCallback cb)
27     : is_installed_(is_installed),
28       cb_(cb),
29       status_code_(-1),
30       pp_nacl_error_(PP_NACL_ERROR_LOAD_SUCCESS) {
31   CHECK(!cb.is_null());
32 }
33
34 ManifestDownloader::~ManifestDownloader() {
35 }
36
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();
43 }
44
45 void ManifestDownloader::didReceiveData(
46     blink::WebURLLoader* loader,
47     const char* data,
48     int data_length,
49     int encoded_data_length) {
50   if (buffer_.size() + data_length > kNaClManifestMaxFileBytes) {
51     pp_nacl_error_ = PP_NACL_ERROR_MANIFEST_TOO_LARGE;
52     buffer_.clear();
53   }
54
55   if (pp_nacl_error_ == PP_NACL_ERROR_LOAD_SUCCESS)
56     buffer_.append(data, data_length);
57 }
58
59 void ManifestDownloader::didFinishLoading(
60     blink::WebURLLoader* loader,
61     double finish_time,
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",
68       status_code_);
69
70   cb_.Run(pp_nacl_error_, buffer_);
71   delete this;
72 }
73
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;
84         break;
85     }
86   } else {
87     // It's a WebKit error.
88     pp_nacl_error_ = PP_NACL_ERROR_MANIFEST_NOACCESS_URL;
89   }
90 }
91
92 }  // namespace nacl