Skip to content

Commit 596323c

Browse files
authored
New print interface (#75)
* Add Print Handler Implementation * Fix merge error * Remove queued_agent.cc * Add missing std::endl * Simplify offset bit manipulations * Address Roger's comments * Add handle closing * Add Print Handler Implementation * Refactor to use scoped object to save files * Add missing CMake * Add Print Handler Implementation * Refactor * Refactors * Refactor * Refactor * Move scoped_print_handle_taken_ to base class
1 parent c2ba682 commit 596323c

27 files changed

+387
-25
lines changed

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ if(WIN)
7777
./agent/src/agent_win.h
7878
./agent/src/event_win.cc
7979
./agent/src/event_win.h
80+
./agent/src/scoped_print_handle_win.cc
81+
./agent/src/scoped_print_handle_win.h
8082
./common/utils_win.cc
8183
./common/utils_win.h
8284
)
@@ -90,6 +92,8 @@ elseif(MAC)
9092
./agent/src/agent_mac.h
9193
./agent/src/event_mac.cc
9294
./agent/src/event_mac.h
95+
./agent/src/scoped_print_handle_mac.cc
96+
./agent/src/scoped_print_handle_mac.h
9397
)
9498
set(PLATFORM_TEST_CODE
9599
./agent/src/event_mac_unittest.cc
@@ -100,6 +104,8 @@ elseif(LINUX)
100104
./agent/src/agent_posix.h
101105
./agent/src/event_posix.cc
102106
./agent/src/event_posix.h
107+
./agent/src/scoped_print_handle_posix.cc
108+
./agent/src/scoped_print_handle_posix.h
103109
)
104110
set(PLATFORM_TEST_CODE
105111
./agent/src/event_posix_unittest.cc
@@ -164,6 +170,8 @@ add_library(cac_agent
164170
./agent/src/agent_base.h
165171
./agent/src/event_base.cc
166172
./agent/src/event_base.h
173+
./agent/src/scoped_print_handle_base.cc
174+
./agent/src/scoped_print_handle_base.h
167175
${PLATFORM_AGENT_CODE}
168176
${PROJECT_BINARY_DIR}/gen/content_analysis/sdk/analysis.pb.cc
169177
)

agent/include/content_analysis/sdk/analysis_agent.h

+23
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,35 @@ class ContentAnalysisEvent {
9696
// for debugging.
9797
virtual std::string DebugString() const = 0;
9898

99+
// Helper class to handle the lifetime and access of print data.
100+
class ScopedPrintHandle {
101+
public:
102+
virtual ~ScopedPrintHandle() = default;
103+
virtual const char* data() = 0;
104+
virtual size_t size() = 0;
105+
106+
protected:
107+
ScopedPrintHandle() = default;
108+
109+
ScopedPrintHandle(const ScopedPrintHandle&) = delete;
110+
ScopedPrintHandle& operator=(const ScopedPrintHandle&) = delete;
111+
112+
ScopedPrintHandle(ScopedPrintHandle&&) = default;
113+
ScopedPrintHandle& operator=(ScopedPrintHandle&&) = default;
114+
};
115+
116+
// Returns a `ScopedPrintHandle` initialized from the event's print data
117+
// if it exists. This only returns a non-null value at most once to avoid
118+
// having duplicate handles initialized.
119+
virtual std::unique_ptr<ScopedPrintHandle> TakeScopedPrintHandle() = 0;
120+
99121
protected:
100122
ContentAnalysisEvent() = default;
101123
ContentAnalysisEvent(const ContentAnalysisEvent& rhs) = delete;
102124
ContentAnalysisEvent(ContentAnalysisEvent&& rhs) = delete;
103125
ContentAnalysisEvent& operator=(const ContentAnalysisEvent& rhs) = delete;
104126
ContentAnalysisEvent& operator=(ContentAnalysisEvent&& rhs) = delete;
127+
105128
};
106129

107130
// Agents should implement this interface in order to handle events as needed.

agent/src/event_base.h

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class ContentAnalysisEventBase : public ContentAnalysisEvent {
2626
AgentToChrome* agent_to_chrome() { return &agent_to_chrome_; }
2727
ContentAnalysisResponse* response() { return agent_to_chrome()->mutable_response(); }
2828

29+
bool scoped_print_handle_taken_ = false;
30+
2931
private:
3032
BrowserInfo browser_info_;
3133
ContentAnalysisRequest request_;

agent/src/event_mac.cc

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "event_mac.h"
66

7+
#include "scoped_print_handle_mac.h"
8+
79
namespace content_analysis {
810
namespace sdk {
911

@@ -22,5 +24,15 @@ std::string ContentAnalysisEventMac::DebugString() const {
2224
return std::string();
2325
}
2426

27+
std::unique_ptr<ContentAnalysisEvent::ScopedPrintHandle>
28+
ContentAnalysisEventMac::TakeScopedPrintHandle() {
29+
if (!GetRequest().has_print_data() || scoped_print_handle_taken_) {
30+
return nullptr;
31+
}
32+
33+
scoped_print_handle_taken_ = true;
34+
return std::make_unique<ScopedPrintHandleMac>(GetRequest().print_data());
35+
}
36+
2537
} // namespace sdk
2638
} // namespace content_analysis

agent/src/event_mac.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ContentAnalysisEventMac : public ContentAnalysisEventBase {
1919
// ContentAnalysisEvent:
2020
ResultCode Send() override;
2121
std::string DebugString() const override;
22+
std::unique_ptr<ScopedPrintHandle> TakeScopedPrintHandle() override;
2223

2324
// TODO(rogerta): Fill in implementation.
2425
};

agent/src/event_posix.cc

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "event_posix.h"
66

7+
#include "scoped_print_handle_posix.h"
8+
79
namespace content_analysis {
810
namespace sdk {
911

@@ -22,5 +24,15 @@ std::string ContentAnalysisEventPosix::DebugString() const {
2224
return std::string();
2325
}
2426

27+
std::unique_ptr<ContentAnalysisEvent::ScopedPrintHandle>
28+
ContentAnalysisEventPosix::TakeScopedPrintHandle() {
29+
if (!GetRequest().has_print_data() || scoped_print_handle_taken_) {
30+
return nullptr;
31+
}
32+
33+
scoped_print_handle_taken_ = true;
34+
return std::make_unique<ScopedPrintHandlePosix>(GetRequest().print_data());
35+
}
36+
2537
} // namespace sdk
2638
} // namespace content_analysis

agent/src/event_posix.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ContentAnalysisEventPosix : public ContentAnalysisEventBase {
1919
// ContentAnalysisEvent:
2020
ResultCode Send() override;
2121
std::string DebugString() const override;
22+
std::unique_ptr<ScopedPrintHandle> TakeScopedPrintHandle() override;
2223

2324
// TODO(rogerta): Fill in implementation.
2425
};

agent/src/event_win.cc

+16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include <utility>
88

99
#include "event_win.h"
10+
1011
#include "agent_utils_win.h"
12+
#include "scoped_print_handle_win.h"
1113

1214
namespace content_analysis {
1315
namespace sdk {
@@ -128,6 +130,20 @@ void ContentAnalysisEventWin::Shutdown() {
128130
FlushFileBuffers(hPipe_);
129131
hPipe_ = INVALID_HANDLE_VALUE;
130132
}
133+
134+
// Taking the print handle here ensures the dupe handle is closed
135+
// correctly if needed.
136+
auto scoped_handle = TakeScopedPrintHandle();
137+
}
138+
139+
std::unique_ptr<ContentAnalysisEvent::ScopedPrintHandle>
140+
ContentAnalysisEventWin::TakeScopedPrintHandle() {
141+
if (!GetRequest().has_print_data() || scoped_print_handle_taken_) {
142+
return nullptr;
143+
}
144+
145+
scoped_print_handle_taken_ = true;
146+
return std::make_unique<ScopedPrintHandleWin>(GetRequest().print_data());
131147
}
132148

133149
} // namespace sdk

agent/src/event_win.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ContentAnalysisEventWin : public ContentAnalysisEventBase {
2828
ResultCode Close() override;
2929
ResultCode Send() override;
3030
std::string DebugString() const override;
31+
std::unique_ptr<ScopedPrintHandle> TakeScopedPrintHandle() override;
3132

3233
private:
3334
void Shutdown();

agent/src/scoped_print_handle_base.cc

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2023 The Chromium Authors.
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 "scoped_print_handle_base.h"
6+
7+
namespace content_analysis {
8+
namespace sdk {
9+
10+
ScopedPrintHandleBase::ScopedPrintHandleBase(
11+
const ContentAnalysisRequest::PrintData& print_data)
12+
: size_(print_data.size()) {}
13+
14+
size_t ScopedPrintHandleBase::size() { return size_; }
15+
16+
} // namespace sdk
17+
} // namespace content_analysis

agent/src/scoped_print_handle_base.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2023 The Chromium Authors.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef CONTENT_ANALYSIS_AGENT_SRC_SCOPED_PRINT_HANDLE_BASE_H_
6+
#define CONTENT_ANALYSIS_AGENT_SRC_SCOPED_PRINT_HANDLE_BASE_H_
7+
8+
#include "content_analysis/sdk/analysis_agent.h"
9+
10+
namespace content_analysis {
11+
namespace sdk {
12+
13+
class ScopedPrintHandleBase : public ContentAnalysisEvent::ScopedPrintHandle {
14+
public:
15+
ScopedPrintHandleBase(const ContentAnalysisRequest::PrintData& print_data);
16+
17+
size_t size() override;
18+
protected:
19+
size_t size_ = 0;
20+
};
21+
22+
} // namespace sdk
23+
} // namespace content_analysis
24+
25+
#endif // CONTENT_ANALYSIS_AGENT_SRC_SCOPED_PRINT_HANDLE_BASE_H_

agent/src/scoped_print_handle_mac.cc

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2023 The Chromium Authors.
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 "scoped_print_handle_mac.h"
6+
7+
namespace content_analysis {
8+
namespace sdk {
9+
10+
// static
11+
std::unique_ptr<ContentAnalysisEvent::ScopedPrintHandle>
12+
ContentAnalysisEvent::ScopedPrintHandle::Create(
13+
const ContentAnalysisRequest& request) {
14+
if (!request.has_print_data()) {
15+
return nullptr;
16+
}
17+
18+
return std::make_unique<ScopedPrintHandleMac>(request.print_data());
19+
}
20+
21+
22+
ScopedPrintHandleMac::ScopedPrintHandleMac(
23+
const ContentAnalysisRequest::PrintData& print_data)
24+
: ScopedPrintHandleBase(print_data) {
25+
// TODO
26+
}
27+
28+
ScopedPrintHandleMac::~ScopedPrintHandleMac() {
29+
// TODO
30+
}
31+
32+
const char* ScopedPrintHandleMac::data() {
33+
// TODO
34+
return nullptr;
35+
}
36+
37+
} // namespace sdk
38+
} // namespace content_analysis

agent/src/scoped_print_handle_mac.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2023 The Chromium Authors.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef CONTENT_ANALYSIS_AGENT_SRC_SCOPED_PRINT_HANDLE_MAC_H_
6+
#define CONTENT_ANALYSIS_AGENT_SRC_SCOPED_PRINT_HANDLE_MAC_H_
7+
8+
#include "scoped_print_handle_base.h"
9+
10+
namespace content_analysis {
11+
namespace sdk {
12+
13+
class ScopedPrintHandleMac : public ScopedPrintHandleBase {
14+
public:
15+
ScopedPrintHandleMac(const ContentAnalysisRequest::PrintData& print_data);
16+
~ScopedPrintHandleMac() override;
17+
18+
const char* data() override;
19+
};
20+
21+
} // namespace sdk
22+
} // namespace content_analysis
23+
24+
#endif // CONTENT_ANALYSIS_AGENT_SRC_SCOPED_PRINT_HANDLE_MAC_H_
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2023 The Chromium Authors.
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 "scoped_print_handle_posix.h"
6+
7+
namespace content_analysis {
8+
namespace sdk {
9+
10+
11+
12+
ScopedPrintHandlePosix::ScopedPrintHandlePosix(
13+
const ContentAnalysisRequest::PrintData& print_data)
14+
: ScopedPrintHandleBase(print_data) {
15+
// TODO
16+
}
17+
18+
ScopedPrintHandlePosix::~ScopedPrintHandlePosix() {
19+
// TODO
20+
}
21+
22+
const char* ScopedPrintHandlePosix::data() {
23+
// TODO
24+
return nullptr;
25+
}
26+
27+
} // namespace sdk
28+
} // namespace content_analysis

agent/src/scoped_print_handle_posix.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2023 The Chromium Authors.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef CONTENT_ANALYSIS_AGENT_SRC_SCOPED_PRINT_HANDLE_POSIX_H_
6+
#define CONTENT_ANALYSIS_AGENT_SRC_SCOPED_PRINT_HANDLE_POSIX_H_
7+
8+
#include "scoped_print_handle_base.h"
9+
10+
namespace content_analysis {
11+
namespace sdk {
12+
13+
class ScopedPrintHandlePosix : public ScopedPrintHandleBase {
14+
public:
15+
ScopedPrintHandlePosix(const ContentAnalysisRequest::PrintData& print_data);
16+
~ScopedPrintHandlePosix() override;
17+
18+
const char* data() override;
19+
};
20+
21+
} // namespace sdk
22+
} // namespace content_analysis
23+
24+
#endif // CONTENT_ANALYSIS_AGENT_SRC_SCOPED_PRINT_HANDLE_POSIX_H_

agent/src/scoped_print_handle_win.cc

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2023 The Chromium Authors.
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 "scoped_print_handle_win.h"
6+
7+
namespace content_analysis {
8+
namespace sdk {
9+
10+
ScopedPrintHandleWin::ScopedPrintHandleWin(
11+
const ContentAnalysisRequest::PrintData& print_data)
12+
: ScopedPrintHandleBase(print_data),
13+
handle_(reinterpret_cast<HANDLE>(print_data.handle())) {
14+
mapped_ = MapViewOfFile(handle_, FILE_MAP_READ, 0, 0, 0);
15+
}
16+
17+
ScopedPrintHandleWin::~ScopedPrintHandleWin() {
18+
CloseHandle(handle_);
19+
}
20+
21+
const char* ScopedPrintHandleWin::data() {
22+
return reinterpret_cast<const char*>(mapped_);
23+
}
24+
25+
} // namespace sdk
26+
} // namespace content_analysis

0 commit comments

Comments
 (0)