Commit 84739024 authored by mihaip@chromium.org's avatar mihaip@chromium.org

Move SafeBeginInstallHelper to be a top-level class (WebstoreInstallHelper).

R=asargent@chromium.org

Review URL: http://codereview.chromium.org/7741019

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@98272 0039d316-1c4b-4281-b951-d872f2087c98
parent e88b4dbe
......@@ -11,6 +11,7 @@
#include "chrome/browser/browser_signin.h"
#include "chrome/browser/extensions/extension_function.h"
#include "chrome/browser/extensions/extension_install_ui.h"
#include "chrome/browser/extensions/webstore_install_helper.h"
#include "chrome/common/net/gaia/google_service_auth_error.h"
#include "content/common/notification_observer.h"
#include "content/common/notification_registrar.h"
......@@ -40,8 +41,10 @@ class BeginInstallFunction : public SyncExtensionFunction {
DECLARE_EXTENSION_FUNCTION_NAME("webstorePrivate.beginInstall");
};
class BeginInstallWithManifestFunction : public AsyncExtensionFunction,
public ExtensionInstallUI::Delegate {
class BeginInstallWithManifestFunction
: public AsyncExtensionFunction,
public ExtensionInstallUI::Delegate,
public WebstoreInstallHelper::Delegate {
public:
BeginInstallWithManifestFunction();
......@@ -85,14 +88,13 @@ class BeginInstallWithManifestFunction : public AsyncExtensionFunction,
// as if the dialog choice was to proceed or abort.
static void SetAutoConfirmForTests(bool should_proceed);
// Called when we've successfully parsed the manifest and decoded the icon in
// the utility process. Ownership of parsed_manifest is transferred.
void OnParseSuccess(const SkBitmap& icon,
base::DictionaryValue* parsed_manifest);
// Called to indicate a parse failure. The |result_code| parameter should
// indicate whether the problem was with the manifest or icon.
void OnParseFailure(ResultCode result_code, const std::string& error_message);
// Implementing WebstoreInstallHelper::Delegate interface.
virtual void OnWebstoreParseSuccess(
const SkBitmap& icon,
base::DictionaryValue* parsed_manifest) OVERRIDE;
virtual void OnWebstoreParseFailure(
InstallHelperResultCode result_code,
const std::string& error_message) OVERRIDE;
// Implementing ExtensionInstallUI::Delegate interface.
virtual void InstallUIProceed() OVERRIDE;
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/webstore_install_helper.h"
#include <string>
#include "base/task.h"
#include "base/values.h"
#include "chrome/common/chrome_utility_messages.h"
#include "content/browser/browser_thread.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_status.h"
namespace {
const char kImageDecodeError[] = "Image decode failed";
} // namespace
WebstoreInstallHelper::WebstoreInstallHelper(
Delegate* delegate,
const std::string& manifest,
const std::string& icon_data,
const GURL& icon_url,
net::URLRequestContextGetter* context_getter)
: delegate_(delegate),
manifest_(manifest),
icon_base64_data_(icon_data),
icon_url_(icon_url),
context_getter_(context_getter),
utility_host_(NULL),
icon_decode_complete_(false),
manifest_parse_complete_(false),
parse_error_(Delegate::UNKNOWN_ERROR) {}
WebstoreInstallHelper::~WebstoreInstallHelper() {}
void WebstoreInstallHelper::Start() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
CHECK(icon_base64_data_.empty() || icon_url_.is_empty());
if (icon_base64_data_.empty() && icon_url_.is_empty())
icon_decode_complete_ = true;
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
NewRunnableMethod(this,
&WebstoreInstallHelper::StartWorkOnIOThread));
if (!icon_url_.is_empty()) {
CHECK(context_getter_);
url_fetcher_.reset(new URLFetcher(icon_url_, URLFetcher::GET, this));
url_fetcher_->set_request_context(context_getter_);
url_fetcher_->Start();
// We'll get called back in OnURLFetchComplete.
}
}
void WebstoreInstallHelper::StartWorkOnIOThread() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
utility_host_ = new UtilityProcessHost(this, BrowserThread::IO);
utility_host_->StartBatchMode();
if (!icon_base64_data_.empty())
utility_host_->Send(
new ChromeUtilityMsg_DecodeImageBase64(icon_base64_data_));
utility_host_->Send(new ChromeUtilityMsg_ParseJSON(manifest_));
}
void WebstoreInstallHelper::OnURLFetchComplete(const URLFetcher* source) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
CHECK(source == url_fetcher_.get());
if (source->status().status() != net::URLRequestStatus::SUCCESS ||
source->response_code() != 200) {
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
NewRunnableMethod(this,
&WebstoreInstallHelper::OnDecodeImageFailed));
} else {
std::string response_data;
source->GetResponseAsString(&response_data);
fetched_icon_data_.insert(fetched_icon_data_.begin(),
response_data.begin(),
response_data.end());
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
NewRunnableMethod(this,
&WebstoreInstallHelper::StartFetchedImageDecode));
}
url_fetcher_.reset();
}
void WebstoreInstallHelper::StartFetchedImageDecode() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
CHECK(utility_host_);
utility_host_->Send(new ChromeUtilityMsg_DecodeImage(fetched_icon_data_));
}
bool WebstoreInstallHelper::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(WebstoreInstallHelper, message)
IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded,
OnDecodeImageSucceeded)
IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed,
OnDecodeImageFailed)
IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Succeeded,
OnJSONParseSucceeded)
IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Failed,
OnJSONParseFailed)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void WebstoreInstallHelper::OnDecodeImageSucceeded(
const SkBitmap& decoded_image) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
icon_ = decoded_image;
icon_decode_complete_ = true;
ReportResultsIfComplete();
}
void WebstoreInstallHelper::OnDecodeImageFailed() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
icon_decode_complete_ = true;
error_ = kImageDecodeError;
parse_error_ = Delegate::ICON_ERROR;
ReportResultsIfComplete();
}
void WebstoreInstallHelper::OnJSONParseSucceeded(const ListValue& wrapper) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
manifest_parse_complete_ = true;
Value* value = NULL;
CHECK(wrapper.Get(0, &value));
if (value->IsType(Value::TYPE_DICTIONARY)) {
parsed_manifest_.reset(
static_cast<DictionaryValue*>(value)->DeepCopy());
} else {
parse_error_ = Delegate::MANIFEST_ERROR;
}
ReportResultsIfComplete();
}
void WebstoreInstallHelper::OnJSONParseFailed(
const std::string& error_message) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
manifest_parse_complete_ = true;
error_ = error_message;
parse_error_ = Delegate::MANIFEST_ERROR;
ReportResultsIfComplete();
}
void WebstoreInstallHelper::ReportResultsIfComplete() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!icon_decode_complete_ || !manifest_parse_complete_)
return;
// The utility_host_ will take care of deleting itself after this call.
utility_host_->EndBatchMode();
utility_host_ = NULL;
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
NewRunnableMethod(this,
&WebstoreInstallHelper::ReportResultFromUIThread));
}
void WebstoreInstallHelper::ReportResultFromUIThread() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (error_.empty() && parsed_manifest_.get())
delegate_->OnWebstoreParseSuccess(icon_, parsed_manifest_.release());
else
delegate_->OnWebstoreParseFailure(parse_error_, error_);
}
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALL_HELPER_H_
#define CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALL_HELPER_H_
#pragma once
#include "content/browser/utility_process_host.h"
#include "content/common/url_fetcher.h"
#include "googleurl/src/gurl.h"
#include "third_party/skia/include/core/SkBitmap.h"
class UtilityProcessHost;
class SkBitmap;
namespace base {
class DictionaryValue;
class ListValue;
}
// This is a class to help dealing with webstore-provided data. It manages
// sending work to the utility process for parsing manifests and
// fetching/decoding icon data. Clients must implement the
// WebstoreInstallHelper::Delegate interface to receive the parsed data.
class WebstoreInstallHelper : public UtilityProcessHost::Client,
public URLFetcher::Delegate {
public:
class Delegate {
public:
enum InstallHelperResultCode {
UNKNOWN_ERROR,
ICON_ERROR,
MANIFEST_ERROR
};
// Called when we've successfully parsed the manifest and decoded the icon
// in the utility process. Ownership of parsed_manifest is transferred.
virtual void OnWebstoreParseSuccess(
const SkBitmap& icon,
base::DictionaryValue* parsed_manifest) = 0;
// Called to indicate a parse failure. The |result_code| parameter should
// indicate whether the problem was with the manifest or icon.
virtual void OnWebstoreParseFailure(
InstallHelperResultCode result_code,
const std::string& error_message) = 0;
};
// Only one of |icon_data| (based64-encoded icon data) or |icon_url| can be
// specified, but it is legal for both to be empty.
WebstoreInstallHelper(Delegate* delegate,
const std::string& manifest,
const std::string& icon_data,
const GURL& icon_url,
net::URLRequestContextGetter* context_getter);
void Start();
private:
~WebstoreInstallHelper();
void StartWorkOnIOThread();
void StartFetchedImageDecode();
void ReportResultsIfComplete();
void ReportResultFromUIThread();
// Implementing the URLFetcher::Delegate interface.
virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
// Implementing pieces of the UtilityProcessHost::Client interface.
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
// Message handlers.
void OnDecodeImageSucceeded(const SkBitmap& decoded_image);
void OnDecodeImageFailed();
void OnJSONParseSucceeded(const base::ListValue& wrapper);
void OnJSONParseFailed(const std::string& error_message);
// The client who we'll report results back to.
Delegate* delegate_;
// The manifest to parse.
std::string manifest_;
// Only one of these should be non-empty. If |icon_base64_data_| is non-emtpy,
// it's a base64-encoded string that needs to be parsed into an SkBitmap. If
// |icon_url_| is non-empty, it needs to be fetched and decoded into an
// SkBitmap.
std::string icon_base64_data_;
GURL icon_url_;
std::vector<unsigned char> fetched_icon_data_;
// For fetching the icon, if needed.
scoped_ptr<URLFetcher> url_fetcher_;
net::URLRequestContextGetter* context_getter_; // Only usable on UI thread.
UtilityProcessHost* utility_host_;
// Flags for whether we're done doing icon decoding and manifest parsing.
bool icon_decode_complete_;
bool manifest_parse_complete_;
// The results of succesful decoding/parsing.
SkBitmap icon_;
scoped_ptr<base::DictionaryValue> parsed_manifest_;
// A details string for keeping track of any errors.
std::string error_;
// A code to distinguish between an error with the icon, and an error with the
// manifest.
Delegate::InstallHelperResultCode parse_error_;
};
#endif // CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALL_HELPER_H_
......@@ -1152,6 +1152,8 @@
'browser/extensions/user_script_listener.h',
'browser/extensions/user_script_master.cc',
'browser/extensions/user_script_master.h',
'browser/extensions/webstore_install_helper.cc',
'browser/extensions/webstore_install_helper.h',
'browser/external_protocol/external_protocol_handler.cc',
'browser/external_protocol/external_protocol_handler.h',
'browser/external_protocol/external_protocol_observer.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment