OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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_COMMON_EXTENSIONS_FILE_LOADING_DELEGATE_H_ |
| 6 #define CHROME_COMMON_EXTENSIONS_FILE_LOADING_DELEGATE_H_ |
| 7 |
| 8 #include "base/callback.h" |
| 9 |
| 10 class ExtensionResource; |
| 11 |
| 12 namespace extensions { |
| 13 |
| 14 // Use this in common code to load extension resources asynchronously. In |
| 15 // process types that can't load resources, pointers to this type may be NULL. |
| 16 class ResourceLoadingDelegate { |
| 17 public: |
| 18 virtual ~FileLoadingDelegate() {} |
| 19 |
| 20 // Loads the file represented by |resource|, parses/transforms it using |
| 21 // |transform_on_bg_thread|, and notifies the caller when everything's done |
| 22 // using |on_loaded| or |on_failure|. Must be called on the UI thread. |
| 23 // |
| 24 // |transform_on_bg_thread| must be thread-safe since it may be called on a |
| 25 // non-UI thread. Usually this means it should be stateless. |
| 26 // |
| 27 // |on_loaded| and |on_failure| should generally be bound to a WeakPtr to deal |
| 28 // with loads that finish after the caller is destroyed. |
| 29 // |
| 30 // |on_failure| may be null, in which case failure will be ignored. |
| 31 template<typename IntermediateType> |
| 32 StartLoad(const ExtensionResource& resource, |
| 33 base::Callback<IntermediateType(const std::string&)> |
| 34 transform_on_bg_thread, |
| 35 base::Callback<void(const IntermediateType&)> on_loaded, |
| 36 base::Callback<void()> on_failure); |
| 37 |
| 38 protected: |
| 39 class LoadInfo { |
| 40 public: |
| 41 virtual ~LoadInfo() {} |
| 42 // Call this on a background thread |
| 43 virtual void TransformOnBgThread(const std::string& file_data) = 0; |
| 44 virtual void OnLoaded() = 0; |
| 45 }; |
| 46 |
| 47 private: |
| 48 // Subclass implementations must load the file referred to by |resource| as a |
| 49 // string of bytes, call load_info->TransformOnBgThread(file_contents), and |
| 50 // then call load_info->OnLoaded(). TransformOnBgThread should be called on a |
| 51 // thread that can afford long-running operations, and OnLoaded must be called |
| 52 // on the UI thread. |
| 53 virtual StartLoadImpl(const ExtensionResource& resource, |
| 54 scoped_ptr<LoadInfo> load_info) = 0; |
| 55 }; |
| 56 |
| 57 template<typename IntermediateType> |
| 58 ResourceLoadingDelegate::StartLoad( |
| 59 const ExtensionResource& resource, |
| 60 base::Callback<IntermediateType(const std::string&)> |
| 61 transform_on_bg_thread, |
| 62 base::Callback<void(const IntermediateType&)> on_loaded) { |
| 63 // Implement LoadInfo for this particular intermediate type: |
| 64 class LoadInfoImpl : public LoadInfo { |
| 65 public: |
| 66 LoadInfoImpl(base::Callback<IntermediateType(const std::string&)> |
| 67 transform_on_bg_thread, |
| 68 base::Callback<void(const IntermediateType&)> on_loaded) |
| 69 : transform_on_bg_thread_(transform_on_bg_thread), |
| 70 on_loaded_(on_loaded) { |
| 71 } |
| 72 virtual void TransformOnBgThread(const std::string& file_data) { |
| 73 intermediate_data_ = transform_on_bg_thread_(file_data); |
| 74 } |
| 75 virtual void OnLoaded() { |
| 76 on_loaded_(intermediate_data_); |
| 77 } |
| 78 }; |
| 79 |
| 80 // Call the subclass's implementation function. |
| 81 StartLoadImpl(resource, scoped_ptr<LoadInfo>( |
| 82 new LoadInfoImpl(transform_on_bg_thread, on_loaded))); |
| 83 } |
| 84 |
| 85 } // namespace extensions |
| 86 |
| 87 #endif // CHROME_COMMON_EXTENSIONS_FILE_LOADING_DELEGATE_H_ |
OLD | NEW |