[ppapi] Merged three RunCallback methods doing the same thing

In jumbo build experiments, the different RunCallback functions
ended up in the same translation unit and thus the same
anonymous namespace where the symbols clashed. Since they,
and VideoDecoderResource::RunCallbackWithError had the same
implementation, this merges the functions into a shared
SafeRunCallback.

Bug: 898475
Change-Id: I3ee20972794d3c6ef73e49882e1ceba24c487e19
Reviewed-on: https://chromium-review.googlesource.com/c/1297427
Reviewed-by: Antoine Labour <piman@chromium.org>
Commit-Queue: Daniel Bratell <bratell@opera.com>
Cr-Original-Commit-Position: refs/heads/master@{#602646}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: eb5dcdedd067d2dc7c993687adcbb5d1649b76b1
diff --git a/proxy/audio_encoder_resource.cc b/proxy/audio_encoder_resource.cc
index 51dcab4..f903663 100644
--- a/proxy/audio_encoder_resource.cc
+++ b/proxy/audio_encoder_resource.cc
@@ -18,18 +18,6 @@
 namespace ppapi {
 namespace proxy {
 
-namespace {
-
-void RunCallback(scoped_refptr<TrackedCallback>* callback, int32_t error) {
-  if (TrackedCallback::IsPending(*callback)) {
-    scoped_refptr<TrackedCallback> temp;
-    callback->swap(temp);
-    temp->Run(error);
-  }
-}
-
-}  // namespace
-
 AudioEncoderResource::AudioEncoderResource(Connection connection,
                                            PP_Instance instance)
     : PluginResource(connection, instance),
@@ -205,12 +193,12 @@
   ArrayWriter writer(output);
   if (params.result() != PP_OK || !writer.is_valid() ||
       !writer.StoreVector(profiles)) {
-    RunCallback(&get_supported_profiles_callback_, PP_ERROR_FAILED);
+    SafeRunCallback(&get_supported_profiles_callback_, PP_ERROR_FAILED);
     return;
   }
 
-  RunCallback(&get_supported_profiles_callback_,
-              base::checked_cast<int32_t>(profiles.size()));
+  SafeRunCallback(&get_supported_profiles_callback_,
+                  base::checked_cast<int32_t>(profiles.size()));
 }
 
 void AudioEncoderResource::OnPluginMsgInitializeReply(
@@ -224,7 +212,7 @@
 
   int32_t error = params.result();
   if (error) {
-    RunCallback(&initialize_callback_, error);
+    SafeRunCallback(&initialize_callback_, error);
     return;
   }
 
@@ -234,7 +222,7 @@
       !audio_buffer_manager_.SetBuffers(
           audio_buffer_count, audio_buffer_size,
           std::make_unique<base::SharedMemory>(buffer_handle, false), true)) {
-    RunCallback(&initialize_callback_, PP_ERROR_NOMEMORY);
+    SafeRunCallback(&initialize_callback_, PP_ERROR_NOMEMORY);
     return;
   }
 
@@ -243,7 +231,7 @@
       !bitstream_buffer_manager_.SetBuffers(
           bitstream_buffer_count, bitstream_buffer_size,
           std::make_unique<base::SharedMemory>(buffer_handle, false), false)) {
-    RunCallback(&initialize_callback_, PP_ERROR_NOMEMORY);
+    SafeRunCallback(&initialize_callback_, PP_ERROR_NOMEMORY);
     return;
   }
 
@@ -255,7 +243,7 @@
   number_of_samples_ = number_of_samples;
   initialized_ = true;
 
-  RunCallback(&initialize_callback_, PP_OK);
+  SafeRunCallback(&initialize_callback_, PP_OK);
 }
 
 void AudioEncoderResource::OnPluginMsgEncodeReply(
@@ -273,7 +261,7 @@
 
   scoped_refptr<TrackedCallback> callback = it->second;
   encode_callbacks_.erase(it);
-  RunCallback(&callback, encoder_last_error_);
+  SafeRunCallback(&callback, encoder_last_error_);
 
   audio_buffer_manager_.EnqueueBuffer(buffer_id);
   // If the plugin is waiting for an audio buffer, we can give the one
@@ -301,15 +289,15 @@
   DCHECK(error);
 
   encoder_last_error_ = error;
-  RunCallback(&get_supported_profiles_callback_, error);
-  RunCallback(&initialize_callback_, error);
-  RunCallback(&get_buffer_callback_, error);
+  SafeRunCallback(&get_supported_profiles_callback_, error);
+  SafeRunCallback(&initialize_callback_, error);
+  SafeRunCallback(&get_buffer_callback_, error);
   get_buffer_data_ = nullptr;
-  RunCallback(&get_bitstream_buffer_callback_, error);
+  SafeRunCallback(&get_bitstream_buffer_callback_, error);
   get_bitstream_buffer_data_ = nullptr;
   for (EncodeMap::iterator it = encode_callbacks_.begin();
        it != encode_callbacks_.end(); ++it)
-    RunCallback(&it->second, error);
+    SafeRunCallback(&it->second, error);
   encode_callbacks_.clear();
 }
 
