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 #include "chrome/browser/extensions/webstore_install_helper.h"
8 #include "base/values.h"
9 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
10 #include "components/safe_json/safe_json_parser.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "net/base/load_flags.h"
13 #include "net/url_request/url_request.h"
15 using content::BrowserThread
;
19 const char kImageDecodeError
[] = "Image decode failed";
23 namespace extensions
{
25 WebstoreInstallHelper::WebstoreInstallHelper(
27 const std::string
& id
,
28 const std::string
& manifest
,
30 net::URLRequestContextGetter
* context_getter
)
31 : delegate_(delegate
),
35 context_getter_(context_getter
),
36 icon_decode_complete_(false),
37 manifest_parse_complete_(false),
38 parse_error_(Delegate::UNKNOWN_ERROR
) {
41 WebstoreInstallHelper::~WebstoreInstallHelper() {}
43 void WebstoreInstallHelper::Start() {
44 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
46 safe_json::SafeJsonParser::Parse(
47 manifest_
, base::Bind(&WebstoreInstallHelper::OnJSONParseSucceeded
, this),
48 base::Bind(&WebstoreInstallHelper::OnJSONParseFailed
, this));
50 if (icon_url_
.is_empty()) {
51 icon_decode_complete_
= true;
53 // No existing |icon_fetcher_| to avoid unbalanced AddRef().
54 CHECK(!icon_fetcher_
.get());
55 AddRef(); // Balanced in OnFetchComplete().
56 icon_fetcher_
.reset(new chrome::BitmapFetcher(icon_url_
, this));
58 context_getter_
, std::string(),
59 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
,
60 net::LOAD_DO_NOT_SAVE_COOKIES
| net::LOAD_DO_NOT_SEND_COOKIES
);
61 icon_fetcher_
->Start();
65 void WebstoreInstallHelper::OnFetchComplete(const GURL
& url
,
66 const SkBitmap
* image
) {
67 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
68 // OnFetchComplete should only be called as icon_fetcher_ delegate to avoid
69 // unbalanced Release().
70 CHECK(icon_fetcher_
.get());
74 icon_decode_complete_
= true;
76 error_
= kImageDecodeError
;
77 parse_error_
= Delegate::ICON_ERROR
;
79 icon_fetcher_
.reset();
81 ReportResultsIfComplete();
82 Release(); // Balanced in Start().
85 void WebstoreInstallHelper::OnJSONParseSucceeded(
86 scoped_ptr
<base::Value
> result
) {
87 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
88 manifest_parse_complete_
= true;
89 const base::DictionaryValue
* value
;
90 if (result
->GetAsDictionary(&value
))
91 parsed_manifest_
.reset(value
->DeepCopy());
93 parse_error_
= Delegate::MANIFEST_ERROR
;
95 ReportResultsIfComplete();
98 void WebstoreInstallHelper::OnJSONParseFailed(
99 const std::string
& error_message
) {
100 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
101 manifest_parse_complete_
= true;
102 error_
= error_message
;
103 parse_error_
= Delegate::MANIFEST_ERROR
;
104 ReportResultsIfComplete();
107 void WebstoreInstallHelper::ReportResultsIfComplete() {
108 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
110 if (!icon_decode_complete_
|| !manifest_parse_complete_
)
113 if (error_
.empty() && parsed_manifest_
)
114 delegate_
->OnWebstoreParseSuccess(id_
, icon_
, parsed_manifest_
.release());
116 delegate_
->OnWebstoreParseFailure(id_
, parse_error_
, error_
);
119 } // namespace extensions