1 // Copyright 2017 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.
4 
5 #include "chrome/browser/devtools/protocol/page_handler.h"
6 
7 #include "chrome/browser/installable/installable_manager.h"
8 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h"
9 #include "ui/gfx/image/image.h"
10 
PageHandler(content::WebContents * web_contents,protocol::UberDispatcher * dispatcher)11 PageHandler::PageHandler(content::WebContents* web_contents,
12                          protocol::UberDispatcher* dispatcher)
13     : content::WebContentsObserver(web_contents) {
14   DCHECK(web_contents);
15   protocol::Page::Dispatcher::wire(dispatcher, this);
16 }
17 
~PageHandler()18 PageHandler::~PageHandler() {
19   ToggleAdBlocking(false /* enabled */);
20 }
21 
ToggleAdBlocking(bool enabled)22 void PageHandler::ToggleAdBlocking(bool enabled) {
23   if (!web_contents())
24     return;
25   if (auto* client =
26           ChromeSubresourceFilterClient::FromWebContents(web_contents())) {
27     client->ToggleForceActivationInCurrentWebContents(enabled);
28   }
29 }
30 
Enable()31 protocol::Response PageHandler::Enable() {
32   enabled_ = true;
33   // Do not mark the command as handled. Let it fall through instead, so that
34   // the handler in content gets a chance to process the command.
35   return protocol::Response::FallThrough();
36 }
37 
Disable()38 protocol::Response PageHandler::Disable() {
39   enabled_ = false;
40   ToggleAdBlocking(false /* enable */);
41   // Do not mark the command as handled. Let it fall through instead, so that
42   // the handler in content gets a chance to process the command.
43   return protocol::Response::FallThrough();
44 }
45 
SetAdBlockingEnabled(bool enabled)46 protocol::Response PageHandler::SetAdBlockingEnabled(bool enabled) {
47   if (!enabled_)
48     return protocol::Response::ServerError("Page domain is disabled.");
49   ToggleAdBlocking(enabled);
50   return protocol::Response::Success();
51 }
52 
GetInstallabilityErrors(std::unique_ptr<GetInstallabilityErrorsCallback> callback)53 void PageHandler::GetInstallabilityErrors(
54     std::unique_ptr<GetInstallabilityErrorsCallback> callback) {
55   auto errors = std::make_unique<protocol::Array<std::string>>();
56   InstallableManager* manager =
57       web_contents() ? InstallableManager::FromWebContents(web_contents())
58                      : nullptr;
59   if (!manager) {
60     callback->sendFailure(
61         protocol::Response::ServerError("Unable to fetch errors for target"));
62     return;
63   }
64   manager->GetAllErrors(base::BindOnce(&PageHandler::GotInstallabilityErrors,
65                                        std::move(callback)));
66 }
67 
68 // static
GotInstallabilityErrors(std::unique_ptr<GetInstallabilityErrorsCallback> callback,std::vector<content::InstallabilityError> installability_errors)69 void PageHandler::GotInstallabilityErrors(
70     std::unique_ptr<GetInstallabilityErrorsCallback> callback,
71     std::vector<content::InstallabilityError> installability_errors) {
72   auto result_installability_errors =
73       std::make_unique<protocol::Array<protocol::Page::InstallabilityError>>();
74   for (const auto& installability_error : installability_errors) {
75     auto installability_error_arguments = std::make_unique<
76         protocol::Array<protocol::Page::InstallabilityErrorArgument>>();
77     for (const auto& error_argument :
78          installability_error.installability_error_arguments) {
79       installability_error_arguments->emplace_back(
80           protocol::Page::InstallabilityErrorArgument::Create()
81               .SetName(error_argument.name)
82               .SetValue(error_argument.value)
83               .Build());
84     }
85     result_installability_errors->emplace_back(
86         protocol::Page::InstallabilityError::Create()
87             .SetErrorId(installability_error.error_id)
88             .SetErrorArguments(std::move(installability_error_arguments))
89             .Build());
90   }
91   callback->sendSuccess(
92       std::move(result_installability_errors));
93 }
94 
GetManifestIcons(std::unique_ptr<GetManifestIconsCallback> callback)95 void PageHandler::GetManifestIcons(
96     std::unique_ptr<GetManifestIconsCallback> callback) {
97   InstallableManager* manager =
98       web_contents() ? InstallableManager::FromWebContents(web_contents())
99                      : nullptr;
100 
101   if (!manager) {
102     callback->sendFailure(
103         protocol::Response::ServerError("Unable to fetch icons for target"));
104     return;
105   }
106 
107   manager->GetPrimaryIcon(
108       base::BindOnce(&PageHandler::GotManifestIcons, std::move(callback)));
109 }
110 
GotManifestIcons(std::unique_ptr<GetManifestIconsCallback> callback,const SkBitmap * primary_icon)111 void PageHandler::GotManifestIcons(
112     std::unique_ptr<GetManifestIconsCallback> callback,
113     const SkBitmap* primary_icon) {
114   protocol::Maybe<protocol::Binary> primaryIconAsBinary;
115 
116   if (primary_icon && !primary_icon->empty()) {
117     primaryIconAsBinary = std::move(protocol::Binary::fromRefCounted(
118         gfx::Image::CreateFrom1xBitmap(*primary_icon).As1xPNGBytes()));
119   }
120 
121   callback->sendSuccess(std::move(primaryIconAsBinary));
122 }
123