OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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_BROWSER_ICON_URL_DATA_MANAGER_H_ |
| 6 #define CHROME_BROWSER_ICON_URL_DATA_MANAGER_H_ |
| 7 |
| 8 #include <string> |
| 9 #include <map> |
| 10 |
| 11 #include "base/ref_counted_memory.h" |
| 12 #include "base/scoped_ptr.h" |
| 13 #include "base/singleton.h" |
| 14 #include "base/task.h" |
| 15 #include "chrome/browser/icon_loader.h" |
| 16 #include "net/url_request/url_request_job.h" |
| 17 |
| 18 class IconURLDataManager; |
| 19 |
| 20 class URLRequestIconJob : public URLRequestJob { |
| 21 public: |
| 22 typedef int RequestID; |
| 23 |
| 24 explicit URLRequestIconJob(URLRequest* request); |
| 25 |
| 26 // URLRequestJob implementation. |
| 27 virtual void Start(); |
| 28 |
| 29 virtual void Kill(); |
| 30 |
| 31 virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read); |
| 32 |
| 33 virtual bool GetMimeType(std::string* mime_type) const; |
| 34 |
| 35 void SetMimeType(const std::string& mime_type); |
| 36 |
| 37 private: |
| 38 friend class IconURLDataManager; |
| 39 |
| 40 virtual ~URLRequestIconJob(); |
| 41 |
| 42 // Helper for Start(), to let us start asynchronously. |
| 43 // (This pattern is shared by most URLRequestJob implementations.) |
| 44 void StartAsync(); |
| 45 |
| 46 bool StartRequest(); |
| 47 |
| 48 void DataAvailable(RefCountedMemory* bytes); |
| 49 |
| 50 // Do the actual copy from data_ (the data we're serving) into |buf|. |
| 51 // Separate from ReadRawData so we can handle async I/O. |
| 52 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read); |
| 53 |
| 54 // The actual data we're serving. NULL until it's been fetched. |
| 55 scoped_refptr<RefCountedMemory> data_; |
| 56 // The current offset into the data that we're handing off to our |
| 57 // callers via the Read interfaces. |
| 58 int data_offset_; |
| 59 |
| 60 // For async reads, we keep around a pointer to the buffer that |
| 61 // we're reading into. |
| 62 scoped_refptr<net::IOBuffer> pending_buf_; |
| 63 |
| 64 int pending_buf_size_; |
| 65 |
| 66 std::string mime_type_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(URLRequestIconJob); |
| 69 }; |
| 70 |
| 71 |
| 72 // IconURI represents a URI that can be used to resolve an icon URI as |
| 73 // specified by http://tools.ietf.org/html/draft-lafayette-icon-uri-scheme-01. |
| 74 // |
| 75 // iconuri = "icon:" ( fextension / mediatype / "unknown" / |
| 76 // "directory" / "parentdir" ) [ ";" size ] |
| 77 // fextension = "." 1*token ; File extension |
| 78 // mediatype = [ type ":" subtype ] ; Internet media type |
| 79 // size = pixels / "small" / "medium" / "large" |
| 80 // pixels = 1*digit ; Size of icon in square pixels |
| 81 class IconURI { |
| 82 public: |
| 83 // Keyword identifiers for an icon. |
| 84 static const std::string DIRECTORY; |
| 85 static const std::string PARENT_DIRECTORY; |
| 86 static const std::string UNKNOWN; |
| 87 |
| 88 // Size identifiers for an icon. |
| 89 static const std::string SMALL_ID; |
| 90 static const std::string MEDIUM_ID; |
| 91 static const std::string LARGE_ID; |
| 92 |
| 93 enum IconSizes { |
| 94 SMALL = 16, |
| 95 MEDIUM = 32, |
| 96 LARGE = 64 |
| 97 }; |
| 98 |
| 99 IconURI(); |
| 100 |
| 101 ~IconURI(); |
| 102 |
| 103 bool ParseIconURI(const GURL& url); |
| 104 |
| 105 // Returns whether the provided URL is a valid icon URI. We return a default |
| 106 // icon image for icon URIs with unresolvable paths. |
| 107 bool IsValid(); |
| 108 |
| 109 std::string extension() const; |
| 110 |
| 111 // Returns the internet content type if specified by the URI. |
| 112 std::string media_type() const; |
| 113 |
| 114 std::string keyword() const; |
| 115 |
| 116 // For now IconLoader only can load 3 sizes. |
| 117 IconLoader::IconSize GetIconLoaderSize() const; |
| 118 |
| 119 // Returns the width/height of the icon. |
| 120 int size() const; |
| 121 |
| 122 private: |
| 123 void SetIdentifier(const std::string& identifier); |
| 124 |
| 125 void SetExtension(const std::string& ext); |
| 126 |
| 127 // Need to map media type to an extension for lookup. |
| 128 void SetMediaType(const std::string& media_type); |
| 129 |
| 130 // Different platforms support different icon sizes. Icons MUST be scaled to |
| 131 // the appropriate size if not available natively. |
| 132 void SetSize(const std::string& size_str); |
| 133 |
| 134 GURL url_; |
| 135 std::string extension_; |
| 136 std::string media_type_; |
| 137 std::string keyword_; |
| 138 int size_; |
| 139 }; |
| 140 |
| 141 |
| 142 class IconJobSource; |
| 143 |
| 144 class IconURLDataManager { |
| 145 public: |
| 146 typedef int RequestID; |
| 147 |
| 148 IconURLDataManager(); |
| 149 |
| 150 ~IconURLDataManager(); |
| 151 |
| 152 static URLRequestJob* Factory(URLRequest* request, const std::string& scheme); |
| 153 |
| 154 void StartDataRequest(const IconURI& icon_uri, |
| 155 URLRequestIconJob* job); |
| 156 |
| 157 void DataAvailable(RequestID request_id, |
| 158 scoped_refptr<RefCountedMemory> bytes); |
| 159 |
| 160 void RemoveRequest(URLRequestIconJob* job); |
| 161 |
| 162 bool HasPendingJob(URLRequestIconJob* job) const; |
| 163 |
| 164 private: |
| 165 scoped_refptr<IconJobSource> data_source_; |
| 166 |
| 167 // The ID we'll use for the next request we receive. |
| 168 RequestID next_request_id_; |
| 169 |
| 170 // All pending URLRequestIconJobs, keyed by ID of the request. |
| 171 // URLRequestChromeJob calls into this object when it's constructed and |
| 172 // destructed to ensure that the pointers in this map remain valid. |
| 173 typedef std::map<RequestID, URLRequestIconJob*> PendingRequestMap; |
| 174 PendingRequestMap pending_requests_; |
| 175 |
| 176 friend struct DefaultSingletonTraits<IconURLDataManager>; |
| 177 DISALLOW_COPY_AND_ASSIGN(IconURLDataManager); |
| 178 }; |
| 179 |
| 180 DISABLE_RUNNABLE_METHOD_REFCOUNT(IconURLDataManager); |
| 181 |
| 182 // Used to initialize the IconURLDataManager on the UI thread. |
| 183 void RegisterURLRequestIconJob(); |
| 184 |
| 185 #endif // CHROME_BROWSER_ICON_URL_DATA_MANAGER_H_ |
OLD | NEW |