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

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

Issue 15342003: app_list: Add web store search. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase and address nits in #3 Created 7 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/extensions/webstore_data_fetcher.h ('k') | chrome/browser/safe_json_parser.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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_data_fetcher.h" 5 #include "chrome/browser/extensions/webstore_data_fetcher.h"
6 6
7 #include "base/bind.h"
7 #include "base/values.h" 8 #include "base/values.h"
8 #include "chrome/browser/extensions/webstore_data_fetcher_delegate.h" 9 #include "chrome/browser/extensions/webstore_data_fetcher_delegate.h"
9 #include "chrome/common/chrome_utility_messages.h" 10 #include "chrome/browser/safe_json_parser.h"
10 #include "chrome/common/extensions/extension_constants.h" 11 #include "chrome/common/extensions/extension_constants.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/utility_process_host.h"
13 #include "content/public/browser/utility_process_host_client.h"
14 #include "net/base/load_flags.h" 12 #include "net/base/load_flags.h"
15 #include "net/url_request/url_fetcher.h" 13 #include "net/url_request/url_fetcher.h"
16 #include "net/url_request/url_request_status.h" 14 #include "net/url_request/url_request_status.h"
17 15
18 using content::BrowserThread;
19 using content::UtilityProcessHost;
20 using content::UtilityProcessHostClient;
21
22 namespace { 16 namespace {
23 17
24 const char kInvalidWebstoreResponseError[] = "Invalid Chrome Web Store reponse"; 18 const char kInvalidWebstoreResponseError[] = "Invalid Chrome Web Store reponse";
25 19
26 } // namespace 20 } // namespace
27 21
28 namespace extensions { 22 namespace extensions {
29 23
30 class WebstoreDataFetcher::SafeWebstoreResponseParser
31 : public UtilityProcessHostClient {
32 public:
33 SafeWebstoreResponseParser(const base::WeakPtr<WebstoreDataFetcher>& client,
34 const std::string& webstore_data)
35 : client_(client),
36 webstore_data_(webstore_data) {}
37
38 void Start() {
39 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
40 BrowserThread::PostTask(
41 BrowserThread::IO,
42 FROM_HERE,
43 base::Bind(&SafeWebstoreResponseParser::StartWorkOnIOThread, this));
44 }
45
46 void StartWorkOnIOThread() {
47 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
48 UtilityProcessHost* host =
49 UtilityProcessHost::Create(
50 this, base::MessageLoopProxy::current());
51 host->EnableZygote();
52 host->Send(new ChromeUtilityMsg_ParseJSON(webstore_data_));
53 }
54
55 // Implementing pieces of the UtilityProcessHostClient interface.
56 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
57 bool handled = true;
58 IPC_BEGIN_MESSAGE_MAP(SafeWebstoreResponseParser, message)
59 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Succeeded,
60 OnJSONParseSucceeded)
61 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Failed,
62 OnJSONParseFailed)
63 IPC_MESSAGE_UNHANDLED(handled = false)
64 IPC_END_MESSAGE_MAP()
65 return handled;
66 }
67
68 void OnJSONParseSucceeded(const base::ListValue& wrapper) {
69 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
70 const Value* value = NULL;
71 CHECK(wrapper.Get(0, &value));
72 if (value->IsType(Value::TYPE_DICTIONARY)) {
73 parsed_webstore_data_.reset(
74 static_cast<const DictionaryValue*>(value)->DeepCopy());
75 } else {
76 error_ = kInvalidWebstoreResponseError;
77 }
78
79 ReportResults();
80 }
81
82 virtual void OnJSONParseFailed(const std::string& error_message) {
83 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
84 error_ = error_message;
85 ReportResults();
86 }
87
88 void ReportResults() {
89 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
90
91 BrowserThread::PostTask(
92 BrowserThread::UI,
93 FROM_HERE,
94 base::Bind(&SafeWebstoreResponseParser::ReportResultOnUIThread, this));
95 }
96
97 void ReportResultOnUIThread() {
98 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
99 if (!client_)
100 return;
101
102 if (error_.empty() && parsed_webstore_data_) {
103 client_->OnWebstoreResponseParseSuccess(parsed_webstore_data_.release());
104 } else {
105 client_->OnWebstoreResponseParseFailure(error_);
106 }
107 }
108
109 private:
110 virtual ~SafeWebstoreResponseParser() {}
111
112 base::WeakPtr<WebstoreDataFetcher> client_;
113
114 std::string webstore_data_;
115 std::string error_;
116 scoped_ptr<DictionaryValue> parsed_webstore_data_;
117
118 DISALLOW_COPY_AND_ASSIGN(SafeWebstoreResponseParser);
119 };
120
121 WebstoreDataFetcher::WebstoreDataFetcher( 24 WebstoreDataFetcher::WebstoreDataFetcher(
122 WebstoreDataFetcherDelegate* delegate, 25 WebstoreDataFetcherDelegate* delegate,
123 net::URLRequestContextGetter* request_context, 26 net::URLRequestContextGetter* request_context,
124 const GURL& referrer_url, 27 const GURL& referrer_url,
125 const std::string webstore_item_id) 28 const std::string webstore_item_id)
126 : delegate_(delegate), 29 : delegate_(delegate),
127 request_context_(request_context), 30 request_context_(request_context),
128 referrer_url_(referrer_url), 31 referrer_url_(referrer_url),
129 id_(webstore_item_id) { 32 id_(webstore_item_id) {
130 } 33 }
131 34
132 WebstoreDataFetcher::~WebstoreDataFetcher() {} 35 WebstoreDataFetcher::~WebstoreDataFetcher() {}
133 36
134 void WebstoreDataFetcher::Start() { 37 void WebstoreDataFetcher::Start() {
135 GURL webstore_data_url(extension_urls::GetWebstoreItemJsonDataURL(id_)); 38 GURL webstore_data_url(extension_urls::GetWebstoreItemJsonDataURL(id_));
136 39
137 webstore_data_url_fetcher_.reset(net::URLFetcher::Create( 40 webstore_data_url_fetcher_.reset(net::URLFetcher::Create(
138 webstore_data_url, net::URLFetcher::GET, this)); 41 webstore_data_url, net::URLFetcher::GET, this));
139 webstore_data_url_fetcher_->SetRequestContext(request_context_); 42 webstore_data_url_fetcher_->SetRequestContext(request_context_);
140 webstore_data_url_fetcher_->SetReferrer(referrer_url_.spec()); 43 webstore_data_url_fetcher_->SetReferrer(referrer_url_.spec());
141 webstore_data_url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | 44 webstore_data_url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
142 net::LOAD_DISABLE_CACHE); 45 net::LOAD_DISABLE_CACHE);
143 webstore_data_url_fetcher_->Start(); 46 webstore_data_url_fetcher_->Start();
144 } 47 }
145 48
146 void WebstoreDataFetcher::OnWebstoreResponseParseSuccess( 49 void WebstoreDataFetcher::OnJsonParseSuccess(
147 base::DictionaryValue* webstore_data) { 50 scoped_ptr<base::Value> parsed_json) {
148 delegate_->OnWebstoreResponseParseSuccess(webstore_data); 51 if (!parsed_json->IsType(base::Value::TYPE_DICTIONARY)) {
52 OnJsonParseFailure(kInvalidWebstoreResponseError);
53 return;
54 }
55
56 delegate_->OnWebstoreResponseParseSuccess(
57 static_cast<base::DictionaryValue*>(parsed_json.release()));
149 } 58 }
150 59
151 void WebstoreDataFetcher::OnWebstoreResponseParseFailure( 60 void WebstoreDataFetcher::OnJsonParseFailure(
152 const std::string& error) { 61 const std::string& error) {
153 delegate_->OnWebstoreResponseParseFailure(error); 62 delegate_->OnWebstoreResponseParseFailure(error);
154 } 63 }
155 64
156 void WebstoreDataFetcher::OnURLFetchComplete(const net::URLFetcher* source) { 65 void WebstoreDataFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
157 CHECK_EQ(webstore_data_url_fetcher_.get(), source); 66 CHECK_EQ(webstore_data_url_fetcher_.get(), source);
158 67
159 scoped_ptr<net::URLFetcher> fetcher(webstore_data_url_fetcher_.Pass()); 68 scoped_ptr<net::URLFetcher> fetcher(webstore_data_url_fetcher_.Pass());
160 69
161 if (!fetcher->GetStatus().is_success() || 70 if (!fetcher->GetStatus().is_success() ||
162 fetcher->GetResponseCode() != 200) { 71 fetcher->GetResponseCode() != 200) {
163 delegate_->OnWebstoreRequestFailure(); 72 delegate_->OnWebstoreRequestFailure();
164 return; 73 return;
165 } 74 }
166 75
167 std::string webstore_json_data; 76 std::string webstore_json_data;
168 fetcher->GetResponseAsString(&webstore_json_data); 77 fetcher->GetResponseAsString(&webstore_json_data);
169 78
170 scoped_refptr<SafeWebstoreResponseParser> parser = 79 scoped_refptr<SafeJsonParser> parser =
171 new SafeWebstoreResponseParser(AsWeakPtr(), webstore_json_data); 80 new SafeJsonParser(webstore_json_data,
172 // The parser will call us back via OnWebstoreResponseParseSucces or 81 base::Bind(&WebstoreDataFetcher::OnJsonParseSuccess,
173 // OnWebstoreResponseParseFailure. 82 AsWeakPtr()),
83 base::Bind(&WebstoreDataFetcher::OnJsonParseFailure,
84 AsWeakPtr()));
85 // The parser will call us back via one of the callbacks.
174 parser->Start(); 86 parser->Start();
175 } 87 }
176 88
177 } // namespace extensions 89 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/webstore_data_fetcher.h ('k') | chrome/browser/safe_json_parser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698