summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/screenshot_source.cc
blob: 9a7798f136b75061d84571fc938697e0563ca400 (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
// Copyright (c) 2012 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/ui/webui/screenshot_source.h"

#include "base/bind.h"
#include "base/callback.h"
#include "base/file_util.h"
#include "base/memory/ref_counted_memory.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string16.h"
#include "base/string_util.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/url_constants.h"
#include "googleurl/src/url_canon.h"
#include "googleurl/src/url_util.h"

#if defined(OS_CHROMEOS)
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "content/public/browser/browser_thread.h"
#endif

static const char kCurrentScreenshotFilename[] = "current";
#if defined(OS_CHROMEOS)
static const char kSavedScreenshotsBasePath[] = "saved/";
#endif

ScreenshotSource::ScreenshotSource(
    std::vector<unsigned char>* current_screenshot)
    : DataSource(chrome::kChromeUIScreenshotPath, MessageLoop::current()) {
  // Setup the last screenshot taken.
  if (current_screenshot)
    current_screenshot_.reset(new ScreenshotData(*current_screenshot));
  else
    current_screenshot_.reset(new ScreenshotData());
}

ScreenshotSource::~ScreenshotSource() {}

void ScreenshotSource::StartDataRequest(const std::string& path, bool,
                                        int request_id) {
  SendScreenshot(path, request_id);
}

std::string ScreenshotSource::GetMimeType(const std::string&) const {
  // We need to explicitly return a mime type, otherwise if the user tries to
  // drag the image they get no extension.
  return "image/png";
}

ScreenshotDataPtr ScreenshotSource::GetCachedScreenshot(
    const std::string& screenshot_path) {
  std::map<std::string, ScreenshotDataPtr>::iterator pos;
  std::string path = screenshot_path.substr(
      0, screenshot_path.find_first_of("?"));
  if ((pos = cached_screenshots_.find(path)) != cached_screenshots_.end()) {
    return pos->second;
  } else {
    return ScreenshotDataPtr(new ScreenshotData);
  }
}

void ScreenshotSource::SendScreenshot(const std::string& screenshot_path,
                                      int request_id) {
  // Strip the query param value - we only use it as a hack to ensure our
  // image gets reloaded instead of being pulled from the browser cache
  std::string path = screenshot_path.substr(
      0, screenshot_path.find_first_of("?"));
  if (path == kCurrentScreenshotFilename) {
    CacheAndSendScreenshot(path, request_id, current_screenshot_);
#if defined(OS_CHROMEOS)
  } else if (path.compare(0, strlen(kSavedScreenshotsBasePath),
                          kSavedScreenshotsBasePath) == 0) {
    using content::BrowserThread;
    BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
                            base::Bind(&ScreenshotSource::SendSavedScreenshot,
                                       base::Unretained(this), path,
                                       request_id));
#endif
  } else {
    CacheAndSendScreenshot(
        path, request_id, ScreenshotDataPtr(new ScreenshotData()));
  }
}

#if defined(OS_CHROMEOS)
void ScreenshotSource::SendSavedScreenshot(const std::string& screenshot_path,
                                           int request_id) {
  ScreenshotDataPtr read_bytes(new ScreenshotData);
  std::string filename = screenshot_path.substr(
      strlen(kSavedScreenshotsBasePath));

  url_canon::RawCanonOutputT<char16> decoded;
  url_util::DecodeURLEscapeSequences(
      filename.data(), filename.size(), &decoded);
  // Screenshot filenames don't use non-ascii characters.
  std::string decoded_filename = UTF16ToASCII(string16(
      decoded.data(), decoded.length()));

  int64 file_size = 0;
  DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
      ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext());
  FilePath file = download_prefs->download_path().Append(decoded_filename);
  if (!file_util::GetFileSize(file, &file_size)) {
    CacheAndSendScreenshot(screenshot_path, request_id, read_bytes);
    return;
  }

  read_bytes->resize(file_size);
  if (!file_util::ReadFile(file, reinterpret_cast<char*>(&read_bytes->front()),
                           static_cast<int>(file_size)))
    read_bytes->clear();

  CacheAndSendScreenshot(screenshot_path, request_id, read_bytes);
}
#endif

void ScreenshotSource::CacheAndSendScreenshot(
    const std::string& screenshot_path,
    int request_id,
    ScreenshotDataPtr bytes) {
  cached_screenshots_[screenshot_path] = bytes;
  SendResponse(request_id, new base::RefCountedBytes(*bytes));
}