OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALL_HELPER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALL_HELPER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALL_HELPER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALL_HELPER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "chrome/browser/image_decoder.h" |
12 #include "content/public/browser/utility_process_host_client.h" | 13 #include "content/public/browser/utility_process_host_client.h" |
13 #include "net/url_request/url_fetcher_delegate.h" | 14 #include "net/url_request/url_fetcher_delegate.h" |
14 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
16 | 17 |
17 class SkBitmap; | 18 class SkBitmap; |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 class DictionaryValue; | 21 class DictionaryValue; |
21 class ListValue; | 22 class ListValue; |
22 } | 23 } |
23 | 24 |
24 namespace content { | 25 namespace content { |
25 class UtilityProcessHost; | 26 class UtilityProcessHost; |
26 } | 27 } |
27 | 28 |
28 namespace net { | 29 namespace net { |
29 class URLFetcher; | 30 class URLFetcher; |
30 class URLRequestContextGetter; | 31 class URLRequestContextGetter; |
31 } | 32 } |
32 | 33 |
33 namespace extensions { | 34 namespace extensions { |
34 | 35 |
35 // This is a class to help dealing with webstore-provided data. It manages | 36 // This is a class to help dealing with webstore-provided data. It manages |
36 // sending work to the utility process for parsing manifests and | 37 // sending work to the utility process for parsing manifests and |
37 // fetching/decoding icon data. Clients must implement the | 38 // fetching/decoding icon data. Clients must implement the |
38 // WebstoreInstallHelper::Delegate interface to receive the parsed data. | 39 // WebstoreInstallHelper::Delegate interface to receive the parsed data. |
39 class WebstoreInstallHelper : public content::UtilityProcessHostClient, | 40 class WebstoreInstallHelper : public ImageDecoder::ImageRequest, |
| 41 public content::UtilityProcessHostClient, |
40 public net::URLFetcherDelegate { | 42 public net::URLFetcherDelegate { |
41 public: | 43 public: |
42 class Delegate { | 44 class Delegate { |
43 public: | 45 public: |
44 enum InstallHelperResultCode { | 46 enum InstallHelperResultCode { |
45 UNKNOWN_ERROR, | 47 UNKNOWN_ERROR, |
46 ICON_ERROR, | 48 ICON_ERROR, |
47 MANIFEST_ERROR | 49 MANIFEST_ERROR |
48 }; | 50 }; |
49 | 51 |
50 // Called when we've successfully parsed the manifest and decoded the icon | 52 // Called when we've successfully parsed the manifest and decoded the icon |
51 // in the utility process. Ownership of parsed_manifest is transferred. | 53 // in the utility process. Ownership of parsed_manifest is transferred. |
52 virtual void OnWebstoreParseSuccess( | 54 virtual void OnWebstoreParseSuccess( |
53 const std::string& id, | 55 const std::string& id, |
54 const SkBitmap& icon, | 56 const SkBitmap& icon, |
55 base::DictionaryValue* parsed_manifest) = 0; | 57 base::DictionaryValue* parsed_manifest) = 0; |
56 | 58 |
57 // Called to indicate a parse failure. The |result_code| parameter should | 59 // Called to indicate a parse failure. The |result_code| parameter should |
58 // indicate whether the problem was with the manifest or icon. | 60 // indicate whether the problem was with the manifest or icon. |
59 virtual void OnWebstoreParseFailure( | 61 virtual void OnWebstoreParseFailure( |
60 const std::string& id, | 62 const std::string& id, |
61 InstallHelperResultCode result_code, | 63 InstallHelperResultCode result_code, |
62 const std::string& error_message) = 0; | 64 const std::string& error_message) = 0; |
63 | 65 |
64 protected: | 66 protected: |
65 virtual ~Delegate() {} | 67 virtual ~Delegate() {} |
66 }; | 68 }; |
67 | 69 |
68 // Only one of |icon_data| (based64-encoded icon data) or |icon_url| can be | 70 // It is legal for |icon_url| to be empty. |
69 // specified, but it is legal for both to be empty. | |
70 WebstoreInstallHelper(Delegate* delegate, | 71 WebstoreInstallHelper(Delegate* delegate, |
71 const std::string& id, | 72 const std::string& id, |
72 const std::string& manifest, | 73 const std::string& manifest, |
73 const std::string& icon_data, | |
74 const GURL& icon_url, | 74 const GURL& icon_url, |
75 net::URLRequestContextGetter* context_getter); | 75 net::URLRequestContextGetter* context_getter); |
76 void Start(); | 76 void Start(); |
77 | 77 |
78 private: | 78 private: |
79 ~WebstoreInstallHelper() override; | 79 ~WebstoreInstallHelper() override; |
80 | 80 |
81 void StartWorkOnIOThread(); | 81 void StartWorkOnIOThread(); |
82 void StartFetchedImageDecode(); | |
83 void ReportResultsIfComplete(); | 82 void ReportResultsIfComplete(); |
84 void ReportResultFromUIThread(); | 83 void ReportResultFromUIThread(); |
85 | 84 |
86 // Implementing the net::URLFetcherDelegate interface. | 85 // Implementing the net::URLFetcherDelegate interface. |
87 void OnURLFetchComplete(const net::URLFetcher* source) override; | 86 void OnURLFetchComplete(const net::URLFetcher* source) override; |
88 | 87 |
89 // Implementing pieces of the UtilityProcessHostClient interface. | 88 // Implementing pieces of the UtilityProcessHostClient interface. |
90 bool OnMessageReceived(const IPC::Message& message) override; | 89 bool OnMessageReceived(const IPC::Message& message) override; |
91 | 90 |
92 // Message handlers. | 91 // Message handlers. |
93 void OnDecodeImageSucceeded(const SkBitmap& decoded_image); | |
94 void OnDecodeImageFailed(); | |
95 void OnJSONParseSucceeded(const base::ListValue& wrapper); | 92 void OnJSONParseSucceeded(const base::ListValue& wrapper); |
96 void OnJSONParseFailed(const std::string& error_message); | 93 void OnJSONParseFailed(const std::string& error_message); |
97 | 94 |
| 95 // ImageDecoder::ImageRequest implementation. |
| 96 void OnImageDecoded(const SkBitmap& decoded_image) override; |
| 97 void OnDecodeImageFailed() override; |
| 98 |
98 // The client who we'll report results back to. | 99 // The client who we'll report results back to. |
99 Delegate* delegate_; | 100 Delegate* delegate_; |
100 | 101 |
101 // The extension id of the manifest we're parsing. | 102 // The extension id of the manifest we're parsing. |
102 std::string id_; | 103 std::string id_; |
103 | 104 |
104 // The manifest to parse. | 105 // The manifest to parse. |
105 std::string manifest_; | 106 std::string manifest_; |
106 | 107 |
107 // Only one of these should be non-empty. If |icon_base64_data_| is non-emtpy, | 108 // If |icon_url_| is non-empty, it needs to be fetched and decoded into an |
108 // it's a base64-encoded string that needs to be parsed into an SkBitmap. If | |
109 // |icon_url_| is non-empty, it needs to be fetched and decoded into an | |
110 // SkBitmap. | 109 // SkBitmap. |
111 std::string icon_base64_data_; | |
112 GURL icon_url_; | 110 GURL icon_url_; |
113 std::vector<unsigned char> fetched_icon_data_; | |
114 | 111 |
115 // For fetching the icon, if needed. | 112 // For fetching the icon, if needed. |
116 scoped_ptr<net::URLFetcher> url_fetcher_; | 113 scoped_ptr<net::URLFetcher> url_fetcher_; |
117 net::URLRequestContextGetter* context_getter_; // Only usable on UI thread. | 114 net::URLRequestContextGetter* context_getter_; // Only usable on UI thread. |
118 | 115 |
119 base::WeakPtr<content::UtilityProcessHost> utility_host_; | 116 base::WeakPtr<content::UtilityProcessHost> utility_host_; |
120 | 117 |
121 // Flags for whether we're done doing icon decoding and manifest parsing. | 118 // Flags for whether we're done doing icon decoding and manifest parsing. |
122 bool icon_decode_complete_; | 119 bool icon_decode_complete_; |
123 bool manifest_parse_complete_; | 120 bool manifest_parse_complete_; |
124 | 121 |
125 // The results of succesful decoding/parsing. | 122 // The results of succesful decoding/parsing. |
126 SkBitmap icon_; | 123 SkBitmap icon_; |
127 scoped_ptr<base::DictionaryValue> parsed_manifest_; | 124 scoped_ptr<base::DictionaryValue> parsed_manifest_; |
128 | 125 |
129 // A details string for keeping track of any errors. | 126 // A details string for keeping track of any errors. |
130 std::string error_; | 127 std::string error_; |
131 | 128 |
132 // A code to distinguish between an error with the icon, and an error with the | 129 // A code to distinguish between an error with the icon, and an error with the |
133 // manifest. | 130 // manifest. |
134 Delegate::InstallHelperResultCode parse_error_; | 131 Delegate::InstallHelperResultCode parse_error_; |
135 }; | 132 }; |
136 | 133 |
137 } // namespace extensions | 134 } // namespace extensions |
138 | 135 |
139 #endif // CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALL_HELPER_H_ | 136 #endif // CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALL_HELPER_H_ |
OLD | NEW |