~azzar1/unity/fix-crash-acc-controller

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
 * Copyright 2011 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3, as
 * published by the  Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License version 3 along with this program.  If not, see
 * <http://www.gnu.org/licenses/>
 **
 * Authored by: Nick Dedekind <nick.dedekind@canonical.com>
 *
 */

#include <pthread.h>
#include <NuxCore/Logger.h>
#include "UnityCore/GLibSource.h"
#include "UnityCore/DesktopUtilities.h"
#include "ThumbnailGenerator.h"
#include <glib/gstdio.h>
#include <unordered_map>

#include "TextureThumbnailProvider.h"
#include "DefaultThumbnailProvider.h"
#include "UserThumbnailProvider.h"
#include "config.h"

namespace unity
{
DECLARE_LOGGER(logger, "unity.dash.thumbnail");

namespace
{
  ThumbnailGenerator* thumbnail_instance = nullptr;

  const unsigned int CLEANUP_DURATION = 60*1000;         // 1 minute
  const unsigned int CLEANUP_PREVIEW_AGE = 6*60*60*1000; // 6 hours

  static std::multimap<std::string, std::string> thumbnail_content_map;
  static std::map<std::string, Thumbnailer::Ptr> thumbnailers_;

  pthread_mutex_t thumbnailers_mutex_ = PTHREAD_MUTEX_INITIALIZER;

  static std::string get_preview_dir()
  {
    return DesktopUtilities::GetUserDataDirectory().append("/previews");
  }
}


class Thumbnail
{
public:
  typedef std::shared_ptr<Thumbnail> Ptr;

  Thumbnail(std::string const& uri, unsigned int size, ThumbnailNotifier::Ptr const& notifier);

  std::string Generate(std::string& error_hint);

  std::string const uri_;
  unsigned int size_;
  ThumbnailNotifier::Ptr notifier_;
};

void ThumbnailNotifier::Cancel()
{
  cancel_.Cancel();
}

bool ThumbnailNotifier::IsCancelled() const
{
  return cancel_.IsCancelled();
}

class ThumbnailGeneratorImpl
{
public:
  ThumbnailGeneratorImpl(ThumbnailGenerator* parent)
  : parent_(parent)
  , thumbnails_mutex_(PTHREAD_MUTEX_INITIALIZER)
  , thumbnail_thread_is_running_(false)
  , thumbnail_thread_(0)
  {}

  ~ThumbnailGeneratorImpl()
  {
    pthread_join(thumbnail_thread_, NULL);
  }

  ThumbnailNotifier::Ptr GetThumbnail(std::string const& uri, int size);
  void DoCleanup();

  bool OnThumbnailComplete();

  static std::list<Thumbnailer::Ptr> GetThumbnailers(std::string const& content_type, std::string& error_hint);

  void RunGenerate();
  void RunManagement();

private:
  void StartCleanupTimer();

private:
  ThumbnailGenerator* parent_;

  glib::Source::UniquePtr thread_create_timer_;
  glib::Source::UniquePtr thread_return_timer_;

  /* Our mutex used when accessing data shared between the main thread and the
   thumbnail thread, i.e. the thumbnail_thread_is_running flag and the
   thumbnails_to_make list. */
  pthread_mutex_t thumbnails_mutex_;

  /* A flag to indicate whether a thumbnail thread is running, so we don't
   start more than one. Lock thumbnails_mutex when accessing this. */
  volatile bool thumbnail_thread_is_running_;
  pthread_t thumbnail_thread_;

  volatile bool management_thread_is_running_;
  pthread_t management_thread_;

  glib::Source::UniquePtr cleanup_timer_;

  std::queue<Thumbnail::Ptr> thumbnails_;

