1// Copyright 2013 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_MEDIA_NATIVE_DESKTOP_MEDIA_LIST_H_
6#define CHROME_BROWSER_MEDIA_NATIVE_DESKTOP_MEDIA_LIST_H_
7
8#include "base/basictypes.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/memory/weak_ptr.h"
11#include "base/sequenced_task_runner.h"
12#include "chrome/browser/media/desktop_media_list.h"
13#include "content/public/browser/desktop_media_id.h"
14#include "ui/gfx/image/image_skia.h"
15
16namespace webrtc {
17class ScreenCapturer;
18class WindowCapturer;
19}
20
21// Implementation of DesktopMediaList that shows native screens and
22// native windows.
23class NativeDesktopMediaList : public DesktopMediaList {
24 public:
25  // Caller may pass NULL for either of the arguments in case when only some
26  // types of sources the model should be populated with (e.g. it will only
27  // contain windows, if |screen_capturer| is NULL).
28  NativeDesktopMediaList(
29      scoped_ptr<webrtc::ScreenCapturer> screen_capturer,
30      scoped_ptr<webrtc::WindowCapturer> window_capturer);
31  virtual ~NativeDesktopMediaList();
32
33  // DesktopMediaList interface.
34  virtual void SetUpdatePeriod(base::TimeDelta period) OVERRIDE;
35  virtual void SetThumbnailSize(const gfx::Size& thumbnail_size) OVERRIDE;
36  virtual void StartUpdating(DesktopMediaListObserver* observer) OVERRIDE;
37  virtual int GetSourceCount() const OVERRIDE;
38  virtual const Source& GetSource(int index) const OVERRIDE;
39  virtual void SetViewDialogWindowId(
40      content::DesktopMediaID::Id dialog_id) OVERRIDE;
41
42 private:
43  class Worker;
44  friend class Worker;
45
46  // Struct used to represent sources list the model gets from the Worker.
47  struct SourceDescription {
48    SourceDescription(content::DesktopMediaID id, const base::string16& name);
49
50    content::DesktopMediaID id;
51    base::string16 name;
52  };
53
54  // Order comparator for sources. Used to sort list of sources.
55  static bool CompareSources(const SourceDescription& a,
56                             const SourceDescription& b);
57
58  // Post a task for the |worker_| to update list of windows and get thumbnails.
59  void Refresh();
60
61  // Called by |worker_| to refresh the model. First it posts tasks for
62  // OnSourcesList() with the fresh list of sources, then follows with
63  // OnSourceThumbnail() for each changed thumbnail and then calls
64  // OnRefreshFinished() at the end.
65  void OnSourcesList(const std::vector<SourceDescription>& sources);
66  void OnSourceThumbnail(int index, const gfx::ImageSkia& thumbnail);
67  void OnRefreshFinished();
68
69  // Capturers specified in SetCapturers() and passed to the |worker_| later.
70  scoped_ptr<webrtc::ScreenCapturer> screen_capturer_;
71  scoped_ptr<webrtc::WindowCapturer> window_capturer_;
72
73  // Time interval between mode updates.
74  base::TimeDelta update_period_;
75
76  // Size of thumbnails generated by the model.
77  gfx::Size thumbnail_size_;
78
79  // ID of the hosting dialog.
80  content::DesktopMediaID::Id view_dialog_id_;
81
82  // The observer passed to StartUpdating().
83  DesktopMediaListObserver* observer_;
84
85  // Task runner used for the |worker_|.
86  scoped_refptr<base::SequencedTaskRunner> capture_task_runner_;
87
88  // An object that does all the work of getting list of sources on a background
89  // thread (see |capture_task_runner_|). Destroyed on |capture_task_runner_|
90  // after the model is destroyed.
91  scoped_ptr<Worker> worker_;
92
93  // Current list of sources.
94  std::vector<Source> sources_;
95
96  base::WeakPtrFactory<NativeDesktopMediaList> weak_factory_;
97
98  DISALLOW_COPY_AND_ASSIGN(NativeDesktopMediaList);
99};
100
101#endif  // CHROME_BROWSER_MEDIA_NATIVE_DESKTOP_MEDIA_LIST_H_
102