summaryrefslogtreecommitdiffstats
path: root/chrome/browser/search/local_ntp_source.cc
blob: 38cbd7a2e2e5dad40a4098a53bd482dc6fd9b1e5 (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
// Copyright 2013 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/search/local_ntp_source.h"

#include "base/logging.h"
#include "base/memory/ref_counted_memory.h"
#include "base/string_util.h"
#include "chrome/common/url_constants.h"
#include "googleurl/src/gurl.h"
#include "grit/browser_resources.h"
#include "grit/ui_resources.h"
#include "net/url_request/url_request.h"
#include "ui/base/resource/resource_bundle.h"

namespace {

const char kHtmlFilename[] = "local-ntp.html";
const char kJSFilename[] = "local-ntp.js";
const char kCssFilename[] = "local-ntp.css";
const char kCloseBarFilename[] = "images/close_2.png";
const char kCloseBarHoverFilename[] = "images/close_2_hover.png";
const char kCloseBarActiveFilename[] = "images/close_2_active.png";

}  // namespace

LocalNtpSource::LocalNtpSource() {
}

LocalNtpSource::~LocalNtpSource() {
}

std::string LocalNtpSource::GetSource() {
  return chrome::kChromeSearchLocalNtpHost;
}

void LocalNtpSource::StartDataRequest(
    const std::string& path,
    bool is_incognito,
    const content::URLDataSource::GotDataCallback& callback) {
  int identifier = -1;
  if (path == kHtmlFilename) {
    identifier = IDR_LOCAL_NTP_HTML;
  } else if (path == kJSFilename) {
    identifier = IDR_LOCAL_NTP_JS;
  } else if (path == kCssFilename) {
    identifier = IDR_LOCAL_NTP_CSS;
  } else if (path == kCloseBarFilename) {
    identifier = IDR_CLOSE_2;
  } else if (path == kCloseBarHoverFilename) {
    identifier = IDR_CLOSE_2_H;
  } else if (path == kCloseBarActiveFilename) {
    identifier = IDR_CLOSE_2_P;
  } else {
    callback.Run(NULL);
    return;
  }

  scoped_refptr<base::RefCountedStaticMemory> response(
      ResourceBundle::GetSharedInstance().LoadDataResourceBytes(identifier));
  callback.Run(response);
}

std::string LocalNtpSource::GetMimeType(
    const std::string& path) const {
  if (path == kHtmlFilename)
    return "text/html";
  if (path == kJSFilename)
    return "application/javascript";
  if (path == kCssFilename)
    return "text/css";
  if (path == kCloseBarFilename || path == kCloseBarHoverFilename ||
      path == kCloseBarActiveFilename) {
    return "image/png";
  }
  return std::string();
}

bool LocalNtpSource::ShouldServiceRequest(
    const net::URLRequest* request) const {
  DCHECK(request->url().host() == chrome::kChromeSearchLocalNtpHost);

  if (request->url().SchemeIs(chrome::kChromeSearchScheme)) {
    DCHECK(StartsWithASCII(request->url().path(), "/", true));
    std::string filename = request->url().path().substr(1);
    return filename == kHtmlFilename || filename == kJSFilename ||
           filename == kCssFilename || filename == kCloseBarFilename ||
           filename == kCloseBarHoverFilename ||
           filename == kCloseBarActiveFilename;
  }
  return false;
}