  struct CompleteThumbnail
  {
    std::string thubnail_uri;
    std::string error_hint;
    ThumbnailNotifier::Ptr notifier;
  };
  std::list<CompleteThumbnail> complete_thumbnails_;
};

static void* thumbnail_thread_start (void* data)
{
  ((ThumbnailGeneratorImpl*)data)->RunGenerate();
  return NULL;
}

bool CheckCache(std::string const& uri_in, std::string& filename_out)
{
  // Check Cache.
  filename_out = get_preview_dir() + "/";
  filename_out += std::to_string(std::hash<std::string>()(uri_in)) + ".png";

  glib::Object<GFile> cache_file(g_file_new_for_path(filename_out.c_str()));
  return g_file_query_exists(cache_file, NULL);
}

ThumbnailNotifier::Ptr ThumbnailGeneratorImpl::GetThumbnail(std::string const& uri, int size)
{
  std::string cache_filename;
  if (CheckCache(uri, cache_filename))
  {
    pthread_mutex_lock (&thumbnails_mutex_);

    CompleteThumbnail complete_thumb;
    complete_thumb.thubnail_uri = cache_filename;
    complete_thumb.notifier = std::make_shared<ThumbnailNotifier>();
    complete_thumbnails_.push_back(complete_thumb);

    // Delay the thumbnail update until after this method has returned with the notifier
    if (!thread_return_timer_)
    {
      thread_return_timer_.reset(new glib::Timeout(0, sigc::mem_fun(this, &ThumbnailGeneratorImpl::OnThumbnailComplete), glib::Source::Priority::LOW));
    }

    pthread_mutex_unlock (&thumbnails_mutex_);

    StartCleanupTimer();

    return complete_thumb.notifier;
  }

  pthread_mutex_lock (&thumbnails_mutex_);
  /*********************************
   * MUTEX LOCKED
   *********************************/

  if (!thread_create_timer_ && thumbnail_thread_is_running_ == false)
  {
    thread_create_timer_.reset(new glib::Timeout(0, [this]()
    {
      thumbnail_thread_is_running_ = true;
      pthread_create (&thumbnail_thread_, NULL, thumbnail_thread_start, this);
      thread_create_timer_.reset();
      return false;
    }, glib::Source::Priority::LOW));
  }

  auto notifier = std::make_shared<ThumbnailNotifier>();
  auto thumb = std::make_shared<Thumbnail>(uri, size, notifier);
  thumbnails_.push(thumb);

  pthread_mutex_unlock (&thumbnails_mutex_);
  /*********************************
   * MUTEX UNLOCKED
   *********************************/

  StartCleanupTimer();


  return notifier;
}

void ThumbnailGeneratorImpl::StartCleanupTimer()
{
  if (!cleanup_timer_)
      cleanup_timer_.reset(new glib::Timeout(CLEANUP_DURATION, [this]() { DoCleanup(); return false; }));
}


void ThumbnailGeneratorImpl::RunGenerate()
{
  for (;;)
  {
    /*********************************
     * MUTEX LOCKED
     *********************************/
    pthread_mutex_lock (&thumbnails_mutex_);

    if (thumbnails_.empty())
    {
      thumbnail_thread_is_running_ = FALSE;
      pthread_mutex_unlock (&thumbnails_mutex_);
      pthread_exit (NULL);
    }

    Thumbnail::Ptr thumb = thumbnails_.front();
    thumbnails_.pop();

    pthread_mutex_unlock (&thumbnails_mutex_);
    /*********************************
     * MUTEX UNLOCKED
     *********************************/

    if (thumb->notifier_->IsCancelled())
      continue;

    std::string error_hint;
    std::string uri_result = thumb->Generate(error_hint);

    /*********************************
     * MUTEX LOCKED
     *********************************/
    pthread_mutex_lock (&thumbnails_mutex_);

    CompleteThumbnail complete_thumb;
    complete_thumb.thubnail_uri = uri_result;
    complete_thumb.error_hint = error_hint;
    complete_thumb.notifier = thumb->notifier_;

    complete_thumbnails_.push_back(complete_thumb);

    if (!thread_return_timer_)
    {
      thread_return_timer_.reset(new glib::Timeout(0, sigc::mem_fun(this, &ThumbnailGeneratorImpl::OnThumbnailComplete), glib::Source::Priority::LOW));
    }

    pthread_mutex_unlock (&thumbnails_mutex_);
    /*********************************
     * MUTEX UNLOCKED
     *********************************/
   }
}

bool ThumbnailGeneratorImpl::OnThumbnailComplete()
{
  for (;;)
  {
    pthread_mutex_lock (&thumbnails_mutex_);

    if (complete_thumbnails_.empty())
    {
      thread_return_timer_.reset();
      pthread_mutex_unlock (&thumbnails_mutex_);
      return false;
    }
    CompleteThumbnail complete_thumbnail = complete_thumbnails_.front();
    complete_thumbnails_.pop_front();

    pthread_mutex_unlock (&thumbnails_mutex_);

    if (complete_thumbnail.notifier->IsCancelled())
      continue;

    if (complete_thumbnail.error_hint.empty())
      complete_thumbnail.notifier->ready.emit(complete_thumbnail.thubnail_uri);
    else
      complete_thumbnail.notifier->error.emit(complete_thumbnail.error_hint);
  }
  return false;
}

std::list<Thumbnailer::Ptr> ThumbnailGeneratorImpl::GetThumbnailers(std::string const& content_type, std::string& error_hint)
{
  std::list<Thumbnailer::Ptr> thumbnailer_list;

  gchar** content_split = g_strsplit(content_type.c_str(), "/", -1);

  std::vector<std::string> content_list;
  int i = 0;
  while(content_split[i] != NULL)
  {
    if (g_strcmp0(content_split[i], "") != 0)
      content_list.push_back(content_split[i]);
    i++;
  }

  unsigned int content_last = content_list.size();
  for (unsigned int i = 0; i < content_list.size()+1; ++i)
  {
    std::stringstream ss_content_type;
    for (unsigned int j = 0; j < content_last; ++j)
    {
      ss_content_type << content_list[j];
      if (j < content_last-1)
        ss_content_type << "/";
    }

    if (content_last == 0)
      ss_content_type << "*";
    else if (content_last < content_list.size())
      ss_content_type << "/*";

    content_last--;

    /*********************************
     * FIND THUMBNAILER
     *********************************/
    pthread_mutex_lock (&thumbnailers_mutex_);

    // have already got this content type?
    auto range = thumbnail_content_map.equal_range(ss_content_type.str());

    for (; range.first != range.second; range.first++)
    {
      // find the thumbnailer.
      auto iter_tumbnailers = thumbnailers_.find(range.first->second);
      if (iter_tumbnailers != thumbnailers_.end())
      {
        thumbnailer_list.push_back(iter_tumbnailers->second);
      }
    }
    pthread_mutex_unlock (&thumbnailers_mutex_);
  }

  return thumbnailer_list;
}

static void* management_thread_start (void* data)
{
  ((ThumbnailGeneratorImpl*)data)->RunManagement();
  return NULL;
}

void ThumbnailGeneratorImpl::DoCleanup()
{
  cleanup_timer_.reset();

  if (!management_thread_is_running_)
  {
    management_thread_is_running_ = true;
    pthread_create (&management_thread_, NULL, management_thread_start, this);
  }
}

void ThumbnailGeneratorImpl::RunManagement()
{
  guint64 time = std::time(NULL) - CLEANUP_PREVIEW_AGE;
  std::string thumbnail_folder_name = get_preview_dir();

  glib::Error err;
  GDir* thumbnailer_dir = g_dir_open(thumbnail_folder_name.c_str(), 0, &err);
  if (err)
  {
    LOG_ERROR(logger) << "Impossible to open directory: " << err;
    return;
  }

  const gchar* file_basename = NULL;;
  while ((file_basename = g_dir_read_name(thumbnailer_dir)) != NULL)
  {
    std::string filename = g_build_filename (thumbnail_folder_name.c_str(), file_basename, NULL);

    glib::Object<GFile> file(g_file_new_for_path(filename.c_str()));

    glib::Error err;
    glib::Object<GFileInfo> file_info(g_file_query_info(file, G_FILE_ATTRIBUTE_TIME_CREATED, G_FILE_QUERY_INFO_NONE, NULL, &err));
    if (err)
    {
      LOG_ERROR(logger) << "Impossible to get file info: " << err;
      return;
    }

    guint64 mtime = g_file_info_get_attribute_uint64(file_info, G_FILE_ATTRIBUTE_TIME_CREATED);

    if (mtime < time)
    {
      g_unlink(filename.c_str());
    }
  }

  thumbnail_thread_is_running_ = false;
}

ThumbnailGenerator::ThumbnailGenerator()
: pimpl(new ThumbnailGeneratorImpl(this))
{
  if (thumbnail_instance)
  {
    LOG_ERROR(logger) << "More than one thumbnail generator created.";
  }
  else
  {
    thumbnail_instance = this;

    UserThumbnailProvider::Initialise();
    TextureThumbnailProvider::Initialise();
    DefaultThumbnailProvider::Initialise();
  }

}

ThumbnailGenerator::~ThumbnailGenerator()
{
  if (this == thumbnail_instance)
    thumbnail_instance = NULL;
}

ThumbnailGenerator& ThumbnailGenerator::Instance()
{
  if (!thumbnail_instance)
  {
    LOG_ERROR(logger) << "No thumbnail generator created yet.";
  }

  return *thumbnail_instance;
}

ThumbnailNotifier::Ptr ThumbnailGenerator::GetThumbnail(std::string const& uri, int size)
{
  if (uri.empty())
    return nullptr;

  return pimpl->GetThumbnail(uri, size);
}

void ThumbnailGenerator::RegisterThumbnailer(std::list<std::string> mime_types, Thumbnailer::Ptr const& thumbnailer)
{
  pthread_mutex_lock (&thumbnailers_mutex_);

  thumbnailers_[thumbnailer->GetName()] = thumbnailer;

  for (std::string mime_type : mime_types)
  {
    thumbnail_content_map.insert(std::pair<std::string,std::string>(mime_type, thumbnailer->GetName()));
  }

  pthread_mutex_unlock (&thumbnailers_mutex_);
}

void ThumbnailGenerator::DoCleanup()
{
  pimpl->DoCleanup();
}

Thumbnail::Thumbnail(std::string const& uri, unsigned int size, ThumbnailNotifier::Ptr const& notifier)
: uri_(uri)
, size_(size)
, notifier_(notifier)
{}

std::string Thumbnail::Generate(std::string& error_hint)
{
  glib::Object<GFile> file(::g_file_new_for_uri(uri_.c_str()));

  glib::Error err;
  glib::Object<GFileInfo> file_info(g_file_query_info(file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, G_FILE_QUERY_INFO_NONE, NULL, &err));
  if (err)
  {
    LOG_ERROR(logger) << "Could not retrieve file info for '" << uri_ << "': " << err;
    error_hint = err.Message();
    return "";
  }

  mkdir(get_preview_dir().c_str(), S_IRWXU);

  std::string file_type = g_file_info_get_attribute_string(file_info, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);

  std::list<Thumbnailer::Ptr> const& thumbnailers = ThumbnailGeneratorImpl::GetThumbnailers(file_type, error_hint);

  std::string output_file = get_preview_dir() + "/";
  output_file += std::to_string(std::hash<std::string>()(uri_)) + ".png";

  for (Thumbnailer::Ptr const& thumbnailer : thumbnailers)
  {
    LOG_TRACE(logger) << "Attempting to generate thumbnail using '" << thumbnailer->GetName() << "' thumbnail provider";

    if (thumbnailer->Run(size_, uri_, output_file, error_hint))
    {
      error_hint.clear();
      return output_file;
    }
  }
  if (error_hint.empty())
    error_hint = "Could not find thumbnailer";
  return "";
}



} // namespace unity