Fix race condition in WebstoreInstallHelper.
[chromium-blink-merge.git] / chrome / browser / extensions / webstore_install_helper.h
blob0a24af1d4817a3edbe25a5f290d6ae2f90129803
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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALL_HELPER_H_
6 #define CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALL_HELPER_H_
8 #include <string>
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_delegate.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "url/gurl.h"
16 namespace base {
17 class DictionaryValue;
18 class Value;
21 namespace chrome {
22 class BitmapFetcher;
25 namespace net {
26 class URLRequestContextGetter;
29 class SafeJsonParser;
31 namespace extensions {
33 // This is a class to help dealing with webstore-provided data. It manages
34 // sending work to the utility process for parsing manifests and
35 // fetching/decoding icon data. Clients must implement the
36 // WebstoreInstallHelper::Delegate interface to receive the parsed data.
37 class WebstoreInstallHelper : public base::RefCounted<WebstoreInstallHelper>,
38 public chrome::BitmapFetcherDelegate {
39 public:
40 class Delegate {
41 public:
42 enum InstallHelperResultCode {
43 UNKNOWN_ERROR,
44 ICON_ERROR,
45 MANIFEST_ERROR
48 // Called when we've successfully parsed the manifest and decoded the icon
49 // in the utility process. Ownership of parsed_manifest is transferred.
50 virtual void OnWebstoreParseSuccess(
51 const std::string& id,
52 const SkBitmap& icon,
53 base::DictionaryValue* parsed_manifest) = 0;
55 // Called to indicate a parse failure. The |result_code| parameter should
56 // indicate whether the problem was with the manifest or icon.
57 virtual void OnWebstoreParseFailure(
58 const std::string& id,
59 InstallHelperResultCode result_code,
60 const std::string& error_message) = 0;
62 protected:
63 virtual ~Delegate() {}
66 // It is legal for |icon_url| to be empty.
67 WebstoreInstallHelper(Delegate* delegate,
68 const std::string& id,
69 const std::string& manifest,
70 const GURL& icon_url,
71 net::URLRequestContextGetter* context_getter);
72 void Start();
74 private:
75 friend class base::RefCounted<WebstoreInstallHelper>;
77 ~WebstoreInstallHelper() override;
79 // Callbacks for the SafeJsonParser.
80 void OnJSONParseSucceeded(scoped_ptr<base::Value> result);
81 void OnJSONParseFailed(const std::string& error_message);
83 // Implementing the chrome::BitmapFetcherDelegate interface.
84 void OnFetchComplete(const GURL& url, const SkBitmap* image) override;
86 void ReportResultsIfComplete();
88 // The client who we'll report results back to.
89 Delegate* delegate_;
91 // The extension id of the manifest we're parsing.
92 std::string id_;
94 // The manifest to parse.
95 std::string manifest_;
97 scoped_refptr<SafeJsonParser> json_parser_;
99 // If |icon_url_| is non-empty, it needs to be fetched and decoded into an
100 // SkBitmap.
101 GURL icon_url_;
102 net::URLRequestContextGetter* context_getter_; // Only usable on UI thread.
103 scoped_ptr<chrome::BitmapFetcher> icon_fetcher_;
105 // Flags for whether we're done doing icon decoding and manifest parsing.
106 bool icon_decode_complete_;
107 bool manifest_parse_complete_;
109 // The results of successful decoding/parsing.
110 SkBitmap icon_;
111 scoped_ptr<base::DictionaryValue> parsed_manifest_;
113 // A details string for keeping track of any errors.
114 std::string error_;
116 // A code to distinguish between an error with the icon, and an error with the
117 // manifest.
118 Delegate::InstallHelperResultCode parse_error_;
121 } // namespace extensions
123 #endif // CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALL_HELPER_H_