Index: chrome/common/extensions/file_loading_delegate.h |
diff --git a/chrome/common/extensions/file_loading_delegate.h b/chrome/common/extensions/file_loading_delegate.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..75733097cbc4cbb666116d6b4421bb8606289ff7 |
--- /dev/null |
+++ b/chrome/common/extensions/file_loading_delegate.h |
@@ -0,0 +1,87 @@ |
+// Copyright (c) 2012 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_COMMON_EXTENSIONS_FILE_LOADING_DELEGATE_H_ |
+#define CHROME_COMMON_EXTENSIONS_FILE_LOADING_DELEGATE_H_ |
+ |
+#include "base/callback.h" |
+ |
+class ExtensionResource; |
+ |
+namespace extensions { |
+ |
+// Use this in common code to load extension resources asynchronously. In |
+// process types that can't load resources, pointers to this type may be NULL. |
+class ResourceLoadingDelegate { |
+ public: |
+ virtual ~FileLoadingDelegate() {} |
+ |
+ // Loads the file represented by |resource|, parses/transforms it using |
+ // |transform_on_bg_thread|, and notifies the caller when everything's done |
+ // using |on_loaded| or |on_failure|. Must be called on the UI thread. |
+ // |
+ // |transform_on_bg_thread| must be thread-safe since it may be called on a |
+ // non-UI thread. Usually this means it should be stateless. |
+ // |
+ // |on_loaded| and |on_failure| should generally be bound to a WeakPtr to deal |
+ // with loads that finish after the caller is destroyed. |
+ // |
+ // |on_failure| may be null, in which case failure will be ignored. |
+ template<typename IntermediateType> |
+ StartLoad(const ExtensionResource& resource, |
+ base::Callback<IntermediateType(const std::string&)> |
+ transform_on_bg_thread, |
+ base::Callback<void(const IntermediateType&)> on_loaded, |
+ base::Callback<void()> on_failure); |
+ |
+ protected: |
+ class LoadInfo { |
+ public: |
+ virtual ~LoadInfo() {} |
+ // Call this on a background thread |
+ virtual void TransformOnBgThread(const std::string& file_data) = 0; |
+ virtual void OnLoaded() = 0; |
+ }; |
+ |
+ private: |
+ // Subclass implementations must load the file referred to by |resource| as a |
+ // string of bytes, call load_info->TransformOnBgThread(file_contents), and |
+ // then call load_info->OnLoaded(). TransformOnBgThread should be called on a |
+ // thread that can afford long-running operations, and OnLoaded must be called |
+ // on the UI thread. |
+ virtual StartLoadImpl(const ExtensionResource& resource, |
+ scoped_ptr<LoadInfo> load_info) = 0; |
+}; |
+ |
+template<typename IntermediateType> |
+ResourceLoadingDelegate::StartLoad( |
+ const ExtensionResource& resource, |
+ base::Callback<IntermediateType(const std::string&)> |
+ transform_on_bg_thread, |
+ base::Callback<void(const IntermediateType&)> on_loaded) { |
+ // Implement LoadInfo for this particular intermediate type: |
+ class LoadInfoImpl : public LoadInfo { |
+ public: |
+ LoadInfoImpl(base::Callback<IntermediateType(const std::string&)> |
+ transform_on_bg_thread, |
+ base::Callback<void(const IntermediateType&)> on_loaded) |
+ : transform_on_bg_thread_(transform_on_bg_thread), |
+ on_loaded_(on_loaded) { |
+ } |
+ virtual void TransformOnBgThread(const std::string& file_data) { |
+ intermediate_data_ = transform_on_bg_thread_(file_data); |
+ } |
+ virtual void OnLoaded() { |
+ on_loaded_(intermediate_data_); |
+ } |
+ }; |
+ |
+ // Call the subclass's implementation function. |
+ StartLoadImpl(resource, scoped_ptr<LoadInfo>( |
+ new LoadInfoImpl(transform_on_bg_thread, on_loaded))); |
+} |
+ |
+} // namespace extensions |
+ |
+#endif // CHROME_COMMON_EXTENSIONS_FILE_LOADING_DELEGATE_H_ |