Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(702)

Side by Side Diff: chrome/browser/extensions/webstore_install_helper.cc

Issue 1140053003: Refactoring: Moving the SafeJsonParser to its own component. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moving to namespace Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "chrome/browser/extensions/webstore_install_helper.h" 5 #include "chrome/browser/extensions/webstore_install_helper.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/weak_ptr.h"
10 #include "base/thread_task_runner_handle.h" 11 #include "base/thread_task_runner_handle.h"
11 #include "base/values.h" 12 #include "base/values.h"
12 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" 13 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
13 #include "chrome/common/chrome_utility_messages.h" 14 #include "chrome/common/chrome_utility_messages.h"
Robert Sesek 2015/05/14 20:12:33 Unused.
Eran Messeri 2015/05/15 12:52:40 Done.
14 #include "chrome/common/extensions/chrome_utility_extensions_messages.h" 15 #include "chrome/common/extensions/chrome_utility_extensions_messages.h"
15 #include "chrome/grit/generated_resources.h" 16 #include "chrome/grit/generated_resources.h"
17 #include "components/safe_json_parser/safe_json_parser.h"
16 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/utility_process_host.h" 19 #include "content/public/browser/utility_process_host.h"
Robert Sesek 2015/05/14 20:12:33 Unused.
Eran Messeri 2015/05/15 12:52:40 Done.
18 #include "net/base/load_flags.h" 20 #include "net/base/load_flags.h"
19 #include "net/url_request/url_request.h" 21 #include "net/url_request/url_request.h"
20 #include "ui/base/l10n/l10n_util.h" 22 #include "ui/base/l10n/l10n_util.h"
21 23
22 using content::BrowserThread; 24 using content::BrowserThread;
23 using content::UtilityProcessHost; 25 using content::UtilityProcessHost;
Robert Sesek 2015/05/14 20:12:33 Unused.
Eran Messeri 2015/05/15 12:52:40 Done.
24 26
25 namespace { 27 namespace {
26 28
27 const char kImageDecodeError[] = "Image decode failed"; 29 const char kImageDecodeError[] = "Image decode failed";
28 30
29 } // namespace 31 } // namespace
30 32
31 namespace extensions { 33 namespace extensions {
32 34
33 WebstoreInstallHelper::WebstoreInstallHelper( 35 WebstoreInstallHelper::WebstoreInstallHelper(
(...skipping 23 matching lines...) Expand all
57 // No existing |icon_fetcher_| to avoid unbalanced AddRef(). 59 // No existing |icon_fetcher_| to avoid unbalanced AddRef().
58 CHECK(!icon_fetcher_.get()); 60 CHECK(!icon_fetcher_.get());
59 AddRef(); // Balanced in OnFetchComplete(). 61 AddRef(); // Balanced in OnFetchComplete().
60 icon_fetcher_.reset(new chrome::BitmapFetcher(icon_url_, this)); 62 icon_fetcher_.reset(new chrome::BitmapFetcher(icon_url_, this));
61 icon_fetcher_->Start( 63 icon_fetcher_->Start(
62 context_getter_, std::string(), 64 context_getter_, std::string(),
63 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, 65 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
64 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES); 66 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES);
65 } 67 }
66 68
67 BrowserThread::PostTask( 69 base::WeakPtrFactory<WebstoreInstallHelper> weak_factory(this);
68 BrowserThread::IO, 70 scoped_refptr<safe_json_parser::SafeJsonParser> parser =
69 FROM_HERE, 71 new safe_json_parser::SafeJsonParser(
70 base::Bind(&WebstoreInstallHelper::StartWorkOnIOThread, this)); 72 manifest_, base::Bind(&WebstoreInstallHelper::OnJSONParseSucceeded,
71 } 73 weak_factory.GetWeakPtr()),
72 74 base::Bind(&WebstoreInstallHelper::OnJSONParseFailed,
73 void WebstoreInstallHelper::StartWorkOnIOThread() { 75 weak_factory.GetWeakPtr()));
74 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 76 parser->Start();
75 utility_host_ = UtilityProcessHost::Create(
76 this, base::ThreadTaskRunnerHandle::Get().get())->AsWeakPtr();
77 utility_host_->SetName(l10n_util::GetStringUTF16(
78 IDS_UTILITY_PROCESS_JSON_PARSER_NAME));
79 utility_host_->StartBatchMode();
80
81 utility_host_->Send(new ChromeUtilityMsg_ParseJSON(manifest_));
82 }
83
84 bool WebstoreInstallHelper::OnMessageReceived(const IPC::Message& message) {
85 bool handled = true;
86 IPC_BEGIN_MESSAGE_MAP(WebstoreInstallHelper, message)
87 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Succeeded,
88 OnJSONParseSucceeded)
89 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Failed,
90 OnJSONParseFailed)
91 IPC_MESSAGE_UNHANDLED(handled = false)
92 IPC_END_MESSAGE_MAP()
93 return handled;
94 } 77 }
95 78
96 void WebstoreInstallHelper::OnFetchComplete(const GURL& url, 79 void WebstoreInstallHelper::OnFetchComplete(const GURL& url,
97 const SkBitmap* image) { 80 const SkBitmap* image) {
98 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 81 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
99 // OnFetchComplete should only be called as icon_fetcher_ delegate to avoid 82 // OnFetchComplete should only be called as icon_fetcher_ delegate to avoid
100 // unbalanced Release(). 83 // unbalanced Release().
101 CHECK(icon_fetcher_.get()); 84 CHECK(icon_fetcher_.get());
102 85
103 if (image) 86 if (image)
104 icon_ = *image; 87 icon_ = *image;
105 icon_decode_complete_ = true; 88 icon_decode_complete_ = true;
106 if (icon_.empty()) { 89 if (icon_.empty()) {
107 error_ = kImageDecodeError; 90 error_ = kImageDecodeError;
108 parse_error_ = Delegate::ICON_ERROR; 91 parse_error_ = Delegate::ICON_ERROR;
109 } 92 }
110 icon_fetcher_.reset(); 93 icon_fetcher_.reset();
111 BrowserThread::PostTask( 94 BrowserThread::PostTask(
112 BrowserThread::IO, 95 BrowserThread::IO,
113 FROM_HERE, 96 FROM_HERE,
114 base::Bind(&WebstoreInstallHelper::ReportResultsIfComplete, this)); 97 base::Bind(&WebstoreInstallHelper::ReportResultsIfComplete, this));
115 Release(); // Balanced in Start(). 98 Release(); // Balanced in Start().
116 } 99 }
117 100
118 void WebstoreInstallHelper::OnJSONParseSucceeded( 101 void WebstoreInstallHelper::OnJSONParseSucceeded(
119 const base::ListValue& wrapper) { 102 scoped_ptr<base::Value> wrapper) {
120 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 103 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
121 manifest_parse_complete_ = true; 104 manifest_parse_complete_ = true;
122 const base::Value* value = NULL; 105 const base::ListValue* wrapper_list = NULL;
123 CHECK(wrapper.Get(0, &value)); 106 if (!wrapper->GetAsList(&wrapper_list)) {
124 if (value->IsType(base::Value::TYPE_DICTIONARY)) { 107 parse_error_ = Delegate::MANIFEST_ERROR;
125 parsed_manifest_.reset(
126 static_cast<const base::DictionaryValue*>(value)->DeepCopy());
127 } else { 108 } else {
128 parse_error_ = Delegate::MANIFEST_ERROR; 109 const base::Value* value = NULL;
110 CHECK(wrapper_list->Get(0, &value));
111 if (value->IsType(base::Value::TYPE_DICTIONARY)) {
112 parsed_manifest_.reset(
113 static_cast<const base::DictionaryValue*>(value)->DeepCopy());
114 } else {
115 parse_error_ = Delegate::MANIFEST_ERROR;
116 }
129 } 117 }
130 ReportResultsIfComplete(); 118 ReportResultsIfComplete();
131 } 119 }
132 120
133 void WebstoreInstallHelper::OnJSONParseFailed( 121 void WebstoreInstallHelper::OnJSONParseFailed(
134 const std::string& error_message) { 122 const std::string& error_message) {
135 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 123 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
136 manifest_parse_complete_ = true; 124 manifest_parse_complete_ = true;
137 error_ = error_message; 125 error_ = error_message;
138 parse_error_ = Delegate::MANIFEST_ERROR; 126 parse_error_ = Delegate::MANIFEST_ERROR;
139 ReportResultsIfComplete(); 127 ReportResultsIfComplete();
140 } 128 }
141 129
142 void WebstoreInstallHelper::ReportResultsIfComplete() { 130 void WebstoreInstallHelper::ReportResultsIfComplete() {
143 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 131 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
144 132
145 if (!icon_decode_complete_ || !manifest_parse_complete_) 133 if (!icon_decode_complete_ || !manifest_parse_complete_)
146 return; 134 return;
147 135
148 // The utility_host_ will take care of deleting itself after this call.
149 if (utility_host_.get()) {
150 utility_host_->EndBatchMode();
151 utility_host_.reset();
152 }
153
154 BrowserThread::PostTask( 136 BrowserThread::PostTask(
155 BrowserThread::UI, 137 BrowserThread::UI,
156 FROM_HERE, 138 FROM_HERE,
157 base::Bind(&WebstoreInstallHelper::ReportResultFromUIThread, this)); 139 base::Bind(&WebstoreInstallHelper::ReportResultFromUIThread, this));
158 } 140 }
159 141
160 void WebstoreInstallHelper::ReportResultFromUIThread() { 142 void WebstoreInstallHelper::ReportResultFromUIThread() {
161 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 143 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
162 if (error_.empty() && parsed_manifest_) 144 if (error_.empty() && parsed_manifest_)
163 delegate_->OnWebstoreParseSuccess(id_, icon_, parsed_manifest_.release()); 145 delegate_->OnWebstoreParseSuccess(id_, icon_, parsed_manifest_.release());
164 else 146 else
165 delegate_->OnWebstoreParseFailure(id_, parse_error_, error_); 147 delegate_->OnWebstoreParseFailure(id_, parse_error_, error_);
166 } 148 }
167 149
168 } // namespace extensions 150 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698