summaryrefslogtreecommitdiff
path: root/chromium/extensions/browser/guest_view/web_view/web_ui/web_ui_url_fetcher.cc
blob: 9c2b738d07e8855034261af9d53d9234e6eebad0 (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
// Copyright 2015 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 "extensions/browser/guest_view/web_view/web_ui/web_ui_url_fetcher.h"

#include "content/public/browser/browser_context.h"
#include "content/public/common/url_fetcher.h"
#include "net/base/load_flags.h"
#include "net/url_request/url_fetcher.h"

WebUIURLFetcher::WebUIURLFetcher(content::BrowserContext* context,
                                 int render_process_id,
                                 int render_view_id,
                                 const GURL& url,
                                 const WebUILoadFileCallback& callback)
    : context_(context),
      render_process_id_(render_process_id),
      render_view_id_(render_view_id),
      url_(url),
      callback_(callback) {
}

WebUIURLFetcher::~WebUIURLFetcher() {
}

void WebUIURLFetcher::Start() {
  fetcher_ = net::URLFetcher::Create(url_, net::URLFetcher::GET, this);
  fetcher_->SetRequestContext(context_->GetRequestContext());
  fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);

  content::AssociateURLFetcherWithRenderFrame(
      fetcher_.get(), url_, render_process_id_, render_view_id_);
  fetcher_->Start();
}

void WebUIURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
  CHECK_EQ(fetcher_.get(), source);

  std::string data;
  bool result = false;
  if (fetcher_->GetStatus().status() == net::URLRequestStatus::SUCCESS) {
    result = fetcher_->GetResponseAsString(&data);
    DCHECK(result);
  }
  fetcher_.reset();
  // We cache the callback and reset it so that any references stored within it
  // are destroyed at the end of the method.
  auto callback_cache = callback_;
  callback_.Reset();
  callback_cache.Run(result, data);
}