summaryrefslogtreecommitdiffstats
path: root/chrome/browser/media/desktop_media_list_base.h
blob: 1ac07a66e110c08a5bc3597da199a09e97aa9839 (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
// Copyright 2016 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.

#ifndef CHROME_BROWSER_MEDIA_DESKTOP_MEDIA_LIST_BASE_H_
#define CHROME_BROWSER_MEDIA_DESKTOP_MEDIA_LIST_BASE_H_

#include "chrome/browser/media/desktop_media_list.h"
#include "content/public/browser/desktop_media_id.h"

namespace gfx {
class Image;
}

// Thumbnail size is 100*100 pixels
static const int kDefaultThumbnailSize = 100;

// Base class for DesktopMediaList implementations. Implements logic shared
// between implementations. Specifically it's responsible for keeping current
// list of sources and calling the observer when the list changes.
class DesktopMediaListBase : public DesktopMediaList {
 public:
  explicit DesktopMediaListBase(base::TimeDelta update_period);
  ~DesktopMediaListBase() override;

  // DesktopMediaList interface.
  void SetUpdatePeriod(base::TimeDelta period) override;
  void SetThumbnailSize(const gfx::Size& thumbnail_size) override;
  void SetViewDialogWindowId(content::DesktopMediaID dialog_id) override;
  void StartUpdating(DesktopMediaListObserver* observer) override;
  int GetSourceCount() const override;
  const Source& GetSource(int index) const override;

  static uint32_t GetImageHash(const gfx::Image& image);

 protected:
  struct SourceDescription {
    SourceDescription(content::DesktopMediaID id, const base::string16& name);

    content::DesktopMediaID id;
    base::string16 name;
  };

  virtual void Refresh() = 0;

  // Update source media list to observer.
  void UpdateSourcesList(const std::vector<SourceDescription>& new_sources);

  // Update a thumbnail to observer.
  void UpdateSourceThumbnail(content::DesktopMediaID id,
                             const gfx::ImageSkia& image);

  // Post a task for next list update.
  void ScheduleNextRefresh();

  // Size of thumbnails generated by the model.
  gfx::Size thumbnail_size_ =
      gfx::Size(kDefaultThumbnailSize, kDefaultThumbnailSize);

  // ID of the hosting dialog.
  content::DesktopMediaID view_dialog_id_ =
      content::DesktopMediaID(content::DesktopMediaID::TYPE_NONE, -1);

 private:
  // Time interval between mode updates.
  base::TimeDelta update_period_;

  // Current list of sources.
  std::vector<Source> sources_;

  // The observer passed to StartUpdating().
  DesktopMediaListObserver* observer_ = nullptr;

  base::WeakPtrFactory<DesktopMediaListBase> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(DesktopMediaListBase);
};

#endif  // CHROME_BROWSER_MEDIA_DESKTOP_MEDIA_LIST_BASE_H_