1// Copyright (c) 2011 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#ifndef CHROME_BROWSER_UI_WEBUI_SCREENSHOT_SOURCE_H_
6#define CHROME_BROWSER_UI_WEBUI_SCREENSHOT_SOURCE_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/compiler_specific.h"
14#include "base/memory/linked_ptr.h"
15#include "content/public/browser/url_data_source.h"
16
17#if defined(OS_CHROMEOS)
18#include "chrome/browser/chromeos/drive/file_system_interface.h"
19#include "chrome/browser/chromeos/login/user_manager.h"
20#include "chrome/browser/google_apis/gdata_errorcode.h"
21#endif
22
23typedef std::vector<unsigned char> ScreenshotData;
24typedef linked_ptr<ScreenshotData> ScreenshotDataPtr;
25
26class Profile;
27
28namespace base {
29class FilePath;
30}
31
32// ScreenshotSource is the data source that serves screenshots (saved
33// or current) to the bug report html ui.
34class ScreenshotSource : public content::URLDataSource {
35 public:
36  explicit ScreenshotSource(
37      std::vector<unsigned char>* current_screenshot,
38      Profile* profile);
39
40#if defined(USE_ASH)
41  // Queries the browser process to determine if screenshots are disabled.
42  static bool AreScreenshotsDisabled();
43
44  // Common access for the screenshot directory, parameter is set to the
45  // requested directory and return value of true is given upon success.
46  static bool GetScreenshotDirectory(base::FilePath* directory);
47#endif
48
49  // Get the basefilename for screenshots
50  static std::string GetScreenshotBaseFilename();
51
52  // content::URLDataSource implementation.
53  virtual std::string GetSource() const OVERRIDE;
54  virtual void StartDataRequest(
55      const std::string& path,
56      int render_process_id,
57      int render_view_id,
58      const content::URLDataSource::GotDataCallback& callback) OVERRIDE;
59  virtual std::string GetMimeType(const std::string&) const OVERRIDE;
60
61  // Get the screenshot specified by the given relative path that we've cached
62  // from a previous request to the screenshots source.
63  // Note: This method strips the query string from the given path.
64  ScreenshotDataPtr GetCachedScreenshot(const std::string& screenshot_path);
65
66  // Url that represents the base directory for screenshots.
67  static const char kScreenshotUrlRoot[];
68  // Identifier for the current screenshot
69  // (relative to screenshot base directory).
70  static const char kScreenshotCurrent[];
71  // Path for directory where screenshots are saved
72  // (relative to screenshot base directory).
73  static const char kScreenshotSaved[];
74#if defined(OS_CHROMEOS)
75  // Common prefix to screenshot filenames.
76  static const char kScreenshotPrefix[];
77  // Common suffix to screenshot filenames.
78  static const char kScreenshotSuffix[];
79#endif
80
81 private:
82  virtual ~ScreenshotSource();
83
84  // Send the screenshot specified by the given relative path to the requestor.
85  // This is the ancestor for SendSavedScreenshot and CacheAndSendScreenshot.
86  // All calls to send a screenshot should only call this method.
87  // Note: This method strips the query string from the given path.
88  void SendScreenshot(const std::string& screenshot_path,
89                      const content::URLDataSource::GotDataCallback& callback);
90#if defined(OS_CHROMEOS)
91  // Send a saved screenshot image file specified by the given screenshot path
92  // to the requestor.
93  void SendSavedScreenshot(
94      const std::string& screenshot_path,
95      const content::URLDataSource::GotDataCallback& callback,
96      const base::FilePath& file);
97
98  // The callback for Drive's getting file method.
99  void GetSavedScreenshotCallback(
100      const std::string& screenshot_path,
101      const content::URLDataSource::GotDataCallback& callback,
102      drive::FileError error,
103      const base::FilePath& file,
104      scoped_ptr<drive::ResourceEntry> entry);
105
106#endif
107  // Sends the screenshot data to the requestor while caching it locally to the
108  // class instance, indexed by path.
109  void CacheAndSendScreenshot(
110      const std::string& screenshot_path,
111      const content::URLDataSource::GotDataCallback& callback,
112      ScreenshotDataPtr bytes);
113
114  // Pointer to the screenshot data for the current screenshot.
115  ScreenshotDataPtr current_screenshot_;
116
117  Profile* profile_;
118
119  // Key: Relative path to the screenshot (including filename)
120  // Value: Pointer to the screenshot data associated with the path.
121  std::map<std::string, ScreenshotDataPtr> cached_screenshots_;
122
123  DISALLOW_COPY_AND_ASSIGN(ScreenshotSource);
124};
125
126#endif  // CHROME_BROWSER_UI_WEBUI_SCREENSHOT_SOURCE_H_
127