This source file includes following definitions.
- IsSimilarUrl
- weak_ptr_factory_
- DidNavigateMainFrame
- WebContentsDestroyed
- OnSignIn
- GetPageSetupParameters
- GetSwitchValueString16
- CallJavascriptFunction
- CancelPrintDataFile
- data_
- SendPrintData
- file_type_
- SetDialogDelegate
- CancelAnyRunningTask
- RegisterMessages
- Observe
- HandleShowDebugger
- ShowDebugger
- CreateCloudPrintDataSender
- HandleSendPrintData
- HandleSetPageParameters
- StoreDialogClientSize
- IsCloudPrintDialogUrl
- keep_alive_when_non_modal_
- keep_alive_when_non_modal_
- GetDialogWidthAndHeightFromPrefs
- Init
- GetDialogModalType
- GetDialogTitle
- GetDialogContentURL
- GetWebUIMessageHandlers
- GetDialogSize
- GetDialogArgs
- OnDialogClosed
- OnCloseContents
- ShouldShowDialogTitle
- HandleContextMenu
- CreateDialogImpl
- CreateDialogForFileImpl
- RegisterProfilePrefs
- CreatePrintDialogForFile
- CreateCloudPrintSigninTab
- CreatePrintDialogForBytes
- CreatePrintDialogFromCommandLine
#include "chrome/browser/printing/print_dialog_cloud.h"
#include "base/base64.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/json/json_reader.h"
#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/devtools/devtools_window.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/printing/cloud_print/cloud_print_url.h"
#include "chrome/browser/printing/print_dialog_cloud_internal.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/print_messages.h"
#include "chrome/common/url_constants.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_view.h"
#include "content/public/browser/web_ui.h"
#include "content/public/common/frame_navigate_params.h"
#include "webkit/common/webpreferences.h"
#if defined(USE_AURA)
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#endif
#if defined(OS_WIN)
#include "ui/base/win/foreground_helper.h"
#endif
using content::BrowserThread;
using content::NavigationController;
using content::NavigationEntry;
using content::RenderViewHost;
using content::WebContents;
using content::WebUIMessageHandler;
using ui::WebDialogDelegate;
namespace {
const int kDefaultWidth = 912;
const int kDefaultHeight = 633;
bool IsSimilarUrl(const GURL& url, const GURL& cloud_print_url) {
return url.host() == cloud_print_url.host() &&
StartsWithASCII(url.path(), cloud_print_url.path(), false) &&
url.scheme() == cloud_print_url.scheme();
}
class SignInObserver : public content::WebContentsObserver {
public:
SignInObserver(content::WebContents* web_contents,
GURL cloud_print_url,
const base::Closure& callback)
: WebContentsObserver(web_contents),
cloud_print_url_(cloud_print_url),
callback_(callback),
weak_ptr_factory_(this) {
}
private:
virtual void DidNavigateMainFrame(
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) OVERRIDE {
if (IsSimilarUrl(params.url, cloud_print_url_)) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&SignInObserver::OnSignIn,
weak_ptr_factory_.GetWeakPtr()));
}
}
virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE {
delete this;
}
void OnSignIn() {
callback_.Run();
if (web_contents())
web_contents()->Close();
}
GURL cloud_print_url_;
base::Closure callback_;
base::WeakPtrFactory<SignInObserver> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(SignInObserver);
};
}
namespace internal_cloud_print_helpers {
bool GetPageSetupParameters(const std::string& json,
PrintMsg_Print_Params& parameters) {
scoped_ptr<base::Value> parsed_value(base::JSONReader::Read(json));
DLOG_IF(ERROR, (!parsed_value.get() ||
!parsed_value->IsType(base::Value::TYPE_DICTIONARY)))
<< "PageSetup call didn't have expected contents";
if (!parsed_value.get() ||
!parsed_value->IsType(base::Value::TYPE_DICTIONARY)) {
return false;
}
bool result = true;
base::DictionaryValue* params =
static_cast<base::DictionaryValue*>(parsed_value.get());
result &= params->GetDouble("dpi", ¶meters.dpi);
result &= params->GetDouble("min_shrink", ¶meters.min_shrink);
result &= params->GetDouble("max_shrink", ¶meters.max_shrink);
result &= params->GetBoolean("selection_only", ¶meters.selection_only);
return result;
}
base::string16 GetSwitchValueString16(const CommandLine& command_line,
const char* switchName) {
#if defined(OS_WIN)
return command_line.GetSwitchValueNative(switchName);
#elif defined(OS_POSIX)
CommandLine::StringType native_switch_val;
native_switch_val = command_line.GetSwitchValueASCII(switchName);
return base::ASCIIToUTF16(native_switch_val);
#endif
}
void CloudPrintDataSenderHelper::CallJavascriptFunction(
const std::string& function_name,
const base::Value& arg1,
const base::Value& arg2) {
web_ui_->CallJavascriptFunction(function_name, arg1, arg2);
}
void CloudPrintDataSender::CancelPrintDataFile() {
base::AutoLock lock(lock_);
helper_ = NULL;
}
CloudPrintDataSender::CloudPrintDataSender(
CloudPrintDataSenderHelper* helper,
const base::string16& print_job_title,
const base::string16& print_ticket,
const std::string& file_type,
const base::RefCountedMemory* data)
: helper_(helper),
print_job_title_(print_job_title),
print_ticket_(print_ticket),
file_type_(file_type),
data_(data) {
}
CloudPrintDataSender::~CloudPrintDataSender() {}
void CloudPrintDataSender::SendPrintData() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!data_.get() || !data_->size())
return;
std::string base64_data;
base::Base64Encode(
base::StringPiece(data_->front_as<char>(), data_->size()),
&base64_data);
std::string header("data:");
header.append(file_type_);
header.append(";base64,");
base64_data.insert(0, header);
base::AutoLock lock(lock_);
if (helper_) {
base::StringValue title(print_job_title_);
base::StringValue ticket(print_ticket_);
helper_->CallJavascriptFunction(
"printApp._printDataUrl", base::StringValue(base64_data), title);
}
}
CloudPrintFlowHandler::CloudPrintFlowHandler(
const base::RefCountedMemory* data,
const base::string16& print_job_title,
const base::string16& print_ticket,
const std::string& file_type)
: dialog_delegate_(NULL),
data_(data),
print_job_title_(print_job_title),
print_ticket_(print_ticket),
file_type_(file_type) {
}
CloudPrintFlowHandler::~CloudPrintFlowHandler() {
CancelAnyRunningTask();
}
void CloudPrintFlowHandler::SetDialogDelegate(
CloudPrintWebDialogDelegate* delegate) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
CancelAnyRunningTask();
dialog_delegate_ = delegate;
}
void CloudPrintFlowHandler::CancelAnyRunningTask() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (print_data_sender_.get()) {
print_data_sender_->CancelPrintDataFile();
print_data_sender_ = NULL;
}
}
void CloudPrintFlowHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("ShowDebugger",
base::Bind(&CloudPrintFlowHandler::HandleShowDebugger,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("SendPrintData",
base::Bind(&CloudPrintFlowHandler::HandleSendPrintData,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("SetPageParameters",
base::Bind(&CloudPrintFlowHandler::HandleSetPageParameters,
base::Unretained(this)));
NavigationController* controller =
&web_ui()->GetWebContents()->GetController();
NavigationEntry* pending_entry = controller->GetPendingEntry();
if (pending_entry) {
Profile* profile = Profile::FromWebUI(web_ui());
pending_entry->SetURL(
CloudPrintURL(profile).GetCloudPrintServiceDialogURL());
}
registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(controller));
registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<NavigationController>(controller));
}
void CloudPrintFlowHandler::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case content::NOTIFICATION_LOAD_STOP: {
GURL url = web_ui()->GetWebContents()->GetURL();
if (IsCloudPrintDialogUrl(url)) {
RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost();
if (rvh) {
WebPreferences webkit_prefs = rvh->GetWebkitPreferences();
webkit_prefs.allow_scripts_to_close_windows = true;
rvh->UpdateWebkitPreferences(webkit_prefs);
} else {
NOTREACHED();
}
HandleSendPrintData(NULL);
}
break;
}
}
}
void CloudPrintFlowHandler::HandleShowDebugger(const base::ListValue* args) {
ShowDebugger();
}
void CloudPrintFlowHandler::ShowDebugger() {
if (web_ui()) {
RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost();
if (rvh)
DevToolsWindow::OpenDevToolsWindow(rvh);
}
}
scoped_refptr<CloudPrintDataSender>
CloudPrintFlowHandler::CreateCloudPrintDataSender() {
DCHECK(web_ui());
print_data_helper_.reset(new CloudPrintDataSenderHelper(web_ui()));
scoped_refptr<CloudPrintDataSender> sender(
new CloudPrintDataSender(print_data_helper_.get(),
print_job_title_,
print_ticket_,
file_type_,
data_.get()));
return sender;
}
void CloudPrintFlowHandler::HandleSendPrintData(const base::ListValue* args) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
CancelAnyRunningTask();
if (web_ui()) {
print_data_sender_ = CreateCloudPrintDataSender();
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&CloudPrintDataSender::SendPrintData, print_data_sender_));
}
}
void CloudPrintFlowHandler::HandleSetPageParameters(
const base::ListValue* args) {
std::string json;
bool ret = args->GetString(0, &json);
if (!ret || json.empty()) {
NOTREACHED() << "Empty json string";
return;
}
const int kDPI = 72;
const int kWidth = static_cast<int>((8.5-0.25-0.25)*kDPI);
const int kHeight = static_cast<int>((11-0.25-0.56)*kDPI);
const double kMinPageShrink = 1.25;
const double kMaxPageShrink = 2.0;
PrintMsg_Print_Params default_settings;
default_settings.content_size = gfx::Size(kWidth, kHeight);
default_settings.printable_area = gfx::Rect(0, 0, kWidth, kHeight);
default_settings.dpi = kDPI;
default_settings.min_shrink = kMinPageShrink;
default_settings.max_shrink = kMaxPageShrink;
default_settings.desired_dpi = kDPI;
default_settings.document_cookie = 0;
default_settings.selection_only = false;
default_settings.preview_request_id = 0;
default_settings.is_first_request = true;
default_settings.print_to_pdf = false;
if (!GetPageSetupParameters(json, default_settings)) {
NOTREACHED();
return;
}
}
void CloudPrintFlowHandler::StoreDialogClientSize() const {
if (web_ui() && web_ui()->GetWebContents() &&
web_ui()->GetWebContents()->GetView()) {
gfx::Size size = web_ui()->GetWebContents()->GetView()->GetContainerSize();
Profile* profile = Profile::FromWebUI(web_ui());
profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogWidth,
size.width());
profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogHeight,
size.height());
}
}
bool CloudPrintFlowHandler::IsCloudPrintDialogUrl(const GURL& url) {
GURL cloud_print_url =
CloudPrintURL(Profile::FromWebUI(web_ui())).GetCloudPrintServiceURL();
return IsSimilarUrl(url, cloud_print_url);
}
CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate(
content::BrowserContext* browser_context,
gfx::NativeWindow modal_parent,
const base::RefCountedMemory* data,
const std::string& json_arguments,
const base::string16& print_job_title,
const base::string16& print_ticket,
const std::string& file_type)
: flow_handler_(
new CloudPrintFlowHandler(data, print_job_title, print_ticket,
file_type)),
modal_parent_(modal_parent),
owns_flow_handler_(true),
keep_alive_when_non_modal_(true) {
Init(browser_context, json_arguments);
}
CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate(
CloudPrintFlowHandler* flow_handler,
const std::string& json_arguments)
: flow_handler_(flow_handler),
modal_parent_(NULL),
owns_flow_handler_(true),
keep_alive_when_non_modal_(false) {
Init(NULL, json_arguments);
}
void GetDialogWidthAndHeightFromPrefs(content::BrowserContext* browser_context,
int* width,
int* height) {
if (!browser_context) {
*width = kDefaultWidth;
*height = kDefaultHeight;
return;
}
PrefService* prefs = Profile::FromBrowserContext(browser_context)->GetPrefs();
*width = prefs->GetInteger(prefs::kCloudPrintDialogWidth);
*height = prefs->GetInteger(prefs::kCloudPrintDialogHeight);
}
void CloudPrintWebDialogDelegate::Init(content::BrowserContext* browser_context,
const std::string& json_arguments) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
params_.url = GURL(chrome::kChromeUICloudPrintResourcesURL);
GetDialogWidthAndHeightFromPrefs(browser_context,
¶ms_.width,
¶ms_.height);
params_.json_input = json_arguments;
flow_handler_->SetDialogDelegate(this);
if (!modal_parent_ && keep_alive_when_non_modal_)
chrome::IncrementKeepAliveCount();
}
CloudPrintWebDialogDelegate::~CloudPrintWebDialogDelegate() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
flow_handler_->SetDialogDelegate(NULL);
if (owns_flow_handler_) {
delete flow_handler_;
}
}
ui::ModalType CloudPrintWebDialogDelegate::GetDialogModalType() const {
return modal_parent_ ? ui::MODAL_TYPE_WINDOW : ui::MODAL_TYPE_NONE;
}
base::string16 CloudPrintWebDialogDelegate::GetDialogTitle() const {
return base::string16();
}
GURL CloudPrintWebDialogDelegate::GetDialogContentURL() const {
return params_.url;
}
void CloudPrintWebDialogDelegate::GetWebUIMessageHandlers(
std::vector<WebUIMessageHandler*>* handlers) const {
handlers->push_back(flow_handler_);
owns_flow_handler_ = false;
}
void CloudPrintWebDialogDelegate::GetDialogSize(gfx::Size* size) const {
size->set_width(params_.width);
size->set_height(params_.height);
}
std::string CloudPrintWebDialogDelegate::GetDialogArgs() const {
return params_.json_input;
}
void CloudPrintWebDialogDelegate::OnDialogClosed(
const std::string& json_retval) {
flow_handler_->StoreDialogClientSize();
if (!modal_parent_ && keep_alive_when_non_modal_) {
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(&chrome::DecrementKeepAliveCount));
}
delete this;
}
void CloudPrintWebDialogDelegate::OnCloseContents(WebContents* source,
bool* out_close_dialog) {
if (out_close_dialog)
*out_close_dialog = true;
}
bool CloudPrintWebDialogDelegate::ShouldShowDialogTitle() const {
return false;
}
bool CloudPrintWebDialogDelegate::HandleContextMenu(
const content::ContextMenuParams& params) {
return true;
}
void CreateDialogImpl(content::BrowserContext* browser_context,
gfx::NativeWindow modal_parent,
const base::RefCountedMemory* data,
const base::string16& print_job_title,
const base::string16& print_ticket,
const std::string& file_type) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
WebDialogDelegate* dialog_delegate =
new internal_cloud_print_helpers::CloudPrintWebDialogDelegate(
browser_context, modal_parent, data, std::string(), print_job_title,
print_ticket, file_type);
#if defined(OS_WIN)
gfx::NativeWindow window =
#endif
chrome::ShowWebDialog(modal_parent,
Profile::FromBrowserContext(browser_context),
dialog_delegate);
#if defined(OS_WIN)
if (window) {
HWND dialog_handle;
#if defined(USE_AURA)
dialog_handle = window->GetHost()->GetAcceleratedWidget();
#else
dialog_handle = window;
#endif
if (::GetForegroundWindow() != dialog_handle) {
ui::ForegroundHelper::SetForeground(dialog_handle);
}
}
#endif
}
void CreateDialogForFileImpl(content::BrowserContext* browser_context,
gfx::NativeWindow modal_parent,
const base::FilePath& path_to_file,
const base::string16& print_job_title,
const base::string16& print_ticket,
const std::string& file_type) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
scoped_refptr<base::RefCountedMemory> data;
int64 file_size = 0;
if (base::GetFileSize(path_to_file, &file_size) && file_size != 0) {
std::string file_data;
if (file_size < kuint32max) {
file_data.reserve(static_cast<unsigned int>(file_size));
} else {
DLOG(WARNING) << " print data file too large to reserve space";
}
if (base::ReadFileToString(path_to_file, &file_data)) {
data = base::RefCountedString::TakeString(&file_data);
}
}
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&print_dialog_cloud::CreatePrintDialogForBytes,
browser_context, modal_parent, data, print_job_title,
print_ticket, file_type));
base::DeleteFile(path_to_file, false);
}
}
namespace print_dialog_cloud {
void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterIntegerPref(
prefs::kCloudPrintDialogWidth,
kDefaultWidth,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
registry->RegisterIntegerPref(
prefs::kCloudPrintDialogHeight,
kDefaultHeight,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}
void CreatePrintDialogForFile(content::BrowserContext* browser_context,
gfx::NativeWindow modal_parent,
const base::FilePath& path_to_file,
const base::string16& print_job_title,
const base::string16& print_ticket,
const std::string& file_type) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) ||
BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(&internal_cloud_print_helpers::CreateDialogForFileImpl,
browser_context, modal_parent, path_to_file, print_job_title,
print_ticket, file_type));
}
void CreateCloudPrintSigninTab(Browser* browser,
bool add_account,
const base::Closure& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
CloudPrintURL cp_url(browser->profile());
content::WebContents* web_contents =
browser->OpenURL(
content::OpenURLParams(add_account ?
cp_url.GetCloudPrintAddAccountURL() :
cp_url.GetCloudPrintSigninURL(),
content::Referrer(), NEW_FOREGROUND_TAB,
content::PAGE_TRANSITION_AUTO_BOOKMARK,
false));
new SignInObserver(web_contents, cp_url.GetCloudPrintServiceURL(), callback);
}
void CreatePrintDialogForBytes(content::BrowserContext* browser_context,
gfx::NativeWindow modal_parent,
const base::RefCountedMemory* data,
const base::string16& print_job_title,
const base::string16& print_ticket,
const std::string& file_type) {
internal_cloud_print_helpers::CreateDialogImpl(browser_context, modal_parent,
data, print_job_title,
print_ticket, file_type);
}
bool CreatePrintDialogFromCommandLine(Profile* profile,
const CommandLine& command_line) {
DCHECK(command_line.HasSwitch(switches::kCloudPrintFile));
if (!command_line.GetSwitchValuePath(switches::kCloudPrintFile).empty()) {
base::FilePath cloud_print_file;
cloud_print_file =
command_line.GetSwitchValuePath(switches::kCloudPrintFile);
if (!cloud_print_file.empty()) {
base::string16 print_job_title;
base::string16 print_job_print_ticket;
if (command_line.HasSwitch(switches::kCloudPrintJobTitle)) {
print_job_title =
internal_cloud_print_helpers::GetSwitchValueString16(
command_line, switches::kCloudPrintJobTitle);
}
if (command_line.HasSwitch(switches::kCloudPrintPrintTicket)) {
print_job_print_ticket =
internal_cloud_print_helpers::GetSwitchValueString16(
command_line, switches::kCloudPrintPrintTicket);
}
std::string file_type = "application/pdf";
if (command_line.HasSwitch(switches::kCloudPrintFileType)) {
file_type = command_line.GetSwitchValueASCII(
switches::kCloudPrintFileType);
}
print_dialog_cloud::CreatePrintDialogForFile(profile, NULL,
cloud_print_file, print_job_title, print_job_print_ticket, file_type);
return true;
}
}
return false;
}
}