2015-03-12 09:18:35 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_TILECACHE_HPP
|
|
|
|
#define INCLUDED_TILECACHE_HPP
|
|
|
|
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <iosfwd>
|
|
|
|
#include <map>
|
2015-03-12 09:18:35 -05:00
|
|
|
#include <memory>
|
2016-05-01 10:53:37 -05:00
|
|
|
#include <mutex>
|
2015-05-05 06:57:51 -05:00
|
|
|
#include <string>
|
2015-03-12 09:18:35 -05:00
|
|
|
|
2015-03-12 18:34:42 -05:00
|
|
|
#include <Poco/Timestamp.h>
|
2016-05-15 16:50:32 -05:00
|
|
|
|
2016-05-21 08:41:23 -05:00
|
|
|
#include "TileDesc.hpp"
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2016-05-16 19:49:36 -05:00
|
|
|
class ClientSession;
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2016-05-21 08:41:23 -05:00
|
|
|
/// Handles the caching of tiles of one document.
|
2015-03-12 18:34:42 -05:00
|
|
|
class TileCache
|
2015-03-12 09:18:35 -05:00
|
|
|
{
|
2016-04-25 04:04:25 -05:00
|
|
|
struct TileBeingRendered;
|
|
|
|
|
2016-05-15 17:47:08 -05:00
|
|
|
std::shared_ptr<TileBeingRendered> findTileBeingRendered(const TileDesc& tile);
|
2016-04-25 04:04:25 -05:00
|
|
|
|
2015-03-12 18:34:42 -05:00
|
|
|
public:
|
2015-08-04 13:37:05 -05:00
|
|
|
/// When the docURL is a non-file:// url, the timestamp has to be provided by the caller.
|
|
|
|
/// For file:// url's, it's ignored.
|
|
|
|
/// When it is missing for non-file:// url, it is assumed the document must be read, and no cached value used.
|
2016-04-22 06:00:11 -05:00
|
|
|
TileCache(const std::string& docURL, const Poco::Timestamp& modifiedTime, const std::string& cacheDir);
|
2016-03-26 06:50:13 -05:00
|
|
|
~TileCache();
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2016-03-25 21:56:18 -05:00
|
|
|
TileCache(const TileCache&) = delete;
|
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
/// Subscribes if no subscription exists and returns the version number.
|
|
|
|
/// Otherwise returns 0 to signify a subscription exists.
|
2016-10-29 20:15:00 -05:00
|
|
|
void subscribeToTileRendering(const TileDesc& tile, const std::shared_ptr<ClientSession>& subscriber);
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2016-08-30 22:15:44 -05:00
|
|
|
/// Cancels all tile requests by the given subscriber.
|
2016-10-29 20:15:00 -05:00
|
|
|
std::string cancelTiles(const std::shared_ptr<ClientSession>& subscriber);
|
2016-08-30 22:15:44 -05:00
|
|
|
|
2016-05-15 17:47:08 -05:00
|
|
|
std::unique_ptr<std::fstream> lookupTile(const TileDesc& tile);
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
void saveTileAndNotify(const TileDesc& tile, const char* data, const size_t size);
|
2016-04-23 11:11:11 -05:00
|
|
|
|
2017-01-17 09:42:31 -06:00
|
|
|
/// Get the content of a cache file.
|
|
|
|
/// @param content Valid only when the call returns true.
|
|
|
|
/// @return true when the file actually exists
|
|
|
|
bool getTextFile(const std::string& fileName, std::string& content);
|
2015-06-24 15:05:49 -05:00
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
// Save some text into a file in the cache directory
|
|
|
|
void saveTextFile(const std::string& text, const std::string& fileName);
|
2015-06-24 15:05:49 -05:00
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
// Set the unsaved-changes state, used for sanity checks, ideally not needed
|
|
|
|
void setUnsavedChanges(bool state);
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2015-11-27 08:12:44 -06:00
|
|
|
// Saves a font / style / etc rendering
|
|
|
|
// The dir parameter should be the type of rendering, like "font", "style", etc
|
2016-10-29 20:15:00 -05:00
|
|
|
void saveRendering(const std::string& name, const std::string& dir, const char* data, size_t size);
|
2015-11-27 08:12:44 -06:00
|
|
|
|
2016-10-24 07:54:37 -05:00
|
|
|
std::unique_ptr<std::fstream> lookupCachedFile(const std::string& name, const std::string& dir);
|
2015-11-27 08:12:44 -06:00
|
|
|
|
2015-05-28 10:39:05 -05:00
|
|
|
// The tiles parameter is an invalidatetiles: message as sent by the child process
|
2015-07-21 06:53:53 -05:00
|
|
|
void invalidateTiles(const std::string& tiles);
|
2015-05-28 10:39:05 -05:00
|
|
|
|
2016-05-01 08:30:31 -05:00
|
|
|
/// Store the timestamp to modtime.txt.
|
|
|
|
void saveLastModified(const Poco::Timestamp& timestamp);
|
|
|
|
|
2016-05-05 01:37:40 -05:00
|
|
|
std::unique_lock<std::mutex> getTilesBeingRenderedLock() { return std::unique_lock<std::mutex>(_tilesBeingRenderedMutex); }
|
|
|
|
|
2016-05-15 17:47:08 -05:00
|
|
|
void forgetTileBeingRendered(const TileDesc& tile);
|
2016-05-05 01:37:40 -05:00
|
|
|
|
2016-05-01 08:30:31 -05:00
|
|
|
private:
|
2015-05-28 10:39:05 -05:00
|
|
|
void invalidateTiles(int part, int x, int y, int width, int height);
|
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
// Removes the given file from the cache
|
2016-04-22 01:38:59 -05:00
|
|
|
void removeFile(const std::string& fileName);
|
2016-02-10 11:51:24 -06:00
|
|
|
|
2016-12-23 00:42:35 -06:00
|
|
|
static std::string cacheFileName(const TileDesc& tile);
|
|
|
|
static bool parseCacheFileName(const std::string& fileName, int& part, int& width, int& height, int& tilePosX, int& tilePosY, int& tileWidth, int& tileHeight);
|
2015-06-24 15:05:49 -05:00
|
|
|
|
|
|
|
/// Extract location from fileName, and check if it intersects with [x, y, width, height].
|
2016-12-23 00:42:35 -06:00
|
|
|
static bool intersectsTile(const std::string& fileName, int part, int x, int y, int width, int height);
|
2015-06-24 15:05:49 -05:00
|
|
|
|
2015-08-04 13:37:05 -05:00
|
|
|
/// Load the timestamp from modtime.txt.
|
2015-03-12 18:34:42 -05:00
|
|
|
Poco::Timestamp getLastModified();
|
2015-08-04 13:37:05 -05:00
|
|
|
|
2016-03-26 06:50:13 -05:00
|
|
|
const std::string _docURL;
|
2015-06-24 15:05:49 -05:00
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
const std::string _cacheDir;
|
2015-08-12 07:31:02 -05:00
|
|
|
|
2016-04-04 19:59:46 -05:00
|
|
|
std::mutex _cacheMutex;
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2016-09-21 17:29:08 -05:00
|
|
|
mutable std::mutex _tilesBeingRenderedMutex;
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
std::map<std::string, std::shared_ptr<TileBeingRendered> > _tilesBeingRendered;
|
2015-03-12 09:18:35 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|