| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_PUBLIC_BROWSER_URL_DATA_SOURCE_DELEGATE_H_ | 5 #ifndef CONTENT_PUBLIC_BROWSER_URL_DATA_SOURCE_DELEGATE_H_ |
| 6 #define CONTENT_PUBLIC_BROWSER_URL_DATA_SOURCE_DELEGATE_H_ | 6 #define CONTENT_PUBLIC_BROWSER_URL_DATA_SOURCE_DELEGATE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "content/common/content_export.h" | 10 #include "content/common/content_export.h" |
| 11 #include "base/callback.h" |
| 11 | 12 |
| 12 class ChromeURLDataManager; | |
| 13 class ChromeURLDataManagerBackend; | |
| 14 class ChromeWebUIDataSource; | |
| 15 class MessageLoop; | 13 class MessageLoop; |
| 16 class URLDataSource; | 14 |
| 15 namespace base { |
| 16 class RefCountedMemory; |
| 17 } |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 | 20 |
| 20 // An interface implemented by a creator of URLDataSource to customize its | 21 // A URLDataSource is an object that can answer requests for data |
| 21 // behavior. | 22 // asynchronously. An implementation of URLDataSource should handle calls to |
| 22 class CONTENT_EXPORT URLDataSourceDelegate { | 23 // StartDataRequest() by starting its (implementation-specific) asynchronous |
| 24 // request for the data, then running the callback given in that method to |
| 25 // notify. |
| 26 class CONTENT_EXPORT URLDataSource { |
| 23 public: | 27 public: |
| 24 URLDataSourceDelegate(); | 28 // Used by StartDataRequest. The string parameter is the path of the request. |
| 25 virtual ~URLDataSourceDelegate(); | 29 // If the callee doesn't want to handle the data, false is returned. Otherwise |
| 30 // true is returned and the GotDataCallback parameter is called either then or |
| 31 // asynchronously with the response. |
| 32 typedef base::Callback<void(base::RefCountedMemory*)> GotDataCallback; |
| 33 |
| 34 virtual ~URLDataSource() {} |
| 26 | 35 |
| 27 // The name of this source. | 36 // The name of this source. |
| 28 // E.g., for favicons, this could be "favicon", which results in paths for | 37 // E.g., for favicons, this could be "favicon", which results in paths for |
| 29 // specific resources like "favicon/34" getting sent to this source. | 38 // specific resources like "favicon/34" getting sent to this source. |
| 30 virtual std::string GetSource() = 0; | 39 virtual std::string GetSource() = 0; |
| 31 | 40 |
| 32 // Called by URLDataSource to request data at |path|. The delegate should | 41 // Called by URLDataSource to request data at |path|. The delegate should |
| 33 // call URLDataSource::SendResponse() when the data is available or if the | 42 // run |callback| when the data is available or if the request could not be |
| 34 // request could not be satisfied. | 43 // satisfied. |
| 35 virtual void StartDataRequest(const std::string& path, | 44 virtual void StartDataRequest(const std::string& path, |
| 36 bool is_incognito, | 45 bool is_incognito, |
| 37 int request_id) = 0; | 46 const GotDataCallback& callback) = 0; |
| 38 | 47 |
| 39 // Return the mimetype that should be sent with this response, or empty | 48 // Return the mimetype that should be sent with this response, or empty |
| 40 // string to specify no mime type. | 49 // string to specify no mime type. |
| 41 virtual std::string GetMimeType(const std::string& path) const = 0; | 50 virtual std::string GetMimeType(const std::string& path) const = 0; |
| 42 | 51 |
| 43 // Returns the MessageLoop on which the delegate wishes to have | 52 // Returns the MessageLoop on which the delegate wishes to have |
| 44 // StartDataRequest called to handle the request for |path|. The default | 53 // StartDataRequest called to handle the request for |path|. The default |
| 45 // implementation returns BrowserThread::UI. If the delegate does not care | 54 // implementation returns BrowserThread::UI. If the delegate does not care |
| 46 // which thread StartDataRequest is called on, this should return NULL. It may | 55 // which thread StartDataRequest is called on, this should return NULL. It may |
| 47 // be beneficial to return NULL for requests that are safe to handle directly | 56 // be beneficial to return NULL for requests that are safe to handle directly |
| 48 // on the IO thread. This can improve performance by satisfying such requests | 57 // on the IO thread. This can improve performance by satisfying such requests |
| 49 // more rapidly when there is a large amount of UI thread contention. Or the | 58 // more rapidly when there is a large amount of UI thread contention. Or the |
| 50 // delegate can return a specific thread's Messageloop if they wish. | 59 // delegate can return a specific thread's Messageloop if they wish. |
| 51 virtual MessageLoop* MessageLoopForRequestPath(const std::string& path) const; | 60 virtual MessageLoop* MessageLoopForRequestPath(const std::string& path) const; |
| 52 | 61 |
| 53 // Returns true if the URLDataSource should replace an existing URLDataSource | 62 // Returns true if the URLDataSource should replace an existing URLDataSource |
| 54 // with the same name that has already been registered. The default is true. | 63 // with the same name that has already been registered. The default is true. |
| 55 // | 64 // |
| 56 // WARNING: this is invoked on the IO thread. | 65 // WARNING: this is invoked on the IO thread. |
| 57 // | 66 // |
| 58 // TODO: nuke this and convert all callers to not replace. | 67 // TODO: nuke this and convert all callers to not replace. |
| 59 virtual bool ShouldReplaceExistingSource() const; | 68 virtual bool ShouldReplaceExistingSource() const; |
| 60 | 69 |
| 61 // Returns true if responses from this URLDataSource can be cached. | 70 // Returns true if responses from this URLDataSource can be cached. |
| 62 virtual bool AllowCaching() const; | 71 virtual bool AllowCaching() const; |
| 63 | |
| 64 void set_url_data_source_for_testing(URLDataSource* url_data_source) { | |
| 65 url_data_source_ = url_data_source; | |
| 66 } | |
| 67 | |
| 68 protected: | |
| 69 URLDataSource* url_data_source() { return url_data_source_; } | |
| 70 | |
| 71 private: | |
| 72 friend class ::ChromeURLDataManager; | |
| 73 friend class ::ChromeURLDataManagerBackend; | |
| 74 friend class ::ChromeWebUIDataSource; | |
| 75 URLDataSource* url_data_source_; | |
| 76 }; | 72 }; |
| 77 | 73 |
| 78 } // namespace content | 74 } // namespace content |
| 79 | 75 |
| 80 #endif // CONTENT_PUBLIC_BROWSER_URL_DATA_SOURCE_DELEGATE_H_ | 76 #endif // CONTENT_PUBLIC_BROWSER_URL_DATA_SOURCE_DELEGATE_H_ |
| OLD | NEW |