summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/metadata_url_request.cc
blob: 57eac508acb17176f51cccf7f97fb2e69428a4cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/net/metadata_url_request.h"

#include "base/file_path.h"
#include "base/message_loop.h"
#include "build/build_config.h"
#include "chrome/browser/parsers/metadata_parser_manager.h"
#include "chrome/browser/parsers/metadata_parser.h"
#include "chrome/common/url_constants.h"
#include "net/base/io_buffer.h"
#include "net/base/net_util.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_job.h"

namespace {

class MetadataRequestHandler : public URLRequestJob {
 public:
  explicit MetadataRequestHandler(URLRequest* request);

  static URLRequestJob* Factory(URLRequest* request, const std::string& scheme);

  // URLRequestJob implementation.
  virtual void Start();
  virtual void Kill();
  virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read);
  virtual bool GetMimeType(std::string* mime_type) const;

 private:
  ~MetadataRequestHandler();

  void StartAsync();
  std::string result_;
  bool parsed;
  int data_offset_;
  DISALLOW_COPY_AND_ASSIGN(MetadataRequestHandler);
};

MetadataRequestHandler::MetadataRequestHandler(URLRequest* request)
    : URLRequestJob(request),
      data_offset_(0) {
  parsed = false;
}

MetadataRequestHandler::~MetadataRequestHandler() {
}

URLRequestJob* MetadataRequestHandler::Factory(URLRequest* request,
                                               const std::string& scheme) {
  return new MetadataRequestHandler(request);
}

void MetadataRequestHandler::Start() {
  // Start reading asynchronously so that all error reporting and data
  // callbacks happen as they would for network requests.
  MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
      this, &MetadataRequestHandler::StartAsync));
}

void MetadataRequestHandler::Kill() {
}

bool MetadataRequestHandler::ReadRawData(net::IOBuffer* buf, int buf_size,
                                         int *bytes_read) {
  FilePath path;

  if (!request()->url().is_valid()) {
    return false;
  }
  if (!net::FileURLToFilePath(request()->url(), &path)) {
    return false;
  }
  if (!parsed) {
    MetadataParserManager* manager = MetadataParserManager::Get();
    scoped_ptr<MetadataParser> parser(manager->GetParserForFile(path));
    if (parser != NULL) {
      result_ = "{\n";
      parser->Parse();
      MetadataPropertyIterator *iter = parser->GetPropertyIterator();
      while (!iter->IsEnd()) {
        std::string key;
        std::string value;
        if (iter->GetNext(&key, &value)) {
          result_ += "\"";
          result_ += key;
          result_ += "\":";
          result_ += "\"";
          result_ += value;
          result_ += "\",\n";
        } else {
          break;
        }
      }
      result_ += "}";
      delete iter;
    } else {
      result_ = "{}";
    }
    parsed = true;
  }
  int remaining = static_cast<int>(result_.size()) - data_offset_;
  if (buf_size > remaining)
    buf_size = remaining;
  if (buf_size > 0) {
    memcpy(buf->data(), &result_[data_offset_], buf_size);
    data_offset_ += buf_size;
  }
  *bytes_read = buf_size;
  return true;
}

bool MetadataRequestHandler::GetMimeType(std::string* mime_type) const {
  *mime_type = "application/json";
  return true;
}

void MetadataRequestHandler::StartAsync() {
  NotifyHeadersComplete();
}

}  // namespace

void RegisterMetadataURLRequestHandler() {
#if defined(OS_CHROMEOS)
  URLRequest::RegisterProtocolFactory(chrome::kMetadataScheme,
                                      &MetadataRequestHandler::Factory);
#endif
}