Add DesktopCapturer GetSourceList SelectSource FocusOnSelectedSource functions

I have chosen part of 2435603010 changes to compose this change.
According to the discussion we have made in previous change, this CL contains,
1. Source structure to represent a source of a DesktopCapturer.
2. GetSourceList / SelectSource / FocusOnSelectedSource functions in
DesktopCapturer.
3. ScreenCapturer and WindowCapturer forward corresponding functions to the new
DesktopCapturer APIs.

After this change, We can remove WindowCapturer & ScreenCapturer references from
Chromium, and use the new APIs.

BUG=webrtc:6513

Committed: https://crrev.com/9cb0b3b4ac916cdf52d97a63d923dfbe73f0541e
Review-Url: https://codereview.webrtc.org/2452263003
Cr-Original-Commit-Position: refs/heads/master@{#14830}
Cr-Commit-Position: refs/heads/master@{#14832}
diff --git a/webrtc/modules/desktop_capture/BUILD.gn b/webrtc/modules/desktop_capture/BUILD.gn
index 1583066..5529537 100644
--- a/webrtc/modules/desktop_capture/BUILD.gn
+++ b/webrtc/modules/desktop_capture/BUILD.gn
@@ -61,6 +61,7 @@
     "desktop_and_cursor_composer.h",
     "desktop_capture_options.cc",
     "desktop_capture_options.h",
+    "desktop_capturer.cc",
     "desktop_capturer.h",
     "desktop_frame_win.cc",
     "desktop_frame_win.h",
@@ -80,6 +81,7 @@
     "mouse_cursor_monitor_mac.mm",
     "mouse_cursor_monitor_win.cc",
     "screen_capture_frame_queue.h",
+    "screen_capturer.cc",
     "screen_capturer.h",
     "screen_capturer_helper.cc",
     "screen_capturer_helper.h",
@@ -118,6 +120,7 @@
     "win/screen_capturer_win_magnifier.h",
     "win/window_capture_utils.cc",
     "win/window_capture_utils.h",
+    "window_capturer.cc",
     "window_capturer.h",
     "window_capturer_mac.mm",
     "window_capturer_win.cc",
diff --git a/webrtc/modules/desktop_capture/desktop_capture.gypi b/webrtc/modules/desktop_capture/desktop_capture.gypi
index 48bafdf..ab1fd32 100644
--- a/webrtc/modules/desktop_capture/desktop_capture.gypi
+++ b/webrtc/modules/desktop_capture/desktop_capture.gypi
@@ -39,6 +39,7 @@
         'desktop_and_cursor_composer.h',
         'desktop_capture_options.h',
         'desktop_capture_options.cc',
+        'desktop_capturer.cc',
         'desktop_capturer.h',
         'desktop_frame_win.cc',
         'desktop_frame_win.h',
diff --git a/webrtc/modules/desktop_capture/desktop_capturer.cc b/webrtc/modules/desktop_capture/desktop_capturer.cc
new file mode 100644
index 0000000..a18976a
--- /dev/null
+++ b/webrtc/modules/desktop_capture/desktop_capturer.cc
@@ -0,0 +1,37 @@
+/*
+ *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "webrtc/modules/desktop_capture/desktop_capturer.h"
+
+#include "webrtc/modules/desktop_capture/desktop_capture_options.h"
+#include "webrtc/modules/desktop_capture/screen_capturer_differ_wrapper.h"
+
+namespace webrtc {
+
+DesktopCapturer::~DesktopCapturer() = default;
+
+void DesktopCapturer::SetSharedMemoryFactory(
+    std::unique_ptr<SharedMemoryFactory> shared_memory_factory) {}
+
+void DesktopCapturer::SetExcludedWindow(WindowId window) {}
+
+bool DesktopCapturer::GetSourceList(SourceList* sources) {
+  return true;
+}
+
+bool DesktopCapturer::SelectSource(SourceId id) {
+  return false;
+}
+
+bool DesktopCapturer::FocusOnSelectedSource() {
+  return false;
+}
+
+}  // namespace webrtc
diff --git a/webrtc/modules/desktop_capture/desktop_capturer.h b/webrtc/modules/desktop_capture/desktop_capturer.h
index d758946..6b9c820 100644
--- a/webrtc/modules/desktop_capture/desktop_capturer.h
+++ b/webrtc/modules/desktop_capture/desktop_capturer.h
@@ -12,8 +12,11 @@
 #define WEBRTC_MODULES_DESKTOP_CAPTURE_DESKTOP_CAPTURER_H_
 
 #include <stddef.h>
+#include <stdint.h>
 
 #include <memory>
+#include <string>
+#include <vector>
 
 #include "webrtc/modules/desktop_capture/desktop_frame.h"
 #include "webrtc/modules/desktop_capture/desktop_capture_types.h"
@@ -21,6 +24,7 @@
 
 namespace webrtc {
 
+class DesktopCaptureOptions;
 class DesktopFrame;
 
 // Abstract interface for screen and window capturers.
@@ -53,7 +57,20 @@
     virtual ~Callback() {}
   };
 
-  virtual ~DesktopCapturer() {}
+  typedef intptr_t SourceId;
+
+  struct Source {
+    // The unique id to represent a Source of current DesktopCapturer.
+    SourceId id;
+
+    // Title of the window or screen in UTF-8 encoding, maybe empty. This field
+    // should not be used to identify a source.
+    std::string title;
+  };
+
+  typedef std::vector<Source> SourceList;
+
+  virtual ~DesktopCapturer();
 
   // Called at the beginning of a capturing session. |callback| must remain
   // valid until capturer is destroyed.
@@ -65,7 +82,7 @@
   // Shared memory is currently supported only by some DesktopCapturer
   // implementations.
   virtual void SetSharedMemoryFactory(
-      std::unique_ptr<SharedMemoryFactory> shared_memory_factory) {}
+      std::unique_ptr<SharedMemoryFactory> shared_memory_factory);
 
   // Captures next frame, and involve callback provided by Start() function.
   // Pending capture requests are canceled when DesktopCapturer is deleted.
@@ -74,7 +91,25 @@
   // Sets the window to be excluded from the captured image in the future
   // Capture calls. Used to exclude the screenshare notification window for
   // screen capturing.
-  virtual void SetExcludedWindow(WindowId window) {}
+  virtual void SetExcludedWindow(WindowId window);
+
+  // TODO(zijiehe): Following functions should be pure virtual. The default
+  // implementations are for backward compatibility only. Remove default
+  // implementations once all DesktopCapturer implementations in Chromium have
+  // implemented these functions.
+
+  // Gets a list of sources current capturer supports. Returns false in case of
+  // a failure.
+  virtual bool GetSourceList(SourceList* sources);
+
+  // Selects a source to be captured. Returns false in case of a failure (e.g.
+  // if there is no source with the specified type and id.)
+  virtual bool SelectSource(SourceId id);
+
+  // Brings the selected source to the front and sets the input focus on it.
+  // Returns false in case of a failure or no source has been selected or the
+  // implementation does not support this functionality.
+  virtual bool FocusOnSelectedSource();
 };
 
 }  // namespace webrtc
diff --git a/webrtc/modules/desktop_capture/screen_capturer.cc b/webrtc/modules/desktop_capture/screen_capturer.cc
new file mode 100644
index 0000000..a35aa1a
--- /dev/null
+++ b/webrtc/modules/desktop_capture/screen_capturer.cc
@@ -0,0 +1,53 @@
+/*
+ *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "webrtc/modules/desktop_capture/screen_capturer.h"
+
+#include <memory>
+#include <utility>
+
+namespace webrtc {
+
+ScreenCapturer::~ScreenCapturer() = default;
+
+bool ScreenCapturer::GetScreenList(ScreenList* screens) {
+  SourceList sources;
+  if (!GetSourceList(&sources)) {
+    return false;
+  }
+
+  for (const Source& source : sources) {
+    screens->push_back({source.id});
+  }
+  return true;
+}
+
+bool ScreenCapturer::SelectScreen(ScreenId id) {
+  return SelectSource(id);
+}
+
+bool ScreenCapturer::GetSourceList(SourceList* sources) {
+  ScreenList screens;
+  if (!GetScreenList(&screens)) {
+    return false;
+  }
+
+  for (const Screen& screen : screens) {
+    sources->push_back({screen.id});
+  }
+
+  return true;
+}
+
+bool ScreenCapturer::SelectSource(SourceId id) {
+  return SelectScreen(id);
+}
+
+}  // namespace webrtc
diff --git a/webrtc/modules/desktop_capture/screen_capturer.h b/webrtc/modules/desktop_capture/screen_capturer.h
index 5f07ba1..a6bbde7 100644
--- a/webrtc/modules/desktop_capture/screen_capturer.h
+++ b/webrtc/modules/desktop_capture/screen_capturer.h
@@ -21,6 +21,7 @@
 
 class DesktopCaptureOptions;
 
+// TODO(zijiehe): Remove this class.
 // Class used to capture video frames asynchronously.
 //
 // The full capture sequence is as follows:
@@ -48,19 +49,25 @@
   };
   typedef std::vector<Screen> ScreenList;
 
-  ~ScreenCapturer() override {}
+  ~ScreenCapturer() override;
 
   // Creates a platform-specific capturer.
   static ScreenCapturer* Create(const DesktopCaptureOptions& options);
 
+  // Deprecated, use GetSourceList().
   // Get the list of screens (not containing kFullDesktopScreenId). Returns
   // false in case of a failure.
-  virtual bool GetScreenList(ScreenList* screens) = 0;
+  virtual bool GetScreenList(ScreenList* screens);
 
+  // Deprecated, use SelectSource().
   // Select the screen to be captured. Returns false in case of a failure (e.g.
   // if there is no screen with the specified id). If this is never called, the
   // full desktop is captured.
-  virtual bool SelectScreen(ScreenId id) = 0;
+  virtual bool SelectScreen(ScreenId id);
+
+  // DesktopCapturer interfaces.
+  bool GetSourceList(SourceList* sources) override;
+  bool SelectSource(SourceId id) override;
 };
 
 }  // namespace webrtc
diff --git a/webrtc/modules/desktop_capture/window_capturer.cc b/webrtc/modules/desktop_capture/window_capturer.cc
new file mode 100644
index 0000000..62932cc
--- /dev/null
+++ b/webrtc/modules/desktop_capture/window_capturer.cc
@@ -0,0 +1,61 @@
+/*
+ *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "webrtc/modules/desktop_capture/window_capturer.h"
+
+#include <string>
+#include <utility>
+
+namespace webrtc {
+
+WindowCapturer::~WindowCapturer() = default;
+
+bool WindowCapturer::GetWindowList(WindowList* windows) {
+  SourceList sources;
+  if (!GetSourceList(&sources)) {
+    return false;
+  }
+
+  for (const Source& source : sources) {
+    windows->push_back({source.id, std::move(source.title)});
+  }
+  return true;
+}
+
+bool WindowCapturer::SelectWindow(WindowId id) {
+  return SelectSource(id);
+}
+
+bool WindowCapturer::BringSelectedWindowToFront() {
+  return FocusOnSelectedSource();
+}
+
+bool WindowCapturer::GetSourceList(SourceList* sources) {
+  WindowList windows;
+  if (!GetWindowList(&windows)) {
+    return false;
+  }
+
+  for (const Window& window : windows) {
+    sources->push_back({window.id, std::move(window.title)});
+  }
+
+  return true;
+}
+
+bool WindowCapturer::SelectSource(SourceId id) {
+  return SelectWindow(id);
+}
+
+bool WindowCapturer::FocusOnSelectedSource() {
+  return BringSelectedWindowToFront();
+}
+
+}  // namespace webrtc
diff --git a/webrtc/modules/desktop_capture/window_capturer.h b/webrtc/modules/desktop_capture/window_capturer.h
index 682778c..5667702 100644
--- a/webrtc/modules/desktop_capture/window_capturer.h
+++ b/webrtc/modules/desktop_capture/window_capturer.h
@@ -23,6 +23,7 @@
 
 class DesktopCaptureOptions;
 
+// TODO(zijiehe): Remove this class.
 class WindowCapturer : public DesktopCapturer {
  public:
   typedef webrtc::WindowId WindowId;
@@ -36,20 +37,29 @@
 
   typedef std::vector<Window> WindowList;
 
+  // Consumers should use DesktopCapturer::CreateWindowCapturer.
   static WindowCapturer* Create(const DesktopCaptureOptions& options);
 
-  ~WindowCapturer() override {}
+  ~WindowCapturer() override;
 
+  // Deprecated, use GetSourceList().
   // Get list of windows. Returns false in case of a failure.
-  virtual bool GetWindowList(WindowList* windows) = 0;
+  virtual bool GetWindowList(WindowList* windows);
 
+  // Deprecated, use SelectSource().
   // Select window to be captured. Returns false in case of a failure (e.g. if
   // there is no window with the specified id).
-  virtual bool SelectWindow(WindowId id) = 0;
+  virtual bool SelectWindow(WindowId id);
 
+  // Deprecated, use FocusOnSelectedSource().
   // Bring the selected window to the front. Returns false in case of a
   // failure or no window selected.
-  virtual bool BringSelectedWindowToFront() = 0;
+  virtual bool BringSelectedWindowToFront();
+
+  // DesktopCapturer interfaces.
+  bool GetSourceList(SourceList* sources) override;
+  bool SelectSource(SourceId id) override;
+  bool FocusOnSelectedSource() override;
 };
 
 }  // namespace webrtc
diff --git a/webrtc/modules/desktop_capture/window_capturer_win.cc b/webrtc/modules/desktop_capture/window_capturer_win.cc
index 9253b86..c43db7e 100644
--- a/webrtc/modules/desktop_capture/window_capturer_win.cc
+++ b/webrtc/modules/desktop_capture/window_capturer_win.cc
@@ -63,7 +63,7 @@
   }
 
   WindowCapturer::Window window;
-  window.id = reinterpret_cast<WindowCapturer::WindowId>(hwnd);
+  window.id = reinterpret_cast<WindowId>(hwnd);
 
   const size_t kTitleLength = 500;
   WCHAR window_title[kTitleLength];