@@ -329,7 +317,7 @@
   // Take a reference for the plugin.
   *get_buffer_data_ = resource->GetReference();
   get_buffer_data_ = nullptr;
-  RunCallback(&get_buffer_callback_, PP_OK);
+  SafeRunCallback(&get_buffer_callback_, PP_OK);
 }
 
 void AudioEncoderResource::TryWriteBitstreamBuffer() {
@@ -345,7 +333,7 @@
   get_bitstream_buffer_data_->buffer = buffer->bitstream.data;
   get_bitstream_buffer_data_->size = buffer->bitstream.data_size;
   get_bitstream_buffer_data_ = nullptr;
-  RunCallback(&get_bitstream_buffer_callback_, PP_OK);
+  SafeRunCallback(&get_bitstream_buffer_callback_, PP_OK);
 }
 
 void AudioEncoderResource::ReleaseBuffers() {
diff --git a/proxy/plugin_resource.cc b/proxy/plugin_resource.cc
index 177012d..7128318 100644
--- a/proxy/plugin_resource.cc
+++ b/proxy/plugin_resource.cc
@@ -13,6 +13,14 @@
 namespace ppapi {
 namespace proxy {
 
+void SafeRunCallback(scoped_refptr<TrackedCallback>* callback, int32_t error) {
+  if (TrackedCallback::IsPending(*callback)) {
+    scoped_refptr<TrackedCallback> temp;
+    callback->swap(temp);
+    temp->Run(error);
+  }
+}
+
 PluginResource::PluginResource(Connection connection, PP_Instance instance)
     : Resource(OBJECT_IS_PROXY, instance),
       connection_(connection),
diff --git a/proxy/plugin_resource.h b/proxy/plugin_resource.h
index 8f32277..54aa2b5 100644
--- a/proxy/plugin_resource.h
+++ b/proxy/plugin_resource.h
@@ -26,6 +26,10 @@
 namespace ppapi {
 namespace proxy {
 
+// A "safe" way to run callbacks, doing nothing if they are not
+// pending (active).
+void SafeRunCallback(scoped_refptr<TrackedCallback>* callback, int32_t error);
+
 class PPAPI_PROXY_EXPORT PluginResource : public Resource {
  public:
   enum Destination {
diff --git a/proxy/video_decoder_resource.cc b/proxy/video_decoder_resource.cc
index bcd6056..94548e1 100644
--- a/proxy/video_decoder_resource.cc
+++ b/proxy/video_decoder_resource.cc
@@ -523,11 +523,7 @@
 
 void VideoDecoderResource::RunCallbackWithError(
     scoped_refptr<TrackedCallback>* callback) {
-  if (TrackedCallback::IsPending(*callback)) {
-    scoped_refptr<TrackedCallback> temp;
-    callback->swap(temp);
-    temp->Run(decoder_last_error_);
-  }
+  SafeRunCallback(callback, decoder_last_error_);
 }
 
 void VideoDecoderResource::DeleteGLTexture(uint32_t id) {
diff --git a/proxy/video_encoder_resource.cc b/proxy/video_encoder_resource.cc
index 1abb315..eef9ea9 100644
--- a/proxy/video_encoder_resource.cc
+++ b/proxy/video_encoder_resource.cc
@@ -26,15 +26,6 @@
 
 namespace {
 
-void RunCallback(scoped_refptr<TrackedCallback>* callback, int32_t error) {
-  if (!TrackedCallback::IsPending(*callback))
-    return;
-
-  scoped_refptr<TrackedCallback> temp;
-  callback->swap(temp);
-  temp->Run(error);
-}
-
 std::vector<PP_VideoProfileDescription_0_1> PP_VideoProfileDescriptionTo_0_1(
     std::vector<PP_VideoProfileDescription> profiles) {
   std::vector<PP_VideoProfileDescription_0_1> profiles_0_1;
@@ -299,7 +290,7 @@
 
   ArrayWriter writer(output);
   if (!writer.is_valid()) {
-    RunCallback(&get_supported_profiles_callback_, PP_ERROR_BADARGUMENT);
+    SafeRunCallback(&get_supported_profiles_callback_, PP_ERROR_BADARGUMENT);
     return;
   }
 
@@ -311,12 +302,12 @@
     write_result = writer.StoreVector(profiles);
 
   if (!write_result) {
-    RunCallback(&get_supported_profiles_callback_, PP_ERROR_FAILED);
+    SafeRunCallback(&get_supported_profiles_callback_, PP_ERROR_FAILED);
     return;
   }
 
-  RunCallback(&get_supported_profiles_callback_,
-              base::checked_cast<int32_t>(profiles.size()));
+  SafeRunCallback(&get_supported_profiles_callback_,
+                  base::checked_cast<int32_t>(profiles.size()));
 }
 
 void VideoEncoderResource::OnPluginMsgInitializeReply(
@@ -332,7 +323,7 @@
   input_frame_count_ = input_frame_count;
   input_coded_size_ = input_coded_size;
 
-  RunCallback(&initialize_callback_, encoder_last_error_);
+  SafeRunCallback(&initialize_callback_, encoder_last_error_);
 }
 
 void VideoEncoderResource::OnPluginMsgGetVideoFramesReply(
@@ -377,7 +368,7 @@
 
   scoped_refptr<TrackedCallback> callback = it->second;
   encode_callbacks_.erase(it);
-  RunCallback(&callback, encoder_last_error_);
+  SafeRunCallback(&callback, encoder_last_error_);
 
   buffer_manager_.EnqueueBuffer(frame_id);
   // If the plugin is waiting for a video frame, we can give the one
@@ -431,16 +422,16 @@
 
 void VideoEncoderResource::NotifyError(int32_t error) {
   encoder_last_error_ = error;
-  RunCallback(&get_supported_profiles_callback_, error);
-  RunCallback(&initialize_callback_, error);
-  RunCallback(&get_video_frame_callback_, error);
+  SafeRunCallback(&get_supported_profiles_callback_, error);
+  SafeRunCallback(&initialize_callback_, error);
+  SafeRunCallback(&get_video_frame_callback_, error);
   get_video_frame_data_ = nullptr;
-  RunCallback(&get_bitstream_buffer_callback_, error);
+  SafeRunCallback(&get_bitstream_buffer_callback_, error);
   get_bitstream_buffer_data_ = nullptr;
   for (EncodeMap::iterator it = encode_callbacks_.begin();
        it != encode_callbacks_.end(); ++it) {
     scoped_refptr<TrackedCallback> callback = it->second;
-    RunCallback(&callback, error);
+    SafeRunCallback(&callback, error);
   }
   encode_callbacks_.clear();
 }
@@ -459,7 +450,7 @@
 
   *get_video_frame_data_ = resource->GetReference();
   get_video_frame_data_ = nullptr;
-  RunCallback(&get_video_frame_callback_, PP_OK);
+  SafeRunCallback(&get_video_frame_callback_, PP_OK);
 }
 
 void VideoEncoderResource::WriteBitstreamBuffer(const BitstreamBuffer& buffer) {
@@ -469,7 +460,7 @@
   get_bitstream_buffer_data_->buffer = shm_buffers_[buffer.id]->shm->memory();
   get_bitstream_buffer_data_->key_frame = PP_FromBool(buffer.key_frame);
   get_bitstream_buffer_data_ = nullptr;
-  RunCallback(&get_bitstream_buffer_callback_, PP_OK);
+  SafeRunCallback(&get_bitstream_buffer_callback_, PP_OK);
 }
 
 void VideoEncoderResource::ReleaseFrames